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.
47 #include <sys/types.h>
49 #define stat_t struct stat
50 #include <sys/ioctl.h>
55 /* Not all systems have MAP_FAILED defined */
57 #define MAP_FAILED ((void *)-1)
62 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
75 #define DRM_MAJOR 226 /* Linux */
79 * This definition needs to be changed on some systems if dev_t is a structure.
80 * If there is a header file we can get it from, there would be best.
83 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
86 #define DRM_MSG_VERBOSITY 3
88 #define DRM_NODE_CONTROL 0
89 #define DRM_NODE_RENDER 1
91 static drmServerInfoPtr drm_server_info;
93 void drmSetServerInfo(drmServerInfoPtr info)
95 drm_server_info = info;
99 * Output a message to stderr.
101 * \param format printf() like format string.
104 * This function is a wrapper around vfprintf().
107 static int drmDebugPrint(const char *format, va_list ap)
109 return vfprintf(stderr, format, ap);
112 static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
115 drmMsg(const char *format, ...)
119 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
121 va_start(ap, format);
122 if (drm_server_info) {
123 drm_server_info->debug_print(format,ap);
125 drm_debug_print(format, ap);
132 drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
134 drm_debug_print = debug_msg_ptr;
137 static void *drmHashTable = NULL; /* Context switch callbacks */
139 void *drmGetHashTable(void)
144 void *drmMalloc(int size)
147 if ((pt = malloc(size)))
152 void drmFree(void *pt)
159 * Call ioctl, restarting if it is interupted
162 drmIoctl(int fd, unsigned long request, void *arg)
167 ret = ioctl(fd, request, arg);
168 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
172 static unsigned long drmGetKeyFromFd(int fd)
181 drmHashEntry *drmGetEntry(int fd)
183 unsigned long key = drmGetKeyFromFd(fd);
188 drmHashTable = drmHashCreate();
190 if (drmHashLookup(drmHashTable, key, &value)) {
191 entry = drmMalloc(sizeof(*entry));
194 entry->tagTable = drmHashCreate();
195 drmHashInsert(drmHashTable, key, entry);
203 * Compare two busid strings
208 * \return 1 if matched.
211 * This function compares two bus ID strings. It understands the older
212 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
213 * domain, b is bus, d is device, f is function.
215 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
217 /* First, check if the IDs are exactly the same */
218 if (strcasecmp(id1, id2) == 0)
221 /* Try to match old/new-style PCI bus IDs. */
222 if (strncasecmp(id1, "pci", 3) == 0) {
223 unsigned int o1, b1, d1, f1;
224 unsigned int o2, b2, d2, f2;
227 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
230 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
235 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
238 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
243 /* If domains aren't properly supported by the kernel interface,
244 * just ignore them, which sucks less than picking a totally random
245 * card with "open by name"
250 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
259 * Handles error checking for chown call.
261 * \param path to file.
262 * \param id of the new owner.
263 * \param id of the new group.
265 * \return zero if success or -1 if failure.
268 * Checks for failure. If failure was caused by signal call chown again.
269 * If any other failure happened then it will output error mesage using
272 static int chown_check_return(const char *path, uid_t owner, gid_t group)
277 rv = chown(path, owner, group);
278 } while (rv != 0 && errno == EINTR);
283 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
284 path, errno, strerror(errno));
289 * Open the DRM device, creating it if necessary.
291 * \param dev major and minor numbers of the device.
292 * \param minor minor number of the device.
294 * \return a file descriptor on success, or a negative value on error.
297 * Assembles the device name from \p minor and opens it, creating the device
298 * special file node with the major and minor numbers specified by \p dev and
299 * parent directory if necessary and was called by root.
301 static int drmOpenDevice(long dev, int minor, int type)
306 mode_t devmode = DRM_DEV_MODE, serv_mode;
307 int isroot = !geteuid();
308 uid_t user = DRM_DEV_UID;
309 gid_t group = DRM_DEV_GID, serv_group;
311 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
312 drmMsg("drmOpenDevice: node name is %s\n", buf);
314 if (drm_server_info) {
315 drm_server_info->get_perms(&serv_group, &serv_mode);
316 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
317 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
318 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
322 if (stat(DRM_DIR_NAME, &st)) {
324 return DRM_ERR_NOT_ROOT;
325 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
326 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
327 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
330 /* Check if the device node exists and create it if necessary. */
331 if (stat(buf, &st)) {
333 return DRM_ERR_NOT_ROOT;
335 mknod(buf, S_IFCHR | devmode, dev);
338 if (drm_server_info) {
339 chown_check_return(buf, user, group);
343 /* if we modprobed then wait for udev */
347 if (stat(DRM_DIR_NAME, &st)) {
351 if (udev_count == 50)
356 if (stat(buf, &st)) {
360 if (udev_count == 50)
367 fd = open(buf, O_RDWR, 0);
368 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
369 fd, fd < 0 ? strerror(errno) : "OK");
374 /* Check if the device node is not what we expect it to be, and recreate it
375 * and try again if so.
377 if (st.st_rdev != dev) {
379 return DRM_ERR_NOT_ROOT;
381 mknod(buf, S_IFCHR | devmode, dev);
382 if (drm_server_info) {
383 chown_check_return(buf, user, group);
387 fd = open(buf, O_RDWR, 0);
388 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
389 fd, fd < 0 ? strerror(errno) : "OK");
393 drmMsg("drmOpenDevice: Open failed\n");
401 * Open the DRM device
403 * \param minor device minor number.
404 * \param create allow to create the device if set.
406 * \return a file descriptor on success, or a negative value on error.
409 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
410 * name from \p minor and opens it.
412 static int drmOpenMinor(int minor, int create, int type)
418 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
420 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
421 if ((fd = open(buf, O_RDWR, 0)) >= 0)
428 * Determine whether the DRM kernel driver has been loaded.
430 * \return 1 if the DRM driver is loaded, 0 otherwise.
433 * Determine the presence of the kernel driver by attempting to open the 0
434 * minor and get version information. For backward compatibility with older
435 * Linux implementations, /proc/dri is also checked.
437 int drmAvailable(void)
439 drmVersionPtr version;
443 if ((fd = drmOpenMinor(0, 1, DRM_NODE_RENDER)) < 0) {
445 /* Try proc for backward Linux compatibility */
446 if (!access("/proc/dri/0", R_OK))
452 if ((version = drmGetVersion(fd))) {
454 drmFreeVersion(version);
463 * Open the device by bus ID.
465 * \param busid bus ID.
467 * \return a file descriptor on success, or a negative value on error.
470 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
471 * comparing the device bus ID with the one supplied.
473 * \sa drmOpenMinor() and drmGetBusid().
475 static int drmOpenByBusid(const char *busid)
477 int i, pci_domain_ok = 1;
482 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
483 for (i = 0; i < DRM_MAX_MINOR; i++) {
484 fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
485 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
487 /* We need to try for 1.4 first for proper PCI domain support
488 * and if that fails, we know the kernel is busted
492 sv.drm_dd_major = -1; /* Don't care */
493 sv.drm_dd_minor = -1; /* Don't care */
494 if (drmSetInterfaceVersion(fd, &sv)) {
500 sv.drm_dd_major = -1; /* Don't care */
501 sv.drm_dd_minor = -1; /* Don't care */
502 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n",fd);
503 drmSetInterfaceVersion(fd, &sv);
505 buf = drmGetBusid(fd);
506 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
507 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
521 * Open the device by name.
523 * \param name driver name.
525 * \return a file descriptor on success, or a negative value on error.
528 * This function opens the first minor number that matches the driver name and
529 * isn't already in use. If it's in use it then it will already have a bus ID
532 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
534 static int drmOpenByName(const char *name)
538 drmVersionPtr version;
541 if (!drmAvailable()) {
542 if (!drm_server_info) {
546 /* try to load the kernel module now */
547 if (!drm_server_info->load_module(name)) {
548 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
555 * Open the first minor number that matches the driver name and isn't
556 * already in use. If it's in use it will have a busid assigned already.
558 for (i = 0; i < DRM_MAX_MINOR; i++) {
559 if ((fd = drmOpenMinor(i, 1, DRM_NODE_RENDER)) >= 0) {
560 if ((version = drmGetVersion(fd))) {
561 if (!strcmp(version->name, name)) {
562 drmFreeVersion(version);
563 id = drmGetBusid(fd);
564 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
573 drmFreeVersion(version);
581 /* Backward-compatibility /proc support */
582 for (i = 0; i < 8; i++) {
583 char proc_name[64], buf[512];
584 char *driver, *pt, *devstring;
587 sprintf(proc_name, "/proc/dri/%d/name", i);
588 if ((fd = open(proc_name, 0, 0)) >= 0) {
589 retcode = read(fd, buf, sizeof(buf)-1);
592 buf[retcode-1] = '\0';
593 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
595 if (*pt) { /* Device is next */
597 if (!strcmp(driver, name)) { /* Match */
598 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
600 if (*pt) { /* Found busid */
601 return drmOpenByBusid(++pt);
602 } else { /* No busid */
603 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_RENDER);
617 * Open the DRM device.
619 * Looks up the specified name and bus ID, and opens the device found. The
620 * entry in /dev/dri is created if necessary and if called by root.
622 * \param name driver name. Not referenced if bus ID is supplied.
623 * \param busid bus ID. Zero if not known.
625 * \return a file descriptor on success, or a negative value on error.
628 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
631 int drmOpen(const char *name, const char *busid)
633 if (!drmAvailable() && name != NULL && drm_server_info) {
634 /* try to load the kernel */
635 if (!drm_server_info->load_module(name)) {
636 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
642 int fd = drmOpenByBusid(busid);
648 return drmOpenByName(name);
653 int drmOpenControl(int minor)
655 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
659 * Free the version information returned by drmGetVersion().
661 * \param v pointer to the version information.
664 * It frees the memory pointed by \p %v as well as all the non-null strings
667 void drmFreeVersion(drmVersionPtr v)
679 * Free the non-public version information returned by the kernel.
681 * \param v pointer to the version information.
684 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
685 * the non-null strings pointers in it.
687 static void drmFreeKernelVersion(drm_version_t *v)
699 * Copy version information.
701 * \param d destination pointer.
702 * \param s source pointer.
705 * Used by drmGetVersion() to translate the information returned by the ioctl
706 * interface in a private structure into the public structure counterpart.
708 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
710 d->version_major = s->version_major;
711 d->version_minor = s->version_minor;
712 d->version_patchlevel = s->version_patchlevel;
713 d->name_len = s->name_len;
714 d->name = strdup(s->name);
715 d->date_len = s->date_len;
716 d->date = strdup(s->date);
717 d->desc_len = s->desc_len;
718 d->desc = strdup(s->desc);
723 * Query the driver version information.
725 * \param fd file descriptor.
727 * \return pointer to a drmVersion structure which should be freed with
730 * \note Similar information is available via /proc/dri.
733 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
734 * first with zeros to get the string lengths, and then the actually strings.
735 * It also null-terminates them since they might not be already.
737 drmVersionPtr drmGetVersion(int fd)
739 drmVersionPtr retval;
740 drm_version_t *version = drmMalloc(sizeof(*version));
742 version->name_len = 0;
743 version->name = NULL;
744 version->date_len = 0;
745 version->date = NULL;
746 version->desc_len = 0;
747 version->desc = NULL;
749 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
750 drmFreeKernelVersion(version);
754 if (version->name_len)
755 version->name = drmMalloc(version->name_len + 1);
756 if (version->date_len)
757 version->date = drmMalloc(version->date_len + 1);
758 if (version->desc_len)
759 version->desc = drmMalloc(version->desc_len + 1);
761 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
762 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
763 drmFreeKernelVersion(version);
767 /* The results might not be null-terminated strings, so terminate them. */
768 if (version->name_len) version->name[version->name_len] = '\0';
769 if (version->date_len) version->date[version->date_len] = '\0';
770 if (version->desc_len) version->desc[version->desc_len] = '\0';
772 retval = drmMalloc(sizeof(*retval));
773 drmCopyVersion(retval, version);
774 drmFreeKernelVersion(version);
780 * Get version information for the DRM user space library.
782 * This version number is driver independent.
784 * \param fd file descriptor.
786 * \return version information.
789 * This function allocates and fills a drm_version structure with a hard coded
792 drmVersionPtr drmGetLibVersion(int fd)
794 drm_version_t *version = drmMalloc(sizeof(*version));
797 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
798 * revision 1.0.x = original DRM interface with no drmGetLibVersion
799 * entry point and many drm<Device> extensions
800 * revision 1.1.x = added drmCommand entry points for device extensions
801 * added drmGetLibVersion to identify libdrm.a version
802 * revision 1.2.x = added drmSetInterfaceVersion
803 * modified drmOpen to handle both busid and name
804 * revision 1.3.x = added server + memory manager
806 version->version_major = 1;
807 version->version_minor = 3;
808 version->version_patchlevel = 0;
810 return (drmVersionPtr)version;
813 int drmGetCap(int fd, uint64_t capability, uint64_t *value)
815 struct drm_get_cap cap = { capability, 0 };
818 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
826 int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
828 struct drm_set_client_cap cap = { capability, value };
830 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
834 * Free the bus ID information.
836 * \param busid bus ID information string as given by drmGetBusid().
839 * This function is just frees the memory pointed by \p busid.
841 void drmFreeBusid(const char *busid)
843 drmFree((void *)busid);
848 * Get the bus ID of the device.
850 * \param fd file descriptor.
852 * \return bus ID string.
855 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
856 * get the string length and data, passing the arguments in a drm_unique
859 char *drmGetBusid(int fd)
866 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
868 u.unique = drmMalloc(u.unique_len + 1);
869 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
871 u.unique[u.unique_len] = '\0';
878 * Set the bus ID of the device.
880 * \param fd file descriptor.
881 * \param busid bus ID string.
883 * \return zero on success, negative on failure.
886 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
887 * the arguments in a drm_unique structure.
889 int drmSetBusid(int fd, const char *busid)
893 u.unique = (char *)busid;
894 u.unique_len = strlen(busid);
896 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
902 int drmGetMagic(int fd, drm_magic_t * magic)
907 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
913 int drmAuthMagic(int fd, drm_magic_t magic)
918 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
924 * Specifies a range of memory that is available for mapping by a
927 * \param fd file descriptor.
928 * \param offset usually the physical address. The actual meaning depends of
929 * the \p type parameter. See below.
930 * \param size of the memory in bytes.
931 * \param type type of the memory to be mapped.
932 * \param flags combination of several flags to modify the function actions.
933 * \param handle will be set to a value that may be used as the offset
934 * parameter for mmap().
936 * \return zero on success or a negative value on error.
938 * \par Mapping the frame buffer
939 * For the frame buffer
940 * - \p offset will be the physical address of the start of the frame buffer,
941 * - \p size will be the size of the frame buffer in bytes, and
942 * - \p type will be DRM_FRAME_BUFFER.
945 * The area mapped will be uncached. If MTRR support is available in the
946 * kernel, the frame buffer area will be set to write combining.
948 * \par Mapping the MMIO register area
949 * For the MMIO register area,
950 * - \p offset will be the physical address of the start of the register area,
951 * - \p size will be the size of the register area bytes, and
952 * - \p type will be DRM_REGISTERS.
954 * The area mapped will be uncached.
956 * \par Mapping the SAREA
958 * - \p offset will be ignored and should be set to zero,
959 * - \p size will be the desired size of the SAREA in bytes,
960 * - \p type will be DRM_SHM.
963 * A shared memory area of the requested size will be created and locked in
964 * kernel memory. This area may be mapped into client-space by using the handle
967 * \note May only be called by root.
970 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
971 * the arguments in a drm_map structure.
973 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
974 drmMapFlags flags, drm_handle_t *handle)
983 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
986 *handle = (drm_handle_t)(uintptr_t)map.handle;
990 int drmRmMap(int fd, drm_handle_t handle)
994 map.handle = (void *)(uintptr_t)handle;
996 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1002 * Make buffers available for DMA transfers.
1004 * \param fd file descriptor.
1005 * \param count number of buffers.
1006 * \param size size of each buffer.
1007 * \param flags buffer allocation flags.
1008 * \param agp_offset offset in the AGP aperture
1010 * \return number of buffers allocated, negative on error.
1013 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1017 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1020 drm_buf_desc_t request;
1022 request.count = count;
1023 request.size = size;
1024 request.low_mark = 0;
1025 request.high_mark = 0;
1026 request.flags = flags;
1027 request.agp_start = agp_offset;
1029 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1031 return request.count;
1034 int drmMarkBufs(int fd, double low, double high)
1036 drm_buf_info_t info;
1042 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1048 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1051 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1052 int retval = -errno;
1057 for (i = 0; i < info.count; i++) {
1058 info.list[i].low_mark = low * info.list[i].count;
1059 info.list[i].high_mark = high * info.list[i].count;
1060 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1061 int retval = -errno;
1074 * \param fd file descriptor.
1075 * \param count number of buffers to free.
1076 * \param list list of buffers to be freed.
1078 * \return zero on success, or a negative value on failure.
1080 * \note This function is primarily used for debugging.
1083 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1084 * the arguments in a drm_buf_free structure.
1086 int drmFreeBufs(int fd, int count, int *list)
1088 drm_buf_free_t request;
1090 request.count = count;
1091 request.list = list;
1092 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1101 * \param fd file descriptor.
1104 * This function closes the file descriptor.
1106 int drmClose(int fd)
1108 unsigned long key = drmGetKeyFromFd(fd);
1109 drmHashEntry *entry = drmGetEntry(fd);
1111 drmHashDestroy(entry->tagTable);
1114 entry->tagTable = NULL;
1116 drmHashDelete(drmHashTable, key);
1124 * Map a region of memory.
1126 * \param fd file descriptor.
1127 * \param handle handle returned by drmAddMap().
1128 * \param size size in bytes. Must match the size used by drmAddMap().
1129 * \param address will contain the user-space virtual address where the mapping
1132 * \return zero on success, or a negative value on failure.
1135 * This function is a wrapper for mmap().
1137 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1139 static unsigned long pagesize_mask = 0;
1145 pagesize_mask = getpagesize() - 1;
1147 size = (size + pagesize_mask) & ~pagesize_mask;
1149 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1150 if (*address == MAP_FAILED)
1157 * Unmap mappings obtained with drmMap().
1159 * \param address address as given by drmMap().
1160 * \param size size in bytes. Must match the size used by drmMap().
1162 * \return zero on success, or a negative value on failure.
1165 * This function is a wrapper for munmap().
1167 int drmUnmap(drmAddress address, drmSize size)
1169 return munmap(address, size);
1172 drmBufInfoPtr drmGetBufInfo(int fd)
1174 drm_buf_info_t info;
1175 drmBufInfoPtr retval;
1181 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1185 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1188 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1193 retval = drmMalloc(sizeof(*retval));
1194 retval->count = info.count;
1195 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1196 for (i = 0; i < info.count; i++) {
1197 retval->list[i].count = info.list[i].count;
1198 retval->list[i].size = info.list[i].size;
1199 retval->list[i].low_mark = info.list[i].low_mark;
1200 retval->list[i].high_mark = info.list[i].high_mark;
1209 * Map all DMA buffers into client-virtual space.
1211 * \param fd file descriptor.
1213 * \return a pointer to a ::drmBufMap structure.
1215 * \note The client may not use these buffers until obtaining buffer indices
1219 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1220 * information about the buffers in a drm_buf_map structure into the
1221 * client-visible data structures.
1223 drmBufMapPtr drmMapBufs(int fd)
1226 drmBufMapPtr retval;
1231 bufs.virtual = NULL;
1232 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1238 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1241 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1246 retval = drmMalloc(sizeof(*retval));
1247 retval->count = bufs.count;
1248 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1249 for (i = 0; i < bufs.count; i++) {
1250 retval->list[i].idx = bufs.list[i].idx;
1251 retval->list[i].total = bufs.list[i].total;
1252 retval->list[i].used = 0;
1253 retval->list[i].address = bufs.list[i].address;
1263 * Unmap buffers allocated with drmMapBufs().
1265 * \return zero on success, or negative value on failure.
1268 * Calls munmap() for every buffer stored in \p bufs and frees the
1269 * memory allocated by drmMapBufs().
1271 int drmUnmapBufs(drmBufMapPtr bufs)
1275 for (i = 0; i < bufs->count; i++) {
1276 munmap(bufs->list[i].address, bufs->list[i].total);
1279 drmFree(bufs->list);
1286 #define DRM_DMA_RETRY 16
1289 * Reserve DMA buffers.
1291 * \param fd file descriptor.
1294 * \return zero on success, or a negative value on failure.
1297 * Assemble the arguments into a drm_dma structure and keeps issuing the
1298 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1300 int drmDMA(int fd, drmDMAReqPtr request)
1305 dma.context = request->context;
1306 dma.send_count = request->send_count;
1307 dma.send_indices = request->send_list;
1308 dma.send_sizes = request->send_sizes;
1309 dma.flags = request->flags;
1310 dma.request_count = request->request_count;
1311 dma.request_size = request->request_size;
1312 dma.request_indices = request->request_list;
1313 dma.request_sizes = request->request_sizes;
1314 dma.granted_count = 0;
1317 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1318 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1321 request->granted_count = dma.granted_count;
1330 * Obtain heavyweight hardware lock.
1332 * \param fd file descriptor.
1333 * \param context context.
1334 * \param flags flags that determine the sate of the hardware when the function
1337 * \return always zero.
1340 * This function translates the arguments into a drm_lock structure and issue
1341 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1343 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1347 lock.context = context;
1349 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1350 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1351 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1352 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1353 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1354 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1356 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1362 * Release the hardware lock.
1364 * \param fd file descriptor.
1365 * \param context context.
1367 * \return zero on success, or a negative value on failure.
1370 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1371 * argument in a drm_lock structure.
1373 int drmUnlock(int fd, drm_context_t context)
1377 lock.context = context;
1379 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1382 drm_context_t *drmGetReservedContextList(int fd, int *count)
1386 drm_context_t * retval;
1390 res.contexts = NULL;
1391 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1397 if (!(list = drmMalloc(res.count * sizeof(*list))))
1399 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1404 res.contexts = list;
1405 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1408 for (i = 0; i < res.count; i++)
1409 retval[i] = list[i].handle;
1416 void drmFreeReservedContextList(drm_context_t *pt)
1424 * Used by the X server during GLXContext initialization. This causes
1425 * per-context kernel-level resources to be allocated.
1427 * \param fd file descriptor.
1428 * \param handle is set on success. To be used by the client when requesting DMA
1429 * dispatch with drmDMA().
1431 * \return zero on success, or a negative value on failure.
1433 * \note May only be called by root.
1436 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1437 * argument in a drm_ctx structure.
1439 int drmCreateContext(int fd, drm_context_t *handle)
1443 ctx.flags = 0; /* Modified with functions below */
1444 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1446 *handle = ctx.handle;
1450 int drmSwitchToContext(int fd, drm_context_t context)
1454 ctx.handle = context;
1455 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1460 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1465 * Context preserving means that no context switches are done between DMA
1466 * buffers from one context and the next. This is suitable for use in the
1467 * X server (which promises to maintain hardware context), or in the
1468 * client-side library when buffers are swapped on behalf of two threads.
1470 ctx.handle = context;
1472 if (flags & DRM_CONTEXT_PRESERVED)
1473 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1474 if (flags & DRM_CONTEXT_2DONLY)
1475 ctx.flags |= _DRM_CONTEXT_2DONLY;
1476 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1481 int drmGetContextFlags(int fd, drm_context_t context,
1482 drm_context_tFlagsPtr flags)
1486 ctx.handle = context;
1487 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1490 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1491 *flags |= DRM_CONTEXT_PRESERVED;
1492 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1493 *flags |= DRM_CONTEXT_2DONLY;
1500 * Free any kernel-level resources allocated with drmCreateContext() associated
1503 * \param fd file descriptor.
1504 * \param handle handle given by drmCreateContext().
1506 * \return zero on success, or a negative value on failure.
1508 * \note May only be called by root.
1511 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1512 * argument in a drm_ctx structure.
1514 int drmDestroyContext(int fd, drm_context_t handle)
1517 ctx.handle = handle;
1518 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1523 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1526 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1528 *handle = draw.handle;
1532 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1535 draw.handle = handle;
1536 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1541 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1542 drm_drawable_info_type_t type, unsigned int num,
1545 drm_update_draw_t update;
1547 update.handle = handle;
1550 update.data = (unsigned long long)(unsigned long)data;
1552 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1559 * Acquire the AGP device.
1561 * Must be called before any of the other AGP related calls.
1563 * \param fd file descriptor.
1565 * \return zero on success, or a negative value on failure.
1568 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1570 int drmAgpAcquire(int fd)
1572 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1579 * Release the AGP device.
1581 * \param fd file descriptor.
1583 * \return zero on success, or a negative value on failure.
1586 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1588 int drmAgpRelease(int fd)
1590 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1599 * \param fd file descriptor.
1600 * \param mode AGP mode.
1602 * \return zero on success, or a negative value on failure.
1605 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1606 * argument in a drm_agp_mode structure.
1608 int drmAgpEnable(int fd, unsigned long mode)
1613 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1620 * Allocate a chunk of AGP memory.
1622 * \param fd file descriptor.
1623 * \param size requested memory size in bytes. Will be rounded to page boundary.
1624 * \param type type of memory to allocate.
1625 * \param address if not zero, will be set to the physical address of the
1627 * \param handle on success will be set to a handle of the allocated memory.
1629 * \return zero on success, or a negative value on failure.
1632 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1633 * arguments in a drm_agp_buffer structure.
1635 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1636 unsigned long *address, drm_handle_t *handle)
1640 *handle = DRM_AGP_NO_HANDLE;
1644 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1647 *address = b.physical;
1654 * Free a chunk of AGP memory.
1656 * \param fd file descriptor.
1657 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1659 * \return zero on success, or a negative value on failure.
1662 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1663 * argument in a drm_agp_buffer structure.
1665 int drmAgpFree(int fd, drm_handle_t handle)
1671 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1678 * Bind a chunk of AGP memory.
1680 * \param fd file descriptor.
1681 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1682 * \param offset offset in bytes. It will round to page boundary.
1684 * \return zero on success, or a negative value on failure.
1687 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1688 * argument in a drm_agp_binding structure.
1690 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1692 drm_agp_binding_t b;
1696 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1703 * Unbind a chunk of AGP memory.
1705 * \param fd file descriptor.
1706 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1708 * \return zero on success, or a negative value on failure.
1711 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1712 * the argument in a drm_agp_binding structure.
1714 int drmAgpUnbind(int fd, drm_handle_t handle)
1716 drm_agp_binding_t b;
1720 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1727 * Get AGP driver major version number.
1729 * \param fd file descriptor.
1731 * \return major version number on success, or a negative value on failure..
1734 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1735 * necessary information in a drm_agp_info structure.
1737 int drmAgpVersionMajor(int fd)
1741 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1743 return i.agp_version_major;
1748 * Get AGP driver minor version number.
1750 * \param fd file descriptor.
1752 * \return minor version number on success, or a negative value on failure.
1755 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1756 * necessary information in a drm_agp_info structure.
1758 int drmAgpVersionMinor(int fd)
1762 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1764 return i.agp_version_minor;
1771 * \param fd file descriptor.
1773 * \return mode on success, or zero on failure.
1776 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1777 * necessary information in a drm_agp_info structure.
1779 unsigned long drmAgpGetMode(int fd)
1783 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1790 * Get AGP aperture base.
1792 * \param fd file descriptor.
1794 * \return aperture base on success, zero on failure.
1797 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1798 * necessary information in a drm_agp_info structure.
1800 unsigned long drmAgpBase(int fd)
1804 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1806 return i.aperture_base;
1811 * Get AGP aperture size.
1813 * \param fd file descriptor.
1815 * \return aperture size on success, zero on failure.
1818 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1819 * necessary information in a drm_agp_info structure.
1821 unsigned long drmAgpSize(int fd)
1825 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1827 return i.aperture_size;
1832 * Get used AGP memory.
1834 * \param fd file descriptor.
1836 * \return memory used on success, or zero on failure.
1839 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1840 * necessary information in a drm_agp_info structure.
1842 unsigned long drmAgpMemoryUsed(int fd)
1846 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1848 return i.memory_used;
1853 * Get available AGP memory.
1855 * \param fd file descriptor.
1857 * \return memory available on success, or zero on failure.
1860 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1861 * necessary information in a drm_agp_info structure.
1863 unsigned long drmAgpMemoryAvail(int fd)
1867 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1869 return i.memory_allowed;
1874 * Get hardware vendor ID.
1876 * \param fd file descriptor.
1878 * \return vendor ID on success, or zero on failure.
1881 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1882 * necessary information in a drm_agp_info structure.
1884 unsigned int drmAgpVendorId(int fd)
1888 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1895 * Get hardware device ID.
1897 * \param fd file descriptor.
1899 * \return zero on success, or zero on failure.
1902 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1903 * necessary information in a drm_agp_info structure.
1905 unsigned int drmAgpDeviceId(int fd)
1909 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1914 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1916 drm_scatter_gather_t sg;
1921 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1923 *handle = sg.handle;
1927 int drmScatterGatherFree(int fd, drm_handle_t handle)
1929 drm_scatter_gather_t sg;
1933 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
1941 * \param fd file descriptor.
1942 * \param vbl pointer to a drmVBlank structure.
1944 * \return zero on success, or a negative value on failure.
1947 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1949 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1951 struct timespec timeout, cur;
1954 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
1956 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
1962 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1963 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1964 if (ret && errno == EINTR) {
1965 clock_gettime(CLOCK_MONOTONIC, &cur);
1966 /* Timeout after 1s */
1967 if (cur.tv_sec > timeout.tv_sec + 1 ||
1968 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
1975 } while (ret && errno == EINTR);
1981 int drmError(int err, const char *label)
1984 case DRM_ERR_NO_DEVICE:
1985 fprintf(stderr, "%s: no device\n", label);
1987 case DRM_ERR_NO_ACCESS:
1988 fprintf(stderr, "%s: no access\n", label);
1990 case DRM_ERR_NOT_ROOT:
1991 fprintf(stderr, "%s: not root\n", label);
1993 case DRM_ERR_INVALID:
1994 fprintf(stderr, "%s: invalid args\n", label);
1999 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2007 * Install IRQ handler.
2009 * \param fd file descriptor.
2010 * \param irq IRQ number.
2012 * \return zero on success, or a negative value on failure.
2015 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2016 * argument in a drm_control structure.
2018 int drmCtlInstHandler(int fd, int irq)
2022 ctl.func = DRM_INST_HANDLER;
2024 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2031 * Uninstall IRQ handler.
2033 * \param fd file descriptor.
2035 * \return zero on success, or a negative value on failure.
2038 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2039 * argument in a drm_control structure.
2041 int drmCtlUninstHandler(int fd)
2045 ctl.func = DRM_UNINST_HANDLER;
2047 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2052 int drmFinish(int fd, int context, drmLockFlags flags)
2056 lock.context = context;
2058 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2059 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2060 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2061 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2062 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2063 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2064 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2070 * Get IRQ from bus ID.
2072 * \param fd file descriptor.
2073 * \param busnum bus number.
2074 * \param devnum device number.
2075 * \param funcnum function number.
2077 * \return IRQ number on success, or a negative value on failure.
2080 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2081 * arguments in a drm_irq_busid structure.
2083 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2089 p.funcnum = funcnum;
2090 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2095 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2097 drmHashEntry *entry = drmGetEntry(fd);
2099 if (drmHashInsert(entry->tagTable, context, tag)) {
2100 drmHashDelete(entry->tagTable, context);
2101 drmHashInsert(entry->tagTable, context, tag);
2106 int drmDelContextTag(int fd, drm_context_t context)
2108 drmHashEntry *entry = drmGetEntry(fd);
2110 return drmHashDelete(entry->tagTable, context);
2113 void *drmGetContextTag(int fd, drm_context_t context)
2115 drmHashEntry *entry = drmGetEntry(fd);
2118 if (drmHashLookup(entry->tagTable, context, &value))
2124 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2125 drm_handle_t handle)
2127 drm_ctx_priv_map_t map;
2129 map.ctx_id = ctx_id;
2130 map.handle = (void *)(uintptr_t)handle;
2132 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2137 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2138 drm_handle_t *handle)
2140 drm_ctx_priv_map_t map;
2142 map.ctx_id = ctx_id;
2144 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2147 *handle = (drm_handle_t)(uintptr_t)map.handle;
2152 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2153 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2159 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2161 *offset = map.offset;
2165 *handle = (unsigned long)map.handle;
2170 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2171 unsigned long *magic, unsigned long *iocs)
2173 drm_client_t client;
2176 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2178 *auth = client.auth;
2181 *magic = client.magic;
2182 *iocs = client.iocs;
2186 int drmGetStats(int fd, drmStatsT *stats)
2191 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2195 memset(stats, 0, sizeof(*stats));
2196 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2200 stats->data[i].long_format = "%-20.20s"; \
2201 stats->data[i].rate_format = "%8.8s"; \
2202 stats->data[i].isvalue = 1; \
2203 stats->data[i].verbose = 0
2206 stats->data[i].long_format = "%-20.20s"; \
2207 stats->data[i].rate_format = "%5.5s"; \
2208 stats->data[i].isvalue = 0; \
2209 stats->data[i].mult_names = "kgm"; \
2210 stats->data[i].mult = 1000; \
2211 stats->data[i].verbose = 0
2214 stats->data[i].long_format = "%-20.20s"; \
2215 stats->data[i].rate_format = "%5.5s"; \
2216 stats->data[i].isvalue = 0; \
2217 stats->data[i].mult_names = "KGM"; \
2218 stats->data[i].mult = 1024; \
2219 stats->data[i].verbose = 0
2222 stats->count = s.count;
2223 for (i = 0; i < s.count; i++) {
2224 stats->data[i].value = s.data[i].value;
2225 switch (s.data[i].type) {
2226 case _DRM_STAT_LOCK:
2227 stats->data[i].long_name = "Lock";
2228 stats->data[i].rate_name = "Lock";
2231 case _DRM_STAT_OPENS:
2232 stats->data[i].long_name = "Opens";
2233 stats->data[i].rate_name = "O";
2235 stats->data[i].verbose = 1;
2237 case _DRM_STAT_CLOSES:
2238 stats->data[i].long_name = "Closes";
2239 stats->data[i].rate_name = "Lock";
2241 stats->data[i].verbose = 1;
2243 case _DRM_STAT_IOCTLS:
2244 stats->data[i].long_name = "Ioctls";
2245 stats->data[i].rate_name = "Ioc/s";
2248 case _DRM_STAT_LOCKS:
2249 stats->data[i].long_name = "Locks";
2250 stats->data[i].rate_name = "Lck/s";
2253 case _DRM_STAT_UNLOCKS:
2254 stats->data[i].long_name = "Unlocks";
2255 stats->data[i].rate_name = "Unl/s";
2259 stats->data[i].long_name = "IRQs";
2260 stats->data[i].rate_name = "IRQ/s";
2263 case _DRM_STAT_PRIMARY:
2264 stats->data[i].long_name = "Primary Bytes";
2265 stats->data[i].rate_name = "PB/s";
2268 case _DRM_STAT_SECONDARY:
2269 stats->data[i].long_name = "Secondary Bytes";
2270 stats->data[i].rate_name = "SB/s";
2274 stats->data[i].long_name = "DMA";
2275 stats->data[i].rate_name = "DMA/s";
2278 case _DRM_STAT_SPECIAL:
2279 stats->data[i].long_name = "Special DMA";
2280 stats->data[i].rate_name = "dma/s";
2283 case _DRM_STAT_MISSED:
2284 stats->data[i].long_name = "Miss";
2285 stats->data[i].rate_name = "Ms/s";
2288 case _DRM_STAT_VALUE:
2289 stats->data[i].long_name = "Value";
2290 stats->data[i].rate_name = "Value";
2293 case _DRM_STAT_BYTE:
2294 stats->data[i].long_name = "Bytes";
2295 stats->data[i].rate_name = "B/s";
2298 case _DRM_STAT_COUNT:
2300 stats->data[i].long_name = "Count";
2301 stats->data[i].rate_name = "Cnt/s";
2310 * Issue a set-version ioctl.
2312 * \param fd file descriptor.
2313 * \param drmCommandIndex command index
2314 * \param data source pointer of the data to be read and written.
2315 * \param size size of the data to be read and written.
2317 * \return zero on success, or a negative value on failure.
2320 * It issues a read-write ioctl given by
2321 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2323 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2326 drm_set_version_t sv;
2328 sv.drm_di_major = version->drm_di_major;
2329 sv.drm_di_minor = version->drm_di_minor;
2330 sv.drm_dd_major = version->drm_dd_major;
2331 sv.drm_dd_minor = version->drm_dd_minor;
2333 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2337 version->drm_di_major = sv.drm_di_major;
2338 version->drm_di_minor = sv.drm_di_minor;
2339 version->drm_dd_major = sv.drm_dd_major;
2340 version->drm_dd_minor = sv.drm_dd_minor;
2346 * Send a device-specific command.
2348 * \param fd file descriptor.
2349 * \param drmCommandIndex command index
2351 * \return zero on success, or a negative value on failure.
2354 * It issues a ioctl given by
2355 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2357 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2359 void *data = NULL; /* dummy */
2360 unsigned long request;
2362 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2364 if (drmIoctl(fd, request, data)) {
2372 * Send a device-specific read command.
2374 * \param fd file descriptor.
2375 * \param drmCommandIndex command index
2376 * \param data destination pointer of the data to be read.
2377 * \param size size of the data to be read.
2379 * \return zero on success, or a negative value on failure.
2382 * It issues a read ioctl given by
2383 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2385 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2388 unsigned long request;
2390 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2391 DRM_COMMAND_BASE + drmCommandIndex, size);
2393 if (drmIoctl(fd, request, data)) {
2401 * Send a device-specific write command.
2403 * \param fd file descriptor.
2404 * \param drmCommandIndex command index
2405 * \param data source pointer of the data to be written.
2406 * \param size size of the data to be written.
2408 * \return zero on success, or a negative value on failure.
2411 * It issues a write ioctl given by
2412 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2414 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2417 unsigned long request;
2419 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2420 DRM_COMMAND_BASE + drmCommandIndex, size);
2422 if (drmIoctl(fd, request, data)) {
2430 * Send a device-specific read-write command.
2432 * \param fd file descriptor.
2433 * \param drmCommandIndex command index
2434 * \param data source pointer of the data to be read and written.
2435 * \param size size of the data to be read and written.
2437 * \return zero on success, or a negative value on failure.
2440 * It issues a read-write ioctl given by
2441 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2443 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2446 unsigned long request;
2448 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2449 DRM_COMMAND_BASE + drmCommandIndex, size);
2451 if (drmIoctl(fd, request, data))
2456 #define DRM_MAX_FDS 16
2461 } connection[DRM_MAX_FDS];
2463 static int nr_fds = 0;
2465 int drmOpenOnce(void *unused,
2472 for (i = 0; i < nr_fds; i++)
2473 if (strcmp(BusID, connection[i].BusID) == 0) {
2474 connection[i].refcount++;
2476 return connection[i].fd;
2479 fd = drmOpen(unused, BusID);
2480 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2483 connection[nr_fds].BusID = strdup(BusID);
2484 connection[nr_fds].fd = fd;
2485 connection[nr_fds].refcount = 1;
2489 fprintf(stderr, "saved connection %d for %s %d\n",
2490 nr_fds, connection[nr_fds].BusID,
2491 strcmp(BusID, connection[nr_fds].BusID));
2498 void drmCloseOnce(int fd)
2502 for (i = 0; i < nr_fds; i++) {
2503 if (fd == connection[i].fd) {
2504 if (--connection[i].refcount == 0) {
2505 drmClose(connection[i].fd);
2506 free(connection[i].BusID);
2509 connection[i] = connection[nr_fds];
2517 int drmSetMaster(int fd)
2519 return ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
2522 int drmDropMaster(int fd)
2524 return ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);
2527 char *drmGetDeviceNameFromFd(int fd)
2534 /* The whole drmOpen thing is a fiasco and we need to find a way
2535 * back to just using open(2). For now, however, lets just make
2536 * things worse with even more ad hoc directory walking code to
2537 * discover the device file name. */
2542 for (i = 0; i < DRM_MAX_MINOR; i++) {
2543 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2544 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2547 if (i == DRM_MAX_MINOR)
2550 return strdup(name);
2553 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2555 struct drm_prime_handle args;
2558 args.handle = handle;
2560 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2564 *prime_fd = args.fd;
2568 int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2570 struct drm_prime_handle args;
2575 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2579 *handle = args.handle;