No empty .Rs/.Re
[netbsd-mini2440.git] / sys / external / bsd / drm / dist / libdrm / xf86drm.c
blob6251f4b4874f622f76fc5c13eb8704ef1f05fd96
1 /**
2 * \file xf86drm.c
3 * User-level interface to DRM device
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Kevin E. Martin <martin@valinux.com>
7 */
9 /*
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
23 * Software.
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.
34 #ifdef HAVE_CONFIG_H
35 # include <config.h>
36 #endif
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <signal.h>
45 #include <time.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #define stat_t struct stat
49 #include <sys/ioctl.h>
50 #include <sys/mman.h>
51 #include <sys/time.h>
52 #include <stdarg.h>
54 /* Not all systems have MAP_FAILED defined */
55 #ifndef MAP_FAILED
56 #define MAP_FAILED ((void *)-1)
57 #endif
59 #include "xf86drm.h"
61 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
62 #define DRM_MAJOR 145
63 #endif
65 #ifdef __NetBSD__
66 #undef DRM_MAJOR
67 #define DRM_MAJOR 180
68 #endif
70 # ifdef __OpenBSD__
71 # define DRM_MAJOR 81
72 # endif
74 #ifndef DRM_MAJOR
75 #define DRM_MAJOR 226 /* Linux */
76 #endif
78 #ifndef DRM_MAX_MINOR
79 #define DRM_MAX_MINOR 16
80 #endif
83 * This definition needs to be changed on some systems if dev_t is a structure.
84 * If there is a header file we can get it from, there would be best.
86 #ifndef makedev
87 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
88 #endif
90 #define DRM_MSG_VERBOSITY 3
92 #define DRM_NODE_CONTROL 0
93 #define DRM_NODE_RENDER 1
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.
107 * \internal
108 * This function is a wrapper around vfprintf().
111 static int drmDebugPrint(const char *format, va_list ap)
113 return vfprintf(stderr, format, ap);
116 static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
118 void
119 drmMsg(const char *format, ...)
121 va_list ap;
122 const char *env;
123 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
125 va_start(ap, format);
126 if (drm_server_info) {
127 drm_server_info->debug_print(format,ap);
128 } else {
129 drm_debug_print(format, ap);
131 va_end(ap);
135 void
136 drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
138 drm_debug_print = debug_msg_ptr;
141 static void *drmHashTable = NULL; /* Context switch callbacks */
143 void *drmGetHashTable(void)
145 return drmHashTable;
148 void *drmMalloc(int size)
150 void *pt;
151 if ((pt = malloc(size)))
152 memset(pt, 0, size);
153 return pt;
156 void drmFree(void *pt)
158 if (pt)
159 free(pt);
162 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
163 static char *drmStrdup(const char *s)
165 char *retval;
167 if (!s)
168 return NULL;
170 retval = malloc(strlen(s)+1);
171 if (!retval)
172 return NULL;
174 strcpy(retval, s);
176 return retval;
180 * Call ioctl, restarting if it is interupted
183 drmIoctl(int fd, unsigned long request, void *arg)
185 int ret;
187 do {
188 ret = ioctl(fd, request, arg);
189 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
190 return ret;
193 static unsigned long drmGetKeyFromFd(int fd)
195 stat_t st;
197 st.st_rdev = 0;
198 fstat(fd, &st);
199 return st.st_rdev;
202 drmHashEntry *drmGetEntry(int fd)
204 unsigned long key = drmGetKeyFromFd(fd);
205 void *value;
206 drmHashEntry *entry;
208 if (!drmHashTable)
209 drmHashTable = drmHashCreate();
211 if (drmHashLookup(drmHashTable, key, &value)) {
212 entry = drmMalloc(sizeof(*entry));
213 entry->fd = fd;
214 entry->f = NULL;
215 entry->tagTable = drmHashCreate();
216 drmHashInsert(drmHashTable, key, entry);
217 } else {
218 entry = value;
220 return entry;
224 * Compare two busid strings
226 * \param first
227 * \param second
229 * \return 1 if matched.
231 * \internal
232 * This function compares two bus ID strings. It understands the older
233 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
234 * domain, b is bus, d is device, f is function.
236 static int drmMatchBusID(const char *id1, const char *id2)
238 /* First, check if the IDs are exactly the same */
239 if (strcasecmp(id1, id2) == 0)
240 return 1;
242 /* Try to match old/new-style PCI bus IDs. */
243 if (strncasecmp(id1, "pci", 3) == 0) {
244 int o1, b1, d1, f1;
245 int o2, b2, d2, f2;
246 int ret;
248 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
249 if (ret != 4) {
250 o1 = 0;
251 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
252 if (ret != 3)
253 return 0;
256 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
257 if (ret != 4) {
258 o2 = 0;
259 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
260 if (ret != 3)
261 return 0;
264 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
265 return 0;
266 else
267 return 1;
269 return 0;
273 * Open the DRM device, creating it if necessary.
275 * \param dev major and minor numbers of the device.
276 * \param minor minor number of the device.
278 * \return a file descriptor on success, or a negative value on error.
280 * \internal
281 * Assembles the device name from \p minor and opens it, creating the device
282 * special file node with the major and minor numbers specified by \p dev and
283 * parent directory if necessary and was called by root.
285 static int drmOpenDevice(long dev, int minor, int type)
287 stat_t st;
288 char buf[64];
289 int fd;
290 mode_t devmode = DRM_DEV_MODE, serv_mode;
291 int isroot = !geteuid();
292 uid_t user = DRM_DEV_UID;
293 gid_t group = DRM_DEV_GID, serv_group;
295 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
296 drmMsg("drmOpenDevice: node name is %s\n", buf);
298 if (drm_server_info) {
299 drm_server_info->get_perms(&serv_group, &serv_mode);
300 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
301 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
302 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
305 #if !defined(UDEV)
306 if (stat(DRM_DIR_NAME, &st)) {
307 if (!isroot)
308 return DRM_ERR_NOT_ROOT;
309 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
310 chown(DRM_DIR_NAME, 0, 0); /* root:root */
311 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
314 /* Check if the device node exists and create it if necessary. */
315 if (stat(buf, &st)) {
316 if (!isroot)
317 return DRM_ERR_NOT_ROOT;
318 remove(buf);
319 mknod(buf, S_IFCHR | devmode, dev);
322 if (drm_server_info) {
323 chown(buf, user, group);
324 chmod(buf, devmode);
326 #else
327 /* if we modprobed then wait for udev */
329 int udev_count = 0;
330 wait_for_udev:
331 if (stat(DRM_DIR_NAME, &st)) {
332 usleep(20);
333 udev_count++;
335 if (udev_count == 50)
336 return -1;
337 goto wait_for_udev;
340 if (stat(buf, &st)) {
341 usleep(20);
342 udev_count++;
344 if (udev_count == 50)
345 return -1;
346 goto wait_for_udev;
349 #endif
351 fd = open(buf, O_RDWR, 0);
352 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
353 fd, fd < 0 ? strerror(errno) : "OK");
354 if (fd >= 0)
355 return fd;
357 /* Check if the device node is not what we expect it to be, and recreate it
358 * and try again if so.
360 if (st.st_rdev != dev) {
361 if (!isroot)
362 return DRM_ERR_NOT_ROOT;
363 remove(buf);
364 mknod(buf, S_IFCHR | devmode, dev);
365 if (drm_server_info) {
366 chown(buf, user, group);
367 chmod(buf, devmode);
370 fd = open(buf, O_RDWR, 0);
371 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
372 fd, fd < 0 ? strerror(errno) : "OK");
373 if (fd >= 0)
374 return fd;
376 drmMsg("drmOpenDevice: Open failed\n");
377 remove(buf);
378 return -errno;
383 * Open the DRM device
385 * \param minor device minor number.
386 * \param create allow to create the device if set.
388 * \return a file descriptor on success, or a negative value on error.
390 * \internal
391 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
392 * name from \p minor and opens it.
394 static int drmOpenMinor(int minor, int create, int type)
396 int fd;
397 char buf[64];
399 if (create)
400 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
402 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
403 if ((fd = open(buf, O_RDWR, 0)) >= 0)
404 return fd;
405 return -errno;
410 * Determine whether the DRM kernel driver has been loaded.
412 * \return 1 if the DRM driver is loaded, 0 otherwise.
414 * \internal
415 * Determine the presence of the kernel driver by attempting to open the 0
416 * minor and get version information. For backward compatibility with older
417 * Linux implementations, /proc/dri is also checked.
419 int drmAvailable(void)
421 drmVersionPtr version;
422 int retval = 0;
423 int fd;
425 if ((fd = drmOpenMinor(0, 1, DRM_NODE_RENDER)) < 0) {
426 #ifdef __linux__
427 /* Try proc for backward Linux compatibility */
428 if (!access("/proc/dri/0", R_OK))
429 return 1;
430 #endif
431 return 0;
434 if ((version = drmGetVersion(fd))) {
435 retval = 1;
436 drmFreeVersion(version);
438 close(fd);
440 return retval;
445 * Open the device by bus ID.
447 * \param busid bus ID.
449 * \return a file descriptor on success, or a negative value on error.
451 * \internal
452 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
453 * comparing the device bus ID with the one supplied.
455 * \sa drmOpenMinor() and drmGetBusid().
457 static int drmOpenByBusid(const char *busid)
459 int i;
460 int fd;
461 const char *buf;
462 drmSetVersion sv;
464 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
465 for (i = 0; i < DRM_MAX_MINOR; i++) {
466 fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
467 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
468 if (fd >= 0) {
469 sv.drm_di_major = 1;
470 sv.drm_di_minor = 1;
471 sv.drm_dd_major = -1; /* Don't care */
472 sv.drm_dd_minor = -1; /* Don't care */
473 drmSetInterfaceVersion(fd, &sv);
474 buf = drmGetBusid(fd);
475 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
476 if (buf && drmMatchBusID(buf, busid)) {
477 drmFreeBusid(buf);
478 return fd;
480 if (buf)
481 drmFreeBusid(buf);
482 close(fd);
485 return -1;
490 * Open the device by name.
492 * \param name driver name.
494 * \return a file descriptor on success, or a negative value on error.
496 * \internal
497 * This function opens the first minor number that matches the driver name and
498 * isn't already in use. If it's in use it then it will already have a bus ID
499 * assigned.
501 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
503 static int drmOpenByName(const char *name)
505 int i;
506 int fd;
507 drmVersionPtr version;
508 char * id;
510 if (!drmAvailable()) {
511 if (!drm_server_info) {
512 return -1;
514 else {
515 /* try to load the kernel module now */
516 if (!drm_server_info->load_module(name)) {
517 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
518 return -1;
524 * Open the first minor number that matches the driver name and isn't
525 * already in use. If it's in use it will have a busid assigned already.
527 for (i = 0; i < DRM_MAX_MINOR; i++) {
528 if ((fd = drmOpenMinor(i, 1, DRM_NODE_RENDER)) >= 0) {
529 if ((version = drmGetVersion(fd))) {
530 if (!strcmp(version->name, name)) {
531 drmFreeVersion(version);
532 id = drmGetBusid(fd);
533 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
534 if (!id || !*id) {
535 if (id)
536 drmFreeBusid(id);
537 return fd;
538 } else {
539 drmFreeBusid(id);
541 } else {
542 drmFreeVersion(version);
545 close(fd);
549 #ifdef __linux__
550 /* Backward-compatibility /proc support */
551 for (i = 0; i < 8; i++) {
552 char proc_name[64], buf[512];
553 char *driver, *pt, *devstring;
554 int retcode;
556 sprintf(proc_name, "/proc/dri/%d/name", i);
557 if ((fd = open(proc_name, 0, 0)) >= 0) {
558 retcode = read(fd, buf, sizeof(buf)-1);
559 close(fd);
560 if (retcode) {
561 buf[retcode-1] = '\0';
562 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
564 if (*pt) { /* Device is next */
565 *pt = '\0';
566 if (!strcmp(driver, name)) { /* Match */
567 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
569 if (*pt) { /* Found busid */
570 return drmOpenByBusid(++pt);
571 } else { /* No busid */
572 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_RENDER);
579 #endif
581 return -1;
586 * Open the DRM device.
588 * Looks up the specified name and bus ID, and opens the device found. The
589 * entry in /dev/dri is created if necessary and if called by root.
591 * \param name driver name. Not referenced if bus ID is supplied.
592 * \param busid bus ID. Zero if not known.
594 * \return a file descriptor on success, or a negative value on error.
596 * \internal
597 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
598 * otherwise.
600 int drmOpen(const char *name, const char *busid)
602 if (!drmAvailable() && name != NULL && drm_server_info) {
603 /* try to load the kernel */
604 if (!drm_server_info->load_module(name)) {
605 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
606 return -1;
610 if (busid) {
611 int fd = drmOpenByBusid(busid);
612 if (fd >= 0)
613 return fd;
616 if (name)
617 return drmOpenByName(name);
619 return -1;
622 int drmOpenControl(int minor)
624 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
628 * Free the version information returned by drmGetVersion().
630 * \param v pointer to the version information.
632 * \internal
633 * It frees the memory pointed by \p %v as well as all the non-null strings
634 * pointers in it.
636 void drmFreeVersion(drmVersionPtr v)
638 if (!v)
639 return;
640 drmFree(v->name);
641 drmFree(v->date);
642 drmFree(v->desc);
643 drmFree(v);
648 * Free the non-public version information returned by the kernel.
650 * \param v pointer to the version information.
652 * \internal
653 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
654 * the non-null strings pointers in it.
656 static void drmFreeKernelVersion(drm_version_t *v)
658 if (!v)
659 return;
660 drmFree(v->name);
661 drmFree(v->date);
662 drmFree(v->desc);
663 drmFree(v);
668 * Copy version information.
670 * \param d destination pointer.
671 * \param s source pointer.
673 * \internal
674 * Used by drmGetVersion() to translate the information returned by the ioctl
675 * interface in a private structure into the public structure counterpart.
677 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
679 d->version_major = s->version_major;
680 d->version_minor = s->version_minor;
681 d->version_patchlevel = s->version_patchlevel;
682 d->name_len = s->name_len;
683 d->name = drmStrdup(s->name);
684 d->date_len = s->date_len;
685 d->date = drmStrdup(s->date);
686 d->desc_len = s->desc_len;
687 d->desc = drmStrdup(s->desc);
692 * Query the driver version information.
694 * \param fd file descriptor.
696 * \return pointer to a drmVersion structure which should be freed with
697 * drmFreeVersion().
699 * \note Similar information is available via /proc/dri.
701 * \internal
702 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
703 * first with zeros to get the string lengths, and then the actually strings.
704 * It also null-terminates them since they might not be already.
706 drmVersionPtr drmGetVersion(int fd)
708 drmVersionPtr retval;
709 drm_version_t *version = drmMalloc(sizeof(*version));
711 version->name_len = 0;
712 version->name = NULL;
713 version->date_len = 0;
714 version->date = NULL;
715 version->desc_len = 0;
716 version->desc = NULL;
718 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
719 drmFreeKernelVersion(version);
720 return NULL;
723 if (version->name_len)
724 version->name = drmMalloc(version->name_len + 1);
725 if (version->date_len)
726 version->date = drmMalloc(version->date_len + 1);
727 if (version->desc_len)
728 version->desc = drmMalloc(version->desc_len + 1);
730 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
731 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
732 drmFreeKernelVersion(version);
733 return NULL;
736 /* The results might not be null-terminated strings, so terminate them. */
737 if (version->name_len) version->name[version->name_len] = '\0';
738 if (version->date_len) version->date[version->date_len] = '\0';
739 if (version->desc_len) version->desc[version->desc_len] = '\0';
741 retval = drmMalloc(sizeof(*retval));
742 drmCopyVersion(retval, version);
743 drmFreeKernelVersion(version);
744 return retval;
749 * Get version information for the DRM user space library.
751 * This version number is driver independent.
753 * \param fd file descriptor.
755 * \return version information.
757 * \internal
758 * This function allocates and fills a drm_version structure with a hard coded
759 * version number.
761 drmVersionPtr drmGetLibVersion(int fd)
763 drm_version_t *version = drmMalloc(sizeof(*version));
765 /* Version history:
766 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
767 * revision 1.0.x = original DRM interface with no drmGetLibVersion
768 * entry point and many drm<Device> extensions
769 * revision 1.1.x = added drmCommand entry points for device extensions
770 * added drmGetLibVersion to identify libdrm.a version
771 * revision 1.2.x = added drmSetInterfaceVersion
772 * modified drmOpen to handle both busid and name
773 * revision 1.3.x = added server + memory manager
775 version->version_major = 1;
776 version->version_minor = 3;
777 version->version_patchlevel = 0;
779 return (drmVersionPtr)version;
784 * Free the bus ID information.
786 * \param busid bus ID information string as given by drmGetBusid().
788 * \internal
789 * This function is just frees the memory pointed by \p busid.
791 void drmFreeBusid(const char *busid)
793 drmFree((void *)busid);
798 * Get the bus ID of the device.
800 * \param fd file descriptor.
802 * \return bus ID string.
804 * \internal
805 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
806 * get the string length and data, passing the arguments in a drm_unique
807 * structure.
809 char *drmGetBusid(int fd)
811 drm_unique_t u;
813 u.unique_len = 0;
814 u.unique = NULL;
816 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
817 return NULL;
818 u.unique = drmMalloc(u.unique_len + 1);
819 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
820 return NULL;
821 u.unique[u.unique_len] = '\0';
823 return u.unique;
828 * Set the bus ID of the device.
830 * \param fd file descriptor.
831 * \param busid bus ID string.
833 * \return zero on success, negative on failure.
835 * \internal
836 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
837 * the arguments in a drm_unique structure.
839 int drmSetBusid(int fd, const char *busid)
841 drm_unique_t u;
843 u.unique = (char *)busid;
844 u.unique_len = strlen(busid);
846 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
847 return -errno;
849 return 0;
852 int drmGetMagic(int fd, drm_magic_t * magic)
854 drm_auth_t auth;
856 *magic = 0;
857 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
858 return -errno;
859 *magic = auth.magic;
860 return 0;
863 int drmAuthMagic(int fd, drm_magic_t magic)
865 drm_auth_t auth;
867 auth.magic = magic;
868 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
869 return -errno;
870 return 0;
874 * Specifies a range of memory that is available for mapping by a
875 * non-root process.
877 * \param fd file descriptor.
878 * \param offset usually the physical address. The actual meaning depends of
879 * the \p type parameter. See below.
880 * \param size of the memory in bytes.
881 * \param type type of the memory to be mapped.
882 * \param flags combination of several flags to modify the function actions.
883 * \param handle will be set to a value that may be used as the offset
884 * parameter for mmap().
886 * \return zero on success or a negative value on error.
888 * \par Mapping the frame buffer
889 * For the frame buffer
890 * - \p offset will be the physical address of the start of the frame buffer,
891 * - \p size will be the size of the frame buffer in bytes, and
892 * - \p type will be DRM_FRAME_BUFFER.
894 * \par
895 * The area mapped will be uncached. If MTRR support is available in the
896 * kernel, the frame buffer area will be set to write combining.
898 * \par Mapping the MMIO register area
899 * For the MMIO register area,
900 * - \p offset will be the physical address of the start of the register area,
901 * - \p size will be the size of the register area bytes, and
902 * - \p type will be DRM_REGISTERS.
903 * \par
904 * The area mapped will be uncached.
906 * \par Mapping the SAREA
907 * For the SAREA,
908 * - \p offset will be ignored and should be set to zero,
909 * - \p size will be the desired size of the SAREA in bytes,
910 * - \p type will be DRM_SHM.
912 * \par
913 * A shared memory area of the requested size will be created and locked in
914 * kernel memory. This area may be mapped into client-space by using the handle
915 * returned.
917 * \note May only be called by root.
919 * \internal
920 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
921 * the arguments in a drm_map structure.
923 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
924 drmMapFlags flags, drm_handle_t *handle)
926 drm_map_t map;
928 map.offset = offset;
929 map.size = size;
930 map.handle = 0;
931 map.type = type;
932 map.flags = flags;
933 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
934 return -errno;
935 if (handle)
936 *handle = (drm_handle_t)map.handle;
937 return 0;
940 int drmRmMap(int fd, drm_handle_t handle)
942 drm_map_t map;
944 map.handle = (void *)handle;
946 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
947 return -errno;
948 return 0;
952 * Make buffers available for DMA transfers.
954 * \param fd file descriptor.
955 * \param count number of buffers.
956 * \param size size of each buffer.
957 * \param flags buffer allocation flags.
958 * \param agp_offset offset in the AGP aperture
960 * \return number of buffers allocated, negative on error.
962 * \internal
963 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
965 * \sa drm_buf_desc.
967 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
968 int agp_offset)
970 drm_buf_desc_t request;
972 request.count = count;
973 request.size = size;
974 request.low_mark = 0;
975 request.high_mark = 0;
976 request.flags = flags;
977 request.agp_start = agp_offset;
979 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
980 return -errno;
981 return request.count;
984 int drmMarkBufs(int fd, double low, double high)
986 drm_buf_info_t info;
987 int i;
989 info.count = 0;
990 info.list = NULL;
992 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
993 return -EINVAL;
995 if (!info.count)
996 return -EINVAL;
998 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
999 return -ENOMEM;
1001 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1002 int retval = -errno;
1003 drmFree(info.list);
1004 return retval;
1007 for (i = 0; i < info.count; i++) {
1008 info.list[i].low_mark = low * info.list[i].count;
1009 info.list[i].high_mark = high * info.list[i].count;
1010 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1011 int retval = -errno;
1012 drmFree(info.list);
1013 return retval;
1016 drmFree(info.list);
1018 return 0;
1022 * Free buffers.
1024 * \param fd file descriptor.
1025 * \param count number of buffers to free.
1026 * \param list list of buffers to be freed.
1028 * \return zero on success, or a negative value on failure.
1030 * \note This function is primarily used for debugging.
1032 * \internal
1033 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1034 * the arguments in a drm_buf_free structure.
1036 int drmFreeBufs(int fd, int count, int *list)
1038 drm_buf_free_t request;
1040 request.count = count;
1041 request.list = list;
1042 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1043 return -errno;
1044 return 0;
1049 * Close the device.
1051 * \param fd file descriptor.
1053 * \internal
1054 * This function closes the file descriptor.
1056 int drmClose(int fd)
1058 unsigned long key = drmGetKeyFromFd(fd);
1059 drmHashEntry *entry = drmGetEntry(fd);
1061 drmHashDestroy(entry->tagTable);
1062 entry->fd = 0;
1063 entry->f = NULL;
1064 entry->tagTable = NULL;
1066 drmHashDelete(drmHashTable, key);
1067 drmFree(entry);
1069 return close(fd);
1074 * Map a region of memory.
1076 * \param fd file descriptor.
1077 * \param handle handle returned by drmAddMap().
1078 * \param size size in bytes. Must match the size used by drmAddMap().
1079 * \param address will contain the user-space virtual address where the mapping
1080 * begins.
1082 * \return zero on success, or a negative value on failure.
1084 * \internal
1085 * This function is a wrapper for mmap().
1087 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1089 static unsigned long pagesize_mask = 0;
1091 if (fd < 0)
1092 return -EINVAL;
1094 if (!pagesize_mask)
1095 pagesize_mask = getpagesize() - 1;
1097 size = (size + pagesize_mask) & ~pagesize_mask;
1099 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1100 if (*address == MAP_FAILED)
1101 return -errno;
1102 return 0;
1107 * Unmap mappings obtained with drmMap().
1109 * \param address address as given by drmMap().
1110 * \param size size in bytes. Must match the size used by drmMap().
1112 * \return zero on success, or a negative value on failure.
1114 * \internal
1115 * This function is a wrapper for munmap().
1117 int drmUnmap(drmAddress address, drmSize size)
1119 return munmap(address, size);
1122 drmBufInfoPtr drmGetBufInfo(int fd)
1124 drm_buf_info_t info;
1125 drmBufInfoPtr retval;
1126 int i;
1128 info.count = 0;
1129 info.list = NULL;
1131 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1132 return NULL;
1134 if (info.count) {
1135 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1136 return NULL;
1138 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1139 drmFree(info.list);
1140 return NULL;
1143 retval = drmMalloc(sizeof(*retval));
1144 retval->count = info.count;
1145 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1146 for (i = 0; i < info.count; i++) {
1147 retval->list[i].count = info.list[i].count;
1148 retval->list[i].size = info.list[i].size;
1149 retval->list[i].low_mark = info.list[i].low_mark;
1150 retval->list[i].high_mark = info.list[i].high_mark;
1152 drmFree(info.list);
1153 return retval;
1155 return NULL;
1159 * Map all DMA buffers into client-virtual space.
1161 * \param fd file descriptor.
1163 * \return a pointer to a ::drmBufMap structure.
1165 * \note The client may not use these buffers until obtaining buffer indices
1166 * with drmDMA().
1168 * \internal
1169 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1170 * information about the buffers in a drm_buf_map structure into the
1171 * client-visible data structures.
1173 drmBufMapPtr drmMapBufs(int fd)
1175 drm_buf_map_t bufs;
1176 drmBufMapPtr retval;
1177 int i;
1179 bufs.count = 0;
1180 bufs.list = NULL;
1181 bufs.virtual = NULL;
1182 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1183 return NULL;
1185 if (!bufs.count)
1186 return NULL;
1188 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1189 return NULL;
1191 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1192 drmFree(bufs.list);
1193 return NULL;
1196 retval = drmMalloc(sizeof(*retval));
1197 retval->count = bufs.count;
1198 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1199 for (i = 0; i < bufs.count; i++) {
1200 retval->list[i].idx = bufs.list[i].idx;
1201 retval->list[i].total = bufs.list[i].total;
1202 retval->list[i].used = 0;
1203 retval->list[i].address = bufs.list[i].address;
1206 drmFree(bufs.list);
1208 return retval;
1213 * Unmap buffers allocated with drmMapBufs().
1215 * \return zero on success, or negative value on failure.
1217 * \internal
1218 * Calls munmap() for every buffer stored in \p bufs and frees the
1219 * memory allocated by drmMapBufs().
1221 int drmUnmapBufs(drmBufMapPtr bufs)
1223 int i;
1225 for (i = 0; i < bufs->count; i++) {
1226 munmap(bufs->list[i].address, bufs->list[i].total);
1229 drmFree(bufs->list);
1230 drmFree(bufs);
1232 return 0;
1236 #define DRM_DMA_RETRY 16
1239 * Reserve DMA buffers.
1241 * \param fd file descriptor.
1242 * \param request
1244 * \return zero on success, or a negative value on failure.
1246 * \internal
1247 * Assemble the arguments into a drm_dma structure and keeps issuing the
1248 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1250 int drmDMA(int fd, drmDMAReqPtr request)
1252 drm_dma_t dma;
1253 int ret, i = 0;
1255 dma.context = request->context;
1256 dma.send_count = request->send_count;
1257 dma.send_indices = request->send_list;
1258 dma.send_sizes = request->send_sizes;
1259 dma.flags = request->flags;
1260 dma.request_count = request->request_count;
1261 dma.request_size = request->request_size;
1262 dma.request_indices = request->request_list;
1263 dma.request_sizes = request->request_sizes;
1264 dma.granted_count = 0;
1266 do {
1267 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1268 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1270 if ( ret == 0 ) {
1271 request->granted_count = dma.granted_count;
1272 return 0;
1273 } else {
1274 return -errno;
1280 * Obtain heavyweight hardware lock.
1282 * \param fd file descriptor.
1283 * \param context context.
1284 * \param flags flags that determine the sate of the hardware when the function
1285 * returns.
1287 * \return always zero.
1289 * \internal
1290 * This function translates the arguments into a drm_lock structure and issue
1291 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1293 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1295 drm_lock_t lock;
1297 lock.context = context;
1298 lock.flags = 0;
1299 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1300 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1301 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1302 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1303 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1304 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1306 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1308 return 0;
1312 * Release the hardware lock.
1314 * \param fd file descriptor.
1315 * \param context context.
1317 * \return zero on success, or a negative value on failure.
1319 * \internal
1320 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1321 * argument in a drm_lock structure.
1323 int drmUnlock(int fd, drm_context_t context)
1325 drm_lock_t lock;
1327 lock.context = context;
1328 lock.flags = 0;
1329 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1332 drm_context_t *drmGetReservedContextList(int fd, int *count)
1334 drm_ctx_res_t res;
1335 drm_ctx_t *list;
1336 drm_context_t * retval;
1337 int i;
1339 res.count = 0;
1340 res.contexts = NULL;
1341 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1342 return NULL;
1344 if (!res.count)
1345 return NULL;
1347 if (!(list = drmMalloc(res.count * sizeof(*list))))
1348 return NULL;
1349 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1350 drmFree(list);
1351 return NULL;
1354 res.contexts = list;
1355 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1356 return NULL;
1358 for (i = 0; i < res.count; i++)
1359 retval[i] = list[i].handle;
1360 drmFree(list);
1362 *count = res.count;
1363 return retval;
1366 void drmFreeReservedContextList(drm_context_t *pt)
1368 drmFree(pt);
1372 * Create context.
1374 * Used by the X server during GLXContext initialization. This causes
1375 * per-context kernel-level resources to be allocated.
1377 * \param fd file descriptor.
1378 * \param handle is set on success. To be used by the client when requesting DMA
1379 * dispatch with drmDMA().
1381 * \return zero on success, or a negative value on failure.
1383 * \note May only be called by root.
1385 * \internal
1386 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1387 * argument in a drm_ctx structure.
1389 int drmCreateContext(int fd, drm_context_t *handle)
1391 drm_ctx_t ctx;
1393 ctx.flags = 0; /* Modified with functions below */
1394 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1395 return -errno;
1396 *handle = ctx.handle;
1397 return 0;
1400 int drmSwitchToContext(int fd, drm_context_t context)
1402 drm_ctx_t ctx;
1404 ctx.handle = context;
1405 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1406 return -errno;
1407 return 0;
1410 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1412 drm_ctx_t ctx;
1415 * Context preserving means that no context switches are done between DMA
1416 * buffers from one context and the next. This is suitable for use in the
1417 * X server (which promises to maintain hardware context), or in the
1418 * client-side library when buffers are swapped on behalf of two threads.
1420 ctx.handle = context;
1421 ctx.flags = 0;
1422 if (flags & DRM_CONTEXT_PRESERVED)
1423 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1424 if (flags & DRM_CONTEXT_2DONLY)
1425 ctx.flags |= _DRM_CONTEXT_2DONLY;
1426 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1427 return -errno;
1428 return 0;
1431 int drmGetContextFlags(int fd, drm_context_t context,
1432 drm_context_tFlagsPtr flags)
1434 drm_ctx_t ctx;
1436 ctx.handle = context;
1437 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1438 return -errno;
1439 *flags = 0;
1440 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1441 *flags |= DRM_CONTEXT_PRESERVED;
1442 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1443 *flags |= DRM_CONTEXT_2DONLY;
1444 return 0;
1448 * Destroy context.
1450 * Free any kernel-level resources allocated with drmCreateContext() associated
1451 * with the context.
1453 * \param fd file descriptor.
1454 * \param handle handle given by drmCreateContext().
1456 * \return zero on success, or a negative value on failure.
1458 * \note May only be called by root.
1460 * \internal
1461 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1462 * argument in a drm_ctx structure.
1464 int drmDestroyContext(int fd, drm_context_t handle)
1466 drm_ctx_t ctx;
1467 ctx.handle = handle;
1468 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1469 return -errno;
1470 return 0;
1473 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1475 drm_draw_t draw;
1476 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1477 return -errno;
1478 *handle = draw.handle;
1479 return 0;
1482 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1484 drm_draw_t draw;
1485 draw.handle = handle;
1486 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1487 return -errno;
1488 return 0;
1491 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1492 drm_drawable_info_type_t type, unsigned int num,
1493 void *data)
1495 drm_update_draw_t update;
1497 update.handle = handle;
1498 update.type = type;
1499 update.num = num;
1500 update.data = (unsigned long long)(unsigned long)data;
1502 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1503 return -errno;
1505 return 0;
1509 * Acquire the AGP device.
1511 * Must be called before any of the other AGP related calls.
1513 * \param fd file descriptor.
1515 * \return zero on success, or a negative value on failure.
1517 * \internal
1518 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1520 int drmAgpAcquire(int fd)
1522 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1523 return -errno;
1524 return 0;
1529 * Release the AGP device.
1531 * \param fd file descriptor.
1533 * \return zero on success, or a negative value on failure.
1535 * \internal
1536 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1538 int drmAgpRelease(int fd)
1540 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1541 return -errno;
1542 return 0;
1547 * Set the AGP mode.
1549 * \param fd file descriptor.
1550 * \param mode AGP mode.
1552 * \return zero on success, or a negative value on failure.
1554 * \internal
1555 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1556 * argument in a drm_agp_mode structure.
1558 int drmAgpEnable(int fd, unsigned long mode)
1560 drm_agp_mode_t m;
1562 m.mode = mode;
1563 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1564 return -errno;
1565 return 0;
1570 * Allocate a chunk of AGP memory.
1572 * \param fd file descriptor.
1573 * \param size requested memory size in bytes. Will be rounded to page boundary.
1574 * \param type type of memory to allocate.
1575 * \param address if not zero, will be set to the physical address of the
1576 * allocated memory.
1577 * \param handle on success will be set to a handle of the allocated memory.
1579 * \return zero on success, or a negative value on failure.
1581 * \internal
1582 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1583 * arguments in a drm_agp_buffer structure.
1585 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1586 unsigned long *address, drm_handle_t *handle)
1588 drm_agp_buffer_t b;
1590 *handle = DRM_AGP_NO_HANDLE;
1591 b.size = size;
1592 b.handle = 0;
1593 b.type = type;
1594 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1595 return -errno;
1596 if (address != 0UL)
1597 *address = b.physical;
1598 *handle = b.handle;
1599 return 0;
1604 * Free a chunk of AGP memory.
1606 * \param fd file descriptor.
1607 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1609 * \return zero on success, or a negative value on failure.
1611 * \internal
1612 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1613 * argument in a drm_agp_buffer structure.
1615 int drmAgpFree(int fd, drm_handle_t handle)
1617 drm_agp_buffer_t b;
1619 b.size = 0;
1620 b.handle = handle;
1621 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1622 return -errno;
1623 return 0;
1628 * Bind a chunk of AGP memory.
1630 * \param fd file descriptor.
1631 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1632 * \param offset offset in bytes. It will round to page boundary.
1634 * \return zero on success, or a negative value on failure.
1636 * \internal
1637 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1638 * argument in a drm_agp_binding structure.
1640 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1642 drm_agp_binding_t b;
1644 b.handle = handle;
1645 b.offset = offset;
1646 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1647 return -errno;
1648 return 0;
1653 * Unbind a chunk of AGP memory.
1655 * \param fd file descriptor.
1656 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1658 * \return zero on success, or a negative value on failure.
1660 * \internal
1661 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1662 * the argument in a drm_agp_binding structure.
1664 int drmAgpUnbind(int fd, drm_handle_t handle)
1666 drm_agp_binding_t b;
1668 b.handle = handle;
1669 b.offset = 0;
1670 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1671 return -errno;
1672 return 0;
1677 * Get AGP driver major version number.
1679 * \param fd file descriptor.
1681 * \return major version number on success, or a negative value on failure..
1683 * \internal
1684 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1685 * necessary information in a drm_agp_info structure.
1687 int drmAgpVersionMajor(int fd)
1689 drm_agp_info_t i;
1691 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1692 return -errno;
1693 return i.agp_version_major;
1698 * Get AGP driver minor version number.
1700 * \param fd file descriptor.
1702 * \return minor version number on success, or a negative value on failure.
1704 * \internal
1705 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1706 * necessary information in a drm_agp_info structure.
1708 int drmAgpVersionMinor(int fd)
1710 drm_agp_info_t i;
1712 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1713 return -errno;
1714 return i.agp_version_minor;
1719 * Get AGP mode.
1721 * \param fd file descriptor.
1723 * \return mode on success, or zero on failure.
1725 * \internal
1726 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1727 * necessary information in a drm_agp_info structure.
1729 unsigned long drmAgpGetMode(int fd)
1731 drm_agp_info_t i;
1733 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1734 return 0;
1735 return i.mode;
1740 * Get AGP aperture base.
1742 * \param fd file descriptor.
1744 * \return aperture base on success, zero on failure.
1746 * \internal
1747 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1748 * necessary information in a drm_agp_info structure.
1750 unsigned long drmAgpBase(int fd)
1752 drm_agp_info_t i;
1754 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1755 return 0;
1756 return i.aperture_base;
1761 * Get AGP aperture size.
1763 * \param fd file descriptor.
1765 * \return aperture size on success, zero on failure.
1767 * \internal
1768 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1769 * necessary information in a drm_agp_info structure.
1771 unsigned long drmAgpSize(int fd)
1773 drm_agp_info_t i;
1775 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1776 return 0;
1777 return i.aperture_size;
1782 * Get used AGP memory.
1784 * \param fd file descriptor.
1786 * \return memory used on success, or zero on failure.
1788 * \internal
1789 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1790 * necessary information in a drm_agp_info structure.
1792 unsigned long drmAgpMemoryUsed(int fd)
1794 drm_agp_info_t i;
1796 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1797 return 0;
1798 return i.memory_used;
1803 * Get available AGP memory.
1805 * \param fd file descriptor.
1807 * \return memory available on success, or zero on failure.
1809 * \internal
1810 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1811 * necessary information in a drm_agp_info structure.
1813 unsigned long drmAgpMemoryAvail(int fd)
1815 drm_agp_info_t i;
1817 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1818 return 0;
1819 return i.memory_allowed;
1824 * Get hardware vendor ID.
1826 * \param fd file descriptor.
1828 * \return vendor ID on success, or zero on failure.
1830 * \internal
1831 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1832 * necessary information in a drm_agp_info structure.
1834 unsigned int drmAgpVendorId(int fd)
1836 drm_agp_info_t i;
1838 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1839 return 0;
1840 return i.id_vendor;
1845 * Get hardware device ID.
1847 * \param fd file descriptor.
1849 * \return zero on success, or zero on failure.
1851 * \internal
1852 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1853 * necessary information in a drm_agp_info structure.
1855 unsigned int drmAgpDeviceId(int fd)
1857 drm_agp_info_t i;
1859 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1860 return 0;
1861 return i.id_device;
1864 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1866 drm_scatter_gather_t sg;
1868 *handle = 0;
1869 sg.size = size;
1870 sg.handle = 0;
1871 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1872 return -errno;
1873 *handle = sg.handle;
1874 return 0;
1877 int drmScatterGatherFree(int fd, drm_handle_t handle)
1879 drm_scatter_gather_t sg;
1881 sg.size = 0;
1882 sg.handle = handle;
1883 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
1884 return -errno;
1885 return 0;
1889 * Wait for VBLANK.
1891 * \param fd file descriptor.
1892 * \param vbl pointer to a drmVBlank structure.
1894 * \return zero on success, or a negative value on failure.
1896 * \internal
1897 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1899 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1901 struct timespec timeout, cur;
1902 int ret;
1904 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
1905 if (ret < 0) {
1906 fprintf(stderr, "clock_gettime failed: %s\n", strerror(ret));
1907 goto out;
1909 timeout.tv_sec++;
1911 do {
1912 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1913 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1914 if (ret && errno == EINTR) {
1915 clock_gettime(CLOCK_MONOTONIC, &cur);
1916 /* Timeout after 1s */
1917 if (cur.tv_sec > timeout.tv_sec + 1 ||
1918 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
1919 timeout.tv_nsec)) {
1920 errno = EBUSY;
1921 ret = -1;
1922 break;
1925 } while (ret && errno == EINTR);
1927 out:
1928 return ret;
1931 int drmError(int err, const char *label)
1933 switch (err) {
1934 case DRM_ERR_NO_DEVICE:
1935 fprintf(stderr, "%s: no device\n", label);
1936 break;
1937 case DRM_ERR_NO_ACCESS:
1938 fprintf(stderr, "%s: no access\n", label);
1939 break;
1940 case DRM_ERR_NOT_ROOT:
1941 fprintf(stderr, "%s: not root\n", label);
1942 break;
1943 case DRM_ERR_INVALID:
1944 fprintf(stderr, "%s: invalid args\n", label);
1945 break;
1946 default:
1947 if (err < 0)
1948 err = -err;
1949 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1950 break;
1953 return 1;
1957 * Install IRQ handler.
1959 * \param fd file descriptor.
1960 * \param irq IRQ number.
1962 * \return zero on success, or a negative value on failure.
1964 * \internal
1965 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1966 * argument in a drm_control structure.
1968 int drmCtlInstHandler(int fd, int irq)
1970 drm_control_t ctl;
1972 ctl.func = DRM_INST_HANDLER;
1973 ctl.irq = irq;
1974 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
1975 return -errno;
1976 return 0;
1981 * Uninstall IRQ handler.
1983 * \param fd file descriptor.
1985 * \return zero on success, or a negative value on failure.
1987 * \internal
1988 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1989 * argument in a drm_control structure.
1991 int drmCtlUninstHandler(int fd)
1993 drm_control_t ctl;
1995 ctl.func = DRM_UNINST_HANDLER;
1996 ctl.irq = 0;
1997 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
1998 return -errno;
1999 return 0;
2002 int drmFinish(int fd, int context, drmLockFlags flags)
2004 drm_lock_t lock;
2006 lock.context = context;
2007 lock.flags = 0;
2008 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2009 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2010 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2011 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2012 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2013 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2014 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2015 return -errno;
2016 return 0;
2020 * Get IRQ from bus ID.
2022 * \param fd file descriptor.
2023 * \param busnum bus number.
2024 * \param devnum device number.
2025 * \param funcnum function number.
2027 * \return IRQ number on success, or a negative value on failure.
2029 * \internal
2030 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2031 * arguments in a drm_irq_busid structure.
2033 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2035 drm_irq_busid_t p;
2037 p.busnum = busnum;
2038 p.devnum = devnum;
2039 p.funcnum = funcnum;
2040 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2041 return -errno;
2042 return p.irq;
2045 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2047 drmHashEntry *entry = drmGetEntry(fd);
2049 if (drmHashInsert(entry->tagTable, context, tag)) {
2050 drmHashDelete(entry->tagTable, context);
2051 drmHashInsert(entry->tagTable, context, tag);
2053 return 0;
2056 int drmDelContextTag(int fd, drm_context_t context)
2058 drmHashEntry *entry = drmGetEntry(fd);
2060 return drmHashDelete(entry->tagTable, context);
2063 void *drmGetContextTag(int fd, drm_context_t context)
2065 drmHashEntry *entry = drmGetEntry(fd);
2066 void *value;
2068 if (drmHashLookup(entry->tagTable, context, &value))
2069 return NULL;
2071 return value;
2074 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2075 drm_handle_t handle)
2077 drm_ctx_priv_map_t map;
2079 map.ctx_id = ctx_id;
2080 map.handle = (void *)handle;
2082 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2083 return -errno;
2084 return 0;
2087 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2088 drm_handle_t *handle)
2090 drm_ctx_priv_map_t map;
2092 map.ctx_id = ctx_id;
2094 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2095 return -errno;
2096 if (handle)
2097 *handle = (drm_handle_t)map.handle;
2099 return 0;
2102 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2103 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2104 int *mtrr)
2106 drm_map_t map;
2108 map.offset = idx;
2109 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2110 return -errno;
2111 *offset = map.offset;
2112 *size = map.size;
2113 *type = map.type;
2114 *flags = map.flags;
2115 *handle = (unsigned long)map.handle;
2116 *mtrr = map.mtrr;
2117 return 0;
2120 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2121 unsigned long *magic, unsigned long *iocs)
2123 drm_client_t client;
2125 client.idx = idx;
2126 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2127 return -errno;
2128 *auth = client.auth;
2129 *pid = client.pid;
2130 *uid = client.uid;
2131 *magic = client.magic;
2132 *iocs = client.iocs;
2133 return 0;
2136 int drmGetStats(int fd, drmStatsT *stats)
2138 drm_stats_t s;
2139 int i;
2141 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2142 return -errno;
2144 stats->count = 0;
2145 memset(stats, 0, sizeof(*stats));
2146 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2147 return -1;
2149 #define SET_VALUE \
2150 stats->data[i].long_format = "%-20.20s"; \
2151 stats->data[i].rate_format = "%8.8s"; \
2152 stats->data[i].isvalue = 1; \
2153 stats->data[i].verbose = 0
2155 #define SET_COUNT \
2156 stats->data[i].long_format = "%-20.20s"; \
2157 stats->data[i].rate_format = "%5.5s"; \
2158 stats->data[i].isvalue = 0; \
2159 stats->data[i].mult_names = "kgm"; \
2160 stats->data[i].mult = 1000; \
2161 stats->data[i].verbose = 0
2163 #define SET_BYTE \
2164 stats->data[i].long_format = "%-20.20s"; \
2165 stats->data[i].rate_format = "%5.5s"; \
2166 stats->data[i].isvalue = 0; \
2167 stats->data[i].mult_names = "KGM"; \
2168 stats->data[i].mult = 1024; \
2169 stats->data[i].verbose = 0
2172 stats->count = s.count;
2173 for (i = 0; i < s.count; i++) {
2174 stats->data[i].value = s.data[i].value;
2175 switch (s.data[i].type) {
2176 case _DRM_STAT_LOCK:
2177 stats->data[i].long_name = "Lock";
2178 stats->data[i].rate_name = "Lock";
2179 SET_VALUE;
2180 break;
2181 case _DRM_STAT_OPENS:
2182 stats->data[i].long_name = "Opens";
2183 stats->data[i].rate_name = "O";
2184 SET_COUNT;
2185 stats->data[i].verbose = 1;
2186 break;
2187 case _DRM_STAT_CLOSES:
2188 stats->data[i].long_name = "Closes";
2189 stats->data[i].rate_name = "Lock";
2190 SET_COUNT;
2191 stats->data[i].verbose = 1;
2192 break;
2193 case _DRM_STAT_IOCTLS:
2194 stats->data[i].long_name = "Ioctls";
2195 stats->data[i].rate_name = "Ioc/s";
2196 SET_COUNT;
2197 break;
2198 case _DRM_STAT_LOCKS:
2199 stats->data[i].long_name = "Locks";
2200 stats->data[i].rate_name = "Lck/s";
2201 SET_COUNT;
2202 break;
2203 case _DRM_STAT_UNLOCKS:
2204 stats->data[i].long_name = "Unlocks";
2205 stats->data[i].rate_name = "Unl/s";
2206 SET_COUNT;
2207 break;
2208 case _DRM_STAT_IRQ:
2209 stats->data[i].long_name = "IRQs";
2210 stats->data[i].rate_name = "IRQ/s";
2211 SET_COUNT;
2212 break;
2213 case _DRM_STAT_PRIMARY:
2214 stats->data[i].long_name = "Primary Bytes";
2215 stats->data[i].rate_name = "PB/s";
2216 SET_BYTE;
2217 break;
2218 case _DRM_STAT_SECONDARY:
2219 stats->data[i].long_name = "Secondary Bytes";
2220 stats->data[i].rate_name = "SB/s";
2221 SET_BYTE;
2222 break;
2223 case _DRM_STAT_DMA:
2224 stats->data[i].long_name = "DMA";
2225 stats->data[i].rate_name = "DMA/s";
2226 SET_COUNT;
2227 break;
2228 case _DRM_STAT_SPECIAL:
2229 stats->data[i].long_name = "Special DMA";
2230 stats->data[i].rate_name = "dma/s";
2231 SET_COUNT;
2232 break;
2233 case _DRM_STAT_MISSED:
2234 stats->data[i].long_name = "Miss";
2235 stats->data[i].rate_name = "Ms/s";
2236 SET_COUNT;
2237 break;
2238 case _DRM_STAT_VALUE:
2239 stats->data[i].long_name = "Value";
2240 stats->data[i].rate_name = "Value";
2241 SET_VALUE;
2242 break;
2243 case _DRM_STAT_BYTE:
2244 stats->data[i].long_name = "Bytes";
2245 stats->data[i].rate_name = "B/s";
2246 SET_BYTE;
2247 break;
2248 case _DRM_STAT_COUNT:
2249 default:
2250 stats->data[i].long_name = "Count";
2251 stats->data[i].rate_name = "Cnt/s";
2252 SET_COUNT;
2253 break;
2256 return 0;
2260 * Issue a set-version ioctl.
2262 * \param fd file descriptor.
2263 * \param drmCommandIndex command index
2264 * \param data source pointer of the data to be read and written.
2265 * \param size size of the data to be read and written.
2267 * \return zero on success, or a negative value on failure.
2269 * \internal
2270 * It issues a read-write ioctl given by
2271 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2273 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2275 int retcode = 0;
2276 drm_set_version_t sv;
2278 sv.drm_di_major = version->drm_di_major;
2279 sv.drm_di_minor = version->drm_di_minor;
2280 sv.drm_dd_major = version->drm_dd_major;
2281 sv.drm_dd_minor = version->drm_dd_minor;
2283 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2284 retcode = -errno;
2287 version->drm_di_major = sv.drm_di_major;
2288 version->drm_di_minor = sv.drm_di_minor;
2289 version->drm_dd_major = sv.drm_dd_major;
2290 version->drm_dd_minor = sv.drm_dd_minor;
2292 return retcode;
2296 * Send a device-specific command.
2298 * \param fd file descriptor.
2299 * \param drmCommandIndex command index
2301 * \return zero on success, or a negative value on failure.
2303 * \internal
2304 * It issues a ioctl given by
2305 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2307 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2309 void *data = NULL; /* dummy */
2310 unsigned long request;
2312 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2314 if (drmIoctl(fd, request, data)) {
2315 return -errno;
2317 return 0;
2322 * Send a device-specific read command.
2324 * \param fd file descriptor.
2325 * \param drmCommandIndex command index
2326 * \param data destination pointer of the data to be read.
2327 * \param size size of the data to be read.
2329 * \return zero on success, or a negative value on failure.
2331 * \internal
2332 * It issues a read ioctl given by
2333 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2335 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2336 unsigned long size)
2338 unsigned long request;
2340 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2341 DRM_COMMAND_BASE + drmCommandIndex, size);
2343 if (drmIoctl(fd, request, data)) {
2344 return -errno;
2346 return 0;
2351 * Send a device-specific write command.
2353 * \param fd file descriptor.
2354 * \param drmCommandIndex command index
2355 * \param data source pointer of the data to be written.
2356 * \param size size of the data to be written.
2358 * \return zero on success, or a negative value on failure.
2360 * \internal
2361 * It issues a write ioctl given by
2362 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2364 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2365 unsigned long size)
2367 unsigned long request;
2369 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2370 DRM_COMMAND_BASE + drmCommandIndex, size);
2372 if (drmIoctl(fd, request, data)) {
2373 return -errno;
2375 return 0;
2380 * Send a device-specific read-write command.
2382 * \param fd file descriptor.
2383 * \param drmCommandIndex command index
2384 * \param data source pointer of the data to be read and written.
2385 * \param size size of the data to be read and written.
2387 * \return zero on success, or a negative value on failure.
2389 * \internal
2390 * It issues a read-write ioctl given by
2391 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2393 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2394 unsigned long size)
2396 unsigned long request;
2398 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2399 DRM_COMMAND_BASE + drmCommandIndex, size);
2401 if (drmIoctl(fd, request, data))
2402 return -errno;
2403 return 0;
2406 #define DRM_MAX_FDS 16
2407 static struct {
2408 char *BusID;
2409 int fd;
2410 int refcount;
2411 } connection[DRM_MAX_FDS];
2413 static int nr_fds = 0;
2415 int drmOpenOnce(void *unused,
2416 const char *BusID,
2417 int *newlyopened)
2419 int i;
2420 int fd;
2422 for (i = 0; i < nr_fds; i++)
2423 if (strcmp(BusID, connection[i].BusID) == 0) {
2424 connection[i].refcount++;
2425 *newlyopened = 0;
2426 return connection[i].fd;
2429 fd = drmOpen(unused, BusID);
2430 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2431 return fd;
2433 connection[nr_fds].BusID = strdup(BusID);
2434 connection[nr_fds].fd = fd;
2435 connection[nr_fds].refcount = 1;
2436 *newlyopened = 1;
2438 if (0)
2439 fprintf(stderr, "saved connection %d for %s %d\n",
2440 nr_fds, connection[nr_fds].BusID,
2441 strcmp(BusID, connection[nr_fds].BusID));
2443 nr_fds++;
2445 return fd;
2448 void drmCloseOnce(int fd)
2450 int i;
2452 for (i = 0; i < nr_fds; i++) {
2453 if (fd == connection[i].fd) {
2454 if (--connection[i].refcount == 0) {
2455 drmClose(connection[i].fd);
2456 free(connection[i].BusID);
2458 if (i < --nr_fds)
2459 connection[i] = connection[nr_fds];
2461 return;
2467 int drmSetMaster(int fd)
2469 int ret;
2471 fprintf(stderr,"Setting master \n");
2472 ret = ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
2473 return ret;
2476 int drmDropMaster(int fd)
2478 int ret;
2479 fprintf(stderr,"Dropping master \n");
2480 ret = ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);
2481 return ret;