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 DRM_NODE_CONTROL 0
89 #define DRM_NODE_PRIMARY 1
90 #define DRM_NODE_RENDER 2
92 #define memclear(s) memset(&s, 0, sizeof(s))
94 static drmServerInfoPtr drm_server_info;
96 void drmSetServerInfo(drmServerInfoPtr info)
98 drm_server_info = info;
102 * Output a message to stderr.
104 * \param format printf() like format string.
107 * This function is a wrapper around vfprintf().
110 static int DRM_PRINTFLIKE(1, 0)
111 drmDebugPrint(const char *format, va_list ap)
113 return vfprintf(stderr, format, ap);
116 typedef int DRM_PRINTFLIKE(1, 0) (*debug_msg_func_t)(const char *format,
119 static debug_msg_func_t drm_debug_print = drmDebugPrint;
122 drmMsg(const char *format, ...)
126 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
128 va_start(ap, format);
129 if (drm_server_info) {
130 drm_server_info->debug_print(format,ap);
132 drm_debug_print(format, ap);
139 drmSetDebugMsgFunction(debug_msg_func_t debug_msg_ptr)
141 drm_debug_print = debug_msg_ptr;
144 static void *drmHashTable = NULL; /* Context switch callbacks */
146 void *drmGetHashTable(void)
151 void *drmMalloc(int size)
154 if ((pt = malloc(size)))
159 void drmFree(void *pt)
166 * Call ioctl, restarting if it is interupted
169 drmIoctl(int fd, unsigned long request, void *arg)
174 ret = ioctl(fd, request, arg);
175 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
179 static unsigned long drmGetKeyFromFd(int fd)
188 drmHashEntry *drmGetEntry(int fd)
190 unsigned long key = drmGetKeyFromFd(fd);
195 drmHashTable = drmHashCreate();
197 if (drmHashLookup(drmHashTable, key, &value)) {
198 entry = drmMalloc(sizeof(*entry));
201 entry->tagTable = drmHashCreate();
202 drmHashInsert(drmHashTable, key, entry);
210 * Compare two busid strings
215 * \return 1 if matched.
218 * This function compares two bus ID strings. It understands the older
219 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
220 * domain, b is bus, d is device, f is function.
222 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
224 /* First, check if the IDs are exactly the same */
225 if (strcasecmp(id1, id2) == 0)
228 /* Try to match old/new-style PCI bus IDs. */
229 if (strncasecmp(id1, "pci", 3) == 0) {
230 unsigned int o1, b1, d1, f1;
231 unsigned int o2, b2, d2, f2;
234 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
237 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
242 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
245 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
250 /* If domains aren't properly supported by the kernel interface,
251 * just ignore them, which sucks less than picking a totally random
252 * card with "open by name"
257 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
266 * Handles error checking for chown call.
268 * \param path to file.
269 * \param id of the new owner.
270 * \param id of the new group.
272 * \return zero if success or -1 if failure.
275 * Checks for failure. If failure was caused by signal call chown again.
276 * If any other failure happened then it will output error mesage using
279 static int chown_check_return(const char *path, uid_t owner, gid_t group)
284 rv = chown(path, owner, group);
285 } while (rv != 0 && errno == EINTR);
290 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
291 path, errno, strerror(errno));
296 * Open the DRM device, creating it if necessary.
298 * \param dev major and minor numbers of the device.
299 * \param minor minor number of the device.
301 * \return a file descriptor on success, or a negative value on error.
304 * Assembles the device name from \p minor and opens it, creating the device
305 * special file node with the major and minor numbers specified by \p dev and
306 * parent directory if necessary and was called by root.
308 static int drmOpenDevice(dev_t dev, int minor, int type)
311 const char *dev_name;
314 mode_t devmode = DRM_DEV_MODE, serv_mode;
315 int isroot = !geteuid();
316 uid_t user = DRM_DEV_UID;
317 gid_t group = DRM_DEV_GID, serv_group;
320 case DRM_NODE_PRIMARY:
321 dev_name = DRM_DEV_NAME;
323 case DRM_NODE_CONTROL:
324 dev_name = DRM_CONTROL_DEV_NAME;
326 case DRM_NODE_RENDER:
327 dev_name = DRM_RENDER_DEV_NAME;
333 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
334 drmMsg("drmOpenDevice: node name is %s\n", buf);
336 if (drm_server_info) {
337 drm_server_info->get_perms(&serv_group, &serv_mode);
338 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
339 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
340 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
344 if (stat(DRM_DIR_NAME, &st)) {
346 return DRM_ERR_NOT_ROOT;
347 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
348 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
349 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
352 /* Check if the device node exists and create it if necessary. */
353 if (stat(buf, &st)) {
355 return DRM_ERR_NOT_ROOT;
357 mknod(buf, S_IFCHR | devmode, dev);
360 if (drm_server_info) {
361 chown_check_return(buf, user, group);
365 /* if we modprobed then wait for udev */
369 if (stat(DRM_DIR_NAME, &st)) {
373 if (udev_count == 50)
378 if (stat(buf, &st)) {
382 if (udev_count == 50)
389 fd = open(buf, O_RDWR, 0);
390 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
391 fd, fd < 0 ? strerror(errno) : "OK");
396 /* Check if the device node is not what we expect it to be, and recreate it
397 * and try again if so.
399 if (st.st_rdev != dev) {
401 return DRM_ERR_NOT_ROOT;
403 mknod(buf, S_IFCHR | devmode, dev);
404 if (drm_server_info) {
405 chown_check_return(buf, user, group);
409 fd = open(buf, O_RDWR, 0);
410 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
411 fd, fd < 0 ? strerror(errno) : "OK");
415 drmMsg("drmOpenDevice: Open failed\n");
423 * Open the DRM device
425 * \param minor device minor number.
426 * \param create allow to create the device if set.
428 * \return a file descriptor on success, or a negative value on error.
431 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
432 * name from \p minor and opens it.
434 static int drmOpenMinor(int minor, int create, int type)
438 const char *dev_name;
441 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
444 case DRM_NODE_PRIMARY:
445 dev_name = DRM_DEV_NAME;
447 case DRM_NODE_CONTROL:
448 dev_name = DRM_CONTROL_DEV_NAME;
450 case DRM_NODE_RENDER:
451 dev_name = DRM_RENDER_DEV_NAME;
457 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
458 if ((fd = open(buf, O_RDWR, 0)) >= 0)
465 * Determine whether the DRM kernel driver has been loaded.
467 * \return 1 if the DRM driver is loaded, 0 otherwise.
470 * Determine the presence of the kernel driver by attempting to open the 0
471 * minor and get version information. For backward compatibility with older
472 * Linux implementations, /proc/dri is also checked.
474 int drmAvailable(void)
476 drmVersionPtr version;
480 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
482 /* Try proc for backward Linux compatibility */
483 if (!access("/proc/dri/0", R_OK))
489 if ((version = drmGetVersion(fd))) {
491 drmFreeVersion(version);
500 * Open the device by bus ID.
502 * \param busid bus ID.
504 * \return a file descriptor on success, or a negative value on error.
507 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
508 * comparing the device bus ID with the one supplied.
510 * \sa drmOpenMinor() and drmGetBusid().
512 static int drmOpenByBusid(const char *busid)
514 int i, pci_domain_ok = 1;
519 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
520 for (i = 0; i < DRM_MAX_MINOR; i++) {
521 fd = drmOpenMinor(i, 1, DRM_NODE_PRIMARY);
522 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
524 /* We need to try for 1.4 first for proper PCI domain support
525 * and if that fails, we know the kernel is busted
529 sv.drm_dd_major = -1; /* Don't care */
530 sv.drm_dd_minor = -1; /* Don't care */
531 if (drmSetInterfaceVersion(fd, &sv)) {
537 sv.drm_dd_major = -1; /* Don't care */
538 sv.drm_dd_minor = -1; /* Don't care */
539 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
540 drmSetInterfaceVersion(fd, &sv);
542 buf = drmGetBusid(fd);
543 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
544 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
558 * Open the device by name.
560 * \param name driver name.
562 * \return a file descriptor on success, or a negative value on error.
565 * This function opens the first minor number that matches the driver name and
566 * isn't already in use. If it's in use it then it will already have a bus ID
569 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
571 static int drmOpenByName(const char *name)
575 drmVersionPtr version;
579 * Open the first minor number that matches the driver name and isn't
580 * already in use. If it's in use it will have a busid assigned already.
582 for (i = 0; i < DRM_MAX_MINOR; i++) {
583 if ((fd = drmOpenMinor(i, 1, DRM_NODE_PRIMARY)) >= 0) {
584 if ((version = drmGetVersion(fd))) {
585 if (!strcmp(version->name, name)) {
586 drmFreeVersion(version);
587 id = drmGetBusid(fd);
588 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
597 drmFreeVersion(version);
605 /* Backward-compatibility /proc support */
606 for (i = 0; i < 8; i++) {
607 char proc_name[64], buf[512];
608 char *driver, *pt, *devstring;
611 sprintf(proc_name, "/proc/dri/%d/name", i);
612 if ((fd = open(proc_name, 0, 0)) >= 0) {
613 retcode = read(fd, buf, sizeof(buf)-1);
616 buf[retcode-1] = '\0';
617 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
619 if (*pt) { /* Device is next */
621 if (!strcmp(driver, name)) { /* Match */
622 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
624 if (*pt) { /* Found busid */
625 return drmOpenByBusid(++pt);
626 } else { /* No busid */
627 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_PRIMARY);
641 * Open the DRM device.
643 * Looks up the specified name and bus ID, and opens the device found. The
644 * entry in /dev/dri is created if necessary and if called by root.
646 * \param name driver name. Not referenced if bus ID is supplied.
647 * \param busid bus ID. Zero if not known.
649 * \return a file descriptor on success, or a negative value on error.
652 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
655 int drmOpen(const char *name, const char *busid)
657 if (!drmAvailable() && name != NULL && drm_server_info) {
658 /* try to load the kernel */
659 if (!drm_server_info->load_module(name)) {
660 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
666 int fd = drmOpenByBusid(busid);
672 return drmOpenByName(name);
677 int drmOpenControl(int minor)
679 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
682 int drmOpenRender(int minor)
684 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
688 * Free the version information returned by drmGetVersion().
690 * \param v pointer to the version information.
693 * It frees the memory pointed by \p %v as well as all the non-null strings
696 void drmFreeVersion(drmVersionPtr v)
708 * Free the non-public version information returned by the kernel.
710 * \param v pointer to the version information.
713 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
714 * the non-null strings pointers in it.
716 static void drmFreeKernelVersion(drm_version_t *v)
728 * Copy version information.
730 * \param d destination pointer.
731 * \param s source pointer.
734 * Used by drmGetVersion() to translate the information returned by the ioctl
735 * interface in a private structure into the public structure counterpart.
737 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
739 d->version_major = s->version_major;
740 d->version_minor = s->version_minor;
741 d->version_patchlevel = s->version_patchlevel;
742 d->name_len = s->name_len;
743 d->name = strdup(s->name);
744 d->date_len = s->date_len;
745 d->date = strdup(s->date);
746 d->desc_len = s->desc_len;
747 d->desc = strdup(s->desc);
752 * Query the driver version information.
754 * \param fd file descriptor.
756 * \return pointer to a drmVersion structure which should be freed with
759 * \note Similar information is available via /proc/dri.
762 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
763 * first with zeros to get the string lengths, and then the actually strings.
764 * It also null-terminates them since they might not be already.
766 drmVersionPtr drmGetVersion(int fd)
768 drmVersionPtr retval;
769 drm_version_t *version = drmMalloc(sizeof(*version));
773 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
774 drmFreeKernelVersion(version);
778 if (version->name_len)
779 version->name = drmMalloc(version->name_len + 1);
780 if (version->date_len)
781 version->date = drmMalloc(version->date_len + 1);
782 if (version->desc_len)
783 version->desc = drmMalloc(version->desc_len + 1);
785 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
786 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
787 drmFreeKernelVersion(version);
791 /* The results might not be null-terminated strings, so terminate them. */
792 if (version->name_len) version->name[version->name_len] = '\0';
793 if (version->date_len) version->date[version->date_len] = '\0';
794 if (version->desc_len) version->desc[version->desc_len] = '\0';
796 retval = drmMalloc(sizeof(*retval));
797 drmCopyVersion(retval, version);
798 drmFreeKernelVersion(version);
804 * Get version information for the DRM user space library.
806 * This version number is driver independent.
808 * \param fd file descriptor.
810 * \return version information.
813 * This function allocates and fills a drm_version structure with a hard coded
816 drmVersionPtr drmGetLibVersion(int fd)
818 drm_version_t *version = drmMalloc(sizeof(*version));
821 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
822 * revision 1.0.x = original DRM interface with no drmGetLibVersion
823 * entry point and many drm<Device> extensions
824 * revision 1.1.x = added drmCommand entry points for device extensions
825 * added drmGetLibVersion to identify libdrm.a version
826 * revision 1.2.x = added drmSetInterfaceVersion
827 * modified drmOpen to handle both busid and name
828 * revision 1.3.x = added server + memory manager
830 version->version_major = 1;
831 version->version_minor = 3;
832 version->version_patchlevel = 0;
834 return (drmVersionPtr)version;
837 int drmGetCap(int fd, uint64_t capability, uint64_t *value)
839 struct drm_get_cap cap;
843 cap.capability = capability;
845 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
853 int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
855 struct drm_set_client_cap cap;
858 cap.capability = capability;
861 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
865 * Free the bus ID information.
867 * \param busid bus ID information string as given by drmGetBusid().
870 * This function is just frees the memory pointed by \p busid.
872 void drmFreeBusid(const char *busid)
874 drmFree((void *)busid);
879 * Get the bus ID of the device.
881 * \param fd file descriptor.
883 * \return bus ID string.
886 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
887 * get the string length and data, passing the arguments in a drm_unique
890 char *drmGetBusid(int fd)
896 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
898 u.unique = drmMalloc(u.unique_len + 1);
899 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
901 u.unique[u.unique_len] = '\0';
908 * Set the bus ID of the device.
910 * \param fd file descriptor.
911 * \param busid bus ID string.
913 * \return zero on success, negative on failure.
916 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
917 * the arguments in a drm_unique structure.
919 int drmSetBusid(int fd, const char *busid)
924 u.unique = (char *)busid;
925 u.unique_len = strlen(busid);
927 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
933 int drmGetMagic(int fd, drm_magic_t * magic)
940 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
946 int drmAuthMagic(int fd, drm_magic_t magic)
952 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
958 * Specifies a range of memory that is available for mapping by a
961 * \param fd file descriptor.
962 * \param offset usually the physical address. The actual meaning depends of
963 * the \p type parameter. See below.
964 * \param size of the memory in bytes.
965 * \param type type of the memory to be mapped.
966 * \param flags combination of several flags to modify the function actions.
967 * \param handle will be set to a value that may be used as the offset
968 * parameter for mmap().
970 * \return zero on success or a negative value on error.
972 * \par Mapping the frame buffer
973 * For the frame buffer
974 * - \p offset will be the physical address of the start of the frame buffer,
975 * - \p size will be the size of the frame buffer in bytes, and
976 * - \p type will be DRM_FRAME_BUFFER.
979 * The area mapped will be uncached. If MTRR support is available in the
980 * kernel, the frame buffer area will be set to write combining.
982 * \par Mapping the MMIO register area
983 * For the MMIO register area,
984 * - \p offset will be the physical address of the start of the register area,
985 * - \p size will be the size of the register area bytes, and
986 * - \p type will be DRM_REGISTERS.
988 * The area mapped will be uncached.
990 * \par Mapping the SAREA
992 * - \p offset will be ignored and should be set to zero,
993 * - \p size will be the desired size of the SAREA in bytes,
994 * - \p type will be DRM_SHM.
997 * A shared memory area of the requested size will be created and locked in
998 * kernel memory. This area may be mapped into client-space by using the handle
1001 * \note May only be called by root.
1004 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1005 * the arguments in a drm_map structure.
1007 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1008 drmMapFlags flags, drm_handle_t *handle)
1013 map.offset = offset;
1017 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1020 *handle = (drm_handle_t)(uintptr_t)map.handle;
1024 int drmRmMap(int fd, drm_handle_t handle)
1029 map.handle = (void *)(uintptr_t)handle;
1031 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1037 * Make buffers available for DMA transfers.
1039 * \param fd file descriptor.
1040 * \param count number of buffers.
1041 * \param size size of each buffer.
1042 * \param flags buffer allocation flags.
1043 * \param agp_offset offset in the AGP aperture
1045 * \return number of buffers allocated, negative on error.
1048 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1052 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1055 drm_buf_desc_t request;
1058 request.count = count;
1059 request.size = size;
1060 request.flags = flags;
1061 request.agp_start = agp_offset;
1063 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1065 return request.count;
1068 int drmMarkBufs(int fd, double low, double high)
1070 drm_buf_info_t info;
1075 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1081 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1084 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1085 int retval = -errno;
1090 for (i = 0; i < info.count; i++) {
1091 info.list[i].low_mark = low * info.list[i].count;
1092 info.list[i].high_mark = high * info.list[i].count;
1093 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1094 int retval = -errno;
1107 * \param fd file descriptor.
1108 * \param count number of buffers to free.
1109 * \param list list of buffers to be freed.
1111 * \return zero on success, or a negative value on failure.
1113 * \note This function is primarily used for debugging.
1116 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1117 * the arguments in a drm_buf_free structure.
1119 int drmFreeBufs(int fd, int count, int *list)
1121 drm_buf_free_t request;
1124 request.count = count;
1125 request.list = list;
1126 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1135 * \param fd file descriptor.
1138 * This function closes the file descriptor.
1140 int drmClose(int fd)
1142 unsigned long key = drmGetKeyFromFd(fd);
1143 drmHashEntry *entry = drmGetEntry(fd);
1145 drmHashDestroy(entry->tagTable);
1148 entry->tagTable = NULL;
1150 drmHashDelete(drmHashTable, key);
1158 * Map a region of memory.
1160 * \param fd file descriptor.
1161 * \param handle handle returned by drmAddMap().
1162 * \param size size in bytes. Must match the size used by drmAddMap().
1163 * \param address will contain the user-space virtual address where the mapping
1166 * \return zero on success, or a negative value on failure.
1169 * This function is a wrapper for mmap().
1171 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1173 static unsigned long pagesize_mask = 0;
1179 pagesize_mask = getpagesize() - 1;
1181 size = (size + pagesize_mask) & ~pagesize_mask;
1183 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1184 if (*address == MAP_FAILED)
1191 * Unmap mappings obtained with drmMap().
1193 * \param address address as given by drmMap().
1194 * \param size size in bytes. Must match the size used by drmMap().
1196 * \return zero on success, or a negative value on failure.
1199 * This function is a wrapper for munmap().
1201 int drmUnmap(drmAddress address, drmSize size)
1203 return drm_munmap(address, size);
1206 drmBufInfoPtr drmGetBufInfo(int fd)
1208 drm_buf_info_t info;
1209 drmBufInfoPtr retval;
1214 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1218 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1221 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1226 retval = drmMalloc(sizeof(*retval));
1227 retval->count = info.count;
1228 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1229 for (i = 0; i < info.count; i++) {
1230 retval->list[i].count = info.list[i].count;
1231 retval->list[i].size = info.list[i].size;
1232 retval->list[i].low_mark = info.list[i].low_mark;
1233 retval->list[i].high_mark = info.list[i].high_mark;
1242 * Map all DMA buffers into client-virtual space.
1244 * \param fd file descriptor.
1246 * \return a pointer to a ::drmBufMap structure.
1248 * \note The client may not use these buffers until obtaining buffer indices
1252 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1253 * information about the buffers in a drm_buf_map structure into the
1254 * client-visible data structures.
1256 drmBufMapPtr drmMapBufs(int fd)
1259 drmBufMapPtr retval;
1263 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1269 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1272 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1277 retval = drmMalloc(sizeof(*retval));
1278 retval->count = bufs.count;
1279 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1280 for (i = 0; i < bufs.count; i++) {
1281 retval->list[i].idx = bufs.list[i].idx;
1282 retval->list[i].total = bufs.list[i].total;
1283 retval->list[i].used = 0;
1284 retval->list[i].address = bufs.list[i].address;
1294 * Unmap buffers allocated with drmMapBufs().
1296 * \return zero on success, or negative value on failure.
1299 * Calls munmap() for every buffer stored in \p bufs and frees the
1300 * memory allocated by drmMapBufs().
1302 int drmUnmapBufs(drmBufMapPtr bufs)
1306 for (i = 0; i < bufs->count; i++) {
1307 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1310 drmFree(bufs->list);
1317 #define DRM_DMA_RETRY 16
1320 * Reserve DMA buffers.
1322 * \param fd file descriptor.
1325 * \return zero on success, or a negative value on failure.
1328 * Assemble the arguments into a drm_dma structure and keeps issuing the
1329 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1331 int drmDMA(int fd, drmDMAReqPtr request)
1336 dma.context = request->context;
1337 dma.send_count = request->send_count;
1338 dma.send_indices = request->send_list;
1339 dma.send_sizes = request->send_sizes;
1340 dma.flags = request->flags;
1341 dma.request_count = request->request_count;
1342 dma.request_size = request->request_size;
1343 dma.request_indices = request->request_list;
1344 dma.request_sizes = request->request_sizes;
1345 dma.granted_count = 0;
1348 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1349 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1352 request->granted_count = dma.granted_count;
1361 * Obtain heavyweight hardware lock.
1363 * \param fd file descriptor.
1364 * \param context context.
1365 * \param flags flags that determine the sate of the hardware when the function
1368 * \return always zero.
1371 * This function translates the arguments into a drm_lock structure and issue
1372 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1374 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1379 lock.context = context;
1381 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1382 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1383 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1384 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1385 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1386 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1388 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1394 * Release the hardware lock.
1396 * \param fd file descriptor.
1397 * \param context context.
1399 * \return zero on success, or a negative value on failure.
1402 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1403 * argument in a drm_lock structure.
1405 int drmUnlock(int fd, drm_context_t context)
1410 lock.context = context;
1411 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1414 drm_context_t *drmGetReservedContextList(int fd, int *count)
1418 drm_context_t * retval;
1422 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1428 if (!(list = drmMalloc(res.count * sizeof(*list))))
1430 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1435 res.contexts = list;
1436 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1439 for (i = 0; i < res.count; i++)
1440 retval[i] = list[i].handle;
1447 void drmFreeReservedContextList(drm_context_t *pt)
1455 * Used by the X server during GLXContext initialization. This causes
1456 * per-context kernel-level resources to be allocated.
1458 * \param fd file descriptor.
1459 * \param handle is set on success. To be used by the client when requesting DMA
1460 * dispatch with drmDMA().
1462 * \return zero on success, or a negative value on failure.
1464 * \note May only be called by root.
1467 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1468 * argument in a drm_ctx structure.
1470 int drmCreateContext(int fd, drm_context_t *handle)
1475 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1477 *handle = ctx.handle;
1481 int drmSwitchToContext(int fd, drm_context_t context)
1486 ctx.handle = context;
1487 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1492 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1497 * Context preserving means that no context switches are done between DMA
1498 * buffers from one context and the next. This is suitable for use in the
1499 * X server (which promises to maintain hardware context), or in the
1500 * client-side library when buffers are swapped on behalf of two threads.
1503 ctx.handle = context;
1504 if (flags & DRM_CONTEXT_PRESERVED)
1505 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1506 if (flags & DRM_CONTEXT_2DONLY)
1507 ctx.flags |= _DRM_CONTEXT_2DONLY;
1508 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1513 int drmGetContextFlags(int fd, drm_context_t context,
1514 drm_context_tFlagsPtr flags)
1519 ctx.handle = context;
1520 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1523 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1524 *flags |= DRM_CONTEXT_PRESERVED;
1525 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1526 *flags |= DRM_CONTEXT_2DONLY;
1533 * Free any kernel-level resources allocated with drmCreateContext() associated
1536 * \param fd file descriptor.
1537 * \param handle handle given by drmCreateContext().
1539 * \return zero on success, or a negative value on failure.
1541 * \note May only be called by root.
1544 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1545 * argument in a drm_ctx structure.
1547 int drmDestroyContext(int fd, drm_context_t handle)
1552 ctx.handle = handle;
1553 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1558 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1563 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1565 *handle = draw.handle;
1569 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1574 draw.handle = handle;
1575 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1580 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1581 drm_drawable_info_type_t type, unsigned int num,
1584 drm_update_draw_t update;
1587 update.handle = handle;
1590 update.data = (unsigned long long)(unsigned long)data;
1592 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1599 * Acquire the AGP device.
1601 * Must be called before any of the other AGP related calls.
1603 * \param fd file descriptor.
1605 * \return zero on success, or a negative value on failure.
1608 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1610 int drmAgpAcquire(int fd)
1612 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1619 * Release the AGP device.
1621 * \param fd file descriptor.
1623 * \return zero on success, or a negative value on failure.
1626 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1628 int drmAgpRelease(int fd)
1630 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1639 * \param fd file descriptor.
1640 * \param mode AGP mode.
1642 * \return zero on success, or a negative value on failure.
1645 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1646 * argument in a drm_agp_mode structure.
1648 int drmAgpEnable(int fd, unsigned long mode)
1654 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1661 * Allocate a chunk of AGP memory.
1663 * \param fd file descriptor.
1664 * \param size requested memory size in bytes. Will be rounded to page boundary.
1665 * \param type type of memory to allocate.
1666 * \param address if not zero, will be set to the physical address of the
1668 * \param handle on success will be set to a handle of the allocated memory.
1670 * \return zero on success, or a negative value on failure.
1673 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1674 * arguments in a drm_agp_buffer structure.
1676 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1677 unsigned long *address, drm_handle_t *handle)
1682 *handle = DRM_AGP_NO_HANDLE;
1685 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1688 *address = b.physical;
1695 * Free a chunk of AGP memory.
1697 * \param fd file descriptor.
1698 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1700 * \return zero on success, or a negative value on failure.
1703 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1704 * argument in a drm_agp_buffer structure.
1706 int drmAgpFree(int fd, drm_handle_t handle)
1712 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1719 * Bind a chunk of AGP memory.
1721 * \param fd file descriptor.
1722 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1723 * \param offset offset in bytes. It will round to page boundary.
1725 * \return zero on success, or a negative value on failure.
1728 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1729 * argument in a drm_agp_binding structure.
1731 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1733 drm_agp_binding_t b;
1738 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1745 * Unbind a chunk of AGP memory.
1747 * \param fd file descriptor.
1748 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1750 * \return zero on success, or a negative value on failure.
1753 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1754 * the argument in a drm_agp_binding structure.
1756 int drmAgpUnbind(int fd, drm_handle_t handle)
1758 drm_agp_binding_t b;
1762 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1769 * Get AGP driver major version number.
1771 * \param fd file descriptor.
1773 * \return major version number on success, or a negative value on failure..
1776 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1777 * necessary information in a drm_agp_info structure.
1779 int drmAgpVersionMajor(int fd)
1785 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1787 return i.agp_version_major;
1792 * Get AGP driver minor version number.
1794 * \param fd file descriptor.
1796 * \return minor version number on success, or a negative value on failure.
1799 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1800 * necessary information in a drm_agp_info structure.
1802 int drmAgpVersionMinor(int fd)
1808 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1810 return i.agp_version_minor;
1817 * \param fd file descriptor.
1819 * \return mode on success, or zero on failure.
1822 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1823 * necessary information in a drm_agp_info structure.
1825 unsigned long drmAgpGetMode(int fd)
1831 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1838 * Get AGP aperture base.
1840 * \param fd file descriptor.
1842 * \return aperture base on success, zero on failure.
1845 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1846 * necessary information in a drm_agp_info structure.
1848 unsigned long drmAgpBase(int fd)
1854 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1856 return i.aperture_base;
1861 * Get AGP aperture size.
1863 * \param fd file descriptor.
1865 * \return aperture size on success, zero on failure.
1868 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1869 * necessary information in a drm_agp_info structure.
1871 unsigned long drmAgpSize(int fd)
1877 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1879 return i.aperture_size;
1884 * Get used AGP memory.
1886 * \param fd file descriptor.
1888 * \return memory used on success, or zero on failure.
1891 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1892 * necessary information in a drm_agp_info structure.
1894 unsigned long drmAgpMemoryUsed(int fd)
1900 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1902 return i.memory_used;
1907 * Get available AGP memory.
1909 * \param fd file descriptor.
1911 * \return memory available on success, or zero on failure.
1914 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1915 * necessary information in a drm_agp_info structure.
1917 unsigned long drmAgpMemoryAvail(int fd)
1923 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1925 return i.memory_allowed;
1930 * Get hardware vendor ID.
1932 * \param fd file descriptor.
1934 * \return vendor ID on success, or zero on failure.
1937 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1938 * necessary information in a drm_agp_info structure.
1940 unsigned int drmAgpVendorId(int fd)
1946 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1953 * Get hardware device ID.
1955 * \param fd file descriptor.
1957 * \return zero on success, or zero on failure.
1960 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1961 * necessary information in a drm_agp_info structure.
1963 unsigned int drmAgpDeviceId(int fd)
1969 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1974 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1976 drm_scatter_gather_t sg;
1982 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1984 *handle = sg.handle;
1988 int drmScatterGatherFree(int fd, drm_handle_t handle)
1990 drm_scatter_gather_t sg;
1994 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2002 * \param fd file descriptor.
2003 * \param vbl pointer to a drmVBlank structure.
2005 * \return zero on success, or a negative value on failure.
2008 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2010 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2012 struct timespec timeout, cur;
2015 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2017 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2023 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2024 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2025 if (ret && errno == EINTR) {
2026 clock_gettime(CLOCK_MONOTONIC, &cur);
2027 /* Timeout after 1s */
2028 if (cur.tv_sec > timeout.tv_sec + 1 ||
2029 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2036 } while (ret && errno == EINTR);
2042 int drmError(int err, const char *label)
2045 case DRM_ERR_NO_DEVICE:
2046 fprintf(stderr, "%s: no device\n", label);
2048 case DRM_ERR_NO_ACCESS:
2049 fprintf(stderr, "%s: no access\n", label);
2051 case DRM_ERR_NOT_ROOT:
2052 fprintf(stderr, "%s: not root\n", label);
2054 case DRM_ERR_INVALID:
2055 fprintf(stderr, "%s: invalid args\n", label);
2060 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2068 * Install IRQ handler.
2070 * \param fd file descriptor.
2071 * \param irq IRQ number.
2073 * \return zero on success, or a negative value on failure.
2076 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2077 * argument in a drm_control structure.
2079 int drmCtlInstHandler(int fd, int irq)
2084 ctl.func = DRM_INST_HANDLER;
2086 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2093 * Uninstall IRQ handler.
2095 * \param fd file descriptor.
2097 * \return zero on success, or a negative value on failure.
2100 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2101 * argument in a drm_control structure.
2103 int drmCtlUninstHandler(int fd)
2108 ctl.func = DRM_UNINST_HANDLER;
2110 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2115 int drmFinish(int fd, int context, drmLockFlags flags)
2120 lock.context = context;
2121 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2122 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2123 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2124 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2125 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2126 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2127 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2133 * Get IRQ from bus ID.
2135 * \param fd file descriptor.
2136 * \param busnum bus number.
2137 * \param devnum device number.
2138 * \param funcnum function number.
2140 * \return IRQ number on success, or a negative value on failure.
2143 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2144 * arguments in a drm_irq_busid structure.
2146 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2153 p.funcnum = funcnum;
2154 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2159 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2161 drmHashEntry *entry = drmGetEntry(fd);
2163 if (drmHashInsert(entry->tagTable, context, tag)) {
2164 drmHashDelete(entry->tagTable, context);
2165 drmHashInsert(entry->tagTable, context, tag);
2170 int drmDelContextTag(int fd, drm_context_t context)
2172 drmHashEntry *entry = drmGetEntry(fd);
2174 return drmHashDelete(entry->tagTable, context);
2177 void *drmGetContextTag(int fd, drm_context_t context)
2179 drmHashEntry *entry = drmGetEntry(fd);
2182 if (drmHashLookup(entry->tagTable, context, &value))
2188 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2189 drm_handle_t handle)
2191 drm_ctx_priv_map_t map;
2194 map.ctx_id = ctx_id;
2195 map.handle = (void *)(uintptr_t)handle;
2197 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2202 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2203 drm_handle_t *handle)
2205 drm_ctx_priv_map_t map;
2208 map.ctx_id = ctx_id;
2210 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2213 *handle = (drm_handle_t)(uintptr_t)map.handle;
2218 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2219 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2226 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2228 *offset = map.offset;
2232 *handle = (unsigned long)map.handle;
2237 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2238 unsigned long *magic, unsigned long *iocs)
2240 drm_client_t client;
2244 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2246 *auth = client.auth;
2249 *magic = client.magic;
2250 *iocs = client.iocs;
2254 int drmGetStats(int fd, drmStatsT *stats)
2260 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2264 memset(stats, 0, sizeof(*stats));
2265 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2269 stats->data[i].long_format = "%-20.20s"; \
2270 stats->data[i].rate_format = "%8.8s"; \
2271 stats->data[i].isvalue = 1; \
2272 stats->data[i].verbose = 0
2275 stats->data[i].long_format = "%-20.20s"; \
2276 stats->data[i].rate_format = "%5.5s"; \
2277 stats->data[i].isvalue = 0; \
2278 stats->data[i].mult_names = "kgm"; \
2279 stats->data[i].mult = 1000; \
2280 stats->data[i].verbose = 0
2283 stats->data[i].long_format = "%-20.20s"; \
2284 stats->data[i].rate_format = "%5.5s"; \
2285 stats->data[i].isvalue = 0; \
2286 stats->data[i].mult_names = "KGM"; \
2287 stats->data[i].mult = 1024; \
2288 stats->data[i].verbose = 0
2291 stats->count = s.count;
2292 for (i = 0; i < s.count; i++) {
2293 stats->data[i].value = s.data[i].value;
2294 switch (s.data[i].type) {
2295 case _DRM_STAT_LOCK:
2296 stats->data[i].long_name = "Lock";
2297 stats->data[i].rate_name = "Lock";
2300 case _DRM_STAT_OPENS:
2301 stats->data[i].long_name = "Opens";
2302 stats->data[i].rate_name = "O";
2304 stats->data[i].verbose = 1;
2306 case _DRM_STAT_CLOSES:
2307 stats->data[i].long_name = "Closes";
2308 stats->data[i].rate_name = "Lock";
2310 stats->data[i].verbose = 1;
2312 case _DRM_STAT_IOCTLS:
2313 stats->data[i].long_name = "Ioctls";
2314 stats->data[i].rate_name = "Ioc/s";
2317 case _DRM_STAT_LOCKS:
2318 stats->data[i].long_name = "Locks";
2319 stats->data[i].rate_name = "Lck/s";
2322 case _DRM_STAT_UNLOCKS:
2323 stats->data[i].long_name = "Unlocks";
2324 stats->data[i].rate_name = "Unl/s";
2328 stats->data[i].long_name = "IRQs";
2329 stats->data[i].rate_name = "IRQ/s";
2332 case _DRM_STAT_PRIMARY:
2333 stats->data[i].long_name = "Primary Bytes";
2334 stats->data[i].rate_name = "PB/s";
2337 case _DRM_STAT_SECONDARY:
2338 stats->data[i].long_name = "Secondary Bytes";
2339 stats->data[i].rate_name = "SB/s";
2343 stats->data[i].long_name = "DMA";
2344 stats->data[i].rate_name = "DMA/s";
2347 case _DRM_STAT_SPECIAL:
2348 stats->data[i].long_name = "Special DMA";
2349 stats->data[i].rate_name = "dma/s";
2352 case _DRM_STAT_MISSED:
2353 stats->data[i].long_name = "Miss";
2354 stats->data[i].rate_name = "Ms/s";
2357 case _DRM_STAT_VALUE:
2358 stats->data[i].long_name = "Value";
2359 stats->data[i].rate_name = "Value";
2362 case _DRM_STAT_BYTE:
2363 stats->data[i].long_name = "Bytes";
2364 stats->data[i].rate_name = "B/s";
2367 case _DRM_STAT_COUNT:
2369 stats->data[i].long_name = "Count";
2370 stats->data[i].rate_name = "Cnt/s";
2379 * Issue a set-version ioctl.
2381 * \param fd file descriptor.
2382 * \param drmCommandIndex command index
2383 * \param data source pointer of the data to be read and written.
2384 * \param size size of the data to be read and written.
2386 * \return zero on success, or a negative value on failure.
2389 * It issues a read-write ioctl given by
2390 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2392 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2395 drm_set_version_t sv;
2398 sv.drm_di_major = version->drm_di_major;
2399 sv.drm_di_minor = version->drm_di_minor;
2400 sv.drm_dd_major = version->drm_dd_major;
2401 sv.drm_dd_minor = version->drm_dd_minor;
2403 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2407 version->drm_di_major = sv.drm_di_major;
2408 version->drm_di_minor = sv.drm_di_minor;
2409 version->drm_dd_major = sv.drm_dd_major;
2410 version->drm_dd_minor = sv.drm_dd_minor;
2416 * Send a device-specific command.
2418 * \param fd file descriptor.
2419 * \param drmCommandIndex command index
2421 * \return zero on success, or a negative value on failure.
2424 * It issues a ioctl given by
2425 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2427 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2429 unsigned long request;
2431 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2433 if (drmIoctl(fd, request, NULL)) {
2441 * Send a device-specific read command.
2443 * \param fd file descriptor.
2444 * \param drmCommandIndex command index
2445 * \param data destination pointer of the data to be read.
2446 * \param size size of the data to be read.
2448 * \return zero on success, or a negative value on failure.
2451 * It issues a read ioctl given by
2452 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2454 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2457 unsigned long request;
2459 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2460 DRM_COMMAND_BASE + drmCommandIndex, size);
2462 if (drmIoctl(fd, request, data)) {
2470 * Send a device-specific write command.
2472 * \param fd file descriptor.
2473 * \param drmCommandIndex command index
2474 * \param data source pointer of the data to be written.
2475 * \param size size of the data to be written.
2477 * \return zero on success, or a negative value on failure.
2480 * It issues a write ioctl given by
2481 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2483 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2486 unsigned long request;
2488 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2489 DRM_COMMAND_BASE + drmCommandIndex, size);
2491 if (drmIoctl(fd, request, data)) {
2499 * Send a device-specific read-write command.
2501 * \param fd file descriptor.
2502 * \param drmCommandIndex command index
2503 * \param data source pointer of the data to be read and written.
2504 * \param size size of the data to be read and written.
2506 * \return zero on success, or a negative value on failure.
2509 * It issues a read-write ioctl given by
2510 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2512 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2515 unsigned long request;
2517 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2518 DRM_COMMAND_BASE + drmCommandIndex, size);
2520 if (drmIoctl(fd, request, data))
2525 #define DRM_MAX_FDS 16
2530 } connection[DRM_MAX_FDS];
2532 static int nr_fds = 0;
2534 int drmOpenOnce(void *unused,
2541 for (i = 0; i < nr_fds; i++)
2542 if (strcmp(BusID, connection[i].BusID) == 0) {
2543 connection[i].refcount++;
2545 return connection[i].fd;
2548 fd = drmOpen(unused, BusID);
2549 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2552 connection[nr_fds].BusID = strdup(BusID);
2553 connection[nr_fds].fd = fd;
2554 connection[nr_fds].refcount = 1;
2558 fprintf(stderr, "saved connection %d for %s %d\n",
2559 nr_fds, connection[nr_fds].BusID,
2560 strcmp(BusID, connection[nr_fds].BusID));
2567 void drmCloseOnce(int fd)
2571 for (i = 0; i < nr_fds; i++) {
2572 if (fd == connection[i].fd) {
2573 if (--connection[i].refcount == 0) {
2574 drmClose(connection[i].fd);
2575 free(connection[i].BusID);
2578 connection[i] = connection[nr_fds];
2586 int drmSetMaster(int fd)
2588 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2591 int drmDropMaster(int fd)
2593 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2596 char *drmGetDeviceNameFromFd(int fd)
2603 /* The whole drmOpen thing is a fiasco and we need to find a way
2604 * back to just using open(2). For now, however, lets just make
2605 * things worse with even more ad hoc directory walking code to
2606 * discover the device file name. */
2611 for (i = 0; i < DRM_MAX_MINOR; i++) {
2612 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2613 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2616 if (i == DRM_MAX_MINOR)
2619 return strdup(name);
2622 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2624 struct drm_prime_handle args;
2627 args.handle = handle;
2629 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2633 *prime_fd = args.fd;
2637 int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2639 struct drm_prime_handle args;
2644 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2648 *handle = args.handle;