3 * User-level interface to DRM device
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Kevin E. Martin <martin@valinux.com>
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
45 #include <sys/types.h>
47 #define stat_t struct stat
48 #include <sys/ioctl.h>
53 /* Not all systems have MAP_FAILED defined */
55 #define MAP_FAILED ((void *)-1)
60 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
73 #define DRM_MAJOR 226 /* Linux */
77 #define DRM_MAX_MINOR 16
81 * This definition needs to be changed on some systems if dev_t is a structure.
82 * If there is a header file we can get it from, there would be best.
85 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
88 #define DRM_MSG_VERBOSITY 3
90 #define DRM_NODE_CONTROL 0
91 #define DRM_NODE_RENDER 1
93 static drmServerInfoPtr drm_server_info;
95 void drmSetServerInfo(drmServerInfoPtr info)
97 drm_server_info = info;
101 * Output a message to stderr.
103 * \param format printf() like format string.
106 * This function is a wrapper around vfprintf().
109 static int drmDebugPrint(const char *format, va_list ap)
111 return vfprintf(stderr, format, ap);
114 static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
117 drmMsg(const char *format, ...)
121 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
123 va_start(ap, format);
124 if (drm_server_info) {
125 drm_server_info->debug_print(format,ap);
127 drm_debug_print(format, ap);
134 drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
136 drm_debug_print = debug_msg_ptr;
139 static void *drmHashTable = NULL; /* Context switch callbacks */
141 void *drmGetHashTable(void)
146 void *drmMalloc(int size)
149 if ((pt = malloc(size)))
154 void drmFree(void *pt)
160 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
161 static char *drmStrdup(const char *s)
168 retval = malloc(strlen(s)+1);
178 static unsigned long drmGetKeyFromFd(int fd)
187 drmHashEntry *drmGetEntry(int fd)
189 unsigned long key = drmGetKeyFromFd(fd);
194 drmHashTable = drmHashCreate();
196 if (drmHashLookup(drmHashTable, key, &value)) {
197 entry = drmMalloc(sizeof(*entry));
200 entry->tagTable = drmHashCreate();
201 drmHashInsert(drmHashTable, key, entry);
209 * Compare two busid strings
214 * \return 1 if matched.
217 * This function compares two bus ID strings. It understands the older
218 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
219 * domain, b is bus, d is device, f is function.
221 static int drmMatchBusID(const char *id1, const char *id2)
223 /* First, check if the IDs are exactly the same */
224 if (strcasecmp(id1, id2) == 0)
227 /* Try to match old/new-style PCI bus IDs. */
228 if (strncasecmp(id1, "pci", 3) == 0) {
233 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
236 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
241 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
244 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
249 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
258 * Open the DRM device, creating it if necessary.
260 * \param dev major and minor numbers of the device.
261 * \param minor minor number of the device.
263 * \return a file descriptor on success, or a negative value on error.
266 * Assembles the device name from \p minor and opens it, creating the device
267 * special file node with the major and minor numbers specified by \p dev and
268 * parent directory if necessary and was called by root.
270 static int drmOpenDevice(long dev, int minor, int type)
275 mode_t devmode = DRM_DEV_MODE, serv_mode;
276 int isroot = !geteuid();
277 uid_t user = DRM_DEV_UID;
278 gid_t group = DRM_DEV_GID, serv_group;
280 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
281 drmMsg("drmOpenDevice: node name is %s\n", buf);
283 if (drm_server_info) {
284 drm_server_info->get_perms(&serv_group, &serv_mode);
285 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
286 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
287 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
290 if (stat(DRM_DIR_NAME, &st)) {
292 return DRM_ERR_NOT_ROOT;
293 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
294 chown(DRM_DIR_NAME, 0, 0); /* root:root */
295 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
298 /* Check if the device node exists and create it if necessary. */
299 if (stat(buf, &st)) {
301 return DRM_ERR_NOT_ROOT;
303 mknod(buf, S_IFCHR | devmode, dev);
306 if (drm_server_info) {
307 chown(buf, user, group);
311 fd = open(buf, O_RDWR, 0);
312 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
313 fd, fd < 0 ? strerror(errno) : "OK");
317 /* Check if the device node is not what we expect it to be, and recreate it
318 * and try again if so.
320 if (st.st_rdev != dev) {
322 return DRM_ERR_NOT_ROOT;
324 mknod(buf, S_IFCHR | devmode, dev);
325 if (drm_server_info) {
326 chown(buf, user, group);
330 fd = open(buf, O_RDWR, 0);
331 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
332 fd, fd < 0 ? strerror(errno) : "OK");
336 drmMsg("drmOpenDevice: Open failed\n");
343 * Open the DRM device
345 * \param minor device minor number.
346 * \param create allow to create the device if set.
348 * \return a file descriptor on success, or a negative value on error.
351 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
352 * name from \p minor and opens it.
354 static int drmOpenMinor(int minor, int create, int type)
360 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
362 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
363 if ((fd = open(buf, O_RDWR, 0)) >= 0)
370 * Determine whether the DRM kernel driver has been loaded.
372 * \return 1 if the DRM driver is loaded, 0 otherwise.
375 * Determine the presence of the kernel driver by attempting to open the 0
376 * minor and get version information. For backward compatibility with older
377 * Linux implementations, /proc/dri is also checked.
379 int drmAvailable(void)
381 drmVersionPtr version;
385 if ((fd = drmOpenMinor(0, 1, DRM_NODE_RENDER)) < 0) {
387 /* Try proc for backward Linux compatibility */
388 if (!access("/proc/dri/0", R_OK))
394 if ((version = drmGetVersion(fd))) {
396 drmFreeVersion(version);
405 * Open the device by bus ID.
407 * \param busid bus ID.
409 * \return a file descriptor on success, or a negative value on error.
412 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
413 * comparing the device bus ID with the one supplied.
415 * \sa drmOpenMinor() and drmGetBusid().
417 static int drmOpenByBusid(const char *busid)
425 * Open the first minor number that matches the driver name and isn't
426 * already in use. If it's in use it will have a busid assigned already.
428 * start at 1, as 0 is the control node, and we should use drmOpenControl
431 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
432 for (i = 1; i < DRM_MAX_MINOR; i++) {
433 fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
434 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
438 sv.drm_dd_major = -1; /* Don't care */
439 sv.drm_dd_minor = -1; /* Don't care */
440 drmSetInterfaceVersion(fd, &sv);
441 buf = drmGetBusid(fd);
442 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
443 if (buf && drmMatchBusID(buf, busid)) {
457 * Open the device by name.
459 * \param name driver name.
461 * \return a file descriptor on success, or a negative value on error.
464 * This function opens the first minor number that matches the driver name and
465 * isn't already in use. If it's in use it then it will already have a bus ID
468 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
470 static int drmOpenByName(const char *name)
474 drmVersionPtr version;
478 * Open the first minor number that matches the driver name and isn't
479 * already in use. If it's in use it will have a busid assigned already.
481 * start at 1, as 0 is the control node, and we should use drmOpenControl
484 for (i = 1; i < DRM_MAX_MINOR; i++) {
485 if ((fd = drmOpenMinor(i, 1, DRM_NODE_RENDER)) >= 0) {
486 if ((version = drmGetVersion(fd))) {
487 if (!strcmp(version->name, name)) {
488 drmFreeVersion(version);
489 id = drmGetBusid(fd);
490 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
499 drmFreeVersion(version);
507 /* Backward-compatibility /proc support */
508 for (i = 0; i < 8; i++) {
509 char proc_name[64], buf[512];
510 char *driver, *pt, *devstring;
513 sprintf(proc_name, "/proc/dri/%d/name", i);
514 if ((fd = open(proc_name, 0, 0)) >= 0) {
515 retcode = read(fd, buf, sizeof(buf)-1);
518 buf[retcode-1] = '\0';
519 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
521 if (*pt) { /* Device is next */
523 if (!strcmp(driver, name)) { /* Match */
524 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
526 if (*pt) { /* Found busid */
527 return drmOpenByBusid(++pt);
528 } else { /* No busid */
529 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_RENDER);
543 * Open the DRM device.
545 * Looks up the specified name and bus ID, and opens the device found. The
546 * entry in /dev/dri is created if necessary and if called by root.
548 * \param name driver name. Not referenced if bus ID is supplied.
549 * \param busid bus ID. Zero if not known.
551 * \return a file descriptor on success, or a negative value on error.
554 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
557 int drmOpen(const char *name, const char *busid)
559 if (!drmAvailable() && name != NULL && drm_server_info) {
560 /* try to load the kernel */
561 if (!drm_server_info->load_module(name)) {
562 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
568 int fd = drmOpenByBusid(busid);
574 return drmOpenByName(name);
579 int drmOpenControl(int minor)
581 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
585 * Free the version information returned by drmGetVersion().
587 * \param v pointer to the version information.
590 * It frees the memory pointed by \p %v as well as all the non-null strings
593 void drmFreeVersion(drmVersionPtr v)
605 * Free the non-public version information returned by the kernel.
607 * \param v pointer to the version information.
610 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
611 * the non-null strings pointers in it.
613 static void drmFreeKernelVersion(drm_version_t *v)
625 * Copy version information.
627 * \param d destination pointer.
628 * \param s source pointer.
631 * Used by drmGetVersion() to translate the information returned by the ioctl
632 * interface in a private structure into the public structure counterpart.
634 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
636 d->version_major = s->version_major;
637 d->version_minor = s->version_minor;
638 d->version_patchlevel = s->version_patchlevel;
639 d->name_len = s->name_len;
640 d->name = drmStrdup(s->name);
641 d->date_len = s->date_len;
642 d->date = drmStrdup(s->date);
643 d->desc_len = s->desc_len;
644 d->desc = drmStrdup(s->desc);
649 * Query the driver version information.
651 * \param fd file descriptor.
653 * \return pointer to a drmVersion structure which should be freed with
656 * \note Similar information is available via /proc/dri.
659 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
660 * first with zeros to get the string lengths, and then the actually strings.
661 * It also null-terminates them since they might not be already.
663 drmVersionPtr drmGetVersion(int fd)
665 drmVersionPtr retval;
666 drm_version_t *version = drmMalloc(sizeof(*version));
668 version->name_len = 0;
669 version->name = NULL;
670 version->date_len = 0;
671 version->date = NULL;
672 version->desc_len = 0;
673 version->desc = NULL;
675 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
676 drmFreeKernelVersion(version);
680 if (version->name_len)
681 version->name = drmMalloc(version->name_len + 1);
682 if (version->date_len)
683 version->date = drmMalloc(version->date_len + 1);
684 if (version->desc_len)
685 version->desc = drmMalloc(version->desc_len + 1);
687 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
688 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
689 drmFreeKernelVersion(version);
693 /* The results might not be null-terminated strings, so terminate them. */
694 if (version->name_len) version->name[version->name_len] = '\0';
695 if (version->date_len) version->date[version->date_len] = '\0';
696 if (version->desc_len) version->desc[version->desc_len] = '\0';
698 retval = drmMalloc(sizeof(*retval));
699 drmCopyVersion(retval, version);
700 drmFreeKernelVersion(version);
706 * Get version information for the DRM user space library.
708 * This version number is driver independent.
710 * \param fd file descriptor.
712 * \return version information.
715 * This function allocates and fills a drm_version structure with a hard coded
718 drmVersionPtr drmGetLibVersion(int fd)
720 drm_version_t *version = drmMalloc(sizeof(*version));
723 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
724 * revision 1.0.x = original DRM interface with no drmGetLibVersion
725 * entry point and many drm<Device> extensions
726 * revision 1.1.x = added drmCommand entry points for device extensions
727 * added drmGetLibVersion to identify libdrm.a version
728 * revision 1.2.x = added drmSetInterfaceVersion
729 * modified drmOpen to handle both busid and name
730 * revision 1.3.x = added server + memory manager
732 version->version_major = 1;
733 version->version_minor = 3;
734 version->version_patchlevel = 0;
736 return (drmVersionPtr)version;
741 * Free the bus ID information.
743 * \param busid bus ID information string as given by drmGetBusid().
746 * This function is just frees the memory pointed by \p busid.
748 void drmFreeBusid(const char *busid)
750 drmFree((void *)busid);
755 * Get the bus ID of the device.
757 * \param fd file descriptor.
759 * \return bus ID string.
762 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
763 * get the string length and data, passing the arguments in a drm_unique
766 char *drmGetBusid(int fd)
773 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
775 u.unique = drmMalloc(u.unique_len + 1);
776 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
778 u.unique[u.unique_len] = '\0';
785 * Set the bus ID of the device.
787 * \param fd file descriptor.
788 * \param busid bus ID string.
790 * \return zero on success, negative on failure.
793 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
794 * the arguments in a drm_unique structure.
796 int drmSetBusid(int fd, const char *busid)
800 u.unique = (char *)busid;
801 u.unique_len = strlen(busid);
803 if (ioctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
809 int drmGetMagic(int fd, drm_magic_t * magic)
814 if (ioctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
820 int drmAuthMagic(int fd, drm_magic_t magic)
825 if (ioctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
831 * Specifies a range of memory that is available for mapping by a
834 * \param fd file descriptor.
835 * \param offset usually the physical address. The actual meaning depends of
836 * the \p type parameter. See below.
837 * \param size of the memory in bytes.
838 * \param type type of the memory to be mapped.
839 * \param flags combination of several flags to modify the function actions.
840 * \param handle will be set to a value that may be used as the offset
841 * parameter for mmap().
843 * \return zero on success or a negative value on error.
845 * \par Mapping the frame buffer
846 * For the frame buffer
847 * - \p offset will be the physical address of the start of the frame buffer,
848 * - \p size will be the size of the frame buffer in bytes, and
849 * - \p type will be DRM_FRAME_BUFFER.
852 * The area mapped will be uncached. If MTRR support is available in the
853 * kernel, the frame buffer area will be set to write combining.
855 * \par Mapping the MMIO register area
856 * For the MMIO register area,
857 * - \p offset will be the physical address of the start of the register area,
858 * - \p size will be the size of the register area bytes, and
859 * - \p type will be DRM_REGISTERS.
861 * The area mapped will be uncached.
863 * \par Mapping the SAREA
865 * - \p offset will be ignored and should be set to zero,
866 * - \p size will be the desired size of the SAREA in bytes,
867 * - \p type will be DRM_SHM.
870 * A shared memory area of the requested size will be created and locked in
871 * kernel memory. This area may be mapped into client-space by using the handle
874 * \note May only be called by root.
877 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
878 * the arguments in a drm_map structure.
880 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
881 drmMapFlags flags, drm_handle_t *handle)
890 if (ioctl(fd, DRM_IOCTL_ADD_MAP, &map))
893 *handle = (drm_handle_t)map.handle;
897 int drmRmMap(int fd, drm_handle_t handle)
901 map.handle = (void *)handle;
903 if(ioctl(fd, DRM_IOCTL_RM_MAP, &map))
909 * Make buffers available for DMA transfers.
911 * \param fd file descriptor.
912 * \param count number of buffers.
913 * \param size size of each buffer.
914 * \param flags buffer allocation flags.
915 * \param agp_offset offset in the AGP aperture
917 * \return number of buffers allocated, negative on error.
920 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
924 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
927 drm_buf_desc_t request;
929 request.count = count;
931 request.low_mark = 0;
932 request.high_mark = 0;
933 request.flags = flags;
934 request.agp_start = agp_offset;
936 if (ioctl(fd, DRM_IOCTL_ADD_BUFS, &request))
938 return request.count;
941 int drmMarkBufs(int fd, double low, double high)
949 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info))
955 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
958 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
964 for (i = 0; i < info.count; i++) {
965 info.list[i].low_mark = low * info.list[i].count;
966 info.list[i].high_mark = high * info.list[i].count;
967 if (ioctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
981 * \param fd file descriptor.
982 * \param count number of buffers to free.
983 * \param list list of buffers to be freed.
985 * \return zero on success, or a negative value on failure.
987 * \note This function is primarily used for debugging.
990 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
991 * the arguments in a drm_buf_free structure.
993 int drmFreeBufs(int fd, int count, int *list)
995 drm_buf_free_t request;
997 request.count = count;
999 if (ioctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1008 * \param fd file descriptor.
1011 * This function closes the file descriptor.
1013 int drmClose(int fd)
1015 unsigned long key = drmGetKeyFromFd(fd);
1016 drmHashEntry *entry = drmGetEntry(fd);
1018 drmHashDestroy(entry->tagTable);
1021 entry->tagTable = NULL;
1023 drmHashDelete(drmHashTable, key);
1031 * Map a region of memory.
1033 * \param fd file descriptor.
1034 * \param handle handle returned by drmAddMap().
1035 * \param size size in bytes. Must match the size used by drmAddMap().
1036 * \param address will contain the user-space virtual address where the mapping
1039 * \return zero on success, or a negative value on failure.
1042 * This function is a wrapper for mmap().
1044 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1046 static unsigned long pagesize_mask = 0;
1052 pagesize_mask = getpagesize() - 1;
1054 size = (size + pagesize_mask) & ~pagesize_mask;
1056 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1057 if (*address == MAP_FAILED)
1064 * Unmap mappings obtained with drmMap().
1066 * \param address address as given by drmMap().
1067 * \param size size in bytes. Must match the size used by drmMap().
1069 * \return zero on success, or a negative value on failure.
1072 * This function is a wrapper for munmap().
1074 int drmUnmap(drmAddress address, drmSize size)
1076 return munmap(address, size);
1079 drmBufInfoPtr drmGetBufInfo(int fd)
1081 drm_buf_info_t info;
1082 drmBufInfoPtr retval;
1088 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1092 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1095 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1100 retval = drmMalloc(sizeof(*retval));
1101 retval->count = info.count;
1102 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1103 for (i = 0; i < info.count; i++) {
1104 retval->list[i].count = info.list[i].count;
1105 retval->list[i].size = info.list[i].size;
1106 retval->list[i].low_mark = info.list[i].low_mark;
1107 retval->list[i].high_mark = info.list[i].high_mark;
1116 * Map all DMA buffers into client-virtual space.
1118 * \param fd file descriptor.
1120 * \return a pointer to a ::drmBufMap structure.
1122 * \note The client may not use these buffers until obtaining buffer indices
1126 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1127 * information about the buffers in a drm_buf_map structure into the
1128 * client-visible data structures.
1130 drmBufMapPtr drmMapBufs(int fd)
1133 drmBufMapPtr retval;
1138 bufs.virtual = NULL;
1139 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1145 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1148 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1153 retval = drmMalloc(sizeof(*retval));
1154 retval->count = bufs.count;
1155 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1156 for (i = 0; i < bufs.count; i++) {
1157 retval->list[i].idx = bufs.list[i].idx;
1158 retval->list[i].total = bufs.list[i].total;
1159 retval->list[i].used = 0;
1160 retval->list[i].address = bufs.list[i].address;
1170 * Unmap buffers allocated with drmMapBufs().
1172 * \return zero on success, or negative value on failure.
1175 * Calls munmap() for every buffer stored in \p bufs and frees the
1176 * memory allocated by drmMapBufs().
1178 int drmUnmapBufs(drmBufMapPtr bufs)
1182 for (i = 0; i < bufs->count; i++) {
1183 munmap(bufs->list[i].address, bufs->list[i].total);
1186 drmFree(bufs->list);
1193 #define DRM_DMA_RETRY 16
1196 * Reserve DMA buffers.
1198 * \param fd file descriptor.
1201 * \return zero on success, or a negative value on failure.
1204 * Assemble the arguments into a drm_dma structure and keeps issuing the
1205 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1207 int drmDMA(int fd, drmDMAReqPtr request)
1212 dma.context = request->context;
1213 dma.send_count = request->send_count;
1214 dma.send_indices = request->send_list;
1215 dma.send_sizes = request->send_sizes;
1216 dma.flags = request->flags;
1217 dma.request_count = request->request_count;
1218 dma.request_size = request->request_size;
1219 dma.request_indices = request->request_list;
1220 dma.request_sizes = request->request_sizes;
1221 dma.granted_count = 0;
1224 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1225 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1228 request->granted_count = dma.granted_count;
1237 * Obtain heavyweight hardware lock.
1239 * \param fd file descriptor.
1240 * \param context context.
1241 * \param flags flags that determine the sate of the hardware when the function
1244 * \return always zero.
1247 * This function translates the arguments into a drm_lock structure and issue
1248 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1250 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1254 lock.context = context;
1256 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1257 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1258 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1259 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1260 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1261 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1263 while (ioctl(fd, DRM_IOCTL_LOCK, &lock))
1269 * Release the hardware lock.
1271 * \param fd file descriptor.
1272 * \param context context.
1274 * \return zero on success, or a negative value on failure.
1277 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1278 * argument in a drm_lock structure.
1280 int drmUnlock(int fd, drm_context_t context)
1284 lock.context = context;
1286 return ioctl(fd, DRM_IOCTL_UNLOCK, &lock);
1289 drm_context_t *drmGetReservedContextList(int fd, int *count)
1293 drm_context_t * retval;
1297 res.contexts = NULL;
1298 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res))
1304 if (!(list = drmMalloc(res.count * sizeof(*list))))
1306 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1311 res.contexts = list;
1312 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res))
1315 for (i = 0; i < res.count; i++)
1316 retval[i] = list[i].handle;
1323 void drmFreeReservedContextList(drm_context_t *pt)
1331 * Used by the X server during GLXContext initialization. This causes
1332 * per-context kernel-level resources to be allocated.
1334 * \param fd file descriptor.
1335 * \param handle is set on success. To be used by the client when requesting DMA
1336 * dispatch with drmDMA().
1338 * \return zero on success, or a negative value on failure.
1340 * \note May only be called by root.
1343 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1344 * argument in a drm_ctx structure.
1346 int drmCreateContext(int fd, drm_context_t *handle)
1350 ctx.flags = 0; /* Modified with functions below */
1351 if (ioctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1353 *handle = ctx.handle;
1357 int drmSwitchToContext(int fd, drm_context_t context)
1361 ctx.handle = context;
1362 if (ioctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1367 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1372 * Context preserving means that no context switches are done between DMA
1373 * buffers from one context and the next. This is suitable for use in the
1374 * X server (which promises to maintain hardware context), or in the
1375 * client-side library when buffers are swapped on behalf of two threads.
1377 ctx.handle = context;
1379 if (flags & DRM_CONTEXT_PRESERVED)
1380 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1381 if (flags & DRM_CONTEXT_2DONLY)
1382 ctx.flags |= _DRM_CONTEXT_2DONLY;
1383 if (ioctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1388 int drmGetContextFlags(int fd, drm_context_t context,
1389 drm_context_tFlagsPtr flags)
1393 ctx.handle = context;
1394 if (ioctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1397 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1398 *flags |= DRM_CONTEXT_PRESERVED;
1399 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1400 *flags |= DRM_CONTEXT_2DONLY;
1407 * Free any kernel-level resources allocated with drmCreateContext() associated
1410 * \param fd file descriptor.
1411 * \param handle handle given by drmCreateContext().
1413 * \return zero on success, or a negative value on failure.
1415 * \note May only be called by root.
1418 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1419 * argument in a drm_ctx structure.
1421 int drmDestroyContext(int fd, drm_context_t handle)
1424 ctx.handle = handle;
1425 if (ioctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1430 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1433 if (ioctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1435 *handle = draw.handle;
1439 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1442 draw.handle = handle;
1443 if (ioctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1448 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1449 drm_drawable_info_type_t type, unsigned int num,
1452 drm_update_draw_t update;
1454 update.handle = handle;
1457 update.data = (unsigned long long)(unsigned long)data;
1459 if (ioctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1466 * Acquire the AGP device.
1468 * Must be called before any of the other AGP related calls.
1470 * \param fd file descriptor.
1472 * \return zero on success, or a negative value on failure.
1475 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1477 int drmAgpAcquire(int fd)
1479 if (ioctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1486 * Release the AGP device.
1488 * \param fd file descriptor.
1490 * \return zero on success, or a negative value on failure.
1493 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1495 int drmAgpRelease(int fd)
1497 if (ioctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1506 * \param fd file descriptor.
1507 * \param mode AGP mode.
1509 * \return zero on success, or a negative value on failure.
1512 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1513 * argument in a drm_agp_mode structure.
1515 int drmAgpEnable(int fd, unsigned long mode)
1520 if (ioctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1527 * Allocate a chunk of AGP memory.
1529 * \param fd file descriptor.
1530 * \param size requested memory size in bytes. Will be rounded to page boundary.
1531 * \param type type of memory to allocate.
1532 * \param address if not zero, will be set to the physical address of the
1534 * \param handle on success will be set to a handle of the allocated memory.
1536 * \return zero on success, or a negative value on failure.
1539 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1540 * arguments in a drm_agp_buffer structure.
1542 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1543 unsigned long *address, drm_handle_t *handle)
1547 *handle = DRM_AGP_NO_HANDLE;
1551 if (ioctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1554 *address = b.physical;
1561 * Free a chunk of AGP memory.
1563 * \param fd file descriptor.
1564 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1566 * \return zero on success, or a negative value on failure.
1569 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1570 * argument in a drm_agp_buffer structure.
1572 int drmAgpFree(int fd, drm_handle_t handle)
1578 if (ioctl(fd, DRM_IOCTL_AGP_FREE, &b))
1585 * Bind a chunk of AGP memory.
1587 * \param fd file descriptor.
1588 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1589 * \param offset offset in bytes. It will round to page boundary.
1591 * \return zero on success, or a negative value on failure.
1594 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1595 * argument in a drm_agp_binding structure.
1597 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1599 drm_agp_binding_t b;
1603 if (ioctl(fd, DRM_IOCTL_AGP_BIND, &b))
1610 * Unbind a chunk of AGP memory.
1612 * \param fd file descriptor.
1613 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1615 * \return zero on success, or a negative value on failure.
1618 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1619 * the argument in a drm_agp_binding structure.
1621 int drmAgpUnbind(int fd, drm_handle_t handle)
1623 drm_agp_binding_t b;
1627 if (ioctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1634 * Get AGP driver major version number.
1636 * \param fd file descriptor.
1638 * \return major version number on success, or a negative value on failure..
1641 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1642 * necessary information in a drm_agp_info structure.
1644 int drmAgpVersionMajor(int fd)
1648 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1650 return i.agp_version_major;
1655 * Get AGP driver minor version number.
1657 * \param fd file descriptor.
1659 * \return minor version number on success, or a negative value on failure.
1662 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1663 * necessary information in a drm_agp_info structure.
1665 int drmAgpVersionMinor(int fd)
1669 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1671 return i.agp_version_minor;
1678 * \param fd file descriptor.
1680 * \return mode on success, or zero on failure.
1683 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1684 * necessary information in a drm_agp_info structure.
1686 unsigned long drmAgpGetMode(int fd)
1690 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1697 * Get AGP aperture base.
1699 * \param fd file descriptor.
1701 * \return aperture base on success, zero on failure.
1704 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1705 * necessary information in a drm_agp_info structure.
1707 unsigned long drmAgpBase(int fd)
1711 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1713 return i.aperture_base;
1718 * Get AGP aperture size.
1720 * \param fd file descriptor.
1722 * \return aperture size on success, zero on failure.
1725 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1726 * necessary information in a drm_agp_info structure.
1728 unsigned long drmAgpSize(int fd)
1732 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1734 return i.aperture_size;
1739 * Get used AGP memory.
1741 * \param fd file descriptor.
1743 * \return memory used on success, or zero on failure.
1746 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1747 * necessary information in a drm_agp_info structure.
1749 unsigned long drmAgpMemoryUsed(int fd)
1753 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1755 return i.memory_used;
1760 * Get available AGP memory.
1762 * \param fd file descriptor.
1764 * \return memory available on success, or zero on failure.
1767 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1768 * necessary information in a drm_agp_info structure.
1770 unsigned long drmAgpMemoryAvail(int fd)
1774 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1776 return i.memory_allowed;
1781 * Get hardware vendor ID.
1783 * \param fd file descriptor.
1785 * \return vendor ID on success, or zero on failure.
1788 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1789 * necessary information in a drm_agp_info structure.
1791 unsigned int drmAgpVendorId(int fd)
1795 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1802 * Get hardware device ID.
1804 * \param fd file descriptor.
1806 * \return zero on success, or zero on failure.
1809 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1810 * necessary information in a drm_agp_info structure.
1812 unsigned int drmAgpDeviceId(int fd)
1816 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1821 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1823 drm_scatter_gather_t sg;
1828 if (ioctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1830 *handle = sg.handle;
1834 int drmScatterGatherFree(int fd, drm_handle_t handle)
1836 drm_scatter_gather_t sg;
1840 if (ioctl(fd, DRM_IOCTL_SG_FREE, &sg))
1848 * \param fd file descriptor.
1849 * \param vbl pointer to a drmVBlank structure.
1851 * \return zero on success, or a negative value on failure.
1854 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1856 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1861 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1862 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1863 } while (ret && errno == EINTR);
1868 int drmError(int err, const char *label)
1871 case DRM_ERR_NO_DEVICE:
1872 fprintf(stderr, "%s: no device\n", label);
1874 case DRM_ERR_NO_ACCESS:
1875 fprintf(stderr, "%s: no access\n", label);
1877 case DRM_ERR_NOT_ROOT:
1878 fprintf(stderr, "%s: not root\n", label);
1880 case DRM_ERR_INVALID:
1881 fprintf(stderr, "%s: invalid args\n", label);
1886 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1894 * Install IRQ handler.
1896 * \param fd file descriptor.
1897 * \param irq IRQ number.
1899 * \return zero on success, or a negative value on failure.
1902 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1903 * argument in a drm_control structure.
1905 int drmCtlInstHandler(int fd, int irq)
1909 ctl.func = DRM_INST_HANDLER;
1911 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl))
1918 * Uninstall IRQ handler.
1920 * \param fd file descriptor.
1922 * \return zero on success, or a negative value on failure.
1925 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1926 * argument in a drm_control structure.
1928 int drmCtlUninstHandler(int fd)
1932 ctl.func = DRM_UNINST_HANDLER;
1934 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl))
1939 int drmFinish(int fd, int context, drmLockFlags flags)
1943 lock.context = context;
1945 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1946 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1947 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1948 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1949 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1950 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1951 if (ioctl(fd, DRM_IOCTL_FINISH, &lock))
1957 * Get IRQ from bus ID.
1959 * \param fd file descriptor.
1960 * \param busnum bus number.
1961 * \param devnum device number.
1962 * \param funcnum function number.
1964 * \return IRQ number on success, or a negative value on failure.
1967 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
1968 * arguments in a drm_irq_busid structure.
1970 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
1976 p.funcnum = funcnum;
1977 if (ioctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
1982 int drmAddContextTag(int fd, drm_context_t context, void *tag)
1984 drmHashEntry *entry = drmGetEntry(fd);
1986 if (drmHashInsert(entry->tagTable, context, tag)) {
1987 drmHashDelete(entry->tagTable, context);
1988 drmHashInsert(entry->tagTable, context, tag);
1993 int drmDelContextTag(int fd, drm_context_t context)
1995 drmHashEntry *entry = drmGetEntry(fd);
1997 return drmHashDelete(entry->tagTable, context);
2000 void *drmGetContextTag(int fd, drm_context_t context)
2002 drmHashEntry *entry = drmGetEntry(fd);
2005 if (drmHashLookup(entry->tagTable, context, &value))
2011 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2012 drm_handle_t handle)
2014 drm_ctx_priv_map_t map;
2016 map.ctx_id = ctx_id;
2017 map.handle = (void *)handle;
2019 if (ioctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2024 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2025 drm_handle_t *handle)
2027 drm_ctx_priv_map_t map;
2029 map.ctx_id = ctx_id;
2031 if (ioctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2034 *handle = (drm_handle_t)map.handle;
2039 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2040 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2046 if (ioctl(fd, DRM_IOCTL_GET_MAP, &map))
2048 *offset = map.offset;
2052 *handle = (unsigned long)map.handle;
2057 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2058 unsigned long *magic, unsigned long *iocs)
2060 drm_client_t client;
2063 if (ioctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2065 *auth = client.auth;
2068 *magic = client.magic;
2069 *iocs = client.iocs;
2073 int drmGetStats(int fd, drmStatsT *stats)
2078 if (ioctl(fd, DRM_IOCTL_GET_STATS, &s))
2082 memset(stats, 0, sizeof(*stats));
2083 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2087 stats->data[i].long_format = "%-20.20s"; \
2088 stats->data[i].rate_format = "%8.8s"; \
2089 stats->data[i].isvalue = 1; \
2090 stats->data[i].verbose = 0
2093 stats->data[i].long_format = "%-20.20s"; \
2094 stats->data[i].rate_format = "%5.5s"; \
2095 stats->data[i].isvalue = 0; \
2096 stats->data[i].mult_names = "kgm"; \
2097 stats->data[i].mult = 1000; \
2098 stats->data[i].verbose = 0
2101 stats->data[i].long_format = "%-20.20s"; \
2102 stats->data[i].rate_format = "%5.5s"; \
2103 stats->data[i].isvalue = 0; \
2104 stats->data[i].mult_names = "KGM"; \
2105 stats->data[i].mult = 1024; \
2106 stats->data[i].verbose = 0
2109 stats->count = s.count;
2110 for (i = 0; i < s.count; i++) {
2111 stats->data[i].value = s.data[i].value;
2112 switch (s.data[i].type) {
2113 case _DRM_STAT_LOCK:
2114 stats->data[i].long_name = "Lock";
2115 stats->data[i].rate_name = "Lock";
2118 case _DRM_STAT_OPENS:
2119 stats->data[i].long_name = "Opens";
2120 stats->data[i].rate_name = "O";
2122 stats->data[i].verbose = 1;
2124 case _DRM_STAT_CLOSES:
2125 stats->data[i].long_name = "Closes";
2126 stats->data[i].rate_name = "Lock";
2128 stats->data[i].verbose = 1;
2130 case _DRM_STAT_IOCTLS:
2131 stats->data[i].long_name = "Ioctls";
2132 stats->data[i].rate_name = "Ioc/s";
2135 case _DRM_STAT_LOCKS:
2136 stats->data[i].long_name = "Locks";
2137 stats->data[i].rate_name = "Lck/s";
2140 case _DRM_STAT_UNLOCKS:
2141 stats->data[i].long_name = "Unlocks";
2142 stats->data[i].rate_name = "Unl/s";
2146 stats->data[i].long_name = "IRQs";
2147 stats->data[i].rate_name = "IRQ/s";
2150 case _DRM_STAT_PRIMARY:
2151 stats->data[i].long_name = "Primary Bytes";
2152 stats->data[i].rate_name = "PB/s";
2155 case _DRM_STAT_SECONDARY:
2156 stats->data[i].long_name = "Secondary Bytes";
2157 stats->data[i].rate_name = "SB/s";
2161 stats->data[i].long_name = "DMA";
2162 stats->data[i].rate_name = "DMA/s";
2165 case _DRM_STAT_SPECIAL:
2166 stats->data[i].long_name = "Special DMA";
2167 stats->data[i].rate_name = "dma/s";
2170 case _DRM_STAT_MISSED:
2171 stats->data[i].long_name = "Miss";
2172 stats->data[i].rate_name = "Ms/s";
2175 case _DRM_STAT_VALUE:
2176 stats->data[i].long_name = "Value";
2177 stats->data[i].rate_name = "Value";
2180 case _DRM_STAT_BYTE:
2181 stats->data[i].long_name = "Bytes";
2182 stats->data[i].rate_name = "B/s";
2185 case _DRM_STAT_COUNT:
2187 stats->data[i].long_name = "Count";
2188 stats->data[i].rate_name = "Cnt/s";
2197 * Issue a set-version ioctl.
2199 * \param fd file descriptor.
2200 * \param drmCommandIndex command index
2201 * \param data source pointer of the data to be read and written.
2202 * \param size size of the data to be read and written.
2204 * \return zero on success, or a negative value on failure.
2207 * It issues a read-write ioctl given by
2208 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2210 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2213 drm_set_version_t sv;
2215 sv.drm_di_major = version->drm_di_major;
2216 sv.drm_di_minor = version->drm_di_minor;
2217 sv.drm_dd_major = version->drm_dd_major;
2218 sv.drm_dd_minor = version->drm_dd_minor;
2220 if (ioctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2224 version->drm_di_major = sv.drm_di_major;
2225 version->drm_di_minor = sv.drm_di_minor;
2226 version->drm_dd_major = sv.drm_dd_major;
2227 version->drm_dd_minor = sv.drm_dd_minor;
2233 * Send a device-specific command.
2235 * \param fd file descriptor.
2236 * \param drmCommandIndex command index
2238 * \return zero on success, or a negative value on failure.
2241 * It issues a ioctl given by
2242 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2244 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2246 void *data = NULL; /* dummy */
2247 unsigned long request;
2249 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2251 if (ioctl(fd, request, data)) {
2259 * Send a device-specific read command.
2261 * \param fd file descriptor.
2262 * \param drmCommandIndex command index
2263 * \param data destination pointer of the data to be read.
2264 * \param size size of the data to be read.
2266 * \return zero on success, or a negative value on failure.
2269 * It issues a read ioctl given by
2270 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2272 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2275 unsigned long request;
2277 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2278 DRM_COMMAND_BASE + drmCommandIndex, size);
2280 if (ioctl(fd, request, data)) {
2288 * Send a device-specific write command.
2290 * \param fd file descriptor.
2291 * \param drmCommandIndex command index
2292 * \param data source pointer of the data to be written.
2293 * \param size size of the data to be written.
2295 * \return zero on success, or a negative value on failure.
2298 * It issues a write ioctl given by
2299 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2301 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2304 unsigned long request;
2306 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2307 DRM_COMMAND_BASE + drmCommandIndex, size);
2309 if (ioctl(fd, request, data)) {
2317 * Send a device-specific read-write command.
2319 * \param fd file descriptor.
2320 * \param drmCommandIndex command index
2321 * \param data source pointer of the data to be read and written.
2322 * \param size size of the data to be read and written.
2324 * \return zero on success, or a negative value on failure.
2327 * It issues a read-write ioctl given by
2328 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2330 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2333 unsigned long request;
2335 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2336 DRM_COMMAND_BASE + drmCommandIndex, size);
2338 if (ioctl(fd, request, data)) {
2347 * DRM_FENCE_FLAG_EMIT
2348 * DRM_FENCE_FLAG_SHAREABLE
2349 * DRM_FENCE_MASK_DRIVER
2352 int drmFenceCreate(int fd, unsigned flags, int fence_class, unsigned type,
2355 drm_fence_arg_t arg;
2357 memset(&arg, 0, sizeof(arg));
2360 arg.fence_class = fence_class;
2362 if (ioctl(fd, DRM_IOCTL_FENCE_CREATE, &arg))
2364 fence->handle = arg.handle;
2365 fence->fence_class = arg.fence_class;
2366 fence->type = arg.type;
2367 fence->flags = arg.flags;
2368 fence->signaled = 0;
2374 * DRM_FENCE_FLAG_SHAREABLE
2375 * DRM_FENCE_MASK_DRIVER
2378 int drmFenceBuffers(int fd, unsigned flags, uint32_t fence_class, drmFence *fence)
2380 drm_fence_arg_t arg;
2382 memset(&arg, 0, sizeof(arg));
2384 arg.fence_class = fence_class;
2386 if (ioctl(fd, DRM_IOCTL_FENCE_BUFFERS, &arg))
2388 fence->handle = arg.handle;
2389 fence->fence_class = arg.fence_class;
2390 fence->type = arg.type;
2391 fence->flags = arg.flags;
2392 fence->sequence = arg.sequence;
2393 fence->signaled = 0;
2397 int drmFenceReference(int fd, unsigned handle, drmFence *fence)
2399 drm_fence_arg_t arg;
2401 memset(&arg, 0, sizeof(arg));
2402 arg.handle = handle;
2404 if (ioctl(fd, DRM_IOCTL_FENCE_REFERENCE, &arg))
2406 fence->handle = arg.handle;
2407 fence->fence_class = arg.fence_class;
2408 fence->type = arg.type;
2409 fence->flags = arg.flags;
2410 fence->signaled = arg.signaled;
2414 int drmFenceUnreference(int fd, const drmFence *fence)
2416 drm_fence_arg_t arg;
2418 memset(&arg, 0, sizeof(arg));
2419 arg.handle = fence->handle;
2421 if (ioctl(fd, DRM_IOCTL_FENCE_UNREFERENCE, &arg))
2426 int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type)
2428 drm_fence_arg_t arg;
2430 memset(&arg, 0, sizeof(arg));
2431 arg.handle = fence->handle;
2432 arg.type = flush_type;
2434 if (ioctl(fd, DRM_IOCTL_FENCE_FLUSH, &arg))
2436 fence->fence_class = arg.fence_class;
2437 fence->type = arg.type;
2438 fence->signaled = arg.signaled;
2442 int drmFenceUpdate(int fd, drmFence *fence)
2444 drm_fence_arg_t arg;
2446 memset(&arg, 0, sizeof(arg));
2447 arg.handle = fence->handle;
2449 if (ioctl(fd, DRM_IOCTL_FENCE_SIGNALED, &arg))
2451 fence->fence_class = arg.fence_class;
2452 fence->type = arg.type;
2453 fence->signaled = arg.signaled;
2457 int drmFenceSignaled(int fd, drmFence *fence, unsigned fenceType,
2460 if ((fence->flags & DRM_FENCE_FLAG_SHAREABLE) ||
2461 ((fenceType & fence->signaled) != fenceType)) {
2462 int ret = drmFenceFlush(fd, fence, fenceType);
2467 *signaled = ((fenceType & fence->signaled) == fenceType);
2474 * DRM_FENCE_FLAG_SHAREABLE
2475 * DRM_FENCE_MASK_DRIVER
2479 int drmFenceEmit(int fd, unsigned flags, drmFence *fence, unsigned emit_type)
2481 drm_fence_arg_t arg;
2483 memset(&arg, 0, sizeof(arg));
2484 arg.fence_class = fence->fence_class;
2486 arg.handle = fence->handle;
2487 arg.type = emit_type;
2489 if (ioctl(fd, DRM_IOCTL_FENCE_EMIT, &arg))
2491 fence->fence_class = arg.fence_class;
2492 fence->type = arg.type;
2493 fence->signaled = arg.signaled;
2494 fence->sequence = arg.sequence;
2500 * DRM_FENCE_FLAG_WAIT_LAZY
2501 * DRM_FENCE_FLAG_WAIT_IGNORE_SIGNALS
2504 #define DRM_IOCTL_TIMEOUT_USEC 3000000UL
2506 static unsigned long
2507 drmTimeDiff(struct timeval *now, struct timeval *then)
2511 val = now->tv_sec - then->tv_sec;
2513 val += now->tv_usec;
2514 val -= then->tv_usec;
2516 return (unsigned long) val;
2520 drmIoctlTimeout(int fd, unsigned long request, void *argp)
2523 struct timeval then, now;
2527 ret = ioctl(fd, request, argp);
2528 if (ret != 0 && errno == EAGAIN) {
2530 gettimeofday(&then, NULL);
2533 gettimeofday(&now, NULL);
2535 } while (ret != 0 && errno == EAGAIN &&
2536 drmTimeDiff(&now, &then) < DRM_IOCTL_TIMEOUT_USEC);
2539 return ((errno == EAGAIN) ? -EBUSY : -errno);
2547 int drmFenceWait(int fd, unsigned flags, drmFence *fence, unsigned flush_type)
2549 drm_fence_arg_t arg;
2552 if (flush_type == 0) {
2553 flush_type = fence->type;
2556 if (!(fence->flags & DRM_FENCE_FLAG_SHAREABLE)) {
2557 if ((flush_type & fence->signaled) == flush_type) {
2562 memset(&arg, 0, sizeof(arg));
2563 arg.handle = fence->handle;
2564 arg.type = flush_type;
2568 ret = drmIoctlTimeout(fd, DRM_IOCTL_FENCE_WAIT, &arg);
2572 fence->fence_class = arg.fence_class;
2573 fence->type = arg.type;
2574 fence->signaled = arg.signaled;
2578 static void drmBOCopyReply(const struct drm_bo_info_rep *rep, drmBO *buf)
2580 buf->handle = rep->handle;
2581 buf->flags = rep->flags;
2582 buf->size = rep->size;
2583 buf->offset = rep->offset;
2584 buf->mapHandle = rep->arg_handle;
2585 buf->proposedFlags = rep->proposed_flags;
2586 buf->start = rep->buffer_start;
2587 buf->fenceFlags = rep->fence_flags;
2588 buf->replyFlags = rep->rep_flags;
2589 buf->pageAlignment = rep->page_alignment;
2590 buf->tileInfo = rep->tile_info;
2591 buf->hwTileStride = rep->hw_tile_stride;
2592 buf->desiredTileStride = rep->desired_tile_stride;
2597 int drmBOCreate(int fd, unsigned long size,
2598 unsigned pageAlignment, void *user_buffer,
2600 unsigned hint, drmBO *buf)
2602 struct drm_bo_create_arg arg;
2603 struct drm_bo_create_req *req = &arg.d.req;
2604 struct drm_bo_info_rep *rep = &arg.d.rep;
2607 memset(buf, 0, sizeof(*buf));
2608 memset(&arg, 0, sizeof(arg));
2612 req->page_alignment = pageAlignment;
2613 req->buffer_start = (unsigned long) user_buffer;
2615 buf->virtual = NULL;
2617 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_CREATE, &arg);
2621 drmBOCopyReply(rep, buf);
2622 buf->virtual = user_buffer;
2628 int drmBOReference(int fd, unsigned handle, drmBO *buf)
2630 struct drm_bo_reference_info_arg arg;
2631 struct drm_bo_handle_arg *req = &arg.d.req;
2632 struct drm_bo_info_rep *rep = &arg.d.rep;
2634 memset(&arg, 0, sizeof(arg));
2635 req->handle = handle;
2637 if (ioctl(fd, DRM_IOCTL_BO_REFERENCE, &arg))
2640 drmBOCopyReply(rep, buf);
2641 buf->mapVirtual = NULL;
2643 buf->virtual = NULL;
2648 int drmBOUnreference(int fd, drmBO *buf)
2650 struct drm_bo_handle_arg arg;
2652 if (buf->mapVirtual && buf->mapHandle) {
2653 (void) munmap(buf->mapVirtual, buf->start + buf->size);
2654 buf->mapVirtual = NULL;
2655 buf->virtual = NULL;
2658 memset(&arg, 0, sizeof(arg));
2659 arg.handle = buf->handle;
2661 if (ioctl(fd, DRM_IOCTL_BO_UNREFERENCE, &arg))
2670 * Flags can be DRM_BO_FLAG_READ, DRM_BO_FLAG_WRITE or'ed together
2671 * Hint currently be DRM_BO_HINT_DONT_BLOCK, which makes the
2672 * call return an -EBUSY if it can' immediately honor the mapping request.
2675 int drmBOMap(int fd, drmBO *buf, unsigned mapFlags, unsigned mapHint,
2678 struct drm_bo_map_wait_idle_arg arg;
2679 struct drm_bo_info_req *req = &arg.d.req;
2680 struct drm_bo_info_rep *rep = &arg.d.rep;
2684 * Make sure we have a virtual address of the buffer.
2687 if (!buf->virtual) {
2689 virtual = mmap(0, buf->size + buf->start,
2690 PROT_READ | PROT_WRITE, MAP_SHARED,
2691 fd, buf->mapHandle);
2692 if (virtual == MAP_FAILED) {
2697 buf->mapVirtual = virtual;
2698 buf->virtual = ((char *) virtual) + buf->start;
2701 memset(&arg, 0, sizeof(arg));
2702 req->handle = buf->handle;
2703 req->mask = mapFlags;
2704 req->hint = mapHint;
2707 * May hang if the buffer object is busy.
2708 * This IOCTL synchronizes the buffer.
2711 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_MAP, &arg);
2715 drmBOCopyReply(rep, buf);
2716 buf->mapFlags = mapFlags;
2718 *address = buf->virtual;
2724 int drmBOUnmap(int fd, drmBO *buf)
2726 struct drm_bo_handle_arg arg;
2728 memset(&arg, 0, sizeof(arg));
2729 arg.handle = buf->handle;
2731 if (ioctl(fd, DRM_IOCTL_BO_UNMAP, &arg)) {
2738 int drmBOSetStatus(int fd, drmBO *buf,
2739 uint64_t flags, uint64_t mask,
2741 unsigned int desired_tile_stride,
2742 unsigned int tile_info)
2745 struct drm_bo_map_wait_idle_arg arg;
2746 struct drm_bo_info_req *req = &arg.d.req;
2747 struct drm_bo_info_rep *rep = &arg.d.rep;
2750 memset(&arg, 0, sizeof(arg));
2753 req->handle = buf->handle;
2755 req->desired_tile_stride = desired_tile_stride;
2756 req->tile_info = tile_info;
2758 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_SETSTATUS, &arg);
2762 drmBOCopyReply(rep, buf);
2767 int drmBOInfo(int fd, drmBO *buf)
2769 struct drm_bo_reference_info_arg arg;
2770 struct drm_bo_handle_arg *req = &arg.d.req;
2771 struct drm_bo_info_rep *rep = &arg.d.rep;
2774 memset(&arg, 0, sizeof(arg));
2775 req->handle = buf->handle;
2777 ret = ioctl(fd, DRM_IOCTL_BO_INFO, &arg);
2781 drmBOCopyReply(rep, buf);
2785 int drmBOWaitIdle(int fd, drmBO *buf, unsigned hint)
2787 struct drm_bo_map_wait_idle_arg arg;
2788 struct drm_bo_info_req *req = &arg.d.req;
2789 struct drm_bo_info_rep *rep = &arg.d.rep;
2792 if ((buf->flags & DRM_BO_FLAG_SHAREABLE) ||
2793 (buf->replyFlags & DRM_BO_REP_BUSY)) {
2794 memset(&arg, 0, sizeof(arg));
2795 req->handle = buf->handle;
2798 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_WAIT_IDLE, &arg);
2802 drmBOCopyReply(rep, buf);
2807 int drmBOBusy(int fd, drmBO *buf, int *busy)
2809 if (!(buf->flags & DRM_BO_FLAG_SHAREABLE) &&
2810 !(buf->replyFlags & DRM_BO_REP_BUSY)) {
2815 int ret = drmBOInfo(fd, buf);
2818 *busy = (buf->replyFlags & DRM_BO_REP_BUSY);
2823 int drmMMInit(int fd, unsigned long pOffset, unsigned long pSize,
2826 struct drm_mm_init_arg arg;
2828 memset(&arg, 0, sizeof(arg));
2830 arg.magic = DRM_BO_INIT_MAGIC;
2831 arg.major = DRM_BO_INIT_MAJOR;
2832 arg.minor = DRM_BO_INIT_MINOR;
2833 arg.p_offset = pOffset;
2835 arg.mem_type = memType;
2837 if (ioctl(fd, DRM_IOCTL_MM_INIT, &arg))
2842 int drmMMTakedown(int fd, unsigned memType)
2844 struct drm_mm_type_arg arg;
2846 memset(&arg, 0, sizeof(arg));
2847 arg.mem_type = memType;
2849 if (ioctl(fd, DRM_IOCTL_MM_TAKEDOWN, &arg))
2855 * If this function returns an error, and lockBM was set to 1,
2856 * the buffer manager is NOT locked.
2859 int drmMMLock(int fd, unsigned memType, int lockBM, int ignoreNoEvict)
2861 struct drm_mm_type_arg arg;
2863 memset(&arg, 0, sizeof(arg));
2864 arg.mem_type = memType;
2865 arg.lock_flags |= (lockBM) ? DRM_BO_LOCK_UNLOCK_BM : 0;
2866 arg.lock_flags |= (ignoreNoEvict) ? DRM_BO_LOCK_IGNORE_NO_EVICT : 0;
2868 return drmIoctlTimeout(fd, DRM_IOCTL_MM_LOCK, &arg);
2871 int drmMMUnlock(int fd, unsigned memType, int unlockBM)
2873 struct drm_mm_type_arg arg;
2875 memset(&arg, 0, sizeof(arg));
2877 arg.mem_type = memType;
2878 arg.lock_flags |= (unlockBM) ? DRM_BO_LOCK_UNLOCK_BM : 0;
2880 return drmIoctlTimeout(fd, DRM_IOCTL_MM_UNLOCK, &arg);
2883 int drmBOVersion(int fd, unsigned int *major,
2884 unsigned int *minor,
2885 unsigned int *patchlevel)
2887 struct drm_bo_version_arg arg;
2890 memset(&arg, 0, sizeof(arg));
2891 ret = ioctl(fd, DRM_IOCTL_BO_VERSION, &arg);
2900 *patchlevel = arg.patchlevel;
2907 #define DRM_MAX_FDS 16
2912 } connection[DRM_MAX_FDS];
2914 static int nr_fds = 0;
2916 int drmOpenOnce(void *unused,
2923 for (i = 0; i < nr_fds; i++)
2924 if (strcmp(BusID, connection[i].BusID) == 0) {
2925 connection[i].refcount++;
2927 return connection[i].fd;
2930 fd = drmOpen(unused, BusID);
2931 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2934 connection[nr_fds].BusID = strdup(BusID);
2935 connection[nr_fds].fd = fd;
2936 connection[nr_fds].refcount = 1;
2940 fprintf(stderr, "saved connection %d for %s %d\n",
2941 nr_fds, connection[nr_fds].BusID,
2942 strcmp(BusID, connection[nr_fds].BusID));
2949 void drmCloseOnce(int fd)
2953 for (i = 0; i < nr_fds; i++) {
2954 if (fd == connection[i].fd) {
2955 if (--connection[i].refcount == 0) {
2956 drmClose(connection[i].fd);
2957 free(connection[i].BusID);
2960 connection[i] = connection[nr_fds];