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>
54 /* Not all systems have MAP_FAILED defined */
56 #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 memclear(s) memset(&s, 0, sizeof(s))
90 static drmServerInfoPtr drm_server_info;
92 void drmSetServerInfo(drmServerInfoPtr info)
94 drm_server_info = info;
98 * Output a message to stderr.
100 * \param format printf() like format string.
103 * This function is a wrapper around vfprintf().
106 static int DRM_PRINTFLIKE(1, 0)
107 drmDebugPrint(const char *format, va_list ap)
109 return vfprintf(stderr, format, ap);
112 typedef int DRM_PRINTFLIKE(1, 0) (*debug_msg_func_t)(const char *format,
115 static debug_msg_func_t drm_debug_print = drmDebugPrint;
118 drmMsg(const char *format, ...)
122 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
124 va_start(ap, format);
125 if (drm_server_info) {
126 drm_server_info->debug_print(format,ap);
128 drm_debug_print(format, ap);
135 drmSetDebugMsgFunction(debug_msg_func_t debug_msg_ptr)
137 drm_debug_print = debug_msg_ptr;
140 static void *drmHashTable = NULL; /* Context switch callbacks */
142 void *drmGetHashTable(void)
147 void *drmMalloc(int size)
150 if ((pt = malloc(size)))
155 void drmFree(void *pt)
162 * Call ioctl, restarting if it is interupted
165 drmIoctl(int fd, unsigned long request, void *arg)
170 ret = ioctl(fd, request, arg);
171 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
175 static unsigned long drmGetKeyFromFd(int fd)
184 drmHashEntry *drmGetEntry(int fd)
186 unsigned long key = drmGetKeyFromFd(fd);
191 drmHashTable = drmHashCreate();
193 if (drmHashLookup(drmHashTable, key, &value)) {
194 entry = drmMalloc(sizeof(*entry));
197 entry->tagTable = drmHashCreate();
198 drmHashInsert(drmHashTable, key, entry);
206 * Compare two busid strings
211 * \return 1 if matched.
214 * This function compares two bus ID strings. It understands the older
215 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
216 * domain, b is bus, d is device, f is function.
218 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
220 /* First, check if the IDs are exactly the same */
221 if (strcasecmp(id1, id2) == 0)
224 /* Try to match old/new-style PCI bus IDs. */
225 if (strncasecmp(id1, "pci", 3) == 0) {
226 unsigned int o1, b1, d1, f1;
227 unsigned int o2, b2, d2, f2;
230 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
233 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
238 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
241 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
246 /* If domains aren't properly supported by the kernel interface,
247 * just ignore them, which sucks less than picking a totally random
248 * card with "open by name"
253 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
262 * Handles error checking for chown call.
264 * \param path to file.
265 * \param id of the new owner.
266 * \param id of the new group.
268 * \return zero if success or -1 if failure.
271 * Checks for failure. If failure was caused by signal call chown again.
272 * If any other failure happened then it will output error mesage using
275 static int chown_check_return(const char *path, uid_t owner, gid_t group)
280 rv = chown(path, owner, group);
281 } while (rv != 0 && errno == EINTR);
286 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
287 path, errno, strerror(errno));
292 * Open the DRM device, creating it if necessary.
294 * \param dev major and minor numbers of the device.
295 * \param minor minor number of the device.
297 * \return a file descriptor on success, or a negative value on error.
300 * Assembles the device name from \p minor and opens it, creating the device
301 * special file node with the major and minor numbers specified by \p dev and
302 * parent directory if necessary and was called by root.
304 static int drmOpenDevice(dev_t dev, int minor, int type)
307 const char *dev_name;
310 mode_t devmode = DRM_DEV_MODE, serv_mode;
311 int isroot = !geteuid();
312 uid_t user = DRM_DEV_UID;
313 gid_t group = DRM_DEV_GID, serv_group;
316 case DRM_NODE_PRIMARY:
317 dev_name = DRM_DEV_NAME;
319 case DRM_NODE_CONTROL:
320 dev_name = DRM_CONTROL_DEV_NAME;
322 case DRM_NODE_RENDER:
323 dev_name = DRM_RENDER_DEV_NAME;
329 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
330 drmMsg("drmOpenDevice: node name is %s\n", buf);
332 if (drm_server_info) {
333 drm_server_info->get_perms(&serv_group, &serv_mode);
334 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
335 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
336 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
340 if (stat(DRM_DIR_NAME, &st)) {
342 return DRM_ERR_NOT_ROOT;
343 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
344 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
345 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
348 /* Check if the device node exists and create it if necessary. */
349 if (stat(buf, &st)) {
351 return DRM_ERR_NOT_ROOT;
353 mknod(buf, S_IFCHR | devmode, dev);
356 if (drm_server_info) {
357 chown_check_return(buf, user, group);
361 /* if we modprobed then wait for udev */
365 if (stat(DRM_DIR_NAME, &st)) {
369 if (udev_count == 50)
374 if (stat(buf, &st)) {
378 if (udev_count == 50)
385 fd = open(buf, O_RDWR, 0);
386 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
387 fd, fd < 0 ? strerror(errno) : "OK");
392 /* Check if the device node is not what we expect it to be, and recreate it
393 * and try again if so.
395 if (st.st_rdev != dev) {
397 return DRM_ERR_NOT_ROOT;
399 mknod(buf, S_IFCHR | devmode, dev);
400 if (drm_server_info) {
401 chown_check_return(buf, user, group);
405 fd = open(buf, O_RDWR, 0);
406 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
407 fd, fd < 0 ? strerror(errno) : "OK");
411 drmMsg("drmOpenDevice: Open failed\n");
419 * Open the DRM device
421 * \param minor device minor number.
422 * \param create allow to create the device if set.
424 * \return a file descriptor on success, or a negative value on error.
427 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
428 * name from \p minor and opens it.
430 static int drmOpenMinor(int minor, int create, int type)
434 const char *dev_name;
437 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
440 case DRM_NODE_PRIMARY:
441 dev_name = DRM_DEV_NAME;
443 case DRM_NODE_CONTROL:
444 dev_name = DRM_CONTROL_DEV_NAME;
446 case DRM_NODE_RENDER:
447 dev_name = DRM_RENDER_DEV_NAME;
453 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
454 if ((fd = open(buf, O_RDWR, 0)) >= 0)
461 * Determine whether the DRM kernel driver has been loaded.
463 * \return 1 if the DRM driver is loaded, 0 otherwise.
466 * Determine the presence of the kernel driver by attempting to open the 0
467 * minor and get version information. For backward compatibility with older
468 * Linux implementations, /proc/dri is also checked.
470 int drmAvailable(void)
472 drmVersionPtr version;
476 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
478 /* Try proc for backward Linux compatibility */
479 if (!access("/proc/dri/0", R_OK))
485 if ((version = drmGetVersion(fd))) {
487 drmFreeVersion(version);
494 static int drmGetMinorBase(int type)
497 case DRM_NODE_PRIMARY:
499 case DRM_NODE_CONTROL:
501 case DRM_NODE_RENDER:
508 static int drmGetMinorType(int minor)
510 int type = minor >> 6;
516 case DRM_NODE_PRIMARY:
517 case DRM_NODE_CONTROL:
518 case DRM_NODE_RENDER:
526 * Open the device by bus ID.
528 * \param busid bus ID.
529 * \param type device node type.
531 * \return a file descriptor on success, or a negative value on error.
534 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
535 * comparing the device bus ID with the one supplied.
537 * \sa drmOpenMinor() and drmGetBusid().
539 static int drmOpenByBusid(const char *busid, int type)
541 int i, pci_domain_ok = 1;
545 int base = drmGetMinorBase(type);
550 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
551 for (i = base; i < base + DRM_MAX_MINOR; i++) {
552 fd = drmOpenMinor(i, 1, type);
553 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
555 /* We need to try for 1.4 first for proper PCI domain support
556 * and if that fails, we know the kernel is busted
560 sv.drm_dd_major = -1; /* Don't care */
561 sv.drm_dd_minor = -1; /* Don't care */
562 if (drmSetInterfaceVersion(fd, &sv)) {
568 sv.drm_dd_major = -1; /* Don't care */
569 sv.drm_dd_minor = -1; /* Don't care */
570 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
571 drmSetInterfaceVersion(fd, &sv);
573 buf = drmGetBusid(fd);
574 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
575 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
589 * Open the device by name.
591 * \param name driver name.
592 * \param type the device node type.
594 * \return a file descriptor on success, or a negative value on error.
597 * This function opens the first minor number that matches the driver name and
598 * isn't already in use. If it's in use it then it will already have a bus ID
601 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
603 static int drmOpenByName(const char *name, int type)
607 drmVersionPtr version;
609 int base = drmGetMinorBase(type);
615 * Open the first minor number that matches the driver name and isn't
616 * already in use. If it's in use it will have a busid assigned already.
618 for (i = base; i < base + DRM_MAX_MINOR; i++) {
619 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
620 if ((version = drmGetVersion(fd))) {
621 if (!strcmp(version->name, name)) {
622 drmFreeVersion(version);
623 id = drmGetBusid(fd);
624 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
633 drmFreeVersion(version);
641 /* Backward-compatibility /proc support */
642 for (i = 0; i < 8; i++) {
643 char proc_name[64], buf[512];
644 char *driver, *pt, *devstring;
647 sprintf(proc_name, "/proc/dri/%d/name", i);
648 if ((fd = open(proc_name, 0, 0)) >= 0) {
649 retcode = read(fd, buf, sizeof(buf)-1);
652 buf[retcode-1] = '\0';
653 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
655 if (*pt) { /* Device is next */
657 if (!strcmp(driver, name)) { /* Match */
658 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
660 if (*pt) { /* Found busid */
661 return drmOpenByBusid(++pt, type);
662 } else { /* No busid */
663 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
677 * Open the DRM device.
679 * Looks up the specified name and bus ID, and opens the device found. The
680 * entry in /dev/dri is created if necessary and if called by root.
682 * \param name driver name. Not referenced if bus ID is supplied.
683 * \param busid bus ID. Zero if not known.
685 * \return a file descriptor on success, or a negative value on error.
688 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
691 int drmOpen(const char *name, const char *busid)
693 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
697 * Open the DRM device with specified type.
699 * Looks up the specified name and bus ID, and opens the device found. The
700 * entry in /dev/dri is created if necessary and if called by root.
702 * \param name driver name. Not referenced if bus ID is supplied.
703 * \param busid bus ID. Zero if not known.
704 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
706 * \return a file descriptor on success, or a negative value on error.
709 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
712 int drmOpenWithType(const char *name, const char *busid, int type)
714 if (!drmAvailable() && name != NULL && drm_server_info) {
715 /* try to load the kernel module */
716 if (!drm_server_info->load_module(name)) {
717 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
723 int fd = drmOpenByBusid(busid, type);
729 return drmOpenByName(name, type);
734 int drmOpenControl(int minor)
736 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
739 int drmOpenRender(int minor)
741 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
745 * Free the version information returned by drmGetVersion().
747 * \param v pointer to the version information.
750 * It frees the memory pointed by \p %v as well as all the non-null strings
753 void drmFreeVersion(drmVersionPtr v)
765 * Free the non-public version information returned by the kernel.
767 * \param v pointer to the version information.
770 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
771 * the non-null strings pointers in it.
773 static void drmFreeKernelVersion(drm_version_t *v)
785 * Copy version information.
787 * \param d destination pointer.
788 * \param s source pointer.
791 * Used by drmGetVersion() to translate the information returned by the ioctl
792 * interface in a private structure into the public structure counterpart.
794 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
796 d->version_major = s->version_major;
797 d->version_minor = s->version_minor;
798 d->version_patchlevel = s->version_patchlevel;
799 d->name_len = s->name_len;
800 d->name = strdup(s->name);
801 d->date_len = s->date_len;
802 d->date = strdup(s->date);
803 d->desc_len = s->desc_len;
804 d->desc = strdup(s->desc);
809 * Query the driver version information.
811 * \param fd file descriptor.
813 * \return pointer to a drmVersion structure which should be freed with
816 * \note Similar information is available via /proc/dri.
819 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
820 * first with zeros to get the string lengths, and then the actually strings.
821 * It also null-terminates them since they might not be already.
823 drmVersionPtr drmGetVersion(int fd)
825 drmVersionPtr retval;
826 drm_version_t *version = drmMalloc(sizeof(*version));
830 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
831 drmFreeKernelVersion(version);
835 if (version->name_len)
836 version->name = drmMalloc(version->name_len + 1);
837 if (version->date_len)
838 version->date = drmMalloc(version->date_len + 1);
839 if (version->desc_len)
840 version->desc = drmMalloc(version->desc_len + 1);
842 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
843 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
844 drmFreeKernelVersion(version);
848 /* The results might not be null-terminated strings, so terminate them. */
849 if (version->name_len) version->name[version->name_len] = '\0';
850 if (version->date_len) version->date[version->date_len] = '\0';
851 if (version->desc_len) version->desc[version->desc_len] = '\0';
853 retval = drmMalloc(sizeof(*retval));
854 drmCopyVersion(retval, version);
855 drmFreeKernelVersion(version);
861 * Get version information for the DRM user space library.
863 * This version number is driver independent.
865 * \param fd file descriptor.
867 * \return version information.
870 * This function allocates and fills a drm_version structure with a hard coded
873 drmVersionPtr drmGetLibVersion(int fd)
875 drm_version_t *version = drmMalloc(sizeof(*version));
878 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
879 * revision 1.0.x = original DRM interface with no drmGetLibVersion
880 * entry point and many drm<Device> extensions
881 * revision 1.1.x = added drmCommand entry points for device extensions
882 * added drmGetLibVersion to identify libdrm.a version
883 * revision 1.2.x = added drmSetInterfaceVersion
884 * modified drmOpen to handle both busid and name
885 * revision 1.3.x = added server + memory manager
887 version->version_major = 1;
888 version->version_minor = 3;
889 version->version_patchlevel = 0;
891 return (drmVersionPtr)version;
894 int drmGetCap(int fd, uint64_t capability, uint64_t *value)
896 struct drm_get_cap cap;
900 cap.capability = capability;
902 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
910 int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
912 struct drm_set_client_cap cap;
915 cap.capability = capability;
918 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
922 * Free the bus ID information.
924 * \param busid bus ID information string as given by drmGetBusid().
927 * This function is just frees the memory pointed by \p busid.
929 void drmFreeBusid(const char *busid)
931 drmFree((void *)busid);
936 * Get the bus ID of the device.
938 * \param fd file descriptor.
940 * \return bus ID string.
943 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
944 * get the string length and data, passing the arguments in a drm_unique
947 char *drmGetBusid(int fd)
953 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
955 u.unique = drmMalloc(u.unique_len + 1);
956 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
958 u.unique[u.unique_len] = '\0';
965 * Set the bus ID of the device.
967 * \param fd file descriptor.
968 * \param busid bus ID string.
970 * \return zero on success, negative on failure.
973 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
974 * the arguments in a drm_unique structure.
976 int drmSetBusid(int fd, const char *busid)
981 u.unique = (char *)busid;
982 u.unique_len = strlen(busid);
984 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
990 int drmGetMagic(int fd, drm_magic_t * magic)
997 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1003 int drmAuthMagic(int fd, drm_magic_t magic)
1009 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1015 * Specifies a range of memory that is available for mapping by a
1018 * \param fd file descriptor.
1019 * \param offset usually the physical address. The actual meaning depends of
1020 * the \p type parameter. See below.
1021 * \param size of the memory in bytes.
1022 * \param type type of the memory to be mapped.
1023 * \param flags combination of several flags to modify the function actions.
1024 * \param handle will be set to a value that may be used as the offset
1025 * parameter for mmap().
1027 * \return zero on success or a negative value on error.
1029 * \par Mapping the frame buffer
1030 * For the frame buffer
1031 * - \p offset will be the physical address of the start of the frame buffer,
1032 * - \p size will be the size of the frame buffer in bytes, and
1033 * - \p type will be DRM_FRAME_BUFFER.
1036 * The area mapped will be uncached. If MTRR support is available in the
1037 * kernel, the frame buffer area will be set to write combining.
1039 * \par Mapping the MMIO register area
1040 * For the MMIO register area,
1041 * - \p offset will be the physical address of the start of the register area,
1042 * - \p size will be the size of the register area bytes, and
1043 * - \p type will be DRM_REGISTERS.
1045 * The area mapped will be uncached.
1047 * \par Mapping the SAREA
1049 * - \p offset will be ignored and should be set to zero,
1050 * - \p size will be the desired size of the SAREA in bytes,
1051 * - \p type will be DRM_SHM.
1054 * A shared memory area of the requested size will be created and locked in
1055 * kernel memory. This area may be mapped into client-space by using the handle
1058 * \note May only be called by root.
1061 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1062 * the arguments in a drm_map structure.
1064 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1065 drmMapFlags flags, drm_handle_t *handle)
1070 map.offset = offset;
1074 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1077 *handle = (drm_handle_t)(uintptr_t)map.handle;
1081 int drmRmMap(int fd, drm_handle_t handle)
1086 map.handle = (void *)(uintptr_t)handle;
1088 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1094 * Make buffers available for DMA transfers.
1096 * \param fd file descriptor.
1097 * \param count number of buffers.
1098 * \param size size of each buffer.
1099 * \param flags buffer allocation flags.
1100 * \param agp_offset offset in the AGP aperture
1102 * \return number of buffers allocated, negative on error.
1105 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1109 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1112 drm_buf_desc_t request;
1115 request.count = count;
1116 request.size = size;
1117 request.flags = flags;
1118 request.agp_start = agp_offset;
1120 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1122 return request.count;
1125 int drmMarkBufs(int fd, double low, double high)
1127 drm_buf_info_t info;
1132 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1138 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1141 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1142 int retval = -errno;
1147 for (i = 0; i < info.count; i++) {
1148 info.list[i].low_mark = low * info.list[i].count;
1149 info.list[i].high_mark = high * info.list[i].count;
1150 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1151 int retval = -errno;
1164 * \param fd file descriptor.
1165 * \param count number of buffers to free.
1166 * \param list list of buffers to be freed.
1168 * \return zero on success, or a negative value on failure.
1170 * \note This function is primarily used for debugging.
1173 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1174 * the arguments in a drm_buf_free structure.
1176 int drmFreeBufs(int fd, int count, int *list)
1178 drm_buf_free_t request;
1181 request.count = count;
1182 request.list = list;
1183 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1192 * \param fd file descriptor.
1195 * This function closes the file descriptor.
1197 int drmClose(int fd)
1199 unsigned long key = drmGetKeyFromFd(fd);
1200 drmHashEntry *entry = drmGetEntry(fd);
1202 drmHashDestroy(entry->tagTable);
1205 entry->tagTable = NULL;
1207 drmHashDelete(drmHashTable, key);
1215 * Map a region of memory.
1217 * \param fd file descriptor.
1218 * \param handle handle returned by drmAddMap().
1219 * \param size size in bytes. Must match the size used by drmAddMap().
1220 * \param address will contain the user-space virtual address where the mapping
1223 * \return zero on success, or a negative value on failure.
1226 * This function is a wrapper for mmap().
1228 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1230 static unsigned long pagesize_mask = 0;
1236 pagesize_mask = getpagesize() - 1;
1238 size = (size + pagesize_mask) & ~pagesize_mask;
1240 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1241 if (*address == MAP_FAILED)
1248 * Unmap mappings obtained with drmMap().
1250 * \param address address as given by drmMap().
1251 * \param size size in bytes. Must match the size used by drmMap().
1253 * \return zero on success, or a negative value on failure.
1256 * This function is a wrapper for munmap().
1258 int drmUnmap(drmAddress address, drmSize size)
1260 return drm_munmap(address, size);
1263 drmBufInfoPtr drmGetBufInfo(int fd)
1265 drm_buf_info_t info;
1266 drmBufInfoPtr retval;
1271 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1275 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1278 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1283 retval = drmMalloc(sizeof(*retval));
1284 retval->count = info.count;
1285 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1286 for (i = 0; i < info.count; i++) {
1287 retval->list[i].count = info.list[i].count;
1288 retval->list[i].size = info.list[i].size;
1289 retval->list[i].low_mark = info.list[i].low_mark;
1290 retval->list[i].high_mark = info.list[i].high_mark;
1299 * Map all DMA buffers into client-virtual space.
1301 * \param fd file descriptor.
1303 * \return a pointer to a ::drmBufMap structure.
1305 * \note The client may not use these buffers until obtaining buffer indices
1309 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1310 * information about the buffers in a drm_buf_map structure into the
1311 * client-visible data structures.
1313 drmBufMapPtr drmMapBufs(int fd)
1316 drmBufMapPtr retval;
1320 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1326 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1329 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1334 retval = drmMalloc(sizeof(*retval));
1335 retval->count = bufs.count;
1336 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1337 for (i = 0; i < bufs.count; i++) {
1338 retval->list[i].idx = bufs.list[i].idx;
1339 retval->list[i].total = bufs.list[i].total;
1340 retval->list[i].used = 0;
1341 retval->list[i].address = bufs.list[i].address;
1351 * Unmap buffers allocated with drmMapBufs().
1353 * \return zero on success, or negative value on failure.
1356 * Calls munmap() for every buffer stored in \p bufs and frees the
1357 * memory allocated by drmMapBufs().
1359 int drmUnmapBufs(drmBufMapPtr bufs)
1363 for (i = 0; i < bufs->count; i++) {
1364 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1367 drmFree(bufs->list);
1374 #define DRM_DMA_RETRY 16
1377 * Reserve DMA buffers.
1379 * \param fd file descriptor.
1382 * \return zero on success, or a negative value on failure.
1385 * Assemble the arguments into a drm_dma structure and keeps issuing the
1386 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1388 int drmDMA(int fd, drmDMAReqPtr request)
1393 dma.context = request->context;
1394 dma.send_count = request->send_count;
1395 dma.send_indices = request->send_list;
1396 dma.send_sizes = request->send_sizes;
1397 dma.flags = request->flags;
1398 dma.request_count = request->request_count;
1399 dma.request_size = request->request_size;
1400 dma.request_indices = request->request_list;
1401 dma.request_sizes = request->request_sizes;
1402 dma.granted_count = 0;
1405 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1406 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1409 request->granted_count = dma.granted_count;
1418 * Obtain heavyweight hardware lock.
1420 * \param fd file descriptor.
1421 * \param context context.
1422 * \param flags flags that determine the sate of the hardware when the function
1425 * \return always zero.
1428 * This function translates the arguments into a drm_lock structure and issue
1429 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1431 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1436 lock.context = context;
1438 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1439 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1440 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1441 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1442 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1443 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1445 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1451 * Release the hardware lock.
1453 * \param fd file descriptor.
1454 * \param context context.
1456 * \return zero on success, or a negative value on failure.
1459 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1460 * argument in a drm_lock structure.
1462 int drmUnlock(int fd, drm_context_t context)
1467 lock.context = context;
1468 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1471 drm_context_t *drmGetReservedContextList(int fd, int *count)
1475 drm_context_t * retval;
1479 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1485 if (!(list = drmMalloc(res.count * sizeof(*list))))
1487 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1492 res.contexts = list;
1493 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1496 for (i = 0; i < res.count; i++)
1497 retval[i] = list[i].handle;
1504 void drmFreeReservedContextList(drm_context_t *pt)
1512 * Used by the X server during GLXContext initialization. This causes
1513 * per-context kernel-level resources to be allocated.
1515 * \param fd file descriptor.
1516 * \param handle is set on success. To be used by the client when requesting DMA
1517 * dispatch with drmDMA().
1519 * \return zero on success, or a negative value on failure.
1521 * \note May only be called by root.
1524 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1525 * argument in a drm_ctx structure.
1527 int drmCreateContext(int fd, drm_context_t *handle)
1532 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1534 *handle = ctx.handle;
1538 int drmSwitchToContext(int fd, drm_context_t context)
1543 ctx.handle = context;
1544 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1549 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1554 * Context preserving means that no context switches are done between DMA
1555 * buffers from one context and the next. This is suitable for use in the
1556 * X server (which promises to maintain hardware context), or in the
1557 * client-side library when buffers are swapped on behalf of two threads.
1560 ctx.handle = context;
1561 if (flags & DRM_CONTEXT_PRESERVED)
1562 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1563 if (flags & DRM_CONTEXT_2DONLY)
1564 ctx.flags |= _DRM_CONTEXT_2DONLY;
1565 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1570 int drmGetContextFlags(int fd, drm_context_t context,
1571 drm_context_tFlagsPtr flags)
1576 ctx.handle = context;
1577 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1580 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1581 *flags |= DRM_CONTEXT_PRESERVED;
1582 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1583 *flags |= DRM_CONTEXT_2DONLY;
1590 * Free any kernel-level resources allocated with drmCreateContext() associated
1593 * \param fd file descriptor.
1594 * \param handle handle given by drmCreateContext().
1596 * \return zero on success, or a negative value on failure.
1598 * \note May only be called by root.
1601 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1602 * argument in a drm_ctx structure.
1604 int drmDestroyContext(int fd, drm_context_t handle)
1609 ctx.handle = handle;
1610 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1615 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1620 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1622 *handle = draw.handle;
1626 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1631 draw.handle = handle;
1632 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1637 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1638 drm_drawable_info_type_t type, unsigned int num,
1641 drm_update_draw_t update;
1644 update.handle = handle;
1647 update.data = (unsigned long long)(unsigned long)data;
1649 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1656 * Acquire the AGP device.
1658 * Must be called before any of the other AGP related calls.
1660 * \param fd file descriptor.
1662 * \return zero on success, or a negative value on failure.
1665 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1667 int drmAgpAcquire(int fd)
1669 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1676 * Release the AGP device.
1678 * \param fd file descriptor.
1680 * \return zero on success, or a negative value on failure.
1683 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1685 int drmAgpRelease(int fd)
1687 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1696 * \param fd file descriptor.
1697 * \param mode AGP mode.
1699 * \return zero on success, or a negative value on failure.
1702 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1703 * argument in a drm_agp_mode structure.
1705 int drmAgpEnable(int fd, unsigned long mode)
1711 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1718 * Allocate a chunk of AGP memory.
1720 * \param fd file descriptor.
1721 * \param size requested memory size in bytes. Will be rounded to page boundary.
1722 * \param type type of memory to allocate.
1723 * \param address if not zero, will be set to the physical address of the
1725 * \param handle on success will be set to a handle of the allocated memory.
1727 * \return zero on success, or a negative value on failure.
1730 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1731 * arguments in a drm_agp_buffer structure.
1733 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1734 unsigned long *address, drm_handle_t *handle)
1739 *handle = DRM_AGP_NO_HANDLE;
1742 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1745 *address = b.physical;
1752 * Free a chunk of AGP memory.
1754 * \param fd file descriptor.
1755 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1757 * \return zero on success, or a negative value on failure.
1760 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1761 * argument in a drm_agp_buffer structure.
1763 int drmAgpFree(int fd, drm_handle_t handle)
1769 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1776 * Bind a chunk of AGP memory.
1778 * \param fd file descriptor.
1779 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1780 * \param offset offset in bytes. It will round to page boundary.
1782 * \return zero on success, or a negative value on failure.
1785 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1786 * argument in a drm_agp_binding structure.
1788 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1790 drm_agp_binding_t b;
1795 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1802 * Unbind a chunk of AGP memory.
1804 * \param fd file descriptor.
1805 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1807 * \return zero on success, or a negative value on failure.
1810 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1811 * the argument in a drm_agp_binding structure.
1813 int drmAgpUnbind(int fd, drm_handle_t handle)
1815 drm_agp_binding_t b;
1819 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1826 * Get AGP driver major version number.
1828 * \param fd file descriptor.
1830 * \return major version number on success, or a negative value on failure..
1833 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1834 * necessary information in a drm_agp_info structure.
1836 int drmAgpVersionMajor(int fd)
1842 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1844 return i.agp_version_major;
1849 * Get AGP driver minor version number.
1851 * \param fd file descriptor.
1853 * \return minor version number on success, or a negative value 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 int drmAgpVersionMinor(int fd)
1865 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1867 return i.agp_version_minor;
1874 * \param fd file descriptor.
1876 * \return mode on success, or zero on failure.
1879 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1880 * necessary information in a drm_agp_info structure.
1882 unsigned long drmAgpGetMode(int fd)
1888 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1895 * Get AGP aperture base.
1897 * \param fd file descriptor.
1899 * \return aperture base on success, zero on failure.
1902 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1903 * necessary information in a drm_agp_info structure.
1905 unsigned long drmAgpBase(int fd)
1911 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1913 return i.aperture_base;
1918 * Get AGP aperture size.
1920 * \param fd file descriptor.
1922 * \return aperture size on success, zero on failure.
1925 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1926 * necessary information in a drm_agp_info structure.
1928 unsigned long drmAgpSize(int fd)
1934 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1936 return i.aperture_size;
1941 * Get used AGP memory.
1943 * \param fd file descriptor.
1945 * \return memory used on success, or zero on failure.
1948 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1949 * necessary information in a drm_agp_info structure.
1951 unsigned long drmAgpMemoryUsed(int fd)
1957 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1959 return i.memory_used;
1964 * Get available AGP memory.
1966 * \param fd file descriptor.
1968 * \return memory available on success, or zero on failure.
1971 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1972 * necessary information in a drm_agp_info structure.
1974 unsigned long drmAgpMemoryAvail(int fd)
1980 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1982 return i.memory_allowed;
1987 * Get hardware vendor ID.
1989 * \param fd file descriptor.
1991 * \return vendor ID on success, or zero on failure.
1994 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1995 * necessary information in a drm_agp_info structure.
1997 unsigned int drmAgpVendorId(int fd)
2003 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2010 * Get hardware device ID.
2012 * \param fd file descriptor.
2014 * \return zero on success, or zero on failure.
2017 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2018 * necessary information in a drm_agp_info structure.
2020 unsigned int drmAgpDeviceId(int fd)
2026 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2031 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
2033 drm_scatter_gather_t sg;
2039 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2041 *handle = sg.handle;
2045 int drmScatterGatherFree(int fd, drm_handle_t handle)
2047 drm_scatter_gather_t sg;
2051 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2059 * \param fd file descriptor.
2060 * \param vbl pointer to a drmVBlank structure.
2062 * \return zero on success, or a negative value on failure.
2065 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2067 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2069 struct timespec timeout, cur;
2072 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2074 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2080 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2081 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2082 if (ret && errno == EINTR) {
2083 clock_gettime(CLOCK_MONOTONIC, &cur);
2084 /* Timeout after 1s */
2085 if (cur.tv_sec > timeout.tv_sec + 1 ||
2086 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2093 } while (ret && errno == EINTR);
2099 int drmError(int err, const char *label)
2102 case DRM_ERR_NO_DEVICE:
2103 fprintf(stderr, "%s: no device\n", label);
2105 case DRM_ERR_NO_ACCESS:
2106 fprintf(stderr, "%s: no access\n", label);
2108 case DRM_ERR_NOT_ROOT:
2109 fprintf(stderr, "%s: not root\n", label);
2111 case DRM_ERR_INVALID:
2112 fprintf(stderr, "%s: invalid args\n", label);
2117 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2125 * Install IRQ handler.
2127 * \param fd file descriptor.
2128 * \param irq IRQ number.
2130 * \return zero on success, or a negative value on failure.
2133 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2134 * argument in a drm_control structure.
2136 int drmCtlInstHandler(int fd, int irq)
2141 ctl.func = DRM_INST_HANDLER;
2143 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2150 * Uninstall IRQ handler.
2152 * \param fd file descriptor.
2154 * \return zero on success, or a negative value on failure.
2157 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2158 * argument in a drm_control structure.
2160 int drmCtlUninstHandler(int fd)
2165 ctl.func = DRM_UNINST_HANDLER;
2167 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2172 int drmFinish(int fd, int context, drmLockFlags flags)
2177 lock.context = context;
2178 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2179 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2180 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2181 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2182 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2183 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2184 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2190 * Get IRQ from bus ID.
2192 * \param fd file descriptor.
2193 * \param busnum bus number.
2194 * \param devnum device number.
2195 * \param funcnum function number.
2197 * \return IRQ number on success, or a negative value on failure.
2200 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2201 * arguments in a drm_irq_busid structure.
2203 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2210 p.funcnum = funcnum;
2211 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2216 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2218 drmHashEntry *entry = drmGetEntry(fd);
2220 if (drmHashInsert(entry->tagTable, context, tag)) {
2221 drmHashDelete(entry->tagTable, context);
2222 drmHashInsert(entry->tagTable, context, tag);
2227 int drmDelContextTag(int fd, drm_context_t context)
2229 drmHashEntry *entry = drmGetEntry(fd);
2231 return drmHashDelete(entry->tagTable, context);
2234 void *drmGetContextTag(int fd, drm_context_t context)
2236 drmHashEntry *entry = drmGetEntry(fd);
2239 if (drmHashLookup(entry->tagTable, context, &value))
2245 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2246 drm_handle_t handle)
2248 drm_ctx_priv_map_t map;
2251 map.ctx_id = ctx_id;
2252 map.handle = (void *)(uintptr_t)handle;
2254 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2259 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2260 drm_handle_t *handle)
2262 drm_ctx_priv_map_t map;
2265 map.ctx_id = ctx_id;
2267 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2270 *handle = (drm_handle_t)(uintptr_t)map.handle;
2275 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2276 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2283 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2285 *offset = map.offset;
2289 *handle = (unsigned long)map.handle;
2294 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2295 unsigned long *magic, unsigned long *iocs)
2297 drm_client_t client;
2301 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2303 *auth = client.auth;
2306 *magic = client.magic;
2307 *iocs = client.iocs;
2311 int drmGetStats(int fd, drmStatsT *stats)
2317 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2321 memset(stats, 0, sizeof(*stats));
2322 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2326 stats->data[i].long_format = "%-20.20s"; \
2327 stats->data[i].rate_format = "%8.8s"; \
2328 stats->data[i].isvalue = 1; \
2329 stats->data[i].verbose = 0
2332 stats->data[i].long_format = "%-20.20s"; \
2333 stats->data[i].rate_format = "%5.5s"; \
2334 stats->data[i].isvalue = 0; \
2335 stats->data[i].mult_names = "kgm"; \
2336 stats->data[i].mult = 1000; \
2337 stats->data[i].verbose = 0
2340 stats->data[i].long_format = "%-20.20s"; \
2341 stats->data[i].rate_format = "%5.5s"; \
2342 stats->data[i].isvalue = 0; \
2343 stats->data[i].mult_names = "KGM"; \
2344 stats->data[i].mult = 1024; \
2345 stats->data[i].verbose = 0
2348 stats->count = s.count;
2349 for (i = 0; i < s.count; i++) {
2350 stats->data[i].value = s.data[i].value;
2351 switch (s.data[i].type) {
2352 case _DRM_STAT_LOCK:
2353 stats->data[i].long_name = "Lock";
2354 stats->data[i].rate_name = "Lock";
2357 case _DRM_STAT_OPENS:
2358 stats->data[i].long_name = "Opens";
2359 stats->data[i].rate_name = "O";
2361 stats->data[i].verbose = 1;
2363 case _DRM_STAT_CLOSES:
2364 stats->data[i].long_name = "Closes";
2365 stats->data[i].rate_name = "Lock";
2367 stats->data[i].verbose = 1;
2369 case _DRM_STAT_IOCTLS:
2370 stats->data[i].long_name = "Ioctls";
2371 stats->data[i].rate_name = "Ioc/s";
2374 case _DRM_STAT_LOCKS:
2375 stats->data[i].long_name = "Locks";
2376 stats->data[i].rate_name = "Lck/s";
2379 case _DRM_STAT_UNLOCKS:
2380 stats->data[i].long_name = "Unlocks";
2381 stats->data[i].rate_name = "Unl/s";
2385 stats->data[i].long_name = "IRQs";
2386 stats->data[i].rate_name = "IRQ/s";
2389 case _DRM_STAT_PRIMARY:
2390 stats->data[i].long_name = "Primary Bytes";
2391 stats->data[i].rate_name = "PB/s";
2394 case _DRM_STAT_SECONDARY:
2395 stats->data[i].long_name = "Secondary Bytes";
2396 stats->data[i].rate_name = "SB/s";
2400 stats->data[i].long_name = "DMA";
2401 stats->data[i].rate_name = "DMA/s";
2404 case _DRM_STAT_SPECIAL:
2405 stats->data[i].long_name = "Special DMA";
2406 stats->data[i].rate_name = "dma/s";
2409 case _DRM_STAT_MISSED:
2410 stats->data[i].long_name = "Miss";
2411 stats->data[i].rate_name = "Ms/s";
2414 case _DRM_STAT_VALUE:
2415 stats->data[i].long_name = "Value";
2416 stats->data[i].rate_name = "Value";
2419 case _DRM_STAT_BYTE:
2420 stats->data[i].long_name = "Bytes";
2421 stats->data[i].rate_name = "B/s";
2424 case _DRM_STAT_COUNT:
2426 stats->data[i].long_name = "Count";
2427 stats->data[i].rate_name = "Cnt/s";
2436 * Issue a set-version ioctl.
2438 * \param fd file descriptor.
2439 * \param drmCommandIndex command index
2440 * \param data source pointer of the data to be read and written.
2441 * \param size size of the data to be read and written.
2443 * \return zero on success, or a negative value on failure.
2446 * It issues a read-write ioctl given by
2447 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2449 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2452 drm_set_version_t sv;
2455 sv.drm_di_major = version->drm_di_major;
2456 sv.drm_di_minor = version->drm_di_minor;
2457 sv.drm_dd_major = version->drm_dd_major;
2458 sv.drm_dd_minor = version->drm_dd_minor;
2460 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2464 version->drm_di_major = sv.drm_di_major;
2465 version->drm_di_minor = sv.drm_di_minor;
2466 version->drm_dd_major = sv.drm_dd_major;
2467 version->drm_dd_minor = sv.drm_dd_minor;
2473 * Send a device-specific command.
2475 * \param fd file descriptor.
2476 * \param drmCommandIndex command index
2478 * \return zero on success, or a negative value on failure.
2481 * It issues a ioctl given by
2482 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2484 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2486 unsigned long request;
2488 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2490 if (drmIoctl(fd, request, NULL)) {
2498 * Send a device-specific read command.
2500 * \param fd file descriptor.
2501 * \param drmCommandIndex command index
2502 * \param data destination pointer of the data to be read.
2503 * \param size size of the data to be read.
2505 * \return zero on success, or a negative value on failure.
2508 * It issues a read ioctl given by
2509 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2511 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2514 unsigned long request;
2516 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2517 DRM_COMMAND_BASE + drmCommandIndex, size);
2519 if (drmIoctl(fd, request, data)) {
2527 * Send a device-specific write command.
2529 * \param fd file descriptor.
2530 * \param drmCommandIndex command index
2531 * \param data source pointer of the data to be written.
2532 * \param size size of the data to be written.
2534 * \return zero on success, or a negative value on failure.
2537 * It issues a write ioctl given by
2538 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2540 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2543 unsigned long request;
2545 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2546 DRM_COMMAND_BASE + drmCommandIndex, size);
2548 if (drmIoctl(fd, request, data)) {
2556 * Send a device-specific read-write command.
2558 * \param fd file descriptor.
2559 * \param drmCommandIndex command index
2560 * \param data source pointer of the data to be read and written.
2561 * \param size size of the data to be read and written.
2563 * \return zero on success, or a negative value on failure.
2566 * It issues a read-write ioctl given by
2567 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2569 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2572 unsigned long request;
2574 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2575 DRM_COMMAND_BASE + drmCommandIndex, size);
2577 if (drmIoctl(fd, request, data))
2582 #define DRM_MAX_FDS 16
2588 } connection[DRM_MAX_FDS];
2590 static int nr_fds = 0;
2592 int drmOpenOnce(void *unused,
2596 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2599 int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2604 for (i = 0; i < nr_fds; i++)
2605 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2606 (connection[i].type == type)) {
2607 connection[i].refcount++;
2609 return connection[i].fd;
2612 fd = drmOpenWithType(NULL, BusID, type);
2613 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2616 connection[nr_fds].BusID = strdup(BusID);
2617 connection[nr_fds].fd = fd;
2618 connection[nr_fds].refcount = 1;
2619 connection[nr_fds].type = type;
2623 fprintf(stderr, "saved connection %d for %s %d\n",
2624 nr_fds, connection[nr_fds].BusID,
2625 strcmp(BusID, connection[nr_fds].BusID));
2632 void drmCloseOnce(int fd)
2636 for (i = 0; i < nr_fds; i++) {
2637 if (fd == connection[i].fd) {
2638 if (--connection[i].refcount == 0) {
2639 drmClose(connection[i].fd);
2640 free(connection[i].BusID);
2643 connection[i] = connection[nr_fds];
2651 int drmSetMaster(int fd)
2653 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2656 int drmDropMaster(int fd)
2658 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2661 char *drmGetDeviceNameFromFd(int fd)
2668 /* The whole drmOpen thing is a fiasco and we need to find a way
2669 * back to just using open(2). For now, however, lets just make
2670 * things worse with even more ad hoc directory walking code to
2671 * discover the device file name. */
2676 for (i = 0; i < DRM_MAX_MINOR; i++) {
2677 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2678 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2681 if (i == DRM_MAX_MINOR)
2684 return strdup(name);
2687 int drmGetNodeTypeFromFd(int fd)
2692 if (fstat(fd, &sbuf))
2695 maj = major(sbuf.st_rdev);
2696 min = minor(sbuf.st_rdev);
2698 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
2703 type = drmGetMinorType(min);
2709 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2711 struct drm_prime_handle args;
2714 args.handle = handle;
2716 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2720 *prime_fd = args.fd;
2724 int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2726 struct drm_prime_handle args;
2731 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2735 *handle = args.handle;