3 * User-level interface to DRM device
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Kevin E. Martin <martin@valinux.com>
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
45 #include <sys/types.h>
47 #define stat_t struct stat
48 #include <sys/ioctl.h>
53 /* Not all systems have MAP_FAILED defined */
55 #define MAP_FAILED ((void *)-1)
60 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
73 #define DRM_MAJOR 226 /* Linux */
77 #define DRM_MAX_MINOR 16
81 * This definition needs to be changed on some systems if dev_t is a structure.
82 * If there is a header file we can get it from, there would be best.
85 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
88 #define DRM_MSG_VERBOSITY 3
90 #define DRM_NODE_CONTROL 0
91 #define DRM_NODE_RENDER 1
93 static drmServerInfoPtr drm_server_info;
95 void drmSetServerInfo(drmServerInfoPtr info)
97 drm_server_info = info;
101 * Output a message to stderr.
103 * \param format printf() like format string.
106 * This function is a wrapper around vfprintf().
109 static int drmDebugPrint(const char *format, va_list ap)
111 return vfprintf(stderr, format, ap);
114 static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
117 drmMsg(const char *format, ...)
121 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
123 va_start(ap, format);
124 if (drm_server_info) {
125 drm_server_info->debug_print(format,ap);
127 drm_debug_print(format, ap);
134 drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
136 drm_debug_print = debug_msg_ptr;
139 static void *drmHashTable = NULL; /* Context switch callbacks */
141 void *drmGetHashTable(void)
146 void *drmMalloc(int size)
149 if ((pt = malloc(size)))
154 void drmFree(void *pt)
160 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
161 static char *drmStrdup(const char *s)
168 retval = malloc(strlen(s)+1);
178 * Call ioctl, restarting if it is interupted
181 drmIoctl(int fd, int request, void *arg)
186 ret = ioctl(fd, request, arg);
187 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
191 static unsigned long drmGetKeyFromFd(int fd)
200 drmHashEntry *drmGetEntry(int fd)
202 unsigned long key = drmGetKeyFromFd(fd);
207 drmHashTable = drmHashCreate();
209 if (drmHashLookup(drmHashTable, key, &value)) {
210 entry = drmMalloc(sizeof(*entry));
213 entry->tagTable = drmHashCreate();
214 drmHashInsert(drmHashTable, key, entry);
222 * Compare two busid strings
227 * \return 1 if matched.
230 * This function compares two bus ID strings. It understands the older
231 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
232 * domain, b is bus, d is device, f is function.
234 static int drmMatchBusID(const char *id1, const char *id2)
236 /* First, check if the IDs are exactly the same */
237 if (strcasecmp(id1, id2) == 0)
240 /* Try to match old/new-style PCI bus IDs. */
241 if (strncasecmp(id1, "pci", 3) == 0) {
246 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
249 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
254 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
257 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
262 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
271 * Open the DRM device, creating it if necessary.
273 * \param dev major and minor numbers of the device.
274 * \param minor minor number of the device.
276 * \return a file descriptor on success, or a negative value on error.
279 * Assembles the device name from \p minor and opens it, creating the device
280 * special file node with the major and minor numbers specified by \p dev and
281 * parent directory if necessary and was called by root.
283 static int drmOpenDevice(long dev, int minor, int type)
288 mode_t devmode = DRM_DEV_MODE, serv_mode;
289 int isroot = !geteuid();
290 uid_t user = DRM_DEV_UID;
291 gid_t group = DRM_DEV_GID, serv_group;
293 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
294 drmMsg("drmOpenDevice: node name is %s\n", buf);
296 if (drm_server_info) {
297 drm_server_info->get_perms(&serv_group, &serv_mode);
298 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
299 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
300 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
303 if (stat(DRM_DIR_NAME, &st)) {
305 return DRM_ERR_NOT_ROOT;
306 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
307 chown(DRM_DIR_NAME, 0, 0); /* root:root */
308 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
311 /* Check if the device node exists and create it if necessary. */
312 if (stat(buf, &st)) {
314 return DRM_ERR_NOT_ROOT;
316 mknod(buf, S_IFCHR | devmode, dev);
319 if (drm_server_info) {
320 chown(buf, user, group);
324 fd = open(buf, O_RDWR, 0);
325 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
326 fd, fd < 0 ? strerror(errno) : "OK");
330 /* Check if the device node is not what we expect it to be, and recreate it
331 * and try again if so.
333 if (st.st_rdev != dev) {
335 return DRM_ERR_NOT_ROOT;
337 mknod(buf, S_IFCHR | devmode, dev);
338 if (drm_server_info) {
339 chown(buf, user, group);
343 fd = open(buf, O_RDWR, 0);
344 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
345 fd, fd < 0 ? strerror(errno) : "OK");
349 drmMsg("drmOpenDevice: Open failed\n");
356 * Open the DRM device
358 * \param minor device minor number.
359 * \param create allow to create the device if set.
361 * \return a file descriptor on success, or a negative value on error.
364 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
365 * name from \p minor and opens it.
367 static int drmOpenMinor(int minor, int create, int type)
373 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
375 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
376 if ((fd = open(buf, O_RDWR, 0)) >= 0)
383 * Determine whether the DRM kernel driver has been loaded.
385 * \return 1 if the DRM driver is loaded, 0 otherwise.
388 * Determine the presence of the kernel driver by attempting to open the 0
389 * minor and get version information. For backward compatibility with older
390 * Linux implementations, /proc/dri is also checked.
392 int drmAvailable(void)
394 drmVersionPtr version;
398 if ((fd = drmOpenMinor(0, 1, DRM_NODE_RENDER)) < 0) {
400 /* Try proc for backward Linux compatibility */
401 if (!access("/proc/dri/0", R_OK))
407 if ((version = drmGetVersion(fd))) {
409 drmFreeVersion(version);
418 * Open the device by bus ID.
420 * \param busid bus ID.
422 * \return a file descriptor on success, or a negative value on error.
425 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
426 * comparing the device bus ID with the one supplied.
428 * \sa drmOpenMinor() and drmGetBusid().
430 static int drmOpenByBusid(const char *busid)
437 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
438 for (i = 0; i < DRM_MAX_MINOR; i++) {
439 fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
440 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
444 sv.drm_dd_major = -1; /* Don't care */
445 sv.drm_dd_minor = -1; /* Don't care */
446 drmSetInterfaceVersion(fd, &sv);
447 buf = drmGetBusid(fd);
448 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
449 if (buf && drmMatchBusID(buf, busid)) {
463 * Open the device by name.
465 * \param name driver name.
467 * \return a file descriptor on success, or a negative value on error.
470 * This function opens the first minor number that matches the driver name and
471 * isn't already in use. If it's in use it then it will already have a bus ID
474 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
476 static int drmOpenByName(const char *name)
480 drmVersionPtr version;
483 if (!drmAvailable()) {
484 if (!drm_server_info) {
488 /* try to load the kernel module now */
489 if (!drm_server_info->load_module(name)) {
490 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
497 * Open the first minor number that matches the driver name and isn't
498 * already in use. If it's in use it will have a busid assigned already.
500 for (i = 0; i < DRM_MAX_MINOR; i++) {
501 if ((fd = drmOpenMinor(i, 1, DRM_NODE_RENDER)) >= 0) {
502 if ((version = drmGetVersion(fd))) {
503 if (!strcmp(version->name, name)) {
504 drmFreeVersion(version);
505 id = drmGetBusid(fd);
506 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
515 drmFreeVersion(version);
523 /* Backward-compatibility /proc support */
524 for (i = 0; i < 8; i++) {
525 char proc_name[64], buf[512];
526 char *driver, *pt, *devstring;
529 sprintf(proc_name, "/proc/dri/%d/name", i);
530 if ((fd = open(proc_name, 0, 0)) >= 0) {
531 retcode = read(fd, buf, sizeof(buf)-1);
534 buf[retcode-1] = '\0';
535 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
537 if (*pt) { /* Device is next */
539 if (!strcmp(driver, name)) { /* Match */
540 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
542 if (*pt) { /* Found busid */
543 return drmOpenByBusid(++pt);
544 } else { /* No busid */
545 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_RENDER);
559 * Open the DRM device.
561 * Looks up the specified name and bus ID, and opens the device found. The
562 * entry in /dev/dri is created if necessary and if called by root.
564 * \param name driver name. Not referenced if bus ID is supplied.
565 * \param busid bus ID. Zero if not known.
567 * \return a file descriptor on success, or a negative value on error.
570 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
573 int drmOpen(const char *name, const char *busid)
575 if (!drmAvailable() && name != NULL && drm_server_info) {
576 /* try to load the kernel */
577 if (!drm_server_info->load_module(name)) {
578 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
584 int fd = drmOpenByBusid(busid);
590 return drmOpenByName(name);
595 int drmOpenControl(int minor)
597 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
601 * Free the version information returned by drmGetVersion().
603 * \param v pointer to the version information.
606 * It frees the memory pointed by \p %v as well as all the non-null strings
609 void drmFreeVersion(drmVersionPtr v)
621 * Free the non-public version information returned by the kernel.
623 * \param v pointer to the version information.
626 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
627 * the non-null strings pointers in it.
629 static void drmFreeKernelVersion(drm_version_t *v)
641 * Copy version information.
643 * \param d destination pointer.
644 * \param s source pointer.
647 * Used by drmGetVersion() to translate the information returned by the ioctl
648 * interface in a private structure into the public structure counterpart.
650 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
652 d->version_major = s->version_major;
653 d->version_minor = s->version_minor;
654 d->version_patchlevel = s->version_patchlevel;
655 d->name_len = s->name_len;
656 d->name = drmStrdup(s->name);
657 d->date_len = s->date_len;
658 d->date = drmStrdup(s->date);
659 d->desc_len = s->desc_len;
660 d->desc = drmStrdup(s->desc);
665 * Query the driver version information.
667 * \param fd file descriptor.
669 * \return pointer to a drmVersion structure which should be freed with
672 * \note Similar information is available via /proc/dri.
675 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
676 * first with zeros to get the string lengths, and then the actually strings.
677 * It also null-terminates them since they might not be already.
679 drmVersionPtr drmGetVersion(int fd)
681 drmVersionPtr retval;
682 drm_version_t *version = drmMalloc(sizeof(*version));
684 version->name_len = 0;
685 version->name = NULL;
686 version->date_len = 0;
687 version->date = NULL;
688 version->desc_len = 0;
689 version->desc = NULL;
691 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
692 drmFreeKernelVersion(version);
696 if (version->name_len)
697 version->name = drmMalloc(version->name_len + 1);
698 if (version->date_len)
699 version->date = drmMalloc(version->date_len + 1);
700 if (version->desc_len)
701 version->desc = drmMalloc(version->desc_len + 1);
703 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
704 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
705 drmFreeKernelVersion(version);
709 /* The results might not be null-terminated strings, so terminate them. */
710 if (version->name_len) version->name[version->name_len] = '\0';
711 if (version->date_len) version->date[version->date_len] = '\0';
712 if (version->desc_len) version->desc[version->desc_len] = '\0';
714 retval = drmMalloc(sizeof(*retval));
715 drmCopyVersion(retval, version);
716 drmFreeKernelVersion(version);
722 * Get version information for the DRM user space library.
724 * This version number is driver independent.
726 * \param fd file descriptor.
728 * \return version information.
731 * This function allocates and fills a drm_version structure with a hard coded
734 drmVersionPtr drmGetLibVersion(int fd)
736 drm_version_t *version = drmMalloc(sizeof(*version));
739 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
740 * revision 1.0.x = original DRM interface with no drmGetLibVersion
741 * entry point and many drm<Device> extensions
742 * revision 1.1.x = added drmCommand entry points for device extensions
743 * added drmGetLibVersion to identify libdrm.a version
744 * revision 1.2.x = added drmSetInterfaceVersion
745 * modified drmOpen to handle both busid and name
746 * revision 1.3.x = added server + memory manager
748 version->version_major = 1;
749 version->version_minor = 3;
750 version->version_patchlevel = 0;
752 return (drmVersionPtr)version;
757 * Free the bus ID information.
759 * \param busid bus ID information string as given by drmGetBusid().
762 * This function is just frees the memory pointed by \p busid.
764 void drmFreeBusid(const char *busid)
766 drmFree((void *)busid);
771 * Get the bus ID of the device.
773 * \param fd file descriptor.
775 * \return bus ID string.
778 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
779 * get the string length and data, passing the arguments in a drm_unique
782 char *drmGetBusid(int fd)
789 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
791 u.unique = drmMalloc(u.unique_len + 1);
792 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
794 u.unique[u.unique_len] = '\0';
801 * Set the bus ID of the device.
803 * \param fd file descriptor.
804 * \param busid bus ID string.
806 * \return zero on success, negative on failure.
809 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
810 * the arguments in a drm_unique structure.
812 int drmSetBusid(int fd, const char *busid)
816 u.unique = (char *)busid;
817 u.unique_len = strlen(busid);
819 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
825 int drmGetMagic(int fd, drm_magic_t * magic)
830 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
836 int drmAuthMagic(int fd, drm_magic_t magic)
841 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
847 * Specifies a range of memory that is available for mapping by a
850 * \param fd file descriptor.
851 * \param offset usually the physical address. The actual meaning depends of
852 * the \p type parameter. See below.
853 * \param size of the memory in bytes.
854 * \param type type of the memory to be mapped.
855 * \param flags combination of several flags to modify the function actions.
856 * \param handle will be set to a value that may be used as the offset
857 * parameter for mmap().
859 * \return zero on success or a negative value on error.
861 * \par Mapping the frame buffer
862 * For the frame buffer
863 * - \p offset will be the physical address of the start of the frame buffer,
864 * - \p size will be the size of the frame buffer in bytes, and
865 * - \p type will be DRM_FRAME_BUFFER.
868 * The area mapped will be uncached. If MTRR support is available in the
869 * kernel, the frame buffer area will be set to write combining.
871 * \par Mapping the MMIO register area
872 * For the MMIO register area,
873 * - \p offset will be the physical address of the start of the register area,
874 * - \p size will be the size of the register area bytes, and
875 * - \p type will be DRM_REGISTERS.
877 * The area mapped will be uncached.
879 * \par Mapping the SAREA
881 * - \p offset will be ignored and should be set to zero,
882 * - \p size will be the desired size of the SAREA in bytes,
883 * - \p type will be DRM_SHM.
886 * A shared memory area of the requested size will be created and locked in
887 * kernel memory. This area may be mapped into client-space by using the handle
890 * \note May only be called by root.
893 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
894 * the arguments in a drm_map structure.
896 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
897 drmMapFlags flags, drm_handle_t *handle)
906 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
909 *handle = (drm_handle_t)map.handle;
913 int drmRmMap(int fd, drm_handle_t handle)
917 map.handle = (void *)handle;
919 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
925 * Make buffers available for DMA transfers.
927 * \param fd file descriptor.
928 * \param count number of buffers.
929 * \param size size of each buffer.
930 * \param flags buffer allocation flags.
931 * \param agp_offset offset in the AGP aperture
933 * \return number of buffers allocated, negative on error.
936 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
940 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
943 drm_buf_desc_t request;
945 request.count = count;
947 request.low_mark = 0;
948 request.high_mark = 0;
949 request.flags = flags;
950 request.agp_start = agp_offset;
952 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
954 return request.count;
957 int drmMarkBufs(int fd, double low, double high)
965 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
971 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
974 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
980 for (i = 0; i < info.count; i++) {
981 info.list[i].low_mark = low * info.list[i].count;
982 info.list[i].high_mark = high * info.list[i].count;
983 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
997 * \param fd file descriptor.
998 * \param count number of buffers to free.
999 * \param list list of buffers to be freed.
1001 * \return zero on success, or a negative value on failure.
1003 * \note This function is primarily used for debugging.
1006 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1007 * the arguments in a drm_buf_free structure.
1009 int drmFreeBufs(int fd, int count, int *list)
1011 drm_buf_free_t request;
1013 request.count = count;
1014 request.list = list;
1015 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1024 * \param fd file descriptor.
1027 * This function closes the file descriptor.
1029 int drmClose(int fd)
1031 unsigned long key = drmGetKeyFromFd(fd);
1032 drmHashEntry *entry = drmGetEntry(fd);
1034 drmHashDestroy(entry->tagTable);
1037 entry->tagTable = NULL;
1039 drmHashDelete(drmHashTable, key);
1047 * Map a region of memory.
1049 * \param fd file descriptor.
1050 * \param handle handle returned by drmAddMap().
1051 * \param size size in bytes. Must match the size used by drmAddMap().
1052 * \param address will contain the user-space virtual address where the mapping
1055 * \return zero on success, or a negative value on failure.
1058 * This function is a wrapper for mmap().
1060 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1062 static unsigned long pagesize_mask = 0;
1068 pagesize_mask = getpagesize() - 1;
1070 size = (size + pagesize_mask) & ~pagesize_mask;
1072 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1073 if (*address == MAP_FAILED)
1080 * Unmap mappings obtained with drmMap().
1082 * \param address address as given by drmMap().
1083 * \param size size in bytes. Must match the size used by drmMap().
1085 * \return zero on success, or a negative value on failure.
1088 * This function is a wrapper for munmap().
1090 int drmUnmap(drmAddress address, drmSize size)
1092 return munmap(address, size);
1095 drmBufInfoPtr drmGetBufInfo(int fd)
1097 drm_buf_info_t info;
1098 drmBufInfoPtr retval;
1104 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1108 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1111 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1116 retval = drmMalloc(sizeof(*retval));
1117 retval->count = info.count;
1118 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1119 for (i = 0; i < info.count; i++) {
1120 retval->list[i].count = info.list[i].count;
1121 retval->list[i].size = info.list[i].size;
1122 retval->list[i].low_mark = info.list[i].low_mark;
1123 retval->list[i].high_mark = info.list[i].high_mark;
1132 * Map all DMA buffers into client-virtual space.
1134 * \param fd file descriptor.
1136 * \return a pointer to a ::drmBufMap structure.
1138 * \note The client may not use these buffers until obtaining buffer indices
1142 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1143 * information about the buffers in a drm_buf_map structure into the
1144 * client-visible data structures.
1146 drmBufMapPtr drmMapBufs(int fd)
1149 drmBufMapPtr retval;
1154 bufs.virtual = NULL;
1155 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1161 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1164 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1169 retval = drmMalloc(sizeof(*retval));
1170 retval->count = bufs.count;
1171 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1172 for (i = 0; i < bufs.count; i++) {
1173 retval->list[i].idx = bufs.list[i].idx;
1174 retval->list[i].total = bufs.list[i].total;
1175 retval->list[i].used = 0;
1176 retval->list[i].address = bufs.list[i].address;
1186 * Unmap buffers allocated with drmMapBufs().
1188 * \return zero on success, or negative value on failure.
1191 * Calls munmap() for every buffer stored in \p bufs and frees the
1192 * memory allocated by drmMapBufs().
1194 int drmUnmapBufs(drmBufMapPtr bufs)
1198 for (i = 0; i < bufs->count; i++) {
1199 munmap(bufs->list[i].address, bufs->list[i].total);
1202 drmFree(bufs->list);
1209 #define DRM_DMA_RETRY 16
1212 * Reserve DMA buffers.
1214 * \param fd file descriptor.
1217 * \return zero on success, or a negative value on failure.
1220 * Assemble the arguments into a drm_dma structure and keeps issuing the
1221 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1223 int drmDMA(int fd, drmDMAReqPtr request)
1228 dma.context = request->context;
1229 dma.send_count = request->send_count;
1230 dma.send_indices = request->send_list;
1231 dma.send_sizes = request->send_sizes;
1232 dma.flags = request->flags;
1233 dma.request_count = request->request_count;
1234 dma.request_size = request->request_size;
1235 dma.request_indices = request->request_list;
1236 dma.request_sizes = request->request_sizes;
1237 dma.granted_count = 0;
1240 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1241 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1244 request->granted_count = dma.granted_count;
1253 * Obtain heavyweight hardware lock.
1255 * \param fd file descriptor.
1256 * \param context context.
1257 * \param flags flags that determine the sate of the hardware when the function
1260 * \return always zero.
1263 * This function translates the arguments into a drm_lock structure and issue
1264 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1266 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1270 lock.context = context;
1272 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1273 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1274 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1275 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1276 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1277 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1279 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1285 * Release the hardware lock.
1287 * \param fd file descriptor.
1288 * \param context context.
1290 * \return zero on success, or a negative value on failure.
1293 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1294 * argument in a drm_lock structure.
1296 int drmUnlock(int fd, drm_context_t context)
1300 lock.context = context;
1302 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1305 drm_context_t *drmGetReservedContextList(int fd, int *count)
1309 drm_context_t * retval;
1313 res.contexts = NULL;
1314 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1320 if (!(list = drmMalloc(res.count * sizeof(*list))))
1322 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1327 res.contexts = list;
1328 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1331 for (i = 0; i < res.count; i++)
1332 retval[i] = list[i].handle;
1339 void drmFreeReservedContextList(drm_context_t *pt)
1347 * Used by the X server during GLXContext initialization. This causes
1348 * per-context kernel-level resources to be allocated.
1350 * \param fd file descriptor.
1351 * \param handle is set on success. To be used by the client when requesting DMA
1352 * dispatch with drmDMA().
1354 * \return zero on success, or a negative value on failure.
1356 * \note May only be called by root.
1359 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1360 * argument in a drm_ctx structure.
1362 int drmCreateContext(int fd, drm_context_t *handle)
1366 ctx.flags = 0; /* Modified with functions below */
1367 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1369 *handle = ctx.handle;
1373 int drmSwitchToContext(int fd, drm_context_t context)
1377 ctx.handle = context;
1378 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1383 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1388 * Context preserving means that no context switches are done between DMA
1389 * buffers from one context and the next. This is suitable for use in the
1390 * X server (which promises to maintain hardware context), or in the
1391 * client-side library when buffers are swapped on behalf of two threads.
1393 ctx.handle = context;
1395 if (flags & DRM_CONTEXT_PRESERVED)
1396 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1397 if (flags & DRM_CONTEXT_2DONLY)
1398 ctx.flags |= _DRM_CONTEXT_2DONLY;
1399 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1404 int drmGetContextFlags(int fd, drm_context_t context,
1405 drm_context_tFlagsPtr flags)
1409 ctx.handle = context;
1410 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1413 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1414 *flags |= DRM_CONTEXT_PRESERVED;
1415 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1416 *flags |= DRM_CONTEXT_2DONLY;
1423 * Free any kernel-level resources allocated with drmCreateContext() associated
1426 * \param fd file descriptor.
1427 * \param handle handle given by drmCreateContext().
1429 * \return zero on success, or a negative value on failure.
1431 * \note May only be called by root.
1434 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1435 * argument in a drm_ctx structure.
1437 int drmDestroyContext(int fd, drm_context_t handle)
1440 ctx.handle = handle;
1441 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1446 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1449 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1451 *handle = draw.handle;
1455 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1458 draw.handle = handle;
1459 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1464 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1465 drm_drawable_info_type_t type, unsigned int num,
1468 drm_update_draw_t update;
1470 update.handle = handle;
1473 update.data = (unsigned long long)(unsigned long)data;
1475 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1482 * Acquire the AGP device.
1484 * Must be called before any of the other AGP related calls.
1486 * \param fd file descriptor.
1488 * \return zero on success, or a negative value on failure.
1491 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1493 int drmAgpAcquire(int fd)
1495 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1502 * Release the AGP device.
1504 * \param fd file descriptor.
1506 * \return zero on success, or a negative value on failure.
1509 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1511 int drmAgpRelease(int fd)
1513 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1522 * \param fd file descriptor.
1523 * \param mode AGP mode.
1525 * \return zero on success, or a negative value on failure.
1528 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1529 * argument in a drm_agp_mode structure.
1531 int drmAgpEnable(int fd, unsigned long mode)
1536 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1543 * Allocate a chunk of AGP memory.
1545 * \param fd file descriptor.
1546 * \param size requested memory size in bytes. Will be rounded to page boundary.
1547 * \param type type of memory to allocate.
1548 * \param address if not zero, will be set to the physical address of the
1550 * \param handle on success will be set to a handle of the allocated memory.
1552 * \return zero on success, or a negative value on failure.
1555 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1556 * arguments in a drm_agp_buffer structure.
1558 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1559 unsigned long *address, drm_handle_t *handle)
1563 *handle = DRM_AGP_NO_HANDLE;
1567 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1570 *address = b.physical;
1577 * Free a chunk of AGP memory.
1579 * \param fd file descriptor.
1580 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1582 * \return zero on success, or a negative value on failure.
1585 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1586 * argument in a drm_agp_buffer structure.
1588 int drmAgpFree(int fd, drm_handle_t handle)
1594 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1601 * Bind a chunk of AGP memory.
1603 * \param fd file descriptor.
1604 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1605 * \param offset offset in bytes. It will round to page boundary.
1607 * \return zero on success, or a negative value on failure.
1610 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1611 * argument in a drm_agp_binding structure.
1613 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1615 drm_agp_binding_t b;
1619 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1626 * Unbind a chunk of AGP memory.
1628 * \param fd file descriptor.
1629 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1631 * \return zero on success, or a negative value on failure.
1634 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1635 * the argument in a drm_agp_binding structure.
1637 int drmAgpUnbind(int fd, drm_handle_t handle)
1639 drm_agp_binding_t b;
1643 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1650 * Get AGP driver major version number.
1652 * \param fd file descriptor.
1654 * \return major version number on success, or a negative value on failure..
1657 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1658 * necessary information in a drm_agp_info structure.
1660 int drmAgpVersionMajor(int fd)
1664 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1666 return i.agp_version_major;
1671 * Get AGP driver minor version number.
1673 * \param fd file descriptor.
1675 * \return minor version number on success, or a negative value on failure.
1678 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1679 * necessary information in a drm_agp_info structure.
1681 int drmAgpVersionMinor(int fd)
1685 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1687 return i.agp_version_minor;
1694 * \param fd file descriptor.
1696 * \return mode on success, or zero on failure.
1699 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1700 * necessary information in a drm_agp_info structure.
1702 unsigned long drmAgpGetMode(int fd)
1706 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1713 * Get AGP aperture base.
1715 * \param fd file descriptor.
1717 * \return aperture base on success, zero on failure.
1720 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1721 * necessary information in a drm_agp_info structure.
1723 unsigned long drmAgpBase(int fd)
1727 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1729 return i.aperture_base;
1734 * Get AGP aperture size.
1736 * \param fd file descriptor.
1738 * \return aperture size on success, zero on failure.
1741 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1742 * necessary information in a drm_agp_info structure.
1744 unsigned long drmAgpSize(int fd)
1748 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1750 return i.aperture_size;
1755 * Get used AGP memory.
1757 * \param fd file descriptor.
1759 * \return memory used on success, or zero on failure.
1762 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1763 * necessary information in a drm_agp_info structure.
1765 unsigned long drmAgpMemoryUsed(int fd)
1769 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1771 return i.memory_used;
1776 * Get available AGP memory.
1778 * \param fd file descriptor.
1780 * \return memory available on success, or zero on failure.
1783 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1784 * necessary information in a drm_agp_info structure.
1786 unsigned long drmAgpMemoryAvail(int fd)
1790 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1792 return i.memory_allowed;
1797 * Get hardware vendor ID.
1799 * \param fd file descriptor.
1801 * \return vendor ID on success, or zero on failure.
1804 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1805 * necessary information in a drm_agp_info structure.
1807 unsigned int drmAgpVendorId(int fd)
1811 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1818 * Get hardware device ID.
1820 * \param fd file descriptor.
1822 * \return zero on success, or zero on failure.
1825 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1826 * necessary information in a drm_agp_info structure.
1828 unsigned int drmAgpDeviceId(int fd)
1832 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1837 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1839 drm_scatter_gather_t sg;
1844 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1846 *handle = sg.handle;
1850 int drmScatterGatherFree(int fd, drm_handle_t handle)
1852 drm_scatter_gather_t sg;
1856 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
1864 * \param fd file descriptor.
1865 * \param vbl pointer to a drmVBlank structure.
1867 * \return zero on success, or a negative value on failure.
1870 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1872 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1877 ret = drmIoctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1878 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1879 } while (ret && errno == EINTR);
1884 int drmError(int err, const char *label)
1887 case DRM_ERR_NO_DEVICE:
1888 fprintf(stderr, "%s: no device\n", label);
1890 case DRM_ERR_NO_ACCESS:
1891 fprintf(stderr, "%s: no access\n", label);
1893 case DRM_ERR_NOT_ROOT:
1894 fprintf(stderr, "%s: not root\n", label);
1896 case DRM_ERR_INVALID:
1897 fprintf(stderr, "%s: invalid args\n", label);
1902 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1910 * Install IRQ handler.
1912 * \param fd file descriptor.
1913 * \param irq IRQ number.
1915 * \return zero on success, or a negative value on failure.
1918 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1919 * argument in a drm_control structure.
1921 int drmCtlInstHandler(int fd, int irq)
1925 ctl.func = DRM_INST_HANDLER;
1927 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
1934 * Uninstall IRQ handler.
1936 * \param fd file descriptor.
1938 * \return zero on success, or a negative value on failure.
1941 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1942 * argument in a drm_control structure.
1944 int drmCtlUninstHandler(int fd)
1948 ctl.func = DRM_UNINST_HANDLER;
1950 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
1955 int drmFinish(int fd, int context, drmLockFlags flags)
1959 lock.context = context;
1961 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1962 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1963 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1964 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1965 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1966 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1967 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
1973 * Get IRQ from bus ID.
1975 * \param fd file descriptor.
1976 * \param busnum bus number.
1977 * \param devnum device number.
1978 * \param funcnum function number.
1980 * \return IRQ number on success, or a negative value on failure.
1983 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
1984 * arguments in a drm_irq_busid structure.
1986 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
1992 p.funcnum = funcnum;
1993 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
1998 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2000 drmHashEntry *entry = drmGetEntry(fd);
2002 if (drmHashInsert(entry->tagTable, context, tag)) {
2003 drmHashDelete(entry->tagTable, context);
2004 drmHashInsert(entry->tagTable, context, tag);
2009 int drmDelContextTag(int fd, drm_context_t context)
2011 drmHashEntry *entry = drmGetEntry(fd);
2013 return drmHashDelete(entry->tagTable, context);
2016 void *drmGetContextTag(int fd, drm_context_t context)
2018 drmHashEntry *entry = drmGetEntry(fd);
2021 if (drmHashLookup(entry->tagTable, context, &value))
2027 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2028 drm_handle_t handle)
2030 drm_ctx_priv_map_t map;
2032 map.ctx_id = ctx_id;
2033 map.handle = (void *)handle;
2035 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2040 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2041 drm_handle_t *handle)
2043 drm_ctx_priv_map_t map;
2045 map.ctx_id = ctx_id;
2047 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2050 *handle = (drm_handle_t)map.handle;
2055 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2056 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2062 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2064 *offset = map.offset;
2068 *handle = (unsigned long)map.handle;
2073 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2074 unsigned long *magic, unsigned long *iocs)
2076 drm_client_t client;
2079 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2081 *auth = client.auth;
2084 *magic = client.magic;
2085 *iocs = client.iocs;
2089 int drmGetStats(int fd, drmStatsT *stats)
2094 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2098 memset(stats, 0, sizeof(*stats));
2099 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2103 stats->data[i].long_format = "%-20.20s"; \
2104 stats->data[i].rate_format = "%8.8s"; \
2105 stats->data[i].isvalue = 1; \
2106 stats->data[i].verbose = 0
2109 stats->data[i].long_format = "%-20.20s"; \
2110 stats->data[i].rate_format = "%5.5s"; \
2111 stats->data[i].isvalue = 0; \
2112 stats->data[i].mult_names = "kgm"; \
2113 stats->data[i].mult = 1000; \
2114 stats->data[i].verbose = 0
2117 stats->data[i].long_format = "%-20.20s"; \
2118 stats->data[i].rate_format = "%5.5s"; \
2119 stats->data[i].isvalue = 0; \
2120 stats->data[i].mult_names = "KGM"; \
2121 stats->data[i].mult = 1024; \
2122 stats->data[i].verbose = 0
2125 stats->count = s.count;
2126 for (i = 0; i < s.count; i++) {
2127 stats->data[i].value = s.data[i].value;
2128 switch (s.data[i].type) {
2129 case _DRM_STAT_LOCK:
2130 stats->data[i].long_name = "Lock";
2131 stats->data[i].rate_name = "Lock";
2134 case _DRM_STAT_OPENS:
2135 stats->data[i].long_name = "Opens";
2136 stats->data[i].rate_name = "O";
2138 stats->data[i].verbose = 1;
2140 case _DRM_STAT_CLOSES:
2141 stats->data[i].long_name = "Closes";
2142 stats->data[i].rate_name = "Lock";
2144 stats->data[i].verbose = 1;
2146 case _DRM_STAT_IOCTLS:
2147 stats->data[i].long_name = "Ioctls";
2148 stats->data[i].rate_name = "Ioc/s";
2151 case _DRM_STAT_LOCKS:
2152 stats->data[i].long_name = "Locks";
2153 stats->data[i].rate_name = "Lck/s";
2156 case _DRM_STAT_UNLOCKS:
2157 stats->data[i].long_name = "Unlocks";
2158 stats->data[i].rate_name = "Unl/s";
2162 stats->data[i].long_name = "IRQs";
2163 stats->data[i].rate_name = "IRQ/s";
2166 case _DRM_STAT_PRIMARY:
2167 stats->data[i].long_name = "Primary Bytes";
2168 stats->data[i].rate_name = "PB/s";
2171 case _DRM_STAT_SECONDARY:
2172 stats->data[i].long_name = "Secondary Bytes";
2173 stats->data[i].rate_name = "SB/s";
2177 stats->data[i].long_name = "DMA";
2178 stats->data[i].rate_name = "DMA/s";
2181 case _DRM_STAT_SPECIAL:
2182 stats->data[i].long_name = "Special DMA";
2183 stats->data[i].rate_name = "dma/s";
2186 case _DRM_STAT_MISSED:
2187 stats->data[i].long_name = "Miss";
2188 stats->data[i].rate_name = "Ms/s";
2191 case _DRM_STAT_VALUE:
2192 stats->data[i].long_name = "Value";
2193 stats->data[i].rate_name = "Value";
2196 case _DRM_STAT_BYTE:
2197 stats->data[i].long_name = "Bytes";
2198 stats->data[i].rate_name = "B/s";
2201 case _DRM_STAT_COUNT:
2203 stats->data[i].long_name = "Count";
2204 stats->data[i].rate_name = "Cnt/s";
2213 * Issue a set-version ioctl.
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 drmSetInterfaceVersion(int fd, drmSetVersion *version)
2229 drm_set_version_t sv;
2231 sv.drm_di_major = version->drm_di_major;
2232 sv.drm_di_minor = version->drm_di_minor;
2233 sv.drm_dd_major = version->drm_dd_major;
2234 sv.drm_dd_minor = version->drm_dd_minor;
2236 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2240 version->drm_di_major = sv.drm_di_major;
2241 version->drm_di_minor = sv.drm_di_minor;
2242 version->drm_dd_major = sv.drm_dd_major;
2243 version->drm_dd_minor = sv.drm_dd_minor;
2249 * Send a device-specific command.
2251 * \param fd file descriptor.
2252 * \param drmCommandIndex command index
2254 * \return zero on success, or a negative value on failure.
2257 * It issues a ioctl given by
2258 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2260 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2262 void *data = NULL; /* dummy */
2263 unsigned long request;
2265 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2267 if (drmIoctl(fd, request, data)) {
2275 * Send a device-specific read command.
2277 * \param fd file descriptor.
2278 * \param drmCommandIndex command index
2279 * \param data destination pointer of the data to be read.
2280 * \param size size of the data to be read.
2282 * \return zero on success, or a negative value on failure.
2285 * It issues a read ioctl given by
2286 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2288 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2291 unsigned long request;
2293 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2294 DRM_COMMAND_BASE + drmCommandIndex, size);
2296 if (drmIoctl(fd, request, data)) {
2304 * Send a device-specific write command.
2306 * \param fd file descriptor.
2307 * \param drmCommandIndex command index
2308 * \param data source pointer of the data to be written.
2309 * \param size size of the data to be written.
2311 * \return zero on success, or a negative value on failure.
2314 * It issues a write ioctl given by
2315 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2317 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2320 unsigned long request;
2322 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2323 DRM_COMMAND_BASE + drmCommandIndex, size);
2325 if (drmIoctl(fd, request, data)) {
2333 * Send a device-specific read-write command.
2335 * \param fd file descriptor.
2336 * \param drmCommandIndex command index
2337 * \param data source pointer of the data to be read and written.
2338 * \param size size of the data to be read and written.
2340 * \return zero on success, or a negative value on failure.
2343 * It issues a read-write ioctl given by
2344 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2346 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2349 unsigned long request;
2351 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2352 DRM_COMMAND_BASE + drmCommandIndex, size);
2354 if (drmIoctl(fd, request, data))
2362 * DRM_FENCE_FLAG_EMIT
2363 * DRM_FENCE_FLAG_SHAREABLE
2364 * DRM_FENCE_MASK_DRIVER
2367 int drmFenceCreate(int fd, unsigned flags, int fence_class, unsigned type,
2370 drm_fence_arg_t arg;
2372 memset(&arg, 0, sizeof(arg));
2375 arg.fence_class = fence_class;
2377 if (drmIoctl(fd, DRM_IOCTL_FENCE_CREATE, &arg))
2379 fence->handle = arg.handle;
2380 fence->fence_class = arg.fence_class;
2381 fence->type = arg.type;
2382 fence->flags = arg.flags;
2383 fence->signaled = 0;
2389 * DRM_FENCE_FLAG_SHAREABLE
2390 * DRM_FENCE_MASK_DRIVER
2393 int drmFenceBuffers(int fd, unsigned flags, uint32_t fence_class, drmFence *fence)
2395 drm_fence_arg_t arg;
2397 memset(&arg, 0, sizeof(arg));
2399 arg.fence_class = fence_class;
2401 if (drmIoctl(fd, DRM_IOCTL_FENCE_BUFFERS, &arg))
2403 fence->handle = arg.handle;
2404 fence->fence_class = arg.fence_class;
2405 fence->type = arg.type;
2406 fence->flags = arg.flags;
2407 fence->sequence = arg.sequence;
2408 fence->signaled = 0;
2412 int drmFenceReference(int fd, unsigned handle, drmFence *fence)
2414 drm_fence_arg_t arg;
2416 memset(&arg, 0, sizeof(arg));
2417 arg.handle = handle;
2419 if (drmIoctl(fd, DRM_IOCTL_FENCE_REFERENCE, &arg))
2421 fence->handle = arg.handle;
2422 fence->fence_class = arg.fence_class;
2423 fence->type = arg.type;
2424 fence->flags = arg.flags;
2425 fence->signaled = arg.signaled;
2429 int drmFenceUnreference(int fd, const drmFence *fence)
2431 drm_fence_arg_t arg;
2433 memset(&arg, 0, sizeof(arg));
2434 arg.handle = fence->handle;
2436 if (drmIoctl(fd, DRM_IOCTL_FENCE_UNREFERENCE, &arg))
2441 int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type)
2443 drm_fence_arg_t arg;
2445 memset(&arg, 0, sizeof(arg));
2446 arg.handle = fence->handle;
2447 arg.type = flush_type;
2449 if (drmIoctl(fd, DRM_IOCTL_FENCE_FLUSH, &arg))
2451 fence->fence_class = arg.fence_class;
2452 fence->type = arg.type;
2453 fence->signaled = arg.signaled;
2457 int drmFenceUpdate(int fd, drmFence *fence)
2459 drm_fence_arg_t arg;
2461 memset(&arg, 0, sizeof(arg));
2462 arg.handle = fence->handle;
2464 if (drmIoctl(fd, DRM_IOCTL_FENCE_SIGNALED, &arg))
2466 fence->fence_class = arg.fence_class;
2467 fence->type = arg.type;
2468 fence->signaled = arg.signaled;
2472 int drmFenceSignaled(int fd, drmFence *fence, unsigned fenceType,
2475 if ((fence->flags & DRM_FENCE_FLAG_SHAREABLE) ||
2476 ((fenceType & fence->signaled) != fenceType)) {
2477 int ret = drmFenceFlush(fd, fence, fenceType);
2482 *signaled = ((fenceType & fence->signaled) == fenceType);
2489 * DRM_FENCE_FLAG_SHAREABLE
2490 * DRM_FENCE_MASK_DRIVER
2494 int drmFenceEmit(int fd, unsigned flags, drmFence *fence, unsigned emit_type)
2496 drm_fence_arg_t arg;
2498 memset(&arg, 0, sizeof(arg));
2499 arg.fence_class = fence->fence_class;
2501 arg.handle = fence->handle;
2502 arg.type = emit_type;
2504 if (drmIoctl(fd, DRM_IOCTL_FENCE_EMIT, &arg))
2506 fence->fence_class = arg.fence_class;
2507 fence->type = arg.type;
2508 fence->signaled = arg.signaled;
2509 fence->sequence = arg.sequence;
2515 * DRM_FENCE_FLAG_WAIT_LAZY
2516 * DRM_FENCE_FLAG_WAIT_IGNORE_SIGNALS
2519 #define DRM_IOCTL_TIMEOUT_USEC 3000000UL
2521 static unsigned long
2522 drmTimeDiff(struct timeval *now, struct timeval *then)
2526 val = now->tv_sec - then->tv_sec;
2528 val += now->tv_usec;
2529 val -= then->tv_usec;
2531 return (unsigned long) val;
2535 drmIoctlTimeout(int fd, unsigned long request, void *argp)
2538 struct timeval then, now;
2542 ret = drmIoctl(fd, request, argp);
2543 if (ret != 0 && errno == EAGAIN) {
2545 gettimeofday(&then, NULL);
2548 gettimeofday(&now, NULL);
2550 } while (ret != 0 && errno == EAGAIN &&
2551 drmTimeDiff(&now, &then) < DRM_IOCTL_TIMEOUT_USEC);
2554 return ((errno == EAGAIN) ? -EBUSY : -errno);
2562 int drmFenceWait(int fd, unsigned flags, drmFence *fence, unsigned flush_type)
2564 drm_fence_arg_t arg;
2567 if (flush_type == 0) {
2568 flush_type = fence->type;
2571 if (!(fence->flags & DRM_FENCE_FLAG_SHAREABLE)) {
2572 if ((flush_type & fence->signaled) == flush_type) {
2577 memset(&arg, 0, sizeof(arg));
2578 arg.handle = fence->handle;
2579 arg.type = flush_type;
2583 ret = drmIoctlTimeout(fd, DRM_IOCTL_FENCE_WAIT, &arg);
2587 fence->fence_class = arg.fence_class;
2588 fence->type = arg.type;
2589 fence->signaled = arg.signaled;
2593 static void drmBOCopyReply(const struct drm_bo_info_rep *rep, drmBO *buf)
2595 buf->handle = rep->handle;
2596 buf->flags = rep->flags;
2597 buf->size = rep->size;
2598 buf->offset = rep->offset;
2599 buf->mapHandle = rep->arg_handle;
2600 buf->proposedFlags = rep->proposed_flags;
2601 buf->start = rep->buffer_start;
2602 buf->fenceFlags = rep->fence_flags;
2603 buf->replyFlags = rep->rep_flags;
2604 buf->pageAlignment = rep->page_alignment;
2605 buf->tileInfo = rep->tile_info;
2606 buf->hwTileStride = rep->hw_tile_stride;
2607 buf->desiredTileStride = rep->desired_tile_stride;
2612 int drmBOCreate(int fd, unsigned long size,
2613 unsigned pageAlignment, void *user_buffer,
2615 unsigned hint, drmBO *buf)
2617 struct drm_bo_create_arg arg;
2618 struct drm_bo_create_req *req = &arg.d.req;
2619 struct drm_bo_info_rep *rep = &arg.d.rep;
2622 memset(buf, 0, sizeof(*buf));
2623 memset(&arg, 0, sizeof(arg));
2627 req->page_alignment = pageAlignment;
2628 req->buffer_start = (unsigned long) user_buffer;
2630 buf->virtual = NULL;
2632 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_CREATE, &arg);
2636 drmBOCopyReply(rep, buf);
2637 buf->virtual = user_buffer;
2643 int drmBOReference(int fd, unsigned handle, drmBO *buf)
2645 struct drm_bo_reference_info_arg arg;
2646 struct drm_bo_handle_arg *req = &arg.d.req;
2647 struct drm_bo_info_rep *rep = &arg.d.rep;
2649 memset(&arg, 0, sizeof(arg));
2650 req->handle = handle;
2652 if (drmIoctl(fd, DRM_IOCTL_BO_REFERENCE, &arg))
2655 drmBOCopyReply(rep, buf);
2656 buf->mapVirtual = NULL;
2658 buf->virtual = NULL;
2663 int drmBOUnreference(int fd, drmBO *buf)
2665 struct drm_bo_handle_arg arg;
2667 if (buf->mapVirtual && buf->mapHandle) {
2668 (void) munmap(buf->mapVirtual, buf->start + buf->size);
2669 buf->mapVirtual = NULL;
2670 buf->virtual = NULL;
2673 memset(&arg, 0, sizeof(arg));
2674 arg.handle = buf->handle;
2676 if (drmIoctl(fd, DRM_IOCTL_BO_UNREFERENCE, &arg))
2685 * Flags can be DRM_BO_FLAG_READ, DRM_BO_FLAG_WRITE or'ed together
2686 * Hint currently be DRM_BO_HINT_DONT_BLOCK, which makes the
2687 * call return an -EBUSY if it can' immediately honor the mapping request.
2690 int drmBOMap(int fd, drmBO *buf, unsigned mapFlags, unsigned mapHint,
2693 struct drm_bo_map_wait_idle_arg arg;
2694 struct drm_bo_info_req *req = &arg.d.req;
2695 struct drm_bo_info_rep *rep = &arg.d.rep;
2699 * Make sure we have a virtual address of the buffer.
2702 if (!buf->virtual) {
2704 virtual = mmap(0, buf->size + buf->start,
2705 PROT_READ | PROT_WRITE, MAP_SHARED,
2706 fd, buf->mapHandle);
2707 if (virtual == MAP_FAILED) {
2712 buf->mapVirtual = virtual;
2713 buf->virtual = ((char *) virtual) + buf->start;
2716 memset(&arg, 0, sizeof(arg));
2717 req->handle = buf->handle;
2718 req->mask = mapFlags;
2719 req->hint = mapHint;
2722 * May hang if the buffer object is busy.
2723 * This IOCTL synchronizes the buffer.
2726 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_MAP, &arg);
2730 drmBOCopyReply(rep, buf);
2731 buf->mapFlags = mapFlags;
2733 *address = buf->virtual;
2739 int drmBOUnmap(int fd, drmBO *buf)
2741 struct drm_bo_handle_arg arg;
2743 memset(&arg, 0, sizeof(arg));
2744 arg.handle = buf->handle;
2746 if (drmIoctl(fd, DRM_IOCTL_BO_UNMAP, &arg)) {
2753 int drmBOSetStatus(int fd, drmBO *buf,
2754 uint64_t flags, uint64_t mask,
2756 unsigned int desired_tile_stride,
2757 unsigned int tile_info)
2760 struct drm_bo_map_wait_idle_arg arg;
2761 struct drm_bo_info_req *req = &arg.d.req;
2762 struct drm_bo_info_rep *rep = &arg.d.rep;
2765 memset(&arg, 0, sizeof(arg));
2768 req->handle = buf->handle;
2770 req->desired_tile_stride = desired_tile_stride;
2771 req->tile_info = tile_info;
2773 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_SETSTATUS, &arg);
2777 drmBOCopyReply(rep, buf);
2782 int drmBOInfo(int fd, drmBO *buf)
2784 struct drm_bo_reference_info_arg arg;
2785 struct drm_bo_handle_arg *req = &arg.d.req;
2786 struct drm_bo_info_rep *rep = &arg.d.rep;
2789 memset(&arg, 0, sizeof(arg));
2790 req->handle = buf->handle;
2792 ret = drmIoctl(fd, DRM_IOCTL_BO_INFO, &arg);
2796 drmBOCopyReply(rep, buf);
2800 int drmBOWaitIdle(int fd, drmBO *buf, unsigned hint)
2802 struct drm_bo_map_wait_idle_arg arg;
2803 struct drm_bo_info_req *req = &arg.d.req;
2804 struct drm_bo_info_rep *rep = &arg.d.rep;
2807 if ((buf->flags & DRM_BO_FLAG_SHAREABLE) ||
2808 (buf->replyFlags & DRM_BO_REP_BUSY)) {
2809 memset(&arg, 0, sizeof(arg));
2810 req->handle = buf->handle;
2813 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_WAIT_IDLE, &arg);
2817 drmBOCopyReply(rep, buf);
2822 int drmBOBusy(int fd, drmBO *buf, int *busy)
2824 int ret = drmBOInfo(fd, buf);
2829 *busy = (buf->replyFlags & DRM_BO_REP_BUSY);
2833 int drmMMInit(int fd, unsigned long pOffset, unsigned long pSize,
2836 struct drm_mm_init_arg arg;
2838 memset(&arg, 0, sizeof(arg));
2840 arg.magic = DRM_BO_INIT_MAGIC;
2841 arg.major = DRM_BO_INIT_MAJOR;
2842 arg.minor = DRM_BO_INIT_MINOR;
2843 arg.p_offset = pOffset;
2845 arg.mem_type = memType;
2847 if (drmIoctl(fd, DRM_IOCTL_MM_INIT, &arg))
2852 int drmMMTakedown(int fd, unsigned memType)
2854 struct drm_mm_type_arg arg;
2856 memset(&arg, 0, sizeof(arg));
2857 arg.mem_type = memType;
2859 if (drmIoctl(fd, DRM_IOCTL_MM_TAKEDOWN, &arg))
2865 * If this function returns an error, and lockBM was set to 1,
2866 * the buffer manager is NOT locked.
2869 int drmMMLock(int fd, unsigned memType, int lockBM, int ignoreNoEvict)
2871 struct drm_mm_type_arg arg;
2873 memset(&arg, 0, sizeof(arg));
2874 arg.mem_type = memType;
2875 arg.lock_flags |= (lockBM) ? DRM_BO_LOCK_UNLOCK_BM : 0;
2876 arg.lock_flags |= (ignoreNoEvict) ? DRM_BO_LOCK_IGNORE_NO_EVICT : 0;
2878 return drmIoctlTimeout(fd, DRM_IOCTL_MM_LOCK, &arg);
2881 int drmMMUnlock(int fd, unsigned memType, int unlockBM)
2883 struct drm_mm_type_arg arg;
2885 memset(&arg, 0, sizeof(arg));
2887 arg.mem_type = memType;
2888 arg.lock_flags |= (unlockBM) ? DRM_BO_LOCK_UNLOCK_BM : 0;
2890 return drmIoctlTimeout(fd, DRM_IOCTL_MM_UNLOCK, &arg);
2893 int drmMMInfo(int fd, unsigned memType, uint64_t *size)
2895 struct drm_mm_info_arg arg;
2897 memset(&arg, 0, sizeof(arg));
2899 arg.mem_type = memType;
2901 if (drmIoctl(fd, DRM_IOCTL_MM_INFO, &arg))
2908 int drmBOVersion(int fd, unsigned int *major,
2909 unsigned int *minor,
2910 unsigned int *patchlevel)
2912 struct drm_bo_version_arg arg;
2915 memset(&arg, 0, sizeof(arg));
2916 ret = drmIoctl(fd, DRM_IOCTL_BO_VERSION, &arg);
2925 *patchlevel = arg.patchlevel;
2932 #define DRM_MAX_FDS 16
2937 } connection[DRM_MAX_FDS];
2939 static int nr_fds = 0;
2941 int drmOpenOnce(void *unused,
2948 for (i = 0; i < nr_fds; i++)
2949 if (strcmp(BusID, connection[i].BusID) == 0) {
2950 connection[i].refcount++;
2952 return connection[i].fd;
2955 fd = drmOpen(unused, BusID);
2956 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2959 connection[nr_fds].BusID = strdup(BusID);
2960 connection[nr_fds].fd = fd;
2961 connection[nr_fds].refcount = 1;
2965 fprintf(stderr, "saved connection %d for %s %d\n",
2966 nr_fds, connection[nr_fds].BusID,
2967 strcmp(BusID, connection[nr_fds].BusID));
2974 void drmCloseOnce(int fd)
2978 for (i = 0; i < nr_fds; i++) {
2979 if (fd == connection[i].fd) {
2980 if (--connection[i].refcount == 0) {
2981 drmClose(connection[i].fd);
2982 free(connection[i].BusID);
2985 connection[i] = connection[nr_fds];
2993 int drmSetMaster(int fd)
2997 fprintf(stderr,"Setting master \n");
2998 ret = ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
3002 int drmDropMaster(int fd)
3005 fprintf(stderr,"Dropping master \n");
3006 ret = ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);