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 DRM_PRINTFLIKE(1, 0)
108 drmDebugPrint(const char *format, va_list ap)
110 return vfprintf(stderr, format, ap);
113 typedef int DRM_PRINTFLIKE(1, 0) (*debug_msg_func_t)(const char *format,
116 static debug_msg_func_t drm_debug_print = drmDebugPrint;
119 drmMsg(const char *format, ...)
123 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
125 va_start(ap, format);
126 if (drm_server_info) {
127 drm_server_info->debug_print(format,ap);
129 drm_debug_print(format, ap);
136 drmSetDebugMsgFunction(debug_msg_func_t debug_msg_ptr)
138 drm_debug_print = debug_msg_ptr;
141 static void *drmHashTable = NULL; /* Context switch callbacks */
143 void *drmGetHashTable(void)
148 void *drmMalloc(int size)
151 if ((pt = malloc(size)))
156 void drmFree(void *pt)
163 * Call ioctl, restarting if it is interupted
166 drmIoctl(int fd, unsigned long request, void *arg)
171 ret = ioctl(fd, request, arg);
172 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
176 static unsigned long drmGetKeyFromFd(int fd)
185 drmHashEntry *drmGetEntry(int fd)
187 unsigned long key = drmGetKeyFromFd(fd);
192 drmHashTable = drmHashCreate();
194 if (drmHashLookup(drmHashTable, key, &value)) {
195 entry = drmMalloc(sizeof(*entry));
198 entry->tagTable = drmHashCreate();
199 drmHashInsert(drmHashTable, key, entry);
207 * Compare two busid strings
212 * \return 1 if matched.
215 * This function compares two bus ID strings. It understands the older
216 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
217 * domain, b is bus, d is device, f is function.
219 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
221 /* First, check if the IDs are exactly the same */
222 if (strcasecmp(id1, id2) == 0)
225 /* Try to match old/new-style PCI bus IDs. */
226 if (strncasecmp(id1, "pci", 3) == 0) {
227 unsigned int o1, b1, d1, f1;
228 unsigned int o2, b2, d2, f2;
231 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
234 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
239 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
242 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
247 /* If domains aren't properly supported by the kernel interface,
248 * just ignore them, which sucks less than picking a totally random
249 * card with "open by name"
254 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
263 * Handles error checking for chown call.
265 * \param path to file.
266 * \param id of the new owner.
267 * \param id of the new group.
269 * \return zero if success or -1 if failure.
272 * Checks for failure. If failure was caused by signal call chown again.
273 * If any other failure happened then it will output error mesage using
276 static int chown_check_return(const char *path, uid_t owner, gid_t group)
281 rv = chown(path, owner, group);
282 } while (rv != 0 && errno == EINTR);
287 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
288 path, errno, strerror(errno));
293 * Open the DRM device, creating it if necessary.
295 * \param dev major and minor numbers of the device.
296 * \param minor minor number of the device.
298 * \return a file descriptor on success, or a negative value on error.
301 * Assembles the device name from \p minor and opens it, creating the device
302 * special file node with the major and minor numbers specified by \p dev and
303 * parent directory if necessary and was called by root.
305 static int drmOpenDevice(long dev, int minor, int type)
310 mode_t devmode = DRM_DEV_MODE, serv_mode;
311 int isroot = !geteuid();
312 uid_t user = DRM_DEV_UID;
313 gid_t group = DRM_DEV_GID, serv_group;
315 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
316 drmMsg("drmOpenDevice: node name is %s\n", buf);
318 if (drm_server_info) {
319 drm_server_info->get_perms(&serv_group, &serv_mode);
320 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
321 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
322 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
326 if (stat(DRM_DIR_NAME, &st)) {
328 return DRM_ERR_NOT_ROOT;
329 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
330 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
331 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
334 /* Check if the device node exists and create it if necessary. */
335 if (stat(buf, &st)) {
337 return DRM_ERR_NOT_ROOT;
339 mknod(buf, S_IFCHR | devmode, dev);
342 if (drm_server_info) {
343 chown_check_return(buf, user, group);
347 /* if we modprobed then wait for udev */
351 if (stat(DRM_DIR_NAME, &st)) {
355 if (udev_count == 50)
360 if (stat(buf, &st)) {
364 if (udev_count == 50)
371 fd = open(buf, O_RDWR, 0);
372 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
373 fd, fd < 0 ? strerror(errno) : "OK");
378 /* Check if the device node is not what we expect it to be, and recreate it
379 * and try again if so.
381 if (st.st_rdev != dev) {
383 return DRM_ERR_NOT_ROOT;
385 mknod(buf, S_IFCHR | devmode, dev);
386 if (drm_server_info) {
387 chown_check_return(buf, user, group);
391 fd = open(buf, O_RDWR, 0);
392 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
393 fd, fd < 0 ? strerror(errno) : "OK");
397 drmMsg("drmOpenDevice: Open failed\n");
405 * Open the DRM device
407 * \param minor device minor number.
408 * \param create allow to create the device if set.
410 * \return a file descriptor on success, or a negative value on error.
413 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
414 * name from \p minor and opens it.
416 static int drmOpenMinor(int minor, int create, int type)
422 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
424 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
425 if ((fd = open(buf, O_RDWR, 0)) >= 0)
432 * Determine whether the DRM kernel driver has been loaded.
434 * \return 1 if the DRM driver is loaded, 0 otherwise.
437 * Determine the presence of the kernel driver by attempting to open the 0
438 * minor and get version information. For backward compatibility with older
439 * Linux implementations, /proc/dri is also checked.
441 int drmAvailable(void)
443 drmVersionPtr version;
447 if ((fd = drmOpenMinor(0, 1, DRM_NODE_RENDER)) < 0) {
449 /* Try proc for backward Linux compatibility */
450 if (!access("/proc/dri/0", R_OK))
456 if ((version = drmGetVersion(fd))) {
458 drmFreeVersion(version);
467 * Open the device by bus ID.
469 * \param busid bus ID.
471 * \return a file descriptor on success, or a negative value on error.
474 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
475 * comparing the device bus ID with the one supplied.
477 * \sa drmOpenMinor() and drmGetBusid().
479 static int drmOpenByBusid(const char *busid)
481 int i, pci_domain_ok = 1;
486 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
487 for (i = 0; i < DRM_MAX_MINOR; i++) {
488 fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
489 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
491 /* We need to try for 1.4 first for proper PCI domain support
492 * and if that fails, we know the kernel is busted
496 sv.drm_dd_major = -1; /* Don't care */
497 sv.drm_dd_minor = -1; /* Don't care */
498 if (drmSetInterfaceVersion(fd, &sv)) {
504 sv.drm_dd_major = -1; /* Don't care */
505 sv.drm_dd_minor = -1; /* Don't care */
506 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n",fd);
507 drmSetInterfaceVersion(fd, &sv);
509 buf = drmGetBusid(fd);
510 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
511 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
525 * Open the device by name.
527 * \param name driver name.
529 * \return a file descriptor on success, or a negative value on error.
532 * This function opens the first minor number that matches the driver name and
533 * isn't already in use. If it's in use it then it will already have a bus ID
536 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
538 static int drmOpenByName(const char *name)
542 drmVersionPtr version;
545 if (!drmAvailable()) {
546 if (!drm_server_info) {
550 /* try to load the kernel module now */
551 if (!drm_server_info->load_module(name)) {
552 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
559 * Open the first minor number that matches the driver name and isn't
560 * already in use. If it's in use it will have a busid assigned already.
562 for (i = 0; i < DRM_MAX_MINOR; i++) {
563 if ((fd = drmOpenMinor(i, 1, DRM_NODE_RENDER)) >= 0) {
564 if ((version = drmGetVersion(fd))) {
565 if (!strcmp(version->name, name)) {
566 drmFreeVersion(version);
567 id = drmGetBusid(fd);
568 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
577 drmFreeVersion(version);
585 /* Backward-compatibility /proc support */
586 for (i = 0; i < 8; i++) {
587 char proc_name[64], buf[512];
588 char *driver, *pt, *devstring;
591 sprintf(proc_name, "/proc/dri/%d/name", i);
592 if ((fd = open(proc_name, 0, 0)) >= 0) {
593 retcode = read(fd, buf, sizeof(buf)-1);
596 buf[retcode-1] = '\0';
597 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
599 if (*pt) { /* Device is next */
601 if (!strcmp(driver, name)) { /* Match */
602 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
604 if (*pt) { /* Found busid */
605 return drmOpenByBusid(++pt);
606 } else { /* No busid */
607 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_RENDER);
621 * Open the DRM device.
623 * Looks up the specified name and bus ID, and opens the device found. The
624 * entry in /dev/dri is created if necessary and if called by root.
626 * \param name driver name. Not referenced if bus ID is supplied.
627 * \param busid bus ID. Zero if not known.
629 * \return a file descriptor on success, or a negative value on error.
632 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
635 int drmOpen(const char *name, const char *busid)
637 if (!drmAvailable() && name != NULL && drm_server_info) {
638 /* try to load the kernel */
639 if (!drm_server_info->load_module(name)) {
640 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
646 int fd = drmOpenByBusid(busid);
652 return drmOpenByName(name);
657 int drmOpenControl(int minor)
659 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
663 * Free the version information returned by drmGetVersion().
665 * \param v pointer to the version information.
668 * It frees the memory pointed by \p %v as well as all the non-null strings
671 void drmFreeVersion(drmVersionPtr v)
683 * Free the non-public version information returned by the kernel.
685 * \param v pointer to the version information.
688 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
689 * the non-null strings pointers in it.
691 static void drmFreeKernelVersion(drm_version_t *v)
703 * Copy version information.
705 * \param d destination pointer.
706 * \param s source pointer.
709 * Used by drmGetVersion() to translate the information returned by the ioctl
710 * interface in a private structure into the public structure counterpart.
712 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
714 d->version_major = s->version_major;
715 d->version_minor = s->version_minor;
716 d->version_patchlevel = s->version_patchlevel;
717 d->name_len = s->name_len;
718 d->name = strdup(s->name);
719 d->date_len = s->date_len;
720 d->date = strdup(s->date);
721 d->desc_len = s->desc_len;
722 d->desc = strdup(s->desc);
727 * Query the driver version information.
729 * \param fd file descriptor.
731 * \return pointer to a drmVersion structure which should be freed with
734 * \note Similar information is available via /proc/dri.
737 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
738 * first with zeros to get the string lengths, and then the actually strings.
739 * It also null-terminates them since they might not be already.
741 drmVersionPtr drmGetVersion(int fd)
743 drmVersionPtr retval;
744 drm_version_t *version = drmMalloc(sizeof(*version));
746 version->name_len = 0;
747 version->name = NULL;
748 version->date_len = 0;
749 version->date = NULL;
750 version->desc_len = 0;
751 version->desc = NULL;
753 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
754 drmFreeKernelVersion(version);
758 if (version->name_len)
759 version->name = drmMalloc(version->name_len + 1);
760 if (version->date_len)
761 version->date = drmMalloc(version->date_len + 1);
762 if (version->desc_len)
763 version->desc = drmMalloc(version->desc_len + 1);
765 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
766 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
767 drmFreeKernelVersion(version);
771 /* The results might not be null-terminated strings, so terminate them. */
772 if (version->name_len) version->name[version->name_len] = '\0';
773 if (version->date_len) version->date[version->date_len] = '\0';
774 if (version->desc_len) version->desc[version->desc_len] = '\0';
776 retval = drmMalloc(sizeof(*retval));
777 drmCopyVersion(retval, version);
778 drmFreeKernelVersion(version);
784 * Get version information for the DRM user space library.
786 * This version number is driver independent.
788 * \param fd file descriptor.
790 * \return version information.
793 * This function allocates and fills a drm_version structure with a hard coded
796 drmVersionPtr drmGetLibVersion(int fd)
798 drm_version_t *version = drmMalloc(sizeof(*version));
801 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
802 * revision 1.0.x = original DRM interface with no drmGetLibVersion
803 * entry point and many drm<Device> extensions
804 * revision 1.1.x = added drmCommand entry points for device extensions
805 * added drmGetLibVersion to identify libdrm.a version
806 * revision 1.2.x = added drmSetInterfaceVersion
807 * modified drmOpen to handle both busid and name
808 * revision 1.3.x = added server + memory manager
810 version->version_major = 1;
811 version->version_minor = 3;
812 version->version_patchlevel = 0;
814 return (drmVersionPtr)version;
817 int drmGetCap(int fd, uint64_t capability, uint64_t *value)
819 struct drm_get_cap cap = { capability, 0 };
822 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
830 int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
832 struct drm_set_client_cap cap = { capability, value };
834 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
838 * Free the bus ID information.
840 * \param busid bus ID information string as given by drmGetBusid().
843 * This function is just frees the memory pointed by \p busid.
845 void drmFreeBusid(const char *busid)
847 drmFree((void *)busid);
852 * Get the bus ID of the device.
854 * \param fd file descriptor.
856 * \return bus ID string.
859 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
860 * get the string length and data, passing the arguments in a drm_unique
863 char *drmGetBusid(int fd)
870 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
872 u.unique = drmMalloc(u.unique_len + 1);
873 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
875 u.unique[u.unique_len] = '\0';
882 * Set the bus ID of the device.
884 * \param fd file descriptor.
885 * \param busid bus ID string.
887 * \return zero on success, negative on failure.
890 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
891 * the arguments in a drm_unique structure.
893 int drmSetBusid(int fd, const char *busid)
897 u.unique = (char *)busid;
898 u.unique_len = strlen(busid);
900 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
906 int drmGetMagic(int fd, drm_magic_t * magic)
911 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
917 int drmAuthMagic(int fd, drm_magic_t magic)
922 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
928 * Specifies a range of memory that is available for mapping by a
931 * \param fd file descriptor.
932 * \param offset usually the physical address. The actual meaning depends of
933 * the \p type parameter. See below.
934 * \param size of the memory in bytes.
935 * \param type type of the memory to be mapped.
936 * \param flags combination of several flags to modify the function actions.
937 * \param handle will be set to a value that may be used as the offset
938 * parameter for mmap().
940 * \return zero on success or a negative value on error.
942 * \par Mapping the frame buffer
943 * For the frame buffer
944 * - \p offset will be the physical address of the start of the frame buffer,
945 * - \p size will be the size of the frame buffer in bytes, and
946 * - \p type will be DRM_FRAME_BUFFER.
949 * The area mapped will be uncached. If MTRR support is available in the
950 * kernel, the frame buffer area will be set to write combining.
952 * \par Mapping the MMIO register area
953 * For the MMIO register area,
954 * - \p offset will be the physical address of the start of the register area,
955 * - \p size will be the size of the register area bytes, and
956 * - \p type will be DRM_REGISTERS.
958 * The area mapped will be uncached.
960 * \par Mapping the SAREA
962 * - \p offset will be ignored and should be set to zero,
963 * - \p size will be the desired size of the SAREA in bytes,
964 * - \p type will be DRM_SHM.
967 * A shared memory area of the requested size will be created and locked in
968 * kernel memory. This area may be mapped into client-space by using the handle
971 * \note May only be called by root.
974 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
975 * the arguments in a drm_map structure.
977 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
978 drmMapFlags flags, drm_handle_t *handle)
987 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
990 *handle = (drm_handle_t)(uintptr_t)map.handle;
994 int drmRmMap(int fd, drm_handle_t handle)
998 map.handle = (void *)(uintptr_t)handle;
1000 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1006 * Make buffers available for DMA transfers.
1008 * \param fd file descriptor.
1009 * \param count number of buffers.
1010 * \param size size of each buffer.
1011 * \param flags buffer allocation flags.
1012 * \param agp_offset offset in the AGP aperture
1014 * \return number of buffers allocated, negative on error.
1017 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1021 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1024 drm_buf_desc_t request;
1026 request.count = count;
1027 request.size = size;
1028 request.low_mark = 0;
1029 request.high_mark = 0;
1030 request.flags = flags;
1031 request.agp_start = agp_offset;
1033 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1035 return request.count;
1038 int drmMarkBufs(int fd, double low, double high)
1040 drm_buf_info_t info;
1046 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1052 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1055 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1056 int retval = -errno;
1061 for (i = 0; i < info.count; i++) {
1062 info.list[i].low_mark = low * info.list[i].count;
1063 info.list[i].high_mark = high * info.list[i].count;
1064 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1065 int retval = -errno;
1078 * \param fd file descriptor.
1079 * \param count number of buffers to free.
1080 * \param list list of buffers to be freed.
1082 * \return zero on success, or a negative value on failure.
1084 * \note This function is primarily used for debugging.
1087 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1088 * the arguments in a drm_buf_free structure.
1090 int drmFreeBufs(int fd, int count, int *list)
1092 drm_buf_free_t request;
1094 request.count = count;
1095 request.list = list;
1096 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1105 * \param fd file descriptor.
1108 * This function closes the file descriptor.
1110 int drmClose(int fd)
1112 unsigned long key = drmGetKeyFromFd(fd);
1113 drmHashEntry *entry = drmGetEntry(fd);
1115 drmHashDestroy(entry->tagTable);
1118 entry->tagTable = NULL;
1120 drmHashDelete(drmHashTable, key);
1128 * Map a region of memory.
1130 * \param fd file descriptor.
1131 * \param handle handle returned by drmAddMap().
1132 * \param size size in bytes. Must match the size used by drmAddMap().
1133 * \param address will contain the user-space virtual address where the mapping
1136 * \return zero on success, or a negative value on failure.
1139 * This function is a wrapper for mmap().
1141 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1143 static unsigned long pagesize_mask = 0;
1149 pagesize_mask = getpagesize() - 1;
1151 size = (size + pagesize_mask) & ~pagesize_mask;
1153 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1154 if (*address == MAP_FAILED)
1161 * Unmap mappings obtained with drmMap().
1163 * \param address address as given by drmMap().
1164 * \param size size in bytes. Must match the size used by drmMap().
1166 * \return zero on success, or a negative value on failure.
1169 * This function is a wrapper for munmap().
1171 int drmUnmap(drmAddress address, drmSize size)
1173 return munmap(address, size);
1176 drmBufInfoPtr drmGetBufInfo(int fd)
1178 drm_buf_info_t info;
1179 drmBufInfoPtr retval;
1185 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1189 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1192 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1197 retval = drmMalloc(sizeof(*retval));
1198 retval->count = info.count;
1199 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1200 for (i = 0; i < info.count; i++) {
1201 retval->list[i].count = info.list[i].count;
1202 retval->list[i].size = info.list[i].size;
1203 retval->list[i].low_mark = info.list[i].low_mark;
1204 retval->list[i].high_mark = info.list[i].high_mark;
1213 * Map all DMA buffers into client-virtual space.
1215 * \param fd file descriptor.
1217 * \return a pointer to a ::drmBufMap structure.
1219 * \note The client may not use these buffers until obtaining buffer indices
1223 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1224 * information about the buffers in a drm_buf_map structure into the
1225 * client-visible data structures.
1227 drmBufMapPtr drmMapBufs(int fd)
1230 drmBufMapPtr retval;
1235 bufs.virtual = NULL;
1236 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1242 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1245 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1250 retval = drmMalloc(sizeof(*retval));
1251 retval->count = bufs.count;
1252 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1253 for (i = 0; i < bufs.count; i++) {
1254 retval->list[i].idx = bufs.list[i].idx;
1255 retval->list[i].total = bufs.list[i].total;
1256 retval->list[i].used = 0;
1257 retval->list[i].address = bufs.list[i].address;
1267 * Unmap buffers allocated with drmMapBufs().
1269 * \return zero on success, or negative value on failure.
1272 * Calls munmap() for every buffer stored in \p bufs and frees the
1273 * memory allocated by drmMapBufs().
1275 int drmUnmapBufs(drmBufMapPtr bufs)
1279 for (i = 0; i < bufs->count; i++) {
1280 munmap(bufs->list[i].address, bufs->list[i].total);
1283 drmFree(bufs->list);
1290 #define DRM_DMA_RETRY 16
1293 * Reserve DMA buffers.
1295 * \param fd file descriptor.
1298 * \return zero on success, or a negative value on failure.
1301 * Assemble the arguments into a drm_dma structure and keeps issuing the
1302 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1304 int drmDMA(int fd, drmDMAReqPtr request)
1309 dma.context = request->context;
1310 dma.send_count = request->send_count;
1311 dma.send_indices = request->send_list;
1312 dma.send_sizes = request->send_sizes;
1313 dma.flags = request->flags;
1314 dma.request_count = request->request_count;
1315 dma.request_size = request->request_size;
1316 dma.request_indices = request->request_list;
1317 dma.request_sizes = request->request_sizes;
1318 dma.granted_count = 0;
1321 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1322 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1325 request->granted_count = dma.granted_count;
1334 * Obtain heavyweight hardware lock.
1336 * \param fd file descriptor.
1337 * \param context context.
1338 * \param flags flags that determine the sate of the hardware when the function
1341 * \return always zero.
1344 * This function translates the arguments into a drm_lock structure and issue
1345 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1347 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1351 lock.context = context;
1353 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1354 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1355 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1356 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1357 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1358 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1360 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1366 * Release the hardware lock.
1368 * \param fd file descriptor.
1369 * \param context context.
1371 * \return zero on success, or a negative value on failure.
1374 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1375 * argument in a drm_lock structure.
1377 int drmUnlock(int fd, drm_context_t context)
1381 lock.context = context;
1383 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1386 drm_context_t *drmGetReservedContextList(int fd, int *count)
1390 drm_context_t * retval;
1394 res.contexts = NULL;
1395 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1401 if (!(list = drmMalloc(res.count * sizeof(*list))))
1403 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1408 res.contexts = list;
1409 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1412 for (i = 0; i < res.count; i++)
1413 retval[i] = list[i].handle;
1420 void drmFreeReservedContextList(drm_context_t *pt)
1428 * Used by the X server during GLXContext initialization. This causes
1429 * per-context kernel-level resources to be allocated.
1431 * \param fd file descriptor.
1432 * \param handle is set on success. To be used by the client when requesting DMA
1433 * dispatch with drmDMA().
1435 * \return zero on success, or a negative value on failure.
1437 * \note May only be called by root.
1440 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1441 * argument in a drm_ctx structure.
1443 int drmCreateContext(int fd, drm_context_t *handle)
1447 ctx.flags = 0; /* Modified with functions below */
1448 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1450 *handle = ctx.handle;
1454 int drmSwitchToContext(int fd, drm_context_t context)
1458 ctx.handle = context;
1459 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1464 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1469 * Context preserving means that no context switches are done between DMA
1470 * buffers from one context and the next. This is suitable for use in the
1471 * X server (which promises to maintain hardware context), or in the
1472 * client-side library when buffers are swapped on behalf of two threads.
1474 ctx.handle = context;
1476 if (flags & DRM_CONTEXT_PRESERVED)
1477 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1478 if (flags & DRM_CONTEXT_2DONLY)
1479 ctx.flags |= _DRM_CONTEXT_2DONLY;
1480 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1485 int drmGetContextFlags(int fd, drm_context_t context,
1486 drm_context_tFlagsPtr flags)
1490 ctx.handle = context;
1491 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1494 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1495 *flags |= DRM_CONTEXT_PRESERVED;
1496 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1497 *flags |= DRM_CONTEXT_2DONLY;
1504 * Free any kernel-level resources allocated with drmCreateContext() associated
1507 * \param fd file descriptor.
1508 * \param handle handle given by drmCreateContext().
1510 * \return zero on success, or a negative value on failure.
1512 * \note May only be called by root.
1515 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1516 * argument in a drm_ctx structure.
1518 int drmDestroyContext(int fd, drm_context_t handle)
1521 ctx.handle = handle;
1522 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1527 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1530 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1532 *handle = draw.handle;
1536 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1539 draw.handle = handle;
1540 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1545 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1546 drm_drawable_info_type_t type, unsigned int num,
1549 drm_update_draw_t update;
1551 update.handle = handle;
1554 update.data = (unsigned long long)(unsigned long)data;
1556 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1563 * Acquire the AGP device.
1565 * Must be called before any of the other AGP related calls.
1567 * \param fd file descriptor.
1569 * \return zero on success, or a negative value on failure.
1572 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1574 int drmAgpAcquire(int fd)
1576 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1583 * Release the AGP device.
1585 * \param fd file descriptor.
1587 * \return zero on success, or a negative value on failure.
1590 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1592 int drmAgpRelease(int fd)
1594 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1603 * \param fd file descriptor.
1604 * \param mode AGP mode.
1606 * \return zero on success, or a negative value on failure.
1609 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1610 * argument in a drm_agp_mode structure.
1612 int drmAgpEnable(int fd, unsigned long mode)
1617 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1624 * Allocate a chunk of AGP memory.
1626 * \param fd file descriptor.
1627 * \param size requested memory size in bytes. Will be rounded to page boundary.
1628 * \param type type of memory to allocate.
1629 * \param address if not zero, will be set to the physical address of the
1631 * \param handle on success will be set to a handle of the allocated memory.
1633 * \return zero on success, or a negative value on failure.
1636 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1637 * arguments in a drm_agp_buffer structure.
1639 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1640 unsigned long *address, drm_handle_t *handle)
1644 *handle = DRM_AGP_NO_HANDLE;
1648 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1651 *address = b.physical;
1658 * Free a chunk of AGP memory.
1660 * \param fd file descriptor.
1661 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1663 * \return zero on success, or a negative value on failure.
1666 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1667 * argument in a drm_agp_buffer structure.
1669 int drmAgpFree(int fd, drm_handle_t handle)
1675 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1682 * Bind a chunk of AGP memory.
1684 * \param fd file descriptor.
1685 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1686 * \param offset offset in bytes. It will round to page boundary.
1688 * \return zero on success, or a negative value on failure.
1691 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1692 * argument in a drm_agp_binding structure.
1694 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1696 drm_agp_binding_t b;
1700 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1707 * Unbind a chunk of AGP memory.
1709 * \param fd file descriptor.
1710 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1712 * \return zero on success, or a negative value on failure.
1715 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1716 * the argument in a drm_agp_binding structure.
1718 int drmAgpUnbind(int fd, drm_handle_t handle)
1720 drm_agp_binding_t b;
1724 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1731 * Get AGP driver major version number.
1733 * \param fd file descriptor.
1735 * \return major version number on success, or a negative value on failure..
1738 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1739 * necessary information in a drm_agp_info structure.
1741 int drmAgpVersionMajor(int fd)
1745 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1747 return i.agp_version_major;
1752 * Get AGP driver minor version number.
1754 * \param fd file descriptor.
1756 * \return minor version number on success, or a negative value on failure.
1759 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1760 * necessary information in a drm_agp_info structure.
1762 int drmAgpVersionMinor(int fd)
1766 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1768 return i.agp_version_minor;
1775 * \param fd file descriptor.
1777 * \return mode on success, or zero on failure.
1780 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1781 * necessary information in a drm_agp_info structure.
1783 unsigned long drmAgpGetMode(int fd)
1787 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1794 * Get AGP aperture base.
1796 * \param fd file descriptor.
1798 * \return aperture base on success, zero on failure.
1801 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1802 * necessary information in a drm_agp_info structure.
1804 unsigned long drmAgpBase(int fd)
1808 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1810 return i.aperture_base;
1815 * Get AGP aperture size.
1817 * \param fd file descriptor.
1819 * \return aperture size on success, zero on failure.
1822 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1823 * necessary information in a drm_agp_info structure.
1825 unsigned long drmAgpSize(int fd)
1829 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1831 return i.aperture_size;
1836 * Get used AGP memory.
1838 * \param fd file descriptor.
1840 * \return memory used on success, or zero on failure.
1843 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1844 * necessary information in a drm_agp_info structure.
1846 unsigned long drmAgpMemoryUsed(int fd)
1850 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1852 return i.memory_used;
1857 * Get available AGP memory.
1859 * \param fd file descriptor.
1861 * \return memory available on success, or zero on failure.
1864 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1865 * necessary information in a drm_agp_info structure.
1867 unsigned long drmAgpMemoryAvail(int fd)
1871 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1873 return i.memory_allowed;
1878 * Get hardware vendor ID.
1880 * \param fd file descriptor.
1882 * \return vendor ID on success, or zero on failure.
1885 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1886 * necessary information in a drm_agp_info structure.
1888 unsigned int drmAgpVendorId(int fd)
1892 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1899 * Get hardware device ID.
1901 * \param fd file descriptor.
1903 * \return zero on success, or zero on failure.
1906 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1907 * necessary information in a drm_agp_info structure.
1909 unsigned int drmAgpDeviceId(int fd)
1913 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1918 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1920 drm_scatter_gather_t sg;
1925 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1927 *handle = sg.handle;
1931 int drmScatterGatherFree(int fd, drm_handle_t handle)
1933 drm_scatter_gather_t sg;
1937 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
1945 * \param fd file descriptor.
1946 * \param vbl pointer to a drmVBlank structure.
1948 * \return zero on success, or a negative value on failure.
1951 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1953 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1955 struct timespec timeout, cur;
1958 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
1960 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
1966 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1967 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1968 if (ret && errno == EINTR) {
1969 clock_gettime(CLOCK_MONOTONIC, &cur);
1970 /* Timeout after 1s */
1971 if (cur.tv_sec > timeout.tv_sec + 1 ||
1972 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
1979 } while (ret && errno == EINTR);
1985 int drmError(int err, const char *label)
1988 case DRM_ERR_NO_DEVICE:
1989 fprintf(stderr, "%s: no device\n", label);
1991 case DRM_ERR_NO_ACCESS:
1992 fprintf(stderr, "%s: no access\n", label);
1994 case DRM_ERR_NOT_ROOT:
1995 fprintf(stderr, "%s: not root\n", label);
1997 case DRM_ERR_INVALID:
1998 fprintf(stderr, "%s: invalid args\n", label);
2003 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2011 * Install IRQ handler.
2013 * \param fd file descriptor.
2014 * \param irq IRQ number.
2016 * \return zero on success, or a negative value on failure.
2019 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2020 * argument in a drm_control structure.
2022 int drmCtlInstHandler(int fd, int irq)
2026 ctl.func = DRM_INST_HANDLER;
2028 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2035 * Uninstall IRQ handler.
2037 * \param fd file descriptor.
2039 * \return zero on success, or a negative value on failure.
2042 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2043 * argument in a drm_control structure.
2045 int drmCtlUninstHandler(int fd)
2049 ctl.func = DRM_UNINST_HANDLER;
2051 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2056 int drmFinish(int fd, int context, drmLockFlags flags)
2060 lock.context = context;
2062 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2063 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2064 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2065 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2066 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2067 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2068 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2074 * Get IRQ from bus ID.
2076 * \param fd file descriptor.
2077 * \param busnum bus number.
2078 * \param devnum device number.
2079 * \param funcnum function number.
2081 * \return IRQ number on success, or a negative value on failure.
2084 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2085 * arguments in a drm_irq_busid structure.
2087 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2093 p.funcnum = funcnum;
2094 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2099 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2101 drmHashEntry *entry = drmGetEntry(fd);
2103 if (drmHashInsert(entry->tagTable, context, tag)) {
2104 drmHashDelete(entry->tagTable, context);
2105 drmHashInsert(entry->tagTable, context, tag);
2110 int drmDelContextTag(int fd, drm_context_t context)
2112 drmHashEntry *entry = drmGetEntry(fd);
2114 return drmHashDelete(entry->tagTable, context);
2117 void *drmGetContextTag(int fd, drm_context_t context)
2119 drmHashEntry *entry = drmGetEntry(fd);
2122 if (drmHashLookup(entry->tagTable, context, &value))
2128 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2129 drm_handle_t handle)
2131 drm_ctx_priv_map_t map;
2133 map.ctx_id = ctx_id;
2134 map.handle = (void *)(uintptr_t)handle;
2136 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2141 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2142 drm_handle_t *handle)
2144 drm_ctx_priv_map_t map;
2146 map.ctx_id = ctx_id;
2148 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2151 *handle = (drm_handle_t)(uintptr_t)map.handle;
2156 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2157 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2163 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2165 *offset = map.offset;
2169 *handle = (unsigned long)map.handle;
2174 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2175 unsigned long *magic, unsigned long *iocs)
2177 drm_client_t client;
2180 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2182 *auth = client.auth;
2185 *magic = client.magic;
2186 *iocs = client.iocs;
2190 int drmGetStats(int fd, drmStatsT *stats)
2195 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2199 memset(stats, 0, sizeof(*stats));
2200 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2204 stats->data[i].long_format = "%-20.20s"; \
2205 stats->data[i].rate_format = "%8.8s"; \
2206 stats->data[i].isvalue = 1; \
2207 stats->data[i].verbose = 0
2210 stats->data[i].long_format = "%-20.20s"; \
2211 stats->data[i].rate_format = "%5.5s"; \
2212 stats->data[i].isvalue = 0; \
2213 stats->data[i].mult_names = "kgm"; \
2214 stats->data[i].mult = 1000; \
2215 stats->data[i].verbose = 0
2218 stats->data[i].long_format = "%-20.20s"; \
2219 stats->data[i].rate_format = "%5.5s"; \
2220 stats->data[i].isvalue = 0; \
2221 stats->data[i].mult_names = "KGM"; \
2222 stats->data[i].mult = 1024; \
2223 stats->data[i].verbose = 0
2226 stats->count = s.count;
2227 for (i = 0; i < s.count; i++) {
2228 stats->data[i].value = s.data[i].value;
2229 switch (s.data[i].type) {
2230 case _DRM_STAT_LOCK:
2231 stats->data[i].long_name = "Lock";
2232 stats->data[i].rate_name = "Lock";
2235 case _DRM_STAT_OPENS:
2236 stats->data[i].long_name = "Opens";
2237 stats->data[i].rate_name = "O";
2239 stats->data[i].verbose = 1;
2241 case _DRM_STAT_CLOSES:
2242 stats->data[i].long_name = "Closes";
2243 stats->data[i].rate_name = "Lock";
2245 stats->data[i].verbose = 1;
2247 case _DRM_STAT_IOCTLS:
2248 stats->data[i].long_name = "Ioctls";
2249 stats->data[i].rate_name = "Ioc/s";
2252 case _DRM_STAT_LOCKS:
2253 stats->data[i].long_name = "Locks";
2254 stats->data[i].rate_name = "Lck/s";
2257 case _DRM_STAT_UNLOCKS:
2258 stats->data[i].long_name = "Unlocks";
2259 stats->data[i].rate_name = "Unl/s";
2263 stats->data[i].long_name = "IRQs";
2264 stats->data[i].rate_name = "IRQ/s";
2267 case _DRM_STAT_PRIMARY:
2268 stats->data[i].long_name = "Primary Bytes";
2269 stats->data[i].rate_name = "PB/s";
2272 case _DRM_STAT_SECONDARY:
2273 stats->data[i].long_name = "Secondary Bytes";
2274 stats->data[i].rate_name = "SB/s";
2278 stats->data[i].long_name = "DMA";
2279 stats->data[i].rate_name = "DMA/s";
2282 case _DRM_STAT_SPECIAL:
2283 stats->data[i].long_name = "Special DMA";
2284 stats->data[i].rate_name = "dma/s";
2287 case _DRM_STAT_MISSED:
2288 stats->data[i].long_name = "Miss";
2289 stats->data[i].rate_name = "Ms/s";
2292 case _DRM_STAT_VALUE:
2293 stats->data[i].long_name = "Value";
2294 stats->data[i].rate_name = "Value";
2297 case _DRM_STAT_BYTE:
2298 stats->data[i].long_name = "Bytes";
2299 stats->data[i].rate_name = "B/s";
2302 case _DRM_STAT_COUNT:
2304 stats->data[i].long_name = "Count";
2305 stats->data[i].rate_name = "Cnt/s";
2314 * Issue a set-version ioctl.
2316 * \param fd file descriptor.
2317 * \param drmCommandIndex command index
2318 * \param data source pointer of the data to be read and written.
2319 * \param size size of the data to be read and written.
2321 * \return zero on success, or a negative value on failure.
2324 * It issues a read-write ioctl given by
2325 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2327 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2330 drm_set_version_t sv;
2332 sv.drm_di_major = version->drm_di_major;
2333 sv.drm_di_minor = version->drm_di_minor;
2334 sv.drm_dd_major = version->drm_dd_major;
2335 sv.drm_dd_minor = version->drm_dd_minor;
2337 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2341 version->drm_di_major = sv.drm_di_major;
2342 version->drm_di_minor = sv.drm_di_minor;
2343 version->drm_dd_major = sv.drm_dd_major;
2344 version->drm_dd_minor = sv.drm_dd_minor;
2350 * Send a device-specific command.
2352 * \param fd file descriptor.
2353 * \param drmCommandIndex command index
2355 * \return zero on success, or a negative value on failure.
2358 * It issues a ioctl given by
2359 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2361 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2363 void *data = NULL; /* dummy */
2364 unsigned long request;
2366 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2368 if (drmIoctl(fd, request, data)) {
2376 * Send a device-specific read command.
2378 * \param fd file descriptor.
2379 * \param drmCommandIndex command index
2380 * \param data destination pointer of the data to be read.
2381 * \param size size of the data to be read.
2383 * \return zero on success, or a negative value on failure.
2386 * It issues a read ioctl given by
2387 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2389 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2392 unsigned long request;
2394 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2395 DRM_COMMAND_BASE + drmCommandIndex, size);
2397 if (drmIoctl(fd, request, data)) {
2405 * Send a device-specific write command.
2407 * \param fd file descriptor.
2408 * \param drmCommandIndex command index
2409 * \param data source pointer of the data to be written.
2410 * \param size size of the data to be written.
2412 * \return zero on success, or a negative value on failure.
2415 * It issues a write ioctl given by
2416 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2418 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2421 unsigned long request;
2423 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2424 DRM_COMMAND_BASE + drmCommandIndex, size);
2426 if (drmIoctl(fd, request, data)) {
2434 * Send a device-specific read-write command.
2436 * \param fd file descriptor.
2437 * \param drmCommandIndex command index
2438 * \param data source pointer of the data to be read and written.
2439 * \param size size of the data to be read and written.
2441 * \return zero on success, or a negative value on failure.
2444 * It issues a read-write ioctl given by
2445 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2447 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2450 unsigned long request;
2452 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2453 DRM_COMMAND_BASE + drmCommandIndex, size);
2455 if (drmIoctl(fd, request, data))
2460 #define DRM_MAX_FDS 16
2465 } connection[DRM_MAX_FDS];
2467 static int nr_fds = 0;
2469 int drmOpenOnce(void *unused,
2476 for (i = 0; i < nr_fds; i++)
2477 if (strcmp(BusID, connection[i].BusID) == 0) {
2478 connection[i].refcount++;
2480 return connection[i].fd;
2483 fd = drmOpen(unused, BusID);
2484 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2487 connection[nr_fds].BusID = strdup(BusID);
2488 connection[nr_fds].fd = fd;
2489 connection[nr_fds].refcount = 1;
2493 fprintf(stderr, "saved connection %d for %s %d\n",
2494 nr_fds, connection[nr_fds].BusID,
2495 strcmp(BusID, connection[nr_fds].BusID));
2502 void drmCloseOnce(int fd)
2506 for (i = 0; i < nr_fds; i++) {
2507 if (fd == connection[i].fd) {
2508 if (--connection[i].refcount == 0) {
2509 drmClose(connection[i].fd);
2510 free(connection[i].BusID);
2513 connection[i] = connection[nr_fds];
2521 int drmSetMaster(int fd)
2523 return ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
2526 int drmDropMaster(int fd)
2528 return ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);
2531 char *drmGetDeviceNameFromFd(int fd)
2538 /* The whole drmOpen thing is a fiasco and we need to find a way
2539 * back to just using open(2). For now, however, lets just make
2540 * things worse with even more ad hoc directory walking code to
2541 * discover the device file name. */
2546 for (i = 0; i < DRM_MAX_MINOR; i++) {
2547 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2548 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2551 if (i == DRM_MAX_MINOR)
2554 return strdup(name);
2557 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2559 struct drm_prime_handle args;
2562 args.handle = handle;
2564 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2568 *prime_fd = args.fd;
2572 int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2574 struct drm_prime_handle args;
2579 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2583 *handle = args.handle;