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>
55 #ifdef HAVE_SYS_MKDEV_H
56 # include <sys/mkdev.h> /* defines major(), minor(), and makedev() on Solaris */
59 /* Not all systems have MAP_FAILED defined */
61 #define MAP_FAILED ((void *)-1)
65 #include "libdrm_macros.h"
67 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
80 #define DRM_MAJOR 226 /* Linux */
84 * This definition needs to be changed on some systems if dev_t is a structure.
85 * If there is a header file we can get it from, there would be best.
88 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
91 #define DRM_MSG_VERBOSITY 3
93 #define memclear(s) memset(&s, 0, sizeof(s))
95 static drmServerInfoPtr drm_server_info;
97 void drmSetServerInfo(drmServerInfoPtr info)
99 drm_server_info = info;
103 * Output a message to stderr.
105 * \param format printf() like format string.
108 * This function is a wrapper around vfprintf().
111 static int DRM_PRINTFLIKE(1, 0)
112 drmDebugPrint(const char *format, va_list ap)
114 return vfprintf(stderr, format, ap);
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 drmDebugPrint(format, ap);
134 static void *drmHashTable = NULL; /* Context switch callbacks */
136 void *drmGetHashTable(void)
141 void *drmMalloc(int size)
143 return calloc(1, size);
146 void drmFree(void *pt)
152 * Call ioctl, restarting if it is interupted
155 drmIoctl(int fd, unsigned long request, void *arg)
160 ret = ioctl(fd, request, arg);
161 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
165 static unsigned long drmGetKeyFromFd(int fd)
174 drmHashEntry *drmGetEntry(int fd)
176 unsigned long key = drmGetKeyFromFd(fd);
181 drmHashTable = drmHashCreate();
183 if (drmHashLookup(drmHashTable, key, &value)) {
184 entry = drmMalloc(sizeof(*entry));
187 entry->tagTable = drmHashCreate();
188 drmHashInsert(drmHashTable, key, entry);
196 * Compare two busid strings
201 * \return 1 if matched.
204 * This function compares two bus ID strings. It understands the older
205 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
206 * domain, b is bus, d is device, f is function.
208 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
210 /* First, check if the IDs are exactly the same */
211 if (strcasecmp(id1, id2) == 0)
214 /* Try to match old/new-style PCI bus IDs. */
215 if (strncasecmp(id1, "pci", 3) == 0) {
216 unsigned int o1, b1, d1, f1;
217 unsigned int o2, b2, d2, f2;
220 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
223 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
228 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
231 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
236 /* If domains aren't properly supported by the kernel interface,
237 * just ignore them, which sucks less than picking a totally random
238 * card with "open by name"
243 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
252 * Handles error checking for chown call.
254 * \param path to file.
255 * \param id of the new owner.
256 * \param id of the new group.
258 * \return zero if success or -1 if failure.
261 * Checks for failure. If failure was caused by signal call chown again.
262 * If any other failure happened then it will output error mesage using
266 static int chown_check_return(const char *path, uid_t owner, gid_t group)
271 rv = chown(path, owner, group);
272 } while (rv != 0 && errno == EINTR);
277 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
278 path, errno, strerror(errno));
284 * Open the DRM device, creating it if necessary.
286 * \param dev major and minor numbers of the device.
287 * \param minor minor number of the device.
289 * \return a file descriptor on success, or a negative value on error.
292 * Assembles the device name from \p minor and opens it, creating the device
293 * special file node with the major and minor numbers specified by \p dev and
294 * parent directory if necessary and was called by root.
296 static int drmOpenDevice(dev_t dev, int minor, int type)
299 const char *dev_name;
302 mode_t devmode = DRM_DEV_MODE, serv_mode;
305 int isroot = !geteuid();
306 uid_t user = DRM_DEV_UID;
307 gid_t group = DRM_DEV_GID;
311 case DRM_NODE_PRIMARY:
312 dev_name = DRM_DEV_NAME;
314 case DRM_NODE_CONTROL:
315 dev_name = DRM_CONTROL_DEV_NAME;
317 case DRM_NODE_RENDER:
318 dev_name = DRM_RENDER_DEV_NAME;
324 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
325 drmMsg("drmOpenDevice: node name is %s\n", buf);
327 if (drm_server_info) {
328 drm_server_info->get_perms(&serv_group, &serv_mode);
329 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
330 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
334 if (stat(DRM_DIR_NAME, &st)) {
336 return DRM_ERR_NOT_ROOT;
337 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
338 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
339 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
342 /* Check if the device node exists and create it if necessary. */
343 if (stat(buf, &st)) {
345 return DRM_ERR_NOT_ROOT;
347 mknod(buf, S_IFCHR | devmode, dev);
350 if (drm_server_info) {
351 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
352 chown_check_return(buf, user, group);
356 /* if we modprobed then wait for udev */
360 if (stat(DRM_DIR_NAME, &st)) {
364 if (udev_count == 50)
369 if (stat(buf, &st)) {
373 if (udev_count == 50)
380 fd = open(buf, O_RDWR, 0);
381 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
382 fd, fd < 0 ? strerror(errno) : "OK");
387 /* Check if the device node is not what we expect it to be, and recreate it
388 * and try again if so.
390 if (st.st_rdev != dev) {
392 return DRM_ERR_NOT_ROOT;
394 mknod(buf, S_IFCHR | devmode, dev);
395 if (drm_server_info) {
396 chown_check_return(buf, user, group);
400 fd = open(buf, O_RDWR, 0);
401 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
402 fd, fd < 0 ? strerror(errno) : "OK");
406 drmMsg("drmOpenDevice: Open failed\n");
414 * Open the DRM device
416 * \param minor device minor number.
417 * \param create allow to create the device if set.
419 * \return a file descriptor on success, or a negative value on error.
422 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
423 * name from \p minor and opens it.
425 static int drmOpenMinor(int minor, int create, int type)
429 const char *dev_name;
432 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
435 case DRM_NODE_PRIMARY:
436 dev_name = DRM_DEV_NAME;
438 case DRM_NODE_CONTROL:
439 dev_name = DRM_CONTROL_DEV_NAME;
441 case DRM_NODE_RENDER:
442 dev_name = DRM_RENDER_DEV_NAME;
448 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
449 if ((fd = open(buf, O_RDWR, 0)) >= 0)
456 * Determine whether the DRM kernel driver has been loaded.
458 * \return 1 if the DRM driver is loaded, 0 otherwise.
461 * Determine the presence of the kernel driver by attempting to open the 0
462 * minor and get version information. For backward compatibility with older
463 * Linux implementations, /proc/dri is also checked.
465 int drmAvailable(void)
467 drmVersionPtr version;
471 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
473 /* Try proc for backward Linux compatibility */
474 if (!access("/proc/dri/0", R_OK))
480 if ((version = drmGetVersion(fd))) {
482 drmFreeVersion(version);
489 static int drmGetMinorBase(int type)
492 case DRM_NODE_PRIMARY:
494 case DRM_NODE_CONTROL:
496 case DRM_NODE_RENDER:
503 static int drmGetMinorType(int minor)
505 int type = minor >> 6;
511 case DRM_NODE_PRIMARY:
512 case DRM_NODE_CONTROL:
513 case DRM_NODE_RENDER:
520 static const char *drmGetMinorName(int type)
523 case DRM_NODE_PRIMARY:
525 case DRM_NODE_CONTROL:
527 case DRM_NODE_RENDER:
535 * Open the device by bus ID.
537 * \param busid bus ID.
538 * \param type device node type.
540 * \return a file descriptor on success, or a negative value on error.
543 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
544 * comparing the device bus ID with the one supplied.
546 * \sa drmOpenMinor() and drmGetBusid().
548 static int drmOpenByBusid(const char *busid, int type)
550 int i, pci_domain_ok = 1;
554 int base = drmGetMinorBase(type);
559 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
560 for (i = base; i < base + DRM_MAX_MINOR; i++) {
561 fd = drmOpenMinor(i, 1, type);
562 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
564 /* We need to try for 1.4 first for proper PCI domain support
565 * and if that fails, we know the kernel is busted
569 sv.drm_dd_major = -1; /* Don't care */
570 sv.drm_dd_minor = -1; /* Don't care */
571 if (drmSetInterfaceVersion(fd, &sv)) {
577 sv.drm_dd_major = -1; /* Don't care */
578 sv.drm_dd_minor = -1; /* Don't care */
579 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
580 drmSetInterfaceVersion(fd, &sv);
582 buf = drmGetBusid(fd);
583 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
584 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
598 * Open the device by name.
600 * \param name driver name.
601 * \param type the device node type.
603 * \return a file descriptor on success, or a negative value on error.
606 * This function opens the first minor number that matches the driver name and
607 * isn't already in use. If it's in use it then it will already have a bus ID
610 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
612 static int drmOpenByName(const char *name, int type)
616 drmVersionPtr version;
618 int base = drmGetMinorBase(type);
624 * Open the first minor number that matches the driver name and isn't
625 * already in use. If it's in use it will have a busid assigned already.
627 for (i = base; i < base + DRM_MAX_MINOR; i++) {
628 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
629 if ((version = drmGetVersion(fd))) {
630 if (!strcmp(version->name, name)) {
631 drmFreeVersion(version);
632 id = drmGetBusid(fd);
633 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
642 drmFreeVersion(version);
650 /* Backward-compatibility /proc support */
651 for (i = 0; i < 8; i++) {
652 char proc_name[64], buf[512];
653 char *driver, *pt, *devstring;
656 sprintf(proc_name, "/proc/dri/%d/name", i);
657 if ((fd = open(proc_name, 0, 0)) >= 0) {
658 retcode = read(fd, buf, sizeof(buf)-1);
661 buf[retcode-1] = '\0';
662 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
664 if (*pt) { /* Device is next */
666 if (!strcmp(driver, name)) { /* Match */
667 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
669 if (*pt) { /* Found busid */
670 return drmOpenByBusid(++pt, type);
671 } else { /* No busid */
672 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
686 * Open the DRM device.
688 * Looks up the specified name and bus ID, and opens the device found. The
689 * entry in /dev/dri is created if necessary and if called by root.
691 * \param name driver name. Not referenced if bus ID is supplied.
692 * \param busid bus ID. Zero if not known.
694 * \return a file descriptor on success, or a negative value on error.
697 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
700 int drmOpen(const char *name, const char *busid)
702 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
706 * Open the DRM device with specified type.
708 * Looks up the specified name and bus ID, and opens the device found. The
709 * entry in /dev/dri is created if necessary and if called by root.
711 * \param name driver name. Not referenced if bus ID is supplied.
712 * \param busid bus ID. Zero if not known.
713 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
715 * \return a file descriptor on success, or a negative value on error.
718 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
721 int drmOpenWithType(const char *name, const char *busid, int type)
723 if (!drmAvailable() && name != NULL && drm_server_info) {
724 /* try to load the kernel module */
725 if (!drm_server_info->load_module(name)) {
726 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
732 int fd = drmOpenByBusid(busid, type);
738 return drmOpenByName(name, type);
743 int drmOpenControl(int minor)
745 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
748 int drmOpenRender(int minor)
750 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
754 * Free the version information returned by drmGetVersion().
756 * \param v pointer to the version information.
759 * It frees the memory pointed by \p %v as well as all the non-null strings
762 void drmFreeVersion(drmVersionPtr v)
774 * Free the non-public version information returned by the kernel.
776 * \param v pointer to the version information.
779 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
780 * the non-null strings pointers in it.
782 static void drmFreeKernelVersion(drm_version_t *v)
794 * Copy version information.
796 * \param d destination pointer.
797 * \param s source pointer.
800 * Used by drmGetVersion() to translate the information returned by the ioctl
801 * interface in a private structure into the public structure counterpart.
803 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
805 d->version_major = s->version_major;
806 d->version_minor = s->version_minor;
807 d->version_patchlevel = s->version_patchlevel;
808 d->name_len = s->name_len;
809 d->name = strdup(s->name);
810 d->date_len = s->date_len;
811 d->date = strdup(s->date);
812 d->desc_len = s->desc_len;
813 d->desc = strdup(s->desc);
818 * Query the driver version information.
820 * \param fd file descriptor.
822 * \return pointer to a drmVersion structure which should be freed with
825 * \note Similar information is available via /proc/dri.
828 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
829 * first with zeros to get the string lengths, and then the actually strings.
830 * It also null-terminates them since they might not be already.
832 drmVersionPtr drmGetVersion(int fd)
834 drmVersionPtr retval;
835 drm_version_t *version = drmMalloc(sizeof(*version));
839 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
840 drmFreeKernelVersion(version);
844 if (version->name_len)
845 version->name = drmMalloc(version->name_len + 1);
846 if (version->date_len)
847 version->date = drmMalloc(version->date_len + 1);
848 if (version->desc_len)
849 version->desc = drmMalloc(version->desc_len + 1);
851 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
852 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
853 drmFreeKernelVersion(version);
857 /* The results might not be null-terminated strings, so terminate them. */
858 if (version->name_len) version->name[version->name_len] = '\0';
859 if (version->date_len) version->date[version->date_len] = '\0';
860 if (version->desc_len) version->desc[version->desc_len] = '\0';
862 retval = drmMalloc(sizeof(*retval));
863 drmCopyVersion(retval, version);
864 drmFreeKernelVersion(version);
870 * Get version information for the DRM user space library.
872 * This version number is driver independent.
874 * \param fd file descriptor.
876 * \return version information.
879 * This function allocates and fills a drm_version structure with a hard coded
882 drmVersionPtr drmGetLibVersion(int fd)
884 drm_version_t *version = drmMalloc(sizeof(*version));
887 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
888 * revision 1.0.x = original DRM interface with no drmGetLibVersion
889 * entry point and many drm<Device> extensions
890 * revision 1.1.x = added drmCommand entry points for device extensions
891 * added drmGetLibVersion to identify libdrm.a version
892 * revision 1.2.x = added drmSetInterfaceVersion
893 * modified drmOpen to handle both busid and name
894 * revision 1.3.x = added server + memory manager
896 version->version_major = 1;
897 version->version_minor = 3;
898 version->version_patchlevel = 0;
900 return (drmVersionPtr)version;
903 int drmGetCap(int fd, uint64_t capability, uint64_t *value)
905 struct drm_get_cap cap;
909 cap.capability = capability;
911 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
919 int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
921 struct drm_set_client_cap cap;
924 cap.capability = capability;
927 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
931 * Free the bus ID information.
933 * \param busid bus ID information string as given by drmGetBusid().
936 * This function is just frees the memory pointed by \p busid.
938 void drmFreeBusid(const char *busid)
940 drmFree((void *)busid);
945 * Get the bus ID of the device.
947 * \param fd file descriptor.
949 * \return bus ID string.
952 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
953 * get the string length and data, passing the arguments in a drm_unique
956 char *drmGetBusid(int fd)
962 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
964 u.unique = drmMalloc(u.unique_len + 1);
965 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
967 u.unique[u.unique_len] = '\0';
974 * Set the bus ID of the device.
976 * \param fd file descriptor.
977 * \param busid bus ID string.
979 * \return zero on success, negative on failure.
982 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
983 * the arguments in a drm_unique structure.
985 int drmSetBusid(int fd, const char *busid)
990 u.unique = (char *)busid;
991 u.unique_len = strlen(busid);
993 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
999 int drmGetMagic(int fd, drm_magic_t * magic)
1006 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1008 *magic = auth.magic;
1012 int drmAuthMagic(int fd, drm_magic_t magic)
1018 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1024 * Specifies a range of memory that is available for mapping by a
1027 * \param fd file descriptor.
1028 * \param offset usually the physical address. The actual meaning depends of
1029 * the \p type parameter. See below.
1030 * \param size of the memory in bytes.
1031 * \param type type of the memory to be mapped.
1032 * \param flags combination of several flags to modify the function actions.
1033 * \param handle will be set to a value that may be used as the offset
1034 * parameter for mmap().
1036 * \return zero on success or a negative value on error.
1038 * \par Mapping the frame buffer
1039 * For the frame buffer
1040 * - \p offset will be the physical address of the start of the frame buffer,
1041 * - \p size will be the size of the frame buffer in bytes, and
1042 * - \p type will be DRM_FRAME_BUFFER.
1045 * The area mapped will be uncached. If MTRR support is available in the
1046 * kernel, the frame buffer area will be set to write combining.
1048 * \par Mapping the MMIO register area
1049 * For the MMIO register area,
1050 * - \p offset will be the physical address of the start of the register area,
1051 * - \p size will be the size of the register area bytes, and
1052 * - \p type will be DRM_REGISTERS.
1054 * The area mapped will be uncached.
1056 * \par Mapping the SAREA
1058 * - \p offset will be ignored and should be set to zero,
1059 * - \p size will be the desired size of the SAREA in bytes,
1060 * - \p type will be DRM_SHM.
1063 * A shared memory area of the requested size will be created and locked in
1064 * kernel memory. This area may be mapped into client-space by using the handle
1067 * \note May only be called by root.
1070 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1071 * the arguments in a drm_map structure.
1073 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1074 drmMapFlags flags, drm_handle_t *handle)
1079 map.offset = offset;
1083 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1086 *handle = (drm_handle_t)(uintptr_t)map.handle;
1090 int drmRmMap(int fd, drm_handle_t handle)
1095 map.handle = (void *)(uintptr_t)handle;
1097 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1103 * Make buffers available for DMA transfers.
1105 * \param fd file descriptor.
1106 * \param count number of buffers.
1107 * \param size size of each buffer.
1108 * \param flags buffer allocation flags.
1109 * \param agp_offset offset in the AGP aperture
1111 * \return number of buffers allocated, negative on error.
1114 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1118 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1121 drm_buf_desc_t request;
1124 request.count = count;
1125 request.size = size;
1126 request.flags = flags;
1127 request.agp_start = agp_offset;
1129 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1131 return request.count;
1134 int drmMarkBufs(int fd, double low, double high)
1136 drm_buf_info_t info;
1141 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1147 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1150 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1151 int retval = -errno;
1156 for (i = 0; i < info.count; i++) {
1157 info.list[i].low_mark = low * info.list[i].count;
1158 info.list[i].high_mark = high * info.list[i].count;
1159 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1160 int retval = -errno;
1173 * \param fd file descriptor.
1174 * \param count number of buffers to free.
1175 * \param list list of buffers to be freed.
1177 * \return zero on success, or a negative value on failure.
1179 * \note This function is primarily used for debugging.
1182 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1183 * the arguments in a drm_buf_free structure.
1185 int drmFreeBufs(int fd, int count, int *list)
1187 drm_buf_free_t request;
1190 request.count = count;
1191 request.list = list;
1192 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1201 * \param fd file descriptor.
1204 * This function closes the file descriptor.
1206 int drmClose(int fd)
1208 unsigned long key = drmGetKeyFromFd(fd);
1209 drmHashEntry *entry = drmGetEntry(fd);
1211 drmHashDestroy(entry->tagTable);
1214 entry->tagTable = NULL;
1216 drmHashDelete(drmHashTable, key);
1224 * Map a region of memory.
1226 * \param fd file descriptor.
1227 * \param handle handle returned by drmAddMap().
1228 * \param size size in bytes. Must match the size used by drmAddMap().
1229 * \param address will contain the user-space virtual address where the mapping
1232 * \return zero on success, or a negative value on failure.
1235 * This function is a wrapper for mmap().
1237 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1239 static unsigned long pagesize_mask = 0;
1245 pagesize_mask = getpagesize() - 1;
1247 size = (size + pagesize_mask) & ~pagesize_mask;
1249 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1250 if (*address == MAP_FAILED)
1257 * Unmap mappings obtained with drmMap().
1259 * \param address address as given by drmMap().
1260 * \param size size in bytes. Must match the size used by drmMap().
1262 * \return zero on success, or a negative value on failure.
1265 * This function is a wrapper for munmap().
1267 int drmUnmap(drmAddress address, drmSize size)
1269 return drm_munmap(address, size);
1272 drmBufInfoPtr drmGetBufInfo(int fd)
1274 drm_buf_info_t info;
1275 drmBufInfoPtr retval;
1280 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1284 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1287 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1292 retval = drmMalloc(sizeof(*retval));
1293 retval->count = info.count;
1294 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1295 for (i = 0; i < info.count; i++) {
1296 retval->list[i].count = info.list[i].count;
1297 retval->list[i].size = info.list[i].size;
1298 retval->list[i].low_mark = info.list[i].low_mark;
1299 retval->list[i].high_mark = info.list[i].high_mark;
1308 * Map all DMA buffers into client-virtual space.
1310 * \param fd file descriptor.
1312 * \return a pointer to a ::drmBufMap structure.
1314 * \note The client may not use these buffers until obtaining buffer indices
1318 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1319 * information about the buffers in a drm_buf_map structure into the
1320 * client-visible data structures.
1322 drmBufMapPtr drmMapBufs(int fd)
1325 drmBufMapPtr retval;
1329 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1335 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1338 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1343 retval = drmMalloc(sizeof(*retval));
1344 retval->count = bufs.count;
1345 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1346 for (i = 0; i < bufs.count; i++) {
1347 retval->list[i].idx = bufs.list[i].idx;
1348 retval->list[i].total = bufs.list[i].total;
1349 retval->list[i].used = 0;
1350 retval->list[i].address = bufs.list[i].address;
1360 * Unmap buffers allocated with drmMapBufs().
1362 * \return zero on success, or negative value on failure.
1365 * Calls munmap() for every buffer stored in \p bufs and frees the
1366 * memory allocated by drmMapBufs().
1368 int drmUnmapBufs(drmBufMapPtr bufs)
1372 for (i = 0; i < bufs->count; i++) {
1373 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1376 drmFree(bufs->list);
1383 #define DRM_DMA_RETRY 16
1386 * Reserve DMA buffers.
1388 * \param fd file descriptor.
1391 * \return zero on success, or a negative value on failure.
1394 * Assemble the arguments into a drm_dma structure and keeps issuing the
1395 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1397 int drmDMA(int fd, drmDMAReqPtr request)
1402 dma.context = request->context;
1403 dma.send_count = request->send_count;
1404 dma.send_indices = request->send_list;
1405 dma.send_sizes = request->send_sizes;
1406 dma.flags = request->flags;
1407 dma.request_count = request->request_count;
1408 dma.request_size = request->request_size;
1409 dma.request_indices = request->request_list;
1410 dma.request_sizes = request->request_sizes;
1411 dma.granted_count = 0;
1414 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1415 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1418 request->granted_count = dma.granted_count;
1427 * Obtain heavyweight hardware lock.
1429 * \param fd file descriptor.
1430 * \param context context.
1431 * \param flags flags that determine the sate of the hardware when the function
1434 * \return always zero.
1437 * This function translates the arguments into a drm_lock structure and issue
1438 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1440 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1445 lock.context = context;
1447 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1448 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1449 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1450 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1451 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1452 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1454 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1460 * Release the hardware lock.
1462 * \param fd file descriptor.
1463 * \param context context.
1465 * \return zero on success, or a negative value on failure.
1468 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1469 * argument in a drm_lock structure.
1471 int drmUnlock(int fd, drm_context_t context)
1476 lock.context = context;
1477 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1480 drm_context_t *drmGetReservedContextList(int fd, int *count)
1484 drm_context_t * retval;
1488 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1494 if (!(list = drmMalloc(res.count * sizeof(*list))))
1496 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1501 res.contexts = list;
1502 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1505 for (i = 0; i < res.count; i++)
1506 retval[i] = list[i].handle;
1513 void drmFreeReservedContextList(drm_context_t *pt)
1521 * Used by the X server during GLXContext initialization. This causes
1522 * per-context kernel-level resources to be allocated.
1524 * \param fd file descriptor.
1525 * \param handle is set on success. To be used by the client when requesting DMA
1526 * dispatch with drmDMA().
1528 * \return zero on success, or a negative value on failure.
1530 * \note May only be called by root.
1533 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1534 * argument in a drm_ctx structure.
1536 int drmCreateContext(int fd, drm_context_t *handle)
1541 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1543 *handle = ctx.handle;
1547 int drmSwitchToContext(int fd, drm_context_t context)
1552 ctx.handle = context;
1553 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1558 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1563 * Context preserving means that no context switches are done between DMA
1564 * buffers from one context and the next. This is suitable for use in the
1565 * X server (which promises to maintain hardware context), or in the
1566 * client-side library when buffers are swapped on behalf of two threads.
1569 ctx.handle = context;
1570 if (flags & DRM_CONTEXT_PRESERVED)
1571 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1572 if (flags & DRM_CONTEXT_2DONLY)
1573 ctx.flags |= _DRM_CONTEXT_2DONLY;
1574 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1579 int drmGetContextFlags(int fd, drm_context_t context,
1580 drm_context_tFlagsPtr flags)
1585 ctx.handle = context;
1586 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1589 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1590 *flags |= DRM_CONTEXT_PRESERVED;
1591 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1592 *flags |= DRM_CONTEXT_2DONLY;
1599 * Free any kernel-level resources allocated with drmCreateContext() associated
1602 * \param fd file descriptor.
1603 * \param handle handle given by drmCreateContext().
1605 * \return zero on success, or a negative value on failure.
1607 * \note May only be called by root.
1610 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1611 * argument in a drm_ctx structure.
1613 int drmDestroyContext(int fd, drm_context_t handle)
1618 ctx.handle = handle;
1619 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1624 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1629 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1631 *handle = draw.handle;
1635 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1640 draw.handle = handle;
1641 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1646 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1647 drm_drawable_info_type_t type, unsigned int num,
1650 drm_update_draw_t update;
1653 update.handle = handle;
1656 update.data = (unsigned long long)(unsigned long)data;
1658 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1665 * Acquire the AGP device.
1667 * Must be called before any of the other AGP related calls.
1669 * \param fd file descriptor.
1671 * \return zero on success, or a negative value on failure.
1674 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1676 int drmAgpAcquire(int fd)
1678 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1685 * Release the AGP device.
1687 * \param fd file descriptor.
1689 * \return zero on success, or a negative value on failure.
1692 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1694 int drmAgpRelease(int fd)
1696 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1705 * \param fd file descriptor.
1706 * \param mode AGP mode.
1708 * \return zero on success, or a negative value on failure.
1711 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1712 * argument in a drm_agp_mode structure.
1714 int drmAgpEnable(int fd, unsigned long mode)
1720 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1727 * Allocate a chunk of AGP memory.
1729 * \param fd file descriptor.
1730 * \param size requested memory size in bytes. Will be rounded to page boundary.
1731 * \param type type of memory to allocate.
1732 * \param address if not zero, will be set to the physical address of the
1734 * \param handle on success will be set to a handle of the allocated memory.
1736 * \return zero on success, or a negative value on failure.
1739 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1740 * arguments in a drm_agp_buffer structure.
1742 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1743 unsigned long *address, drm_handle_t *handle)
1748 *handle = DRM_AGP_NO_HANDLE;
1751 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1754 *address = b.physical;
1761 * Free a chunk of AGP memory.
1763 * \param fd file descriptor.
1764 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1766 * \return zero on success, or a negative value on failure.
1769 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1770 * argument in a drm_agp_buffer structure.
1772 int drmAgpFree(int fd, drm_handle_t handle)
1778 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1785 * Bind a chunk of AGP memory.
1787 * \param fd file descriptor.
1788 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1789 * \param offset offset in bytes. It will round to page boundary.
1791 * \return zero on success, or a negative value on failure.
1794 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1795 * argument in a drm_agp_binding structure.
1797 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1799 drm_agp_binding_t b;
1804 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1811 * Unbind a chunk of AGP memory.
1813 * \param fd file descriptor.
1814 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1816 * \return zero on success, or a negative value on failure.
1819 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1820 * the argument in a drm_agp_binding structure.
1822 int drmAgpUnbind(int fd, drm_handle_t handle)
1824 drm_agp_binding_t b;
1828 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1835 * Get AGP driver major version number.
1837 * \param fd file descriptor.
1839 * \return major version number on success, or a negative value on failure..
1842 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1843 * necessary information in a drm_agp_info structure.
1845 int drmAgpVersionMajor(int fd)
1851 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1853 return i.agp_version_major;
1858 * Get AGP driver minor version number.
1860 * \param fd file descriptor.
1862 * \return minor version number on success, or a negative value on failure.
1865 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1866 * necessary information in a drm_agp_info structure.
1868 int drmAgpVersionMinor(int fd)
1874 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1876 return i.agp_version_minor;
1883 * \param fd file descriptor.
1885 * \return mode on success, or zero on failure.
1888 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1889 * necessary information in a drm_agp_info structure.
1891 unsigned long drmAgpGetMode(int fd)
1897 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1904 * Get AGP aperture base.
1906 * \param fd file descriptor.
1908 * \return aperture base on success, zero on failure.
1911 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1912 * necessary information in a drm_agp_info structure.
1914 unsigned long drmAgpBase(int fd)
1920 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1922 return i.aperture_base;
1927 * Get AGP aperture size.
1929 * \param fd file descriptor.
1931 * \return aperture size on success, zero on failure.
1934 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1935 * necessary information in a drm_agp_info structure.
1937 unsigned long drmAgpSize(int fd)
1943 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1945 return i.aperture_size;
1950 * Get used AGP memory.
1952 * \param fd file descriptor.
1954 * \return memory used on success, or zero on failure.
1957 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1958 * necessary information in a drm_agp_info structure.
1960 unsigned long drmAgpMemoryUsed(int fd)
1966 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1968 return i.memory_used;
1973 * Get available AGP memory.
1975 * \param fd file descriptor.
1977 * \return memory available on success, or zero on failure.
1980 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1981 * necessary information in a drm_agp_info structure.
1983 unsigned long drmAgpMemoryAvail(int fd)
1989 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1991 return i.memory_allowed;
1996 * Get hardware vendor ID.
1998 * \param fd file descriptor.
2000 * \return vendor ID on success, or zero on failure.
2003 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2004 * necessary information in a drm_agp_info structure.
2006 unsigned int drmAgpVendorId(int fd)
2012 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2019 * Get hardware device ID.
2021 * \param fd file descriptor.
2023 * \return zero on success, or zero on failure.
2026 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2027 * necessary information in a drm_agp_info structure.
2029 unsigned int drmAgpDeviceId(int fd)
2035 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2040 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
2042 drm_scatter_gather_t sg;
2048 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2050 *handle = sg.handle;
2054 int drmScatterGatherFree(int fd, drm_handle_t handle)
2056 drm_scatter_gather_t sg;
2060 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2068 * \param fd file descriptor.
2069 * \param vbl pointer to a drmVBlank structure.
2071 * \return zero on success, or a negative value on failure.
2074 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2076 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2078 struct timespec timeout, cur;
2081 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2083 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2089 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2090 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2091 if (ret && errno == EINTR) {
2092 clock_gettime(CLOCK_MONOTONIC, &cur);
2093 /* Timeout after 1s */
2094 if (cur.tv_sec > timeout.tv_sec + 1 ||
2095 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2102 } while (ret && errno == EINTR);
2108 int drmError(int err, const char *label)
2111 case DRM_ERR_NO_DEVICE:
2112 fprintf(stderr, "%s: no device\n", label);
2114 case DRM_ERR_NO_ACCESS:
2115 fprintf(stderr, "%s: no access\n", label);
2117 case DRM_ERR_NOT_ROOT:
2118 fprintf(stderr, "%s: not root\n", label);
2120 case DRM_ERR_INVALID:
2121 fprintf(stderr, "%s: invalid args\n", label);
2126 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2134 * Install IRQ handler.
2136 * \param fd file descriptor.
2137 * \param irq IRQ number.
2139 * \return zero on success, or a negative value on failure.
2142 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2143 * argument in a drm_control structure.
2145 int drmCtlInstHandler(int fd, int irq)
2150 ctl.func = DRM_INST_HANDLER;
2152 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2159 * Uninstall IRQ handler.
2161 * \param fd file descriptor.
2163 * \return zero on success, or a negative value on failure.
2166 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2167 * argument in a drm_control structure.
2169 int drmCtlUninstHandler(int fd)
2174 ctl.func = DRM_UNINST_HANDLER;
2176 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2181 int drmFinish(int fd, int context, drmLockFlags flags)
2186 lock.context = context;
2187 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2188 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2189 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2190 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2191 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2192 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2193 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2199 * Get IRQ from bus ID.
2201 * \param fd file descriptor.
2202 * \param busnum bus number.
2203 * \param devnum device number.
2204 * \param funcnum function number.
2206 * \return IRQ number on success, or a negative value on failure.
2209 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2210 * arguments in a drm_irq_busid structure.
2212 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2219 p.funcnum = funcnum;
2220 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2225 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2227 drmHashEntry *entry = drmGetEntry(fd);
2229 if (drmHashInsert(entry->tagTable, context, tag)) {
2230 drmHashDelete(entry->tagTable, context);
2231 drmHashInsert(entry->tagTable, context, tag);
2236 int drmDelContextTag(int fd, drm_context_t context)
2238 drmHashEntry *entry = drmGetEntry(fd);
2240 return drmHashDelete(entry->tagTable, context);
2243 void *drmGetContextTag(int fd, drm_context_t context)
2245 drmHashEntry *entry = drmGetEntry(fd);
2248 if (drmHashLookup(entry->tagTable, context, &value))
2254 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2255 drm_handle_t handle)
2257 drm_ctx_priv_map_t map;
2260 map.ctx_id = ctx_id;
2261 map.handle = (void *)(uintptr_t)handle;
2263 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2268 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2269 drm_handle_t *handle)
2271 drm_ctx_priv_map_t map;
2274 map.ctx_id = ctx_id;
2276 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2279 *handle = (drm_handle_t)(uintptr_t)map.handle;
2284 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2285 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2292 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2294 *offset = map.offset;
2298 *handle = (unsigned long)map.handle;
2303 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2304 unsigned long *magic, unsigned long *iocs)
2306 drm_client_t client;
2310 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2312 *auth = client.auth;
2315 *magic = client.magic;
2316 *iocs = client.iocs;
2320 int drmGetStats(int fd, drmStatsT *stats)
2326 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2330 memset(stats, 0, sizeof(*stats));
2331 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2335 stats->data[i].long_format = "%-20.20s"; \
2336 stats->data[i].rate_format = "%8.8s"; \
2337 stats->data[i].isvalue = 1; \
2338 stats->data[i].verbose = 0
2341 stats->data[i].long_format = "%-20.20s"; \
2342 stats->data[i].rate_format = "%5.5s"; \
2343 stats->data[i].isvalue = 0; \
2344 stats->data[i].mult_names = "kgm"; \
2345 stats->data[i].mult = 1000; \
2346 stats->data[i].verbose = 0
2349 stats->data[i].long_format = "%-20.20s"; \
2350 stats->data[i].rate_format = "%5.5s"; \
2351 stats->data[i].isvalue = 0; \
2352 stats->data[i].mult_names = "KGM"; \
2353 stats->data[i].mult = 1024; \
2354 stats->data[i].verbose = 0
2357 stats->count = s.count;
2358 for (i = 0; i < s.count; i++) {
2359 stats->data[i].value = s.data[i].value;
2360 switch (s.data[i].type) {
2361 case _DRM_STAT_LOCK:
2362 stats->data[i].long_name = "Lock";
2363 stats->data[i].rate_name = "Lock";
2366 case _DRM_STAT_OPENS:
2367 stats->data[i].long_name = "Opens";
2368 stats->data[i].rate_name = "O";
2370 stats->data[i].verbose = 1;
2372 case _DRM_STAT_CLOSES:
2373 stats->data[i].long_name = "Closes";
2374 stats->data[i].rate_name = "Lock";
2376 stats->data[i].verbose = 1;
2378 case _DRM_STAT_IOCTLS:
2379 stats->data[i].long_name = "Ioctls";
2380 stats->data[i].rate_name = "Ioc/s";
2383 case _DRM_STAT_LOCKS:
2384 stats->data[i].long_name = "Locks";
2385 stats->data[i].rate_name = "Lck/s";
2388 case _DRM_STAT_UNLOCKS:
2389 stats->data[i].long_name = "Unlocks";
2390 stats->data[i].rate_name = "Unl/s";
2394 stats->data[i].long_name = "IRQs";
2395 stats->data[i].rate_name = "IRQ/s";
2398 case _DRM_STAT_PRIMARY:
2399 stats->data[i].long_name = "Primary Bytes";
2400 stats->data[i].rate_name = "PB/s";
2403 case _DRM_STAT_SECONDARY:
2404 stats->data[i].long_name = "Secondary Bytes";
2405 stats->data[i].rate_name = "SB/s";
2409 stats->data[i].long_name = "DMA";
2410 stats->data[i].rate_name = "DMA/s";
2413 case _DRM_STAT_SPECIAL:
2414 stats->data[i].long_name = "Special DMA";
2415 stats->data[i].rate_name = "dma/s";
2418 case _DRM_STAT_MISSED:
2419 stats->data[i].long_name = "Miss";
2420 stats->data[i].rate_name = "Ms/s";
2423 case _DRM_STAT_VALUE:
2424 stats->data[i].long_name = "Value";
2425 stats->data[i].rate_name = "Value";
2428 case _DRM_STAT_BYTE:
2429 stats->data[i].long_name = "Bytes";
2430 stats->data[i].rate_name = "B/s";
2433 case _DRM_STAT_COUNT:
2435 stats->data[i].long_name = "Count";
2436 stats->data[i].rate_name = "Cnt/s";
2445 * Issue a set-version ioctl.
2447 * \param fd file descriptor.
2448 * \param drmCommandIndex command index
2449 * \param data source pointer of the data to be read and written.
2450 * \param size size of the data to be read and written.
2452 * \return zero on success, or a negative value on failure.
2455 * It issues a read-write ioctl given by
2456 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2458 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2461 drm_set_version_t sv;
2464 sv.drm_di_major = version->drm_di_major;
2465 sv.drm_di_minor = version->drm_di_minor;
2466 sv.drm_dd_major = version->drm_dd_major;
2467 sv.drm_dd_minor = version->drm_dd_minor;
2469 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2473 version->drm_di_major = sv.drm_di_major;
2474 version->drm_di_minor = sv.drm_di_minor;
2475 version->drm_dd_major = sv.drm_dd_major;
2476 version->drm_dd_minor = sv.drm_dd_minor;
2482 * Send a device-specific command.
2484 * \param fd file descriptor.
2485 * \param drmCommandIndex command index
2487 * \return zero on success, or a negative value on failure.
2490 * It issues a ioctl given by
2491 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2493 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2495 unsigned long request;
2497 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2499 if (drmIoctl(fd, request, NULL)) {
2507 * Send a device-specific read command.
2509 * \param fd file descriptor.
2510 * \param drmCommandIndex command index
2511 * \param data destination pointer of the data to be read.
2512 * \param size size of the data to be read.
2514 * \return zero on success, or a negative value on failure.
2517 * It issues a read ioctl given by
2518 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2520 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2523 unsigned long request;
2525 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2526 DRM_COMMAND_BASE + drmCommandIndex, size);
2528 if (drmIoctl(fd, request, data)) {
2536 * Send a device-specific write command.
2538 * \param fd file descriptor.
2539 * \param drmCommandIndex command index
2540 * \param data source pointer of the data to be written.
2541 * \param size size of the data to be written.
2543 * \return zero on success, or a negative value on failure.
2546 * It issues a write ioctl given by
2547 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2549 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2552 unsigned long request;
2554 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2555 DRM_COMMAND_BASE + drmCommandIndex, size);
2557 if (drmIoctl(fd, request, data)) {
2565 * Send a device-specific read-write command.
2567 * \param fd file descriptor.
2568 * \param drmCommandIndex command index
2569 * \param data source pointer of the data to be read and written.
2570 * \param size size of the data to be read and written.
2572 * \return zero on success, or a negative value on failure.
2575 * It issues a read-write ioctl given by
2576 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2578 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2581 unsigned long request;
2583 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2584 DRM_COMMAND_BASE + drmCommandIndex, size);
2586 if (drmIoctl(fd, request, data))
2591 #define DRM_MAX_FDS 16
2597 } connection[DRM_MAX_FDS];
2599 static int nr_fds = 0;
2601 int drmOpenOnce(void *unused,
2605 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2608 int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2613 for (i = 0; i < nr_fds; i++)
2614 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2615 (connection[i].type == type)) {
2616 connection[i].refcount++;
2618 return connection[i].fd;
2621 fd = drmOpenWithType(NULL, BusID, type);
2622 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2625 connection[nr_fds].BusID = strdup(BusID);
2626 connection[nr_fds].fd = fd;
2627 connection[nr_fds].refcount = 1;
2628 connection[nr_fds].type = type;
2632 fprintf(stderr, "saved connection %d for %s %d\n",
2633 nr_fds, connection[nr_fds].BusID,
2634 strcmp(BusID, connection[nr_fds].BusID));
2641 void drmCloseOnce(int fd)
2645 for (i = 0; i < nr_fds; i++) {
2646 if (fd == connection[i].fd) {
2647 if (--connection[i].refcount == 0) {
2648 drmClose(connection[i].fd);
2649 free(connection[i].BusID);
2652 connection[i] = connection[nr_fds];
2660 int drmSetMaster(int fd)
2662 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2665 int drmDropMaster(int fd)
2667 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2670 char *drmGetDeviceNameFromFd(int fd)
2677 /* The whole drmOpen thing is a fiasco and we need to find a way
2678 * back to just using open(2). For now, however, lets just make
2679 * things worse with even more ad hoc directory walking code to
2680 * discover the device file name. */
2685 for (i = 0; i < DRM_MAX_MINOR; i++) {
2686 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2687 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2690 if (i == DRM_MAX_MINOR)
2693 return strdup(name);
2696 int drmGetNodeTypeFromFd(int fd)
2701 if (fstat(fd, &sbuf))
2704 maj = major(sbuf.st_rdev);
2705 min = minor(sbuf.st_rdev);
2707 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
2712 type = drmGetMinorType(min);
2718 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2720 struct drm_prime_handle args;
2725 args.handle = handle;
2727 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2731 *prime_fd = args.fd;
2735 int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2737 struct drm_prime_handle args;
2742 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2746 *handle = args.handle;
2750 static char *drmGetMinorNameForFD(int fd, int type)
2754 struct dirent *pent, *ent;
2756 const char *name = drmGetMinorName(type);
2758 char dev_name[64], buf[64];
2767 if (fstat(fd, &sbuf))
2770 maj = major(sbuf.st_rdev);
2771 min = minor(sbuf.st_rdev);
2773 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2776 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2778 sysdir = opendir(buf);
2782 name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
2786 pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
2790 while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
2791 if (strncmp(ent->d_name, name, len) == 0) {
2795 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2797 return strdup(dev_name);
2809 char *drmGetPrimaryDeviceNameFromFd(int fd)
2811 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2814 char *drmGetRenderDeviceNameFromFd(int fd)
2816 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);