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.
49 #include <sys/types.h>
51 #define stat_t struct stat
52 #include <sys/ioctl.h>
56 /* Not all systems have MAP_FAILED defined */
58 #define MAP_FAILED ((void *)-1)
64 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
77 #define DRM_MAJOR 226 /* Linux */
81 * This definition needs to be changed on some systems if dev_t is a structure.
82 * If there is a header file we can get it from, there would be best.
85 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
88 #define DRM_MSG_VERBOSITY 3
90 #define memclear(s) memset(&s, 0, sizeof(s))
92 static drmServerInfoPtr drm_server_info;
94 void drmSetServerInfo(drmServerInfoPtr info)
96 drm_server_info = info;
100 * Output a message to stderr.
102 * \param format printf() like format string.
105 * This function is a wrapper around vfprintf().
108 static int DRM_PRINTFLIKE(1, 0)
109 drmDebugPrint(const char *format, va_list ap)
111 return vfprintf(stderr, format, ap);
114 typedef int DRM_PRINTFLIKE(1, 0) (*debug_msg_func_t)(const char *format,
117 static debug_msg_func_t drm_debug_print = drmDebugPrint;
120 drmMsg(const char *format, ...)
124 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
126 va_start(ap, format);
127 if (drm_server_info) {
128 drm_server_info->debug_print(format,ap);
130 drm_debug_print(format, ap);
137 drmSetDebugMsgFunction(debug_msg_func_t debug_msg_ptr)
139 drm_debug_print = debug_msg_ptr;
142 static void *drmHashTable = NULL; /* Context switch callbacks */
144 void *drmGetHashTable(void)
149 void *drmMalloc(int size)
152 if ((pt = malloc(size)))
157 void drmFree(void *pt)
164 * Call ioctl, restarting if it is interupted
167 drmIoctl(int fd, unsigned long request, void *arg)
172 ret = ioctl(fd, request, arg);
173 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
177 static unsigned long drmGetKeyFromFd(int fd)
186 drmHashEntry *drmGetEntry(int fd)
188 unsigned long key = drmGetKeyFromFd(fd);
193 drmHashTable = drmHashCreate();
195 if (drmHashLookup(drmHashTable, key, &value)) {
196 entry = drmMalloc(sizeof(*entry));
199 entry->tagTable = drmHashCreate();
200 drmHashInsert(drmHashTable, key, entry);
208 * Compare two busid strings
213 * \return 1 if matched.
216 * This function compares two bus ID strings. It understands the older
217 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
218 * domain, b is bus, d is device, f is function.
220 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
222 /* First, check if the IDs are exactly the same */
223 if (strcasecmp(id1, id2) == 0)
226 /* Try to match old/new-style PCI bus IDs. */
227 if (strncasecmp(id1, "pci", 3) == 0) {
228 unsigned int o1, b1, d1, f1;
229 unsigned int o2, b2, d2, f2;
232 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
235 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
240 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
243 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
248 /* If domains aren't properly supported by the kernel interface,
249 * just ignore them, which sucks less than picking a totally random
250 * card with "open by name"
255 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
264 * Handles error checking for chown call.
266 * \param path to file.
267 * \param id of the new owner.
268 * \param id of the new group.
270 * \return zero if success or -1 if failure.
273 * Checks for failure. If failure was caused by signal call chown again.
274 * If any other failure happened then it will output error mesage using
277 static int chown_check_return(const char *path, uid_t owner, gid_t group)
282 rv = chown(path, owner, group);
283 } while (rv != 0 && errno == EINTR);
288 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
289 path, errno, strerror(errno));
294 * Open the DRM device, creating it if necessary.
296 * \param dev major and minor numbers of the device.
297 * \param minor minor number of the device.
299 * \return a file descriptor on success, or a negative value on error.
302 * Assembles the device name from \p minor and opens it, creating the device
303 * special file node with the major and minor numbers specified by \p dev and
304 * parent directory if necessary and was called by root.
306 static int drmOpenDevice(dev_t dev, int minor, int type)
309 const char *dev_name;
312 mode_t devmode = DRM_DEV_MODE, serv_mode;
313 int isroot = !geteuid();
314 uid_t user = DRM_DEV_UID;
315 gid_t group = DRM_DEV_GID, serv_group;
318 case DRM_NODE_PRIMARY:
319 dev_name = DRM_DEV_NAME;
321 case DRM_NODE_CONTROL:
322 dev_name = DRM_CONTROL_DEV_NAME;
324 case DRM_NODE_RENDER:
325 dev_name = DRM_RENDER_DEV_NAME;
331 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
332 drmMsg("drmOpenDevice: node name is %s\n", buf);
334 if (drm_server_info) {
335 drm_server_info->get_perms(&serv_group, &serv_mode);
336 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
337 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
338 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
342 if (stat(DRM_DIR_NAME, &st)) {
344 return DRM_ERR_NOT_ROOT;
345 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
346 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
347 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
350 /* Check if the device node exists and create it if necessary. */
351 if (stat(buf, &st)) {
353 return DRM_ERR_NOT_ROOT;
355 mknod(buf, S_IFCHR | devmode, dev);
358 if (drm_server_info) {
359 chown_check_return(buf, user, group);
363 /* if we modprobed then wait for udev */
367 if (stat(DRM_DIR_NAME, &st)) {
371 if (udev_count == 50)
376 if (stat(buf, &st)) {
380 if (udev_count == 50)
387 fd = open(buf, O_RDWR, 0);
388 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
389 fd, fd < 0 ? strerror(errno) : "OK");
394 /* Check if the device node is not what we expect it to be, and recreate it
395 * and try again if so.
397 if (st.st_rdev != dev) {
399 return DRM_ERR_NOT_ROOT;
401 mknod(buf, S_IFCHR | devmode, dev);
402 if (drm_server_info) {
403 chown_check_return(buf, user, group);
407 fd = open(buf, O_RDWR, 0);
408 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
409 fd, fd < 0 ? strerror(errno) : "OK");
413 drmMsg("drmOpenDevice: Open failed\n");
421 * Open the DRM device
423 * \param minor device minor number.
424 * \param create allow to create the device if set.
426 * \return a file descriptor on success, or a negative value on error.
429 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
430 * name from \p minor and opens it.
432 static int drmOpenMinor(int minor, int create, int type)
436 const char *dev_name;
439 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
442 case DRM_NODE_PRIMARY:
443 dev_name = DRM_DEV_NAME;
445 case DRM_NODE_CONTROL:
446 dev_name = DRM_CONTROL_DEV_NAME;
448 case DRM_NODE_RENDER:
449 dev_name = DRM_RENDER_DEV_NAME;
455 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
456 if ((fd = open(buf, O_RDWR, 0)) >= 0)
463 * Determine whether the DRM kernel driver has been loaded.
465 * \return 1 if the DRM driver is loaded, 0 otherwise.
468 * Determine the presence of the kernel driver by attempting to open the 0
469 * minor and get version information. For backward compatibility with older
470 * Linux implementations, /proc/dri is also checked.
472 int drmAvailable(void)
474 drmVersionPtr version;
478 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
480 /* Try proc for backward Linux compatibility */
481 if (!access("/proc/dri/0", R_OK))
487 if ((version = drmGetVersion(fd))) {
489 drmFreeVersion(version);
496 static int drmGetMinorBase(int type)
499 case DRM_NODE_PRIMARY:
501 case DRM_NODE_CONTROL:
503 case DRM_NODE_RENDER:
510 static int drmGetMinorType(int minor)
512 int type = minor >> 6;
518 case DRM_NODE_PRIMARY:
519 case DRM_NODE_CONTROL:
520 case DRM_NODE_RENDER:
527 static const char *drmGetMinorName(int type)
530 case DRM_NODE_PRIMARY:
532 case DRM_NODE_CONTROL:
534 case DRM_NODE_RENDER:
542 * Open the device by bus ID.
544 * \param busid bus ID.
545 * \param type device node type.
547 * \return a file descriptor on success, or a negative value on error.
550 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
551 * comparing the device bus ID with the one supplied.
553 * \sa drmOpenMinor() and drmGetBusid().
555 static int drmOpenByBusid(const char *busid, int type)
557 int i, pci_domain_ok = 1;
561 int base = drmGetMinorBase(type);
566 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
567 for (i = base; i < base + DRM_MAX_MINOR; i++) {
568 fd = drmOpenMinor(i, 1, type);
569 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
571 /* We need to try for 1.4 first for proper PCI domain support
572 * and if that fails, we know the kernel is busted
576 sv.drm_dd_major = -1; /* Don't care */
577 sv.drm_dd_minor = -1; /* Don't care */
578 if (drmSetInterfaceVersion(fd, &sv)) {
584 sv.drm_dd_major = -1; /* Don't care */
585 sv.drm_dd_minor = -1; /* Don't care */
586 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
587 drmSetInterfaceVersion(fd, &sv);
589 buf = drmGetBusid(fd);
590 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
591 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
605 * Open the device by name.
607 * \param name driver name.
608 * \param type the device node type.
610 * \return a file descriptor on success, or a negative value on error.
613 * This function opens the first minor number that matches the driver name and
614 * isn't already in use. If it's in use it then it will already have a bus ID
617 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
619 static int drmOpenByName(const char *name, int type)
623 drmVersionPtr version;
625 int base = drmGetMinorBase(type);
631 * Open the first minor number that matches the driver name and isn't
632 * already in use. If it's in use it will have a busid assigned already.
634 for (i = base; i < base + DRM_MAX_MINOR; i++) {
635 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
636 if ((version = drmGetVersion(fd))) {
637 if (!strcmp(version->name, name)) {
638 drmFreeVersion(version);
639 id = drmGetBusid(fd);
640 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
649 drmFreeVersion(version);
657 /* Backward-compatibility /proc support */
658 for (i = 0; i < 8; i++) {
659 char proc_name[64], buf[512];
660 char *driver, *pt, *devstring;
663 sprintf(proc_name, "/proc/dri/%d/name", i);
664 if ((fd = open(proc_name, 0, 0)) >= 0) {
665 retcode = read(fd, buf, sizeof(buf)-1);
668 buf[retcode-1] = '\0';
669 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
671 if (*pt) { /* Device is next */
673 if (!strcmp(driver, name)) { /* Match */
674 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
676 if (*pt) { /* Found busid */
677 return drmOpenByBusid(++pt, type);
678 } else { /* No busid */
679 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
693 * Open the DRM device.
695 * Looks up the specified name and bus ID, and opens the device found. The
696 * entry in /dev/dri is created if necessary and if called by root.
698 * \param name driver name. Not referenced if bus ID is supplied.
699 * \param busid bus ID. Zero if not known.
701 * \return a file descriptor on success, or a negative value on error.
704 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
707 int drmOpen(const char *name, const char *busid)
709 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
713 * Open the DRM device with specified type.
715 * Looks up the specified name and bus ID, and opens the device found. The
716 * entry in /dev/dri is created if necessary and if called by root.
718 * \param name driver name. Not referenced if bus ID is supplied.
719 * \param busid bus ID. Zero if not known.
720 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
722 * \return a file descriptor on success, or a negative value on error.
725 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
728 int drmOpenWithType(const char *name, const char *busid, int type)
730 if (!drmAvailable() && name != NULL && drm_server_info) {
731 /* try to load the kernel module */
732 if (!drm_server_info->load_module(name)) {
733 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
739 int fd = drmOpenByBusid(busid, type);
745 return drmOpenByName(name, type);
750 int drmOpenControl(int minor)
752 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
755 int drmOpenRender(int minor)
757 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
761 * Free the version information returned by drmGetVersion().
763 * \param v pointer to the version information.
766 * It frees the memory pointed by \p %v as well as all the non-null strings
769 void drmFreeVersion(drmVersionPtr v)
781 * Free the non-public version information returned by the kernel.
783 * \param v pointer to the version information.
786 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
787 * the non-null strings pointers in it.
789 static void drmFreeKernelVersion(drm_version_t *v)
801 * Copy version information.
803 * \param d destination pointer.
804 * \param s source pointer.
807 * Used by drmGetVersion() to translate the information returned by the ioctl
808 * interface in a private structure into the public structure counterpart.
810 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
812 d->version_major = s->version_major;
813 d->version_minor = s->version_minor;
814 d->version_patchlevel = s->version_patchlevel;
815 d->name_len = s->name_len;
816 d->name = strdup(s->name);
817 d->date_len = s->date_len;
818 d->date = strdup(s->date);
819 d->desc_len = s->desc_len;
820 d->desc = strdup(s->desc);
825 * Query the driver version information.
827 * \param fd file descriptor.
829 * \return pointer to a drmVersion structure which should be freed with
832 * \note Similar information is available via /proc/dri.
835 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
836 * first with zeros to get the string lengths, and then the actually strings.
837 * It also null-terminates them since they might not be already.
839 drmVersionPtr drmGetVersion(int fd)
841 drmVersionPtr retval;
842 drm_version_t *version = drmMalloc(sizeof(*version));
846 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
847 drmFreeKernelVersion(version);
851 if (version->name_len)
852 version->name = drmMalloc(version->name_len + 1);
853 if (version->date_len)
854 version->date = drmMalloc(version->date_len + 1);
855 if (version->desc_len)
856 version->desc = drmMalloc(version->desc_len + 1);
858 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
859 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
860 drmFreeKernelVersion(version);
864 /* The results might not be null-terminated strings, so terminate them. */
865 if (version->name_len) version->name[version->name_len] = '\0';
866 if (version->date_len) version->date[version->date_len] = '\0';
867 if (version->desc_len) version->desc[version->desc_len] = '\0';
869 retval = drmMalloc(sizeof(*retval));
870 drmCopyVersion(retval, version);
871 drmFreeKernelVersion(version);
877 * Get version information for the DRM user space library.
879 * This version number is driver independent.
881 * \param fd file descriptor.
883 * \return version information.
886 * This function allocates and fills a drm_version structure with a hard coded
889 drmVersionPtr drmGetLibVersion(int fd)
891 drm_version_t *version = drmMalloc(sizeof(*version));
894 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
895 * revision 1.0.x = original DRM interface with no drmGetLibVersion
896 * entry point and many drm<Device> extensions
897 * revision 1.1.x = added drmCommand entry points for device extensions
898 * added drmGetLibVersion to identify libdrm.a version
899 * revision 1.2.x = added drmSetInterfaceVersion
900 * modified drmOpen to handle both busid and name
901 * revision 1.3.x = added server + memory manager
903 version->version_major = 1;
904 version->version_minor = 3;
905 version->version_patchlevel = 0;
907 return (drmVersionPtr)version;
910 int drmGetCap(int fd, uint64_t capability, uint64_t *value)
912 struct drm_get_cap cap;
916 cap.capability = capability;
918 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
926 int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
928 struct drm_set_client_cap cap;
931 cap.capability = capability;
934 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
938 * Free the bus ID information.
940 * \param busid bus ID information string as given by drmGetBusid().
943 * This function is just frees the memory pointed by \p busid.
945 void drmFreeBusid(const char *busid)
947 drmFree((void *)busid);
952 * Get the bus ID of the device.
954 * \param fd file descriptor.
956 * \return bus ID string.
959 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
960 * get the string length and data, passing the arguments in a drm_unique
963 char *drmGetBusid(int fd)
969 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
971 u.unique = drmMalloc(u.unique_len + 1);
972 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
974 u.unique[u.unique_len] = '\0';
981 * Set the bus ID of the device.
983 * \param fd file descriptor.
984 * \param busid bus ID string.
986 * \return zero on success, negative on failure.
989 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
990 * the arguments in a drm_unique structure.
992 int drmSetBusid(int fd, const char *busid)
997 u.unique = (char *)busid;
998 u.unique_len = strlen(busid);
1000 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1006 int drmGetMagic(int fd, drm_magic_t * magic)
1013 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1015 *magic = auth.magic;
1019 int drmAuthMagic(int fd, drm_magic_t magic)
1025 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1031 * Specifies a range of memory that is available for mapping by a
1034 * \param fd file descriptor.
1035 * \param offset usually the physical address. The actual meaning depends of
1036 * the \p type parameter. See below.
1037 * \param size of the memory in bytes.
1038 * \param type type of the memory to be mapped.
1039 * \param flags combination of several flags to modify the function actions.
1040 * \param handle will be set to a value that may be used as the offset
1041 * parameter for mmap().
1043 * \return zero on success or a negative value on error.
1045 * \par Mapping the frame buffer
1046 * For the frame buffer
1047 * - \p offset will be the physical address of the start of the frame buffer,
1048 * - \p size will be the size of the frame buffer in bytes, and
1049 * - \p type will be DRM_FRAME_BUFFER.
1052 * The area mapped will be uncached. If MTRR support is available in the
1053 * kernel, the frame buffer area will be set to write combining.
1055 * \par Mapping the MMIO register area
1056 * For the MMIO register area,
1057 * - \p offset will be the physical address of the start of the register area,
1058 * - \p size will be the size of the register area bytes, and
1059 * - \p type will be DRM_REGISTERS.
1061 * The area mapped will be uncached.
1063 * \par Mapping the SAREA
1065 * - \p offset will be ignored and should be set to zero,
1066 * - \p size will be the desired size of the SAREA in bytes,
1067 * - \p type will be DRM_SHM.
1070 * A shared memory area of the requested size will be created and locked in
1071 * kernel memory. This area may be mapped into client-space by using the handle
1074 * \note May only be called by root.
1077 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1078 * the arguments in a drm_map structure.
1080 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1081 drmMapFlags flags, drm_handle_t *handle)
1086 map.offset = offset;
1090 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1093 *handle = (drm_handle_t)(uintptr_t)map.handle;
1097 int drmRmMap(int fd, drm_handle_t handle)
1102 map.handle = (void *)(uintptr_t)handle;
1104 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1110 * Make buffers available for DMA transfers.
1112 * \param fd file descriptor.
1113 * \param count number of buffers.
1114 * \param size size of each buffer.
1115 * \param flags buffer allocation flags.
1116 * \param agp_offset offset in the AGP aperture
1118 * \return number of buffers allocated, negative on error.
1121 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1125 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1128 drm_buf_desc_t request;
1131 request.count = count;
1132 request.size = size;
1133 request.flags = flags;
1134 request.agp_start = agp_offset;
1136 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1138 return request.count;
1141 int drmMarkBufs(int fd, double low, double high)
1143 drm_buf_info_t info;
1148 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1154 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1157 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1158 int retval = -errno;
1163 for (i = 0; i < info.count; i++) {
1164 info.list[i].low_mark = low * info.list[i].count;
1165 info.list[i].high_mark = high * info.list[i].count;
1166 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1167 int retval = -errno;
1180 * \param fd file descriptor.
1181 * \param count number of buffers to free.
1182 * \param list list of buffers to be freed.
1184 * \return zero on success, or a negative value on failure.
1186 * \note This function is primarily used for debugging.
1189 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1190 * the arguments in a drm_buf_free structure.
1192 int drmFreeBufs(int fd, int count, int *list)
1194 drm_buf_free_t request;
1197 request.count = count;
1198 request.list = list;
1199 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1208 * \param fd file descriptor.
1211 * This function closes the file descriptor.
1213 int drmClose(int fd)
1215 unsigned long key = drmGetKeyFromFd(fd);
1216 drmHashEntry *entry = drmGetEntry(fd);
1218 drmHashDestroy(entry->tagTable);
1221 entry->tagTable = NULL;
1223 drmHashDelete(drmHashTable, key);
1231 * Map a region of memory.
1233 * \param fd file descriptor.
1234 * \param handle handle returned by drmAddMap().
1235 * \param size size in bytes. Must match the size used by drmAddMap().
1236 * \param address will contain the user-space virtual address where the mapping
1239 * \return zero on success, or a negative value on failure.
1242 * This function is a wrapper for mmap().
1244 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1246 static unsigned long pagesize_mask = 0;
1252 pagesize_mask = getpagesize() - 1;
1254 size = (size + pagesize_mask) & ~pagesize_mask;
1256 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1257 if (*address == MAP_FAILED)
1264 * Unmap mappings obtained with drmMap().
1266 * \param address address as given by drmMap().
1267 * \param size size in bytes. Must match the size used by drmMap().
1269 * \return zero on success, or a negative value on failure.
1272 * This function is a wrapper for munmap().
1274 int drmUnmap(drmAddress address, drmSize size)
1276 return drm_munmap(address, size);
1279 drmBufInfoPtr drmGetBufInfo(int fd)
1281 drm_buf_info_t info;
1282 drmBufInfoPtr retval;
1287 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1291 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1294 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1299 retval = drmMalloc(sizeof(*retval));
1300 retval->count = info.count;
1301 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1302 for (i = 0; i < info.count; i++) {
1303 retval->list[i].count = info.list[i].count;
1304 retval->list[i].size = info.list[i].size;
1305 retval->list[i].low_mark = info.list[i].low_mark;
1306 retval->list[i].high_mark = info.list[i].high_mark;
1315 * Map all DMA buffers into client-virtual space.
1317 * \param fd file descriptor.
1319 * \return a pointer to a ::drmBufMap structure.
1321 * \note The client may not use these buffers until obtaining buffer indices
1325 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1326 * information about the buffers in a drm_buf_map structure into the
1327 * client-visible data structures.
1329 drmBufMapPtr drmMapBufs(int fd)
1332 drmBufMapPtr retval;
1336 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1342 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1345 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1350 retval = drmMalloc(sizeof(*retval));
1351 retval->count = bufs.count;
1352 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1353 for (i = 0; i < bufs.count; i++) {
1354 retval->list[i].idx = bufs.list[i].idx;
1355 retval->list[i].total = bufs.list[i].total;
1356 retval->list[i].used = 0;
1357 retval->list[i].address = bufs.list[i].address;
1367 * Unmap buffers allocated with drmMapBufs().
1369 * \return zero on success, or negative value on failure.
1372 * Calls munmap() for every buffer stored in \p bufs and frees the
1373 * memory allocated by drmMapBufs().
1375 int drmUnmapBufs(drmBufMapPtr bufs)
1379 for (i = 0; i < bufs->count; i++) {
1380 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1383 drmFree(bufs->list);
1390 #define DRM_DMA_RETRY 16
1393 * Reserve DMA buffers.
1395 * \param fd file descriptor.
1398 * \return zero on success, or a negative value on failure.
1401 * Assemble the arguments into a drm_dma structure and keeps issuing the
1402 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1404 int drmDMA(int fd, drmDMAReqPtr request)
1409 dma.context = request->context;
1410 dma.send_count = request->send_count;
1411 dma.send_indices = request->send_list;
1412 dma.send_sizes = request->send_sizes;
1413 dma.flags = request->flags;
1414 dma.request_count = request->request_count;
1415 dma.request_size = request->request_size;
1416 dma.request_indices = request->request_list;
1417 dma.request_sizes = request->request_sizes;
1418 dma.granted_count = 0;
1421 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1422 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1425 request->granted_count = dma.granted_count;
1434 * Obtain heavyweight hardware lock.
1436 * \param fd file descriptor.
1437 * \param context context.
1438 * \param flags flags that determine the sate of the hardware when the function
1441 * \return always zero.
1444 * This function translates the arguments into a drm_lock structure and issue
1445 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1447 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1452 lock.context = context;
1454 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1455 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1456 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1457 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1458 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1459 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1461 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1467 * Release the hardware lock.
1469 * \param fd file descriptor.
1470 * \param context context.
1472 * \return zero on success, or a negative value on failure.
1475 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1476 * argument in a drm_lock structure.
1478 int drmUnlock(int fd, drm_context_t context)
1483 lock.context = context;
1484 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1487 drm_context_t *drmGetReservedContextList(int fd, int *count)
1491 drm_context_t * retval;
1495 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1501 if (!(list = drmMalloc(res.count * sizeof(*list))))
1503 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1508 res.contexts = list;
1509 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1512 for (i = 0; i < res.count; i++)
1513 retval[i] = list[i].handle;
1520 void drmFreeReservedContextList(drm_context_t *pt)
1528 * Used by the X server during GLXContext initialization. This causes
1529 * per-context kernel-level resources to be allocated.
1531 * \param fd file descriptor.
1532 * \param handle is set on success. To be used by the client when requesting DMA
1533 * dispatch with drmDMA().
1535 * \return zero on success, or a negative value on failure.
1537 * \note May only be called by root.
1540 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1541 * argument in a drm_ctx structure.
1543 int drmCreateContext(int fd, drm_context_t *handle)
1548 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1550 *handle = ctx.handle;
1554 int drmSwitchToContext(int fd, drm_context_t context)
1559 ctx.handle = context;
1560 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1565 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1570 * Context preserving means that no context switches are done between DMA
1571 * buffers from one context and the next. This is suitable for use in the
1572 * X server (which promises to maintain hardware context), or in the
1573 * client-side library when buffers are swapped on behalf of two threads.
1576 ctx.handle = context;
1577 if (flags & DRM_CONTEXT_PRESERVED)
1578 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1579 if (flags & DRM_CONTEXT_2DONLY)
1580 ctx.flags |= _DRM_CONTEXT_2DONLY;
1581 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1586 int drmGetContextFlags(int fd, drm_context_t context,
1587 drm_context_tFlagsPtr flags)
1592 ctx.handle = context;
1593 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1596 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1597 *flags |= DRM_CONTEXT_PRESERVED;
1598 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1599 *flags |= DRM_CONTEXT_2DONLY;
1606 * Free any kernel-level resources allocated with drmCreateContext() associated
1609 * \param fd file descriptor.
1610 * \param handle handle given by drmCreateContext().
1612 * \return zero on success, or a negative value on failure.
1614 * \note May only be called by root.
1617 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1618 * argument in a drm_ctx structure.
1620 int drmDestroyContext(int fd, drm_context_t handle)
1625 ctx.handle = handle;
1626 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1631 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1636 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1638 *handle = draw.handle;
1642 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1647 draw.handle = handle;
1648 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1653 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1654 drm_drawable_info_type_t type, unsigned int num,
1657 drm_update_draw_t update;
1660 update.handle = handle;
1663 update.data = (unsigned long long)(unsigned long)data;
1665 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1672 * Acquire the AGP device.
1674 * Must be called before any of the other AGP related calls.
1676 * \param fd file descriptor.
1678 * \return zero on success, or a negative value on failure.
1681 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1683 int drmAgpAcquire(int fd)
1685 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1692 * Release the AGP device.
1694 * \param fd file descriptor.
1696 * \return zero on success, or a negative value on failure.
1699 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1701 int drmAgpRelease(int fd)
1703 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1712 * \param fd file descriptor.
1713 * \param mode AGP mode.
1715 * \return zero on success, or a negative value on failure.
1718 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1719 * argument in a drm_agp_mode structure.
1721 int drmAgpEnable(int fd, unsigned long mode)
1727 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1734 * Allocate a chunk of AGP memory.
1736 * \param fd file descriptor.
1737 * \param size requested memory size in bytes. Will be rounded to page boundary.
1738 * \param type type of memory to allocate.
1739 * \param address if not zero, will be set to the physical address of the
1741 * \param handle on success will be set to a handle of the allocated memory.
1743 * \return zero on success, or a negative value on failure.
1746 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1747 * arguments in a drm_agp_buffer structure.
1749 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1750 unsigned long *address, drm_handle_t *handle)
1755 *handle = DRM_AGP_NO_HANDLE;
1758 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1761 *address = b.physical;
1768 * Free a chunk of AGP memory.
1770 * \param fd file descriptor.
1771 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1773 * \return zero on success, or a negative value on failure.
1776 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1777 * argument in a drm_agp_buffer structure.
1779 int drmAgpFree(int fd, drm_handle_t handle)
1785 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1792 * Bind a chunk of AGP memory.
1794 * \param fd file descriptor.
1795 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1796 * \param offset offset in bytes. It will round to page boundary.
1798 * \return zero on success, or a negative value on failure.
1801 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1802 * argument in a drm_agp_binding structure.
1804 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1806 drm_agp_binding_t b;
1811 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1818 * Unbind a chunk of AGP memory.
1820 * \param fd file descriptor.
1821 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1823 * \return zero on success, or a negative value on failure.
1826 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1827 * the argument in a drm_agp_binding structure.
1829 int drmAgpUnbind(int fd, drm_handle_t handle)
1831 drm_agp_binding_t b;
1835 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1842 * Get AGP driver major version number.
1844 * \param fd file descriptor.
1846 * \return major version number on success, or a negative value on failure..
1849 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1850 * necessary information in a drm_agp_info structure.
1852 int drmAgpVersionMajor(int fd)
1858 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1860 return i.agp_version_major;
1865 * Get AGP driver minor version number.
1867 * \param fd file descriptor.
1869 * \return minor version number on success, or a negative value on failure.
1872 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1873 * necessary information in a drm_agp_info structure.
1875 int drmAgpVersionMinor(int fd)
1881 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1883 return i.agp_version_minor;
1890 * \param fd file descriptor.
1892 * \return mode on success, or zero on failure.
1895 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1896 * necessary information in a drm_agp_info structure.
1898 unsigned long drmAgpGetMode(int fd)
1904 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1911 * Get AGP aperture base.
1913 * \param fd file descriptor.
1915 * \return aperture base on success, zero on failure.
1918 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1919 * necessary information in a drm_agp_info structure.
1921 unsigned long drmAgpBase(int fd)
1927 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1929 return i.aperture_base;
1934 * Get AGP aperture size.
1936 * \param fd file descriptor.
1938 * \return aperture size on success, zero on failure.
1941 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1942 * necessary information in a drm_agp_info structure.
1944 unsigned long drmAgpSize(int fd)
1950 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1952 return i.aperture_size;
1957 * Get used AGP memory.
1959 * \param fd file descriptor.
1961 * \return memory used on success, or zero on failure.
1964 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1965 * necessary information in a drm_agp_info structure.
1967 unsigned long drmAgpMemoryUsed(int fd)
1973 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1975 return i.memory_used;
1980 * Get available AGP memory.
1982 * \param fd file descriptor.
1984 * \return memory available on success, or zero on failure.
1987 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1988 * necessary information in a drm_agp_info structure.
1990 unsigned long drmAgpMemoryAvail(int fd)
1996 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1998 return i.memory_allowed;
2003 * Get hardware vendor ID.
2005 * \param fd file descriptor.
2007 * \return vendor ID on success, or zero on failure.
2010 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2011 * necessary information in a drm_agp_info structure.
2013 unsigned int drmAgpVendorId(int fd)
2019 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2026 * Get hardware device ID.
2028 * \param fd file descriptor.
2030 * \return zero on success, or zero on failure.
2033 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2034 * necessary information in a drm_agp_info structure.
2036 unsigned int drmAgpDeviceId(int fd)
2042 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2047 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
2049 drm_scatter_gather_t sg;
2055 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2057 *handle = sg.handle;
2061 int drmScatterGatherFree(int fd, drm_handle_t handle)
2063 drm_scatter_gather_t sg;
2067 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2075 * \param fd file descriptor.
2076 * \param vbl pointer to a drmVBlank structure.
2078 * \return zero on success, or a negative value on failure.
2081 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2083 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2085 struct timespec timeout, cur;
2088 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2090 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2096 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2097 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2098 if (ret && errno == EINTR) {
2099 clock_gettime(CLOCK_MONOTONIC, &cur);
2100 /* Timeout after 1s */
2101 if (cur.tv_sec > timeout.tv_sec + 1 ||
2102 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2109 } while (ret && errno == EINTR);
2115 int drmError(int err, const char *label)
2118 case DRM_ERR_NO_DEVICE:
2119 fprintf(stderr, "%s: no device\n", label);
2121 case DRM_ERR_NO_ACCESS:
2122 fprintf(stderr, "%s: no access\n", label);
2124 case DRM_ERR_NOT_ROOT:
2125 fprintf(stderr, "%s: not root\n", label);
2127 case DRM_ERR_INVALID:
2128 fprintf(stderr, "%s: invalid args\n", label);
2133 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2141 * Install IRQ handler.
2143 * \param fd file descriptor.
2144 * \param irq IRQ number.
2146 * \return zero on success, or a negative value on failure.
2149 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2150 * argument in a drm_control structure.
2152 int drmCtlInstHandler(int fd, int irq)
2157 ctl.func = DRM_INST_HANDLER;
2159 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2166 * Uninstall IRQ handler.
2168 * \param fd file descriptor.
2170 * \return zero on success, or a negative value on failure.
2173 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2174 * argument in a drm_control structure.
2176 int drmCtlUninstHandler(int fd)
2181 ctl.func = DRM_UNINST_HANDLER;
2183 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2188 int drmFinish(int fd, int context, drmLockFlags flags)
2193 lock.context = context;
2194 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2195 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2196 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2197 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2198 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2199 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2200 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2206 * Get IRQ from bus ID.
2208 * \param fd file descriptor.
2209 * \param busnum bus number.
2210 * \param devnum device number.
2211 * \param funcnum function number.
2213 * \return IRQ number on success, or a negative value on failure.
2216 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2217 * arguments in a drm_irq_busid structure.
2219 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2226 p.funcnum = funcnum;
2227 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2232 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2234 drmHashEntry *entry = drmGetEntry(fd);
2236 if (drmHashInsert(entry->tagTable, context, tag)) {
2237 drmHashDelete(entry->tagTable, context);
2238 drmHashInsert(entry->tagTable, context, tag);
2243 int drmDelContextTag(int fd, drm_context_t context)
2245 drmHashEntry *entry = drmGetEntry(fd);
2247 return drmHashDelete(entry->tagTable, context);
2250 void *drmGetContextTag(int fd, drm_context_t context)
2252 drmHashEntry *entry = drmGetEntry(fd);
2255 if (drmHashLookup(entry->tagTable, context, &value))
2261 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2262 drm_handle_t handle)
2264 drm_ctx_priv_map_t map;
2267 map.ctx_id = ctx_id;
2268 map.handle = (void *)(uintptr_t)handle;
2270 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2275 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2276 drm_handle_t *handle)
2278 drm_ctx_priv_map_t map;
2281 map.ctx_id = ctx_id;
2283 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2286 *handle = (drm_handle_t)(uintptr_t)map.handle;
2291 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2292 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2299 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2301 *offset = map.offset;
2305 *handle = (unsigned long)map.handle;
2310 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2311 unsigned long *magic, unsigned long *iocs)
2313 drm_client_t client;
2317 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2319 *auth = client.auth;
2322 *magic = client.magic;
2323 *iocs = client.iocs;
2327 int drmGetStats(int fd, drmStatsT *stats)
2333 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2337 memset(stats, 0, sizeof(*stats));
2338 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2342 stats->data[i].long_format = "%-20.20s"; \
2343 stats->data[i].rate_format = "%8.8s"; \
2344 stats->data[i].isvalue = 1; \
2345 stats->data[i].verbose = 0
2348 stats->data[i].long_format = "%-20.20s"; \
2349 stats->data[i].rate_format = "%5.5s"; \
2350 stats->data[i].isvalue = 0; \
2351 stats->data[i].mult_names = "kgm"; \
2352 stats->data[i].mult = 1000; \
2353 stats->data[i].verbose = 0
2356 stats->data[i].long_format = "%-20.20s"; \
2357 stats->data[i].rate_format = "%5.5s"; \
2358 stats->data[i].isvalue = 0; \
2359 stats->data[i].mult_names = "KGM"; \
2360 stats->data[i].mult = 1024; \
2361 stats->data[i].verbose = 0
2364 stats->count = s.count;
2365 for (i = 0; i < s.count; i++) {
2366 stats->data[i].value = s.data[i].value;
2367 switch (s.data[i].type) {
2368 case _DRM_STAT_LOCK:
2369 stats->data[i].long_name = "Lock";
2370 stats->data[i].rate_name = "Lock";
2373 case _DRM_STAT_OPENS:
2374 stats->data[i].long_name = "Opens";
2375 stats->data[i].rate_name = "O";
2377 stats->data[i].verbose = 1;
2379 case _DRM_STAT_CLOSES:
2380 stats->data[i].long_name = "Closes";
2381 stats->data[i].rate_name = "Lock";
2383 stats->data[i].verbose = 1;
2385 case _DRM_STAT_IOCTLS:
2386 stats->data[i].long_name = "Ioctls";
2387 stats->data[i].rate_name = "Ioc/s";
2390 case _DRM_STAT_LOCKS:
2391 stats->data[i].long_name = "Locks";
2392 stats->data[i].rate_name = "Lck/s";
2395 case _DRM_STAT_UNLOCKS:
2396 stats->data[i].long_name = "Unlocks";
2397 stats->data[i].rate_name = "Unl/s";
2401 stats->data[i].long_name = "IRQs";
2402 stats->data[i].rate_name = "IRQ/s";
2405 case _DRM_STAT_PRIMARY:
2406 stats->data[i].long_name = "Primary Bytes";
2407 stats->data[i].rate_name = "PB/s";
2410 case _DRM_STAT_SECONDARY:
2411 stats->data[i].long_name = "Secondary Bytes";
2412 stats->data[i].rate_name = "SB/s";
2416 stats->data[i].long_name = "DMA";
2417 stats->data[i].rate_name = "DMA/s";
2420 case _DRM_STAT_SPECIAL:
2421 stats->data[i].long_name = "Special DMA";
2422 stats->data[i].rate_name = "dma/s";
2425 case _DRM_STAT_MISSED:
2426 stats->data[i].long_name = "Miss";
2427 stats->data[i].rate_name = "Ms/s";
2430 case _DRM_STAT_VALUE:
2431 stats->data[i].long_name = "Value";
2432 stats->data[i].rate_name = "Value";
2435 case _DRM_STAT_BYTE:
2436 stats->data[i].long_name = "Bytes";
2437 stats->data[i].rate_name = "B/s";
2440 case _DRM_STAT_COUNT:
2442 stats->data[i].long_name = "Count";
2443 stats->data[i].rate_name = "Cnt/s";
2452 * Issue a set-version ioctl.
2454 * \param fd file descriptor.
2455 * \param drmCommandIndex command index
2456 * \param data source pointer of the data to be read and written.
2457 * \param size size of the data to be read and written.
2459 * \return zero on success, or a negative value on failure.
2462 * It issues a read-write ioctl given by
2463 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2465 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2468 drm_set_version_t sv;
2471 sv.drm_di_major = version->drm_di_major;
2472 sv.drm_di_minor = version->drm_di_minor;
2473 sv.drm_dd_major = version->drm_dd_major;
2474 sv.drm_dd_minor = version->drm_dd_minor;
2476 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2480 version->drm_di_major = sv.drm_di_major;
2481 version->drm_di_minor = sv.drm_di_minor;
2482 version->drm_dd_major = sv.drm_dd_major;
2483 version->drm_dd_minor = sv.drm_dd_minor;
2489 * Send a device-specific command.
2491 * \param fd file descriptor.
2492 * \param drmCommandIndex command index
2494 * \return zero on success, or a negative value on failure.
2497 * It issues a ioctl given by
2498 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2500 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2502 unsigned long request;
2504 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2506 if (drmIoctl(fd, request, NULL)) {
2514 * Send a device-specific read command.
2516 * \param fd file descriptor.
2517 * \param drmCommandIndex command index
2518 * \param data destination pointer of the data to be read.
2519 * \param size size of the data to be read.
2521 * \return zero on success, or a negative value on failure.
2524 * It issues a read ioctl given by
2525 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2527 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2530 unsigned long request;
2532 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2533 DRM_COMMAND_BASE + drmCommandIndex, size);
2535 if (drmIoctl(fd, request, data)) {
2543 * Send a device-specific write command.
2545 * \param fd file descriptor.
2546 * \param drmCommandIndex command index
2547 * \param data source pointer of the data to be written.
2548 * \param size size of the data to be written.
2550 * \return zero on success, or a negative value on failure.
2553 * It issues a write ioctl given by
2554 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2556 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2559 unsigned long request;
2561 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2562 DRM_COMMAND_BASE + drmCommandIndex, size);
2564 if (drmIoctl(fd, request, data)) {
2572 * Send a device-specific read-write command.
2574 * \param fd file descriptor.
2575 * \param drmCommandIndex command index
2576 * \param data source pointer of the data to be read and written.
2577 * \param size size of the data to be read and written.
2579 * \return zero on success, or a negative value on failure.
2582 * It issues a read-write ioctl given by
2583 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2585 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2588 unsigned long request;
2590 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2591 DRM_COMMAND_BASE + drmCommandIndex, size);
2593 if (drmIoctl(fd, request, data))
2598 #define DRM_MAX_FDS 16
2604 } connection[DRM_MAX_FDS];
2606 static int nr_fds = 0;
2608 int drmOpenOnce(void *unused,
2612 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2615 int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2620 for (i = 0; i < nr_fds; i++)
2621 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2622 (connection[i].type == type)) {
2623 connection[i].refcount++;
2625 return connection[i].fd;
2628 fd = drmOpenWithType(NULL, BusID, type);
2629 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2632 connection[nr_fds].BusID = strdup(BusID);
2633 connection[nr_fds].fd = fd;
2634 connection[nr_fds].refcount = 1;
2635 connection[nr_fds].type = type;
2639 fprintf(stderr, "saved connection %d for %s %d\n",
2640 nr_fds, connection[nr_fds].BusID,
2641 strcmp(BusID, connection[nr_fds].BusID));
2648 void drmCloseOnce(int fd)
2652 for (i = 0; i < nr_fds; i++) {
2653 if (fd == connection[i].fd) {
2654 if (--connection[i].refcount == 0) {
2655 drmClose(connection[i].fd);
2656 free(connection[i].BusID);
2659 connection[i] = connection[nr_fds];
2667 int drmSetMaster(int fd)
2669 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2672 int drmDropMaster(int fd)
2674 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2677 char *drmGetDeviceNameFromFd(int fd)
2684 /* The whole drmOpen thing is a fiasco and we need to find a way
2685 * back to just using open(2). For now, however, lets just make
2686 * things worse with even more ad hoc directory walking code to
2687 * discover the device file name. */
2692 for (i = 0; i < DRM_MAX_MINOR; i++) {
2693 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2694 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2697 if (i == DRM_MAX_MINOR)
2700 return strdup(name);
2703 int drmGetNodeTypeFromFd(int fd)
2708 if (fstat(fd, &sbuf))
2711 maj = major(sbuf.st_rdev);
2712 min = minor(sbuf.st_rdev);
2714 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
2719 type = drmGetMinorType(min);
2725 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2727 struct drm_prime_handle args;
2730 args.handle = handle;
2732 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2736 *prime_fd = args.fd;
2740 int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2742 struct drm_prime_handle args;
2747 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2751 *handle = args.handle;
2755 static char *drmGetMinorNameForFD(int fd, int type)
2759 struct dirent *pent, *ent;
2761 const char *name = drmGetMinorName(type);
2763 char dev_name[64], buf[64];
2772 if (fstat(fd, &sbuf))
2775 maj = major(sbuf.st_rdev);
2776 min = minor(sbuf.st_rdev);
2778 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2781 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2783 sysdir = opendir(buf);
2787 name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
2791 pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
2795 while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
2796 if (strncmp(ent->d_name, name, len) == 0) {
2800 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2802 return strdup(dev_name);
2814 char *drmGetPrimaryDeviceNameFromFd(int fd)
2816 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2819 char *drmGetRenderDeviceNameFromFd(int fd)
2821 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);