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)
158 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
159 static char *drmStrdup(const char *s)
166 retval = malloc(strlen(s)+1);
176 * Call ioctl, restarting if it is interupted
179 drmIoctl(int fd, unsigned long request, void *arg)
184 ret = ioctl(fd, request, arg);
185 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
189 static unsigned long drmGetKeyFromFd(int fd)
198 drmHashEntry *drmGetEntry(int fd)
200 unsigned long key = drmGetKeyFromFd(fd);
205 drmHashTable = drmHashCreate();
207 if (drmHashLookup(drmHashTable, key, &value)) {
208 entry = drmMalloc(sizeof(*entry));
211 entry->tagTable = drmHashCreate();
212 drmHashInsert(drmHashTable, key, entry);
220 * Compare two busid strings
225 * \return 1 if matched.
228 * This function compares two bus ID strings. It understands the older
229 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
230 * domain, b is bus, d is device, f is function.
232 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
234 /* First, check if the IDs are exactly the same */
235 if (strcasecmp(id1, id2) == 0)
238 /* Try to match old/new-style PCI bus IDs. */
239 if (strncasecmp(id1, "pci", 3) == 0) {
240 unsigned int o1, b1, d1, f1;
241 unsigned int o2, b2, d2, f2;
244 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
247 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
252 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
255 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
260 /* If domains aren't properly supported by the kernel interface,
261 * just ignore them, which sucks less than picking a totally random
262 * card with "open by name"
267 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
276 * Handles error checking for chown call.
278 * \param path to file.
279 * \param id of the new owner.
280 * \param id of the new group.
282 * \return zero if success or -1 if failure.
285 * Checks for failure. If failure was caused by signal call chown again.
286 * If any other failure happened then it will output error mesage using
289 static int chown_check_return(const char *path, uid_t owner, gid_t group)
294 rv = chown(path, owner, group);
295 } while (rv != 0 && errno == EINTR);
300 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
301 path, errno, strerror(errno));
306 * Open the DRM device, creating it if necessary.
308 * \param dev major and minor numbers of the device.
309 * \param minor minor number of the device.
311 * \return a file descriptor on success, or a negative value on error.
314 * Assembles the device name from \p minor and opens it, creating the device
315 * special file node with the major and minor numbers specified by \p dev and
316 * parent directory if necessary and was called by root.
318 static int drmOpenDevice(long dev, int minor, int type)
323 mode_t devmode = DRM_DEV_MODE, serv_mode;
324 int isroot = !geteuid();
325 uid_t user = DRM_DEV_UID;
326 gid_t group = DRM_DEV_GID, serv_group;
328 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
329 drmMsg("drmOpenDevice: node name is %s\n", buf);
331 if (drm_server_info) {
332 drm_server_info->get_perms(&serv_group, &serv_mode);
333 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
334 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
335 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
339 if (stat(DRM_DIR_NAME, &st)) {
341 return DRM_ERR_NOT_ROOT;
342 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
343 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
344 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
347 /* Check if the device node exists and create it if necessary. */
348 if (stat(buf, &st)) {
350 return DRM_ERR_NOT_ROOT;
352 mknod(buf, S_IFCHR | devmode, dev);
355 if (drm_server_info) {
356 chown_check_return(buf, user, group);
360 /* if we modprobed then wait for udev */
364 if (stat(DRM_DIR_NAME, &st)) {
368 if (udev_count == 50)
373 if (stat(buf, &st)) {
377 if (udev_count == 50)
384 fd = open(buf, O_RDWR, 0);
385 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
386 fd, fd < 0 ? strerror(errno) : "OK");
390 /* Check if the device node is not what we expect it to be, and recreate it
391 * and try again if so.
393 if (st.st_rdev != dev) {
395 return DRM_ERR_NOT_ROOT;
397 mknod(buf, S_IFCHR | devmode, dev);
398 if (drm_server_info) {
399 chown_check_return(buf, user, group);
403 fd = open(buf, O_RDWR, 0);
404 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
405 fd, fd < 0 ? strerror(errno) : "OK");
409 drmMsg("drmOpenDevice: Open failed\n");
416 * Open the DRM device
418 * \param minor device minor number.
419 * \param create allow to create the device if set.
421 * \return a file descriptor on success, or a negative value on error.
424 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
425 * name from \p minor and opens it.
427 static int drmOpenMinor(int minor, int create, int type)
433 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
435 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
436 if ((fd = open(buf, O_RDWR, 0)) >= 0)
443 * Determine whether the DRM kernel driver has been loaded.
445 * \return 1 if the DRM driver is loaded, 0 otherwise.
448 * Determine the presence of the kernel driver by attempting to open the 0
449 * minor and get version information. For backward compatibility with older
450 * Linux implementations, /proc/dri is also checked.
452 int drmAvailable(void)
454 drmVersionPtr version;
458 if ((fd = drmOpenMinor(0, 1, DRM_NODE_RENDER)) < 0) {
460 /* Try proc for backward Linux compatibility */
461 if (!access("/proc/dri/0", R_OK))
467 if ((version = drmGetVersion(fd))) {
469 drmFreeVersion(version);
478 * Open the device by bus ID.
480 * \param busid bus ID.
482 * \return a file descriptor on success, or a negative value on error.
485 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
486 * comparing the device bus ID with the one supplied.
488 * \sa drmOpenMinor() and drmGetBusid().
490 static int drmOpenByBusid(const char *busid)
492 int i, pci_domain_ok = 1;
497 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
498 for (i = 0; i < DRM_MAX_MINOR; i++) {
499 fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
500 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
502 /* We need to try for 1.4 first for proper PCI domain support
503 * and if that fails, we know the kernel is busted
507 sv.drm_dd_major = -1; /* Don't care */
508 sv.drm_dd_minor = -1; /* Don't care */
509 if (drmSetInterfaceVersion(fd, &sv)) {
515 sv.drm_dd_major = -1; /* Don't care */
516 sv.drm_dd_minor = -1; /* Don't care */
517 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n",fd);
518 drmSetInterfaceVersion(fd, &sv);
520 buf = drmGetBusid(fd);
521 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
522 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
536 * Open the device by name.
538 * \param name driver name.
540 * \return a file descriptor on success, or a negative value on error.
543 * This function opens the first minor number that matches the driver name and
544 * isn't already in use. If it's in use it then it will already have a bus ID
547 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
549 static int drmOpenByName(const char *name)
553 drmVersionPtr version;
556 if (!drmAvailable()) {
557 if (!drm_server_info) {
561 /* try to load the kernel module now */
562 if (!drm_server_info->load_module(name)) {
563 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
570 * Open the first minor number that matches the driver name and isn't
571 * already in use. If it's in use it will have a busid assigned already.
573 for (i = 0; i < DRM_MAX_MINOR; i++) {
574 if ((fd = drmOpenMinor(i, 1, DRM_NODE_RENDER)) >= 0) {
575 if ((version = drmGetVersion(fd))) {
576 if (!strcmp(version->name, name)) {
577 drmFreeVersion(version);
578 id = drmGetBusid(fd);
579 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
588 drmFreeVersion(version);
596 /* Backward-compatibility /proc support */
597 for (i = 0; i < 8; i++) {
598 char proc_name[64], buf[512];
599 char *driver, *pt, *devstring;
602 sprintf(proc_name, "/proc/dri/%d/name", i);
603 if ((fd = open(proc_name, 0, 0)) >= 0) {
604 retcode = read(fd, buf, sizeof(buf)-1);
607 buf[retcode-1] = '\0';
608 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
610 if (*pt) { /* Device is next */
612 if (!strcmp(driver, name)) { /* Match */
613 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
615 if (*pt) { /* Found busid */
616 return drmOpenByBusid(++pt);
617 } else { /* No busid */
618 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_RENDER);
632 * Open the DRM device.
634 * Looks up the specified name and bus ID, and opens the device found. The
635 * entry in /dev/dri is created if necessary and if called by root.
637 * \param name driver name. Not referenced if bus ID is supplied.
638 * \param busid bus ID. Zero if not known.
640 * \return a file descriptor on success, or a negative value on error.
643 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
646 int drmOpen(const char *name, const char *busid)
648 if (!drmAvailable() && name != NULL && drm_server_info) {
649 /* try to load the kernel */
650 if (!drm_server_info->load_module(name)) {
651 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
657 int fd = drmOpenByBusid(busid);
663 return drmOpenByName(name);
668 int drmOpenControl(int minor)
670 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
674 * Free the version information returned by drmGetVersion().
676 * \param v pointer to the version information.
679 * It frees the memory pointed by \p %v as well as all the non-null strings
682 void drmFreeVersion(drmVersionPtr v)
694 * Free the non-public version information returned by the kernel.
696 * \param v pointer to the version information.
699 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
700 * the non-null strings pointers in it.
702 static void drmFreeKernelVersion(drm_version_t *v)
714 * Copy version information.
716 * \param d destination pointer.
717 * \param s source pointer.
720 * Used by drmGetVersion() to translate the information returned by the ioctl
721 * interface in a private structure into the public structure counterpart.
723 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
725 d->version_major = s->version_major;
726 d->version_minor = s->version_minor;
727 d->version_patchlevel = s->version_patchlevel;
728 d->name_len = s->name_len;
729 d->name = drmStrdup(s->name);
730 d->date_len = s->date_len;
731 d->date = drmStrdup(s->date);
732 d->desc_len = s->desc_len;
733 d->desc = drmStrdup(s->desc);
738 * Query the driver version information.
740 * \param fd file descriptor.
742 * \return pointer to a drmVersion structure which should be freed with
745 * \note Similar information is available via /proc/dri.
748 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
749 * first with zeros to get the string lengths, and then the actually strings.
750 * It also null-terminates them since they might not be already.
752 drmVersionPtr drmGetVersion(int fd)
754 drmVersionPtr retval;
755 drm_version_t *version = drmMalloc(sizeof(*version));
757 version->name_len = 0;
758 version->name = NULL;
759 version->date_len = 0;
760 version->date = NULL;
761 version->desc_len = 0;
762 version->desc = NULL;
764 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
765 drmFreeKernelVersion(version);
769 if (version->name_len)
770 version->name = drmMalloc(version->name_len + 1);
771 if (version->date_len)
772 version->date = drmMalloc(version->date_len + 1);
773 if (version->desc_len)
774 version->desc = drmMalloc(version->desc_len + 1);
776 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
777 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
778 drmFreeKernelVersion(version);
782 /* The results might not be null-terminated strings, so terminate them. */
783 if (version->name_len) version->name[version->name_len] = '\0';
784 if (version->date_len) version->date[version->date_len] = '\0';
785 if (version->desc_len) version->desc[version->desc_len] = '\0';
787 retval = drmMalloc(sizeof(*retval));
788 drmCopyVersion(retval, version);
789 drmFreeKernelVersion(version);
795 * Get version information for the DRM user space library.
797 * This version number is driver independent.
799 * \param fd file descriptor.
801 * \return version information.
804 * This function allocates and fills a drm_version structure with a hard coded
807 drmVersionPtr drmGetLibVersion(int fd)
809 drm_version_t *version = drmMalloc(sizeof(*version));
812 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
813 * revision 1.0.x = original DRM interface with no drmGetLibVersion
814 * entry point and many drm<Device> extensions
815 * revision 1.1.x = added drmCommand entry points for device extensions
816 * added drmGetLibVersion to identify libdrm.a version
817 * revision 1.2.x = added drmSetInterfaceVersion
818 * modified drmOpen to handle both busid and name
819 * revision 1.3.x = added server + memory manager
821 version->version_major = 1;
822 version->version_minor = 3;
823 version->version_patchlevel = 0;
825 return (drmVersionPtr)version;
830 * Free the bus ID information.
832 * \param busid bus ID information string as given by drmGetBusid().
835 * This function is just frees the memory pointed by \p busid.
837 void drmFreeBusid(const char *busid)
839 drmFree((void *)busid);
844 * Get the bus ID of the device.
846 * \param fd file descriptor.
848 * \return bus ID string.
851 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
852 * get the string length and data, passing the arguments in a drm_unique
855 char *drmGetBusid(int fd)
862 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
864 u.unique = drmMalloc(u.unique_len + 1);
865 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
867 u.unique[u.unique_len] = '\0';
874 * Set the bus ID of the device.
876 * \param fd file descriptor.
877 * \param busid bus ID string.
879 * \return zero on success, negative on failure.
882 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
883 * the arguments in a drm_unique structure.
885 int drmSetBusid(int fd, const char *busid)
889 u.unique = (char *)busid;
890 u.unique_len = strlen(busid);
892 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
898 int drmGetMagic(int fd, drm_magic_t * magic)
903 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
909 int drmAuthMagic(int fd, drm_magic_t magic)
914 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
920 * Specifies a range of memory that is available for mapping by a
923 * \param fd file descriptor.
924 * \param offset usually the physical address. The actual meaning depends of
925 * the \p type parameter. See below.
926 * \param size of the memory in bytes.
927 * \param type type of the memory to be mapped.
928 * \param flags combination of several flags to modify the function actions.
929 * \param handle will be set to a value that may be used as the offset
930 * parameter for mmap().
932 * \return zero on success or a negative value on error.
934 * \par Mapping the frame buffer
935 * For the frame buffer
936 * - \p offset will be the physical address of the start of the frame buffer,
937 * - \p size will be the size of the frame buffer in bytes, and
938 * - \p type will be DRM_FRAME_BUFFER.
941 * The area mapped will be uncached. If MTRR support is available in the
942 * kernel, the frame buffer area will be set to write combining.
944 * \par Mapping the MMIO register area
945 * For the MMIO register area,
946 * - \p offset will be the physical address of the start of the register area,
947 * - \p size will be the size of the register area bytes, and
948 * - \p type will be DRM_REGISTERS.
950 * The area mapped will be uncached.
952 * \par Mapping the SAREA
954 * - \p offset will be ignored and should be set to zero,
955 * - \p size will be the desired size of the SAREA in bytes,
956 * - \p type will be DRM_SHM.
959 * A shared memory area of the requested size will be created and locked in
960 * kernel memory. This area may be mapped into client-space by using the handle
963 * \note May only be called by root.
966 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
967 * the arguments in a drm_map structure.
969 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
970 drmMapFlags flags, drm_handle_t *handle)
979 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
982 *handle = (drm_handle_t)map.handle;
986 int drmRmMap(int fd, drm_handle_t handle)
990 map.handle = (void *)handle;
992 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
998 * Make buffers available for DMA transfers.
1000 * \param fd file descriptor.
1001 * \param count number of buffers.
1002 * \param size size of each buffer.
1003 * \param flags buffer allocation flags.
1004 * \param agp_offset offset in the AGP aperture
1006 * \return number of buffers allocated, negative on error.
1009 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1013 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1016 drm_buf_desc_t request;
1018 request.count = count;
1019 request.size = size;
1020 request.low_mark = 0;
1021 request.high_mark = 0;
1022 request.flags = flags;
1023 request.agp_start = agp_offset;
1025 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1027 return request.count;
1030 int drmMarkBufs(int fd, double low, double high)
1032 drm_buf_info_t info;
1038 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1044 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1047 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1048 int retval = -errno;
1053 for (i = 0; i < info.count; i++) {
1054 info.list[i].low_mark = low * info.list[i].count;
1055 info.list[i].high_mark = high * info.list[i].count;
1056 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1057 int retval = -errno;
1070 * \param fd file descriptor.
1071 * \param count number of buffers to free.
1072 * \param list list of buffers to be freed.
1074 * \return zero on success, or a negative value on failure.
1076 * \note This function is primarily used for debugging.
1079 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1080 * the arguments in a drm_buf_free structure.
1082 int drmFreeBufs(int fd, int count, int *list)
1084 drm_buf_free_t request;
1086 request.count = count;
1087 request.list = list;
1088 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1097 * \param fd file descriptor.
1100 * This function closes the file descriptor.
1102 int drmClose(int fd)
1104 unsigned long key = drmGetKeyFromFd(fd);
1105 drmHashEntry *entry = drmGetEntry(fd);
1107 drmHashDestroy(entry->tagTable);
1110 entry->tagTable = NULL;
1112 drmHashDelete(drmHashTable, key);
1120 * Map a region of memory.
1122 * \param fd file descriptor.
1123 * \param handle handle returned by drmAddMap().
1124 * \param size size in bytes. Must match the size used by drmAddMap().
1125 * \param address will contain the user-space virtual address where the mapping
1128 * \return zero on success, or a negative value on failure.
1131 * This function is a wrapper for mmap().
1133 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1135 static unsigned long pagesize_mask = 0;
1141 pagesize_mask = getpagesize() - 1;
1143 size = (size + pagesize_mask) & ~pagesize_mask;
1145 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1146 if (*address == MAP_FAILED)
1153 * Unmap mappings obtained with drmMap().
1155 * \param address address as given by drmMap().
1156 * \param size size in bytes. Must match the size used by drmMap().
1158 * \return zero on success, or a negative value on failure.
1161 * This function is a wrapper for munmap().
1163 int drmUnmap(drmAddress address, drmSize size)
1165 return munmap(address, size);
1168 drmBufInfoPtr drmGetBufInfo(int fd)
1170 drm_buf_info_t info;
1171 drmBufInfoPtr retval;
1177 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1181 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1184 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1189 retval = drmMalloc(sizeof(*retval));
1190 retval->count = info.count;
1191 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1192 for (i = 0; i < info.count; i++) {
1193 retval->list[i].count = info.list[i].count;
1194 retval->list[i].size = info.list[i].size;
1195 retval->list[i].low_mark = info.list[i].low_mark;
1196 retval->list[i].high_mark = info.list[i].high_mark;
1205 * Map all DMA buffers into client-virtual space.
1207 * \param fd file descriptor.
1209 * \return a pointer to a ::drmBufMap structure.
1211 * \note The client may not use these buffers until obtaining buffer indices
1215 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1216 * information about the buffers in a drm_buf_map structure into the
1217 * client-visible data structures.
1219 drmBufMapPtr drmMapBufs(int fd)
1222 drmBufMapPtr retval;
1227 bufs.virtual = NULL;
1228 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1234 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1237 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1242 retval = drmMalloc(sizeof(*retval));
1243 retval->count = bufs.count;
1244 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1245 for (i = 0; i < bufs.count; i++) {
1246 retval->list[i].idx = bufs.list[i].idx;
1247 retval->list[i].total = bufs.list[i].total;
1248 retval->list[i].used = 0;
1249 retval->list[i].address = bufs.list[i].address;
1259 * Unmap buffers allocated with drmMapBufs().
1261 * \return zero on success, or negative value on failure.
1264 * Calls munmap() for every buffer stored in \p bufs and frees the
1265 * memory allocated by drmMapBufs().
1267 int drmUnmapBufs(drmBufMapPtr bufs)
1271 for (i = 0; i < bufs->count; i++) {
1272 munmap(bufs->list[i].address, bufs->list[i].total);
1275 drmFree(bufs->list);
1282 #define DRM_DMA_RETRY 16
1285 * Reserve DMA buffers.
1287 * \param fd file descriptor.
1290 * \return zero on success, or a negative value on failure.
1293 * Assemble the arguments into a drm_dma structure and keeps issuing the
1294 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1296 int drmDMA(int fd, drmDMAReqPtr request)
1301 dma.context = request->context;
1302 dma.send_count = request->send_count;
1303 dma.send_indices = request->send_list;
1304 dma.send_sizes = request->send_sizes;
1305 dma.flags = request->flags;
1306 dma.request_count = request->request_count;
1307 dma.request_size = request->request_size;
1308 dma.request_indices = request->request_list;
1309 dma.request_sizes = request->request_sizes;
1310 dma.granted_count = 0;
1313 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1314 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1317 request->granted_count = dma.granted_count;
1326 * Obtain heavyweight hardware lock.
1328 * \param fd file descriptor.
1329 * \param context context.
1330 * \param flags flags that determine the sate of the hardware when the function
1333 * \return always zero.
1336 * This function translates the arguments into a drm_lock structure and issue
1337 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1339 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1343 lock.context = context;
1345 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1346 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1347 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1348 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1349 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1350 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1352 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1358 * Release the hardware lock.
1360 * \param fd file descriptor.
1361 * \param context context.
1363 * \return zero on success, or a negative value on failure.
1366 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1367 * argument in a drm_lock structure.
1369 int drmUnlock(int fd, drm_context_t context)
1373 lock.context = context;
1375 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1378 drm_context_t *drmGetReservedContextList(int fd, int *count)
1382 drm_context_t * retval;
1386 res.contexts = NULL;
1387 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1393 if (!(list = drmMalloc(res.count * sizeof(*list))))
1395 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1400 res.contexts = list;
1401 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1404 for (i = 0; i < res.count; i++)
1405 retval[i] = list[i].handle;
1412 void drmFreeReservedContextList(drm_context_t *pt)
1420 * Used by the X server during GLXContext initialization. This causes
1421 * per-context kernel-level resources to be allocated.
1423 * \param fd file descriptor.
1424 * \param handle is set on success. To be used by the client when requesting DMA
1425 * dispatch with drmDMA().
1427 * \return zero on success, or a negative value on failure.
1429 * \note May only be called by root.
1432 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1433 * argument in a drm_ctx structure.
1435 int drmCreateContext(int fd, drm_context_t *handle)
1439 ctx.flags = 0; /* Modified with functions below */
1440 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1442 *handle = ctx.handle;
1446 int drmSwitchToContext(int fd, drm_context_t context)
1450 ctx.handle = context;
1451 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1456 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1461 * Context preserving means that no context switches are done between DMA
1462 * buffers from one context and the next. This is suitable for use in the
1463 * X server (which promises to maintain hardware context), or in the
1464 * client-side library when buffers are swapped on behalf of two threads.
1466 ctx.handle = context;
1468 if (flags & DRM_CONTEXT_PRESERVED)
1469 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1470 if (flags & DRM_CONTEXT_2DONLY)
1471 ctx.flags |= _DRM_CONTEXT_2DONLY;
1472 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1477 int drmGetContextFlags(int fd, drm_context_t context,
1478 drm_context_tFlagsPtr flags)
1482 ctx.handle = context;
1483 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1486 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1487 *flags |= DRM_CONTEXT_PRESERVED;
1488 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1489 *flags |= DRM_CONTEXT_2DONLY;
1496 * Free any kernel-level resources allocated with drmCreateContext() associated
1499 * \param fd file descriptor.
1500 * \param handle handle given by drmCreateContext().
1502 * \return zero on success, or a negative value on failure.
1504 * \note May only be called by root.
1507 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1508 * argument in a drm_ctx structure.
1510 int drmDestroyContext(int fd, drm_context_t handle)
1513 ctx.handle = handle;
1514 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1519 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1522 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1524 *handle = draw.handle;
1528 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1531 draw.handle = handle;
1532 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1537 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1538 drm_drawable_info_type_t type, unsigned int num,
1541 drm_update_draw_t update;
1543 update.handle = handle;
1546 update.data = (unsigned long long)(unsigned long)data;
1548 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1555 * Acquire the AGP device.
1557 * Must be called before any of the other AGP related calls.
1559 * \param fd file descriptor.
1561 * \return zero on success, or a negative value on failure.
1564 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1566 int drmAgpAcquire(int fd)
1568 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1575 * Release the AGP device.
1577 * \param fd file descriptor.
1579 * \return zero on success, or a negative value on failure.
1582 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1584 int drmAgpRelease(int fd)
1586 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1595 * \param fd file descriptor.
1596 * \param mode AGP mode.
1598 * \return zero on success, or a negative value on failure.
1601 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1602 * argument in a drm_agp_mode structure.
1604 int drmAgpEnable(int fd, unsigned long mode)
1609 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1616 * Allocate a chunk of AGP memory.
1618 * \param fd file descriptor.
1619 * \param size requested memory size in bytes. Will be rounded to page boundary.
1620 * \param type type of memory to allocate.
1621 * \param address if not zero, will be set to the physical address of the
1623 * \param handle on success will be set to a handle of the allocated memory.
1625 * \return zero on success, or a negative value on failure.
1628 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1629 * arguments in a drm_agp_buffer structure.
1631 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1632 unsigned long *address, drm_handle_t *handle)
1636 *handle = DRM_AGP_NO_HANDLE;
1640 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1643 *address = b.physical;
1650 * Free a chunk of AGP memory.
1652 * \param fd file descriptor.
1653 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1655 * \return zero on success, or a negative value on failure.
1658 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1659 * argument in a drm_agp_buffer structure.
1661 int drmAgpFree(int fd, drm_handle_t handle)
1667 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1674 * Bind a chunk of AGP memory.
1676 * \param fd file descriptor.
1677 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1678 * \param offset offset in bytes. It will round to page boundary.
1680 * \return zero on success, or a negative value on failure.
1683 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1684 * argument in a drm_agp_binding structure.
1686 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1688 drm_agp_binding_t b;
1692 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1699 * Unbind a chunk of AGP memory.
1701 * \param fd file descriptor.
1702 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1704 * \return zero on success, or a negative value on failure.
1707 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1708 * the argument in a drm_agp_binding structure.
1710 int drmAgpUnbind(int fd, drm_handle_t handle)
1712 drm_agp_binding_t b;
1716 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1723 * Get AGP driver major version number.
1725 * \param fd file descriptor.
1727 * \return major version number on success, or a negative value on failure..
1730 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1731 * necessary information in a drm_agp_info structure.
1733 int drmAgpVersionMajor(int fd)
1737 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1739 return i.agp_version_major;
1744 * Get AGP driver minor version number.
1746 * \param fd file descriptor.
1748 * \return minor version number on success, or a negative value on failure.
1751 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1752 * necessary information in a drm_agp_info structure.
1754 int drmAgpVersionMinor(int fd)
1758 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1760 return i.agp_version_minor;
1767 * \param fd file descriptor.
1769 * \return mode on success, or zero on failure.
1772 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1773 * necessary information in a drm_agp_info structure.
1775 unsigned long drmAgpGetMode(int fd)
1779 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1786 * Get AGP aperture base.
1788 * \param fd file descriptor.
1790 * \return aperture base on success, zero on failure.
1793 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1794 * necessary information in a drm_agp_info structure.
1796 unsigned long drmAgpBase(int fd)
1800 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1802 return i.aperture_base;
1807 * Get AGP aperture size.
1809 * \param fd file descriptor.
1811 * \return aperture size on success, zero on failure.
1814 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1815 * necessary information in a drm_agp_info structure.
1817 unsigned long drmAgpSize(int fd)
1821 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1823 return i.aperture_size;
1828 * Get used AGP memory.
1830 * \param fd file descriptor.
1832 * \return memory used on success, or zero on failure.
1835 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1836 * necessary information in a drm_agp_info structure.
1838 unsigned long drmAgpMemoryUsed(int fd)
1842 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1844 return i.memory_used;
1849 * Get available AGP memory.
1851 * \param fd file descriptor.
1853 * \return memory available on success, or zero on failure.
1856 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1857 * necessary information in a drm_agp_info structure.
1859 unsigned long drmAgpMemoryAvail(int fd)
1863 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1865 return i.memory_allowed;
1870 * Get hardware vendor ID.
1872 * \param fd file descriptor.
1874 * \return vendor ID on success, or zero on failure.
1877 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1878 * necessary information in a drm_agp_info structure.
1880 unsigned int drmAgpVendorId(int fd)
1884 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1891 * Get hardware device ID.
1893 * \param fd file descriptor.
1895 * \return zero on success, or zero on failure.
1898 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1899 * necessary information in a drm_agp_info structure.
1901 unsigned int drmAgpDeviceId(int fd)
1905 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1910 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1912 drm_scatter_gather_t sg;
1917 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1919 *handle = sg.handle;
1923 int drmScatterGatherFree(int fd, drm_handle_t handle)
1925 drm_scatter_gather_t sg;
1929 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
1937 * \param fd file descriptor.
1938 * \param vbl pointer to a drmVBlank structure.
1940 * \return zero on success, or a negative value on failure.
1943 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1945 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1947 struct timespec timeout, cur;
1950 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
1952 fprintf(stderr, "clock_gettime failed: %s\n", strerror(ret));
1958 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1959 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1960 if (ret && errno == EINTR) {
1961 clock_gettime(CLOCK_MONOTONIC, &cur);
1962 /* Timeout after 1s */
1963 if (cur.tv_sec > timeout.tv_sec + 1 ||
1964 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
1971 } while (ret && errno == EINTR);
1977 int drmError(int err, const char *label)
1980 case DRM_ERR_NO_DEVICE:
1981 fprintf(stderr, "%s: no device\n", label);
1983 case DRM_ERR_NO_ACCESS:
1984 fprintf(stderr, "%s: no access\n", label);
1986 case DRM_ERR_NOT_ROOT:
1987 fprintf(stderr, "%s: not root\n", label);
1989 case DRM_ERR_INVALID:
1990 fprintf(stderr, "%s: invalid args\n", label);
1995 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2003 * Install IRQ handler.
2005 * \param fd file descriptor.
2006 * \param irq IRQ number.
2008 * \return zero on success, or a negative value on failure.
2011 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2012 * argument in a drm_control structure.
2014 int drmCtlInstHandler(int fd, int irq)
2018 ctl.func = DRM_INST_HANDLER;
2020 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2027 * Uninstall IRQ handler.
2029 * \param fd file descriptor.
2031 * \return zero on success, or a negative value on failure.
2034 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2035 * argument in a drm_control structure.
2037 int drmCtlUninstHandler(int fd)
2041 ctl.func = DRM_UNINST_HANDLER;
2043 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2048 int drmFinish(int fd, int context, drmLockFlags flags)
2052 lock.context = context;
2054 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2055 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2056 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2057 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2058 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2059 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2060 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2066 * Get IRQ from bus ID.
2068 * \param fd file descriptor.
2069 * \param busnum bus number.
2070 * \param devnum device number.
2071 * \param funcnum function number.
2073 * \return IRQ number on success, or a negative value on failure.
2076 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2077 * arguments in a drm_irq_busid structure.
2079 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2085 p.funcnum = funcnum;
2086 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2091 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2093 drmHashEntry *entry = drmGetEntry(fd);
2095 if (drmHashInsert(entry->tagTable, context, tag)) {
2096 drmHashDelete(entry->tagTable, context);
2097 drmHashInsert(entry->tagTable, context, tag);
2102 int drmDelContextTag(int fd, drm_context_t context)
2104 drmHashEntry *entry = drmGetEntry(fd);
2106 return drmHashDelete(entry->tagTable, context);
2109 void *drmGetContextTag(int fd, drm_context_t context)
2111 drmHashEntry *entry = drmGetEntry(fd);
2114 if (drmHashLookup(entry->tagTable, context, &value))
2120 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2121 drm_handle_t handle)
2123 drm_ctx_priv_map_t map;
2125 map.ctx_id = ctx_id;
2126 map.handle = (void *)handle;
2128 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2133 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2134 drm_handle_t *handle)
2136 drm_ctx_priv_map_t map;
2138 map.ctx_id = ctx_id;
2140 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2143 *handle = (drm_handle_t)map.handle;
2148 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2149 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2155 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2157 *offset = map.offset;
2161 *handle = (unsigned long)map.handle;
2166 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2167 unsigned long *magic, unsigned long *iocs)
2169 drm_client_t client;
2172 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2174 *auth = client.auth;
2177 *magic = client.magic;
2178 *iocs = client.iocs;
2182 int drmGetStats(int fd, drmStatsT *stats)
2187 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2191 memset(stats, 0, sizeof(*stats));
2192 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2196 stats->data[i].long_format = "%-20.20s"; \
2197 stats->data[i].rate_format = "%8.8s"; \
2198 stats->data[i].isvalue = 1; \
2199 stats->data[i].verbose = 0
2202 stats->data[i].long_format = "%-20.20s"; \
2203 stats->data[i].rate_format = "%5.5s"; \
2204 stats->data[i].isvalue = 0; \
2205 stats->data[i].mult_names = "kgm"; \
2206 stats->data[i].mult = 1000; \
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 = 1024; \
2215 stats->data[i].verbose = 0
2218 stats->count = s.count;
2219 for (i = 0; i < s.count; i++) {
2220 stats->data[i].value = s.data[i].value;
2221 switch (s.data[i].type) {
2222 case _DRM_STAT_LOCK:
2223 stats->data[i].long_name = "Lock";
2224 stats->data[i].rate_name = "Lock";
2227 case _DRM_STAT_OPENS:
2228 stats->data[i].long_name = "Opens";
2229 stats->data[i].rate_name = "O";
2231 stats->data[i].verbose = 1;
2233 case _DRM_STAT_CLOSES:
2234 stats->data[i].long_name = "Closes";
2235 stats->data[i].rate_name = "Lock";
2237 stats->data[i].verbose = 1;
2239 case _DRM_STAT_IOCTLS:
2240 stats->data[i].long_name = "Ioctls";
2241 stats->data[i].rate_name = "Ioc/s";
2244 case _DRM_STAT_LOCKS:
2245 stats->data[i].long_name = "Locks";
2246 stats->data[i].rate_name = "Lck/s";
2249 case _DRM_STAT_UNLOCKS:
2250 stats->data[i].long_name = "Unlocks";
2251 stats->data[i].rate_name = "Unl/s";
2255 stats->data[i].long_name = "IRQs";
2256 stats->data[i].rate_name = "IRQ/s";
2259 case _DRM_STAT_PRIMARY:
2260 stats->data[i].long_name = "Primary Bytes";
2261 stats->data[i].rate_name = "PB/s";
2264 case _DRM_STAT_SECONDARY:
2265 stats->data[i].long_name = "Secondary Bytes";
2266 stats->data[i].rate_name = "SB/s";
2270 stats->data[i].long_name = "DMA";
2271 stats->data[i].rate_name = "DMA/s";
2274 case _DRM_STAT_SPECIAL:
2275 stats->data[i].long_name = "Special DMA";
2276 stats->data[i].rate_name = "dma/s";
2279 case _DRM_STAT_MISSED:
2280 stats->data[i].long_name = "Miss";
2281 stats->data[i].rate_name = "Ms/s";
2284 case _DRM_STAT_VALUE:
2285 stats->data[i].long_name = "Value";
2286 stats->data[i].rate_name = "Value";
2289 case _DRM_STAT_BYTE:
2290 stats->data[i].long_name = "Bytes";
2291 stats->data[i].rate_name = "B/s";
2294 case _DRM_STAT_COUNT:
2296 stats->data[i].long_name = "Count";
2297 stats->data[i].rate_name = "Cnt/s";
2306 * Issue a set-version ioctl.
2308 * \param fd file descriptor.
2309 * \param drmCommandIndex command index
2310 * \param data source pointer of the data to be read and written.
2311 * \param size size of the data to be read and written.
2313 * \return zero on success, or a negative value on failure.
2316 * It issues a read-write ioctl given by
2317 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2319 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2322 drm_set_version_t sv;
2324 sv.drm_di_major = version->drm_di_major;
2325 sv.drm_di_minor = version->drm_di_minor;
2326 sv.drm_dd_major = version->drm_dd_major;
2327 sv.drm_dd_minor = version->drm_dd_minor;
2329 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2333 version->drm_di_major = sv.drm_di_major;
2334 version->drm_di_minor = sv.drm_di_minor;
2335 version->drm_dd_major = sv.drm_dd_major;
2336 version->drm_dd_minor = sv.drm_dd_minor;
2342 * Send a device-specific command.
2344 * \param fd file descriptor.
2345 * \param drmCommandIndex command index
2347 * \return zero on success, or a negative value on failure.
2350 * It issues a ioctl given by
2351 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2353 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2355 void *data = NULL; /* dummy */
2356 unsigned long request;
2358 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2360 if (drmIoctl(fd, request, data)) {
2368 * Send a device-specific read command.
2370 * \param fd file descriptor.
2371 * \param drmCommandIndex command index
2372 * \param data destination pointer of the data to be read.
2373 * \param size size of the data to be read.
2375 * \return zero on success, or a negative value on failure.
2378 * It issues a read ioctl given by
2379 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2381 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2384 unsigned long request;
2386 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2387 DRM_COMMAND_BASE + drmCommandIndex, size);
2389 if (drmIoctl(fd, request, data)) {
2397 * Send a device-specific write command.
2399 * \param fd file descriptor.
2400 * \param drmCommandIndex command index
2401 * \param data source pointer of the data to be written.
2402 * \param size size of the data to be written.
2404 * \return zero on success, or a negative value on failure.
2407 * It issues a write ioctl given by
2408 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2410 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2413 unsigned long request;
2415 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2416 DRM_COMMAND_BASE + drmCommandIndex, size);
2418 if (drmIoctl(fd, request, data)) {
2426 * Send a device-specific read-write command.
2428 * \param fd file descriptor.
2429 * \param drmCommandIndex command index
2430 * \param data source pointer of the data to be read and written.
2431 * \param size size of the data to be read and written.
2433 * \return zero on success, or a negative value on failure.
2436 * It issues a read-write ioctl given by
2437 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2439 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2442 unsigned long request;
2444 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2445 DRM_COMMAND_BASE + drmCommandIndex, size);
2447 if (drmIoctl(fd, request, data))
2452 #define DRM_MAX_FDS 16
2457 } connection[DRM_MAX_FDS];
2459 static int nr_fds = 0;
2461 int drmOpenOnce(void *unused,
2468 for (i = 0; i < nr_fds; i++)
2469 if (strcmp(BusID, connection[i].BusID) == 0) {
2470 connection[i].refcount++;
2472 return connection[i].fd;
2475 fd = drmOpen(unused, BusID);
2476 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2479 connection[nr_fds].BusID = strdup(BusID);
2480 connection[nr_fds].fd = fd;
2481 connection[nr_fds].refcount = 1;
2485 fprintf(stderr, "saved connection %d for %s %d\n",
2486 nr_fds, connection[nr_fds].BusID,
2487 strcmp(BusID, connection[nr_fds].BusID));
2494 void drmCloseOnce(int fd)
2498 for (i = 0; i < nr_fds; i++) {
2499 if (fd == connection[i].fd) {
2500 if (--connection[i].refcount == 0) {
2501 drmClose(connection[i].fd);
2502 free(connection[i].BusID);
2505 connection[i] = connection[nr_fds];
2513 int drmSetMaster(int fd)
2515 return ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
2518 int drmDropMaster(int fd)
2520 return ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);
2523 char *drmGetDeviceNameFromFd(int fd)
2530 /* The whole drmOpen thing is a fiasco and we need to find a way
2531 * back to just using open(2). For now, however, lets just make
2532 * things worse with even more ad hoc directory walking code to
2533 * discover the device file name. */
2538 for (i = 0; i < DRM_MAX_MINOR; i++) {
2539 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2540 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2543 if (i == DRM_MAX_MINOR)
2546 return drmStrdup(name);