3 * \brief User-level interface to DRM device
5 * This file is an user-friendly interface to the DRM ioctls defined in drm.h.
7 * This covers only the device-independent ioctls -- it is up to the driver to
8 * wrap the device-dependent ioctls.
10 * \author Rickard E. (Rik) Faith <faith@valinux.com>
11 * \author Kevin E. Martin <martin@valinux.com>
15 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
16 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
17 * All Rights Reserved.
19 * Permission is hereby granted, free of charge, to any person obtaining a
20 * copy of this software and associated documentation files (the "Software"),
21 * to deal in the Software without restriction, including without limitation
22 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
23 * and/or sell copies of the Software, and to permit persons to whom the
24 * Software is furnished to do so, subject to the following conditions:
26 * The above copyright notice and this permission notice (including the next
27 * paragraph) shall be included in all copies or substantial portions of the
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
33 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
34 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
35 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
36 * DEALINGS IN THE SOFTWARE.
47 # include <sys/types.h>
48 # include <sys/stat.h>
49 # include <sys/ioctl.h>
50 # include <sys/mman.h>
51 # include <sys/time.h>
55 /* Not all systems have MAP_FAILED defined */
57 #define MAP_FAILED ((void *)-1)
63 #define DRM_MAJOR 226 /* Linux */
68 #define DRM_MAJOR 145 /* Should set in drm.h for *BSD */
72 #define DRM_MAX_MINOR 16
76 #include <sys/sysmacros.h> /* for makedev() */
80 /* This definition needs to be changed on
81 some systems if dev_t is a structure.
82 If there is a header file we can get it
83 from, there would be best. */
84 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
89 * \brief Output a message to stderr.
91 * \param format printf() like format string.
94 * This function is a wrapper around vfprintf().
97 drmMsg(const char *format
, ...)
102 if ((env
= getenv("LIBGL_DEBUG")) && strstr(env
, "verbose"))
104 va_start(ap
, format
);
105 vfprintf(stderr
, format
, ap
);
113 * \brief Open the DRM device, creating it if necessary.
115 * \param dev major and minor numbers of the device.
116 * \param minor minor number of the device.
118 * \return a file descriptor on success, or a negative value on error.
121 * Assembles the device name from \p minor and opens it, creating the device
122 * special file node with the major and minor numbers specified by \p dev and
123 * parent directory if necessary and was called by root.
125 static int drmOpenDevice(long dev
, int minor
)
130 mode_t devmode
= DRM_DEV_MODE
;
131 int isroot
= !geteuid();
132 #if defined(XFree86Server)
133 uid_t user
= DRM_DEV_UID
;
134 gid_t group
= DRM_DEV_GID
;
137 drmMsg("drmOpenDevice: minor is %d\n", minor
);
139 #if defined(XFree86Server)
140 devmode
= xf86ConfigDRI
.mode
? xf86ConfigDRI
.mode
: DRM_DEV_MODE
;
141 devmode
&= ~(S_IXUSR
|S_IXGRP
|S_IXOTH
);
142 group
= (xf86ConfigDRI
.group
>= 0) ? xf86ConfigDRI
.group
: DRM_DEV_GID
;
145 if (stat(DRM_DIR_NAME
, &st
)) {
146 if (!isroot
) return DRM_ERR_NOT_ROOT
;
147 mkdir(DRM_DIR_NAME
, DRM_DEV_DIRMODE
);
148 chown(DRM_DIR_NAME
, 0, 0); /* root:root */
149 chmod(DRM_DIR_NAME
, DRM_DEV_DIRMODE
);
152 sprintf(buf
, DRM_DEV_NAME
, DRM_DIR_NAME
, minor
);
153 drmMsg("drmOpenDevice: node name is %s\n", buf
);
154 if (stat(buf
, &st
)) {
155 if (!isroot
) return DRM_ERR_NOT_ROOT
;
157 mknod(buf
, S_IFCHR
| devmode
, dev
);
159 #if defined(XFree86Server)
160 chown(buf
, user
, group
);
164 fd
= open(buf
, O_RDWR
, 0);
165 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
166 fd
, fd
< 0 ? strerror(errno
) : "OK");
167 if (fd
>= 0) return fd
;
169 if (st
.st_rdev
!= dev
) {
170 if (!isroot
) return DRM_ERR_NOT_ROOT
;
172 mknod(buf
, S_IFCHR
| devmode
, dev
);
174 fd
= open(buf
, O_RDWR
, 0);
175 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
176 fd
, fd
< 0 ? strerror(errno
) : "OK");
177 if (fd
>= 0) return fd
;
179 drmMsg("drmOpenDevice: Open failed\n");
186 * \brief Open the DRM device
188 * \param minor device minor number.
189 * \param create allow to create the device if set.
191 * \return a file descriptor on success, or a negative value on error.
194 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
195 * name from \p minor and opens it.
197 static int drmOpenMinor(int minor
, int create
)
202 if (create
) return drmOpenDevice(makedev(DRM_MAJOR
, minor
), minor
);
204 sprintf(buf
, DRM_DEV_NAME
, DRM_DIR_NAME
, minor
);
205 if ((fd
= open(buf
, O_RDWR
, 0)) >= 0) return fd
;
206 drmMsg("drmOpenMinor: open result is %d, (%s)\n",
207 fd
, fd
< 0 ? strerror(errno
) : "OK");
213 * \brief Determine whether the DRM kernel driver has been loaded.
215 * \return 1 if the DRM driver is loaded, 0 otherwise.
218 * Determine the presence of the kernel driver by attempting to open the 0
219 * minor and get version information. For backward compatibility with older
220 * Linux implementations, /proc/dri is also checked.
222 int drmAvailable(void)
224 drmVersionPtr version
;
228 if ((fd
= drmOpenMinor(0, 1)) < 0) {
229 /* Try proc for backward Linux compatibility */
230 if (!access("/proc/dri/0", R_OK
)) return 1;
234 if ((version
= drmGetVersion(fd
))) {
236 drmFreeVersion(version
);
239 drmMsg("close %d\n", fd
);
246 * \brief Open the device by bus ID.
248 * \param busid bus ID.
250 * \return a file descriptor on success, or a negative value on error.
253 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
254 * comparing the device bus ID with the one supplied.
256 * \sa drmOpenMinor() and drmGetBusid().
258 static int drmOpenByBusid(const char *busid
)
264 drmMsg("drmOpenByBusid: busid is %s\n", busid
);
265 for (i
= 0; i
< DRM_MAX_MINOR
; i
++) {
266 fd
= drmOpenMinor(i
, 1);
267 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd
);
269 buf
= drmGetBusid(fd
);
270 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf
);
271 if (buf
&& !strcmp(buf
, busid
)) {
275 if (buf
) drmFreeBusid(buf
);
277 drmMsg("close %d\n", fd
);
285 * \brief Open the device by name.
287 * \param name driver name.
289 * \return a file descriptor on success, or a negative value on error.
292 * This function opens the first minor number that matches the driver name and
293 * isn't already in use. If it's in use it then it will already have a bus ID
296 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
298 static int drmOpenByName(const char *name
)
302 drmVersionPtr version
;
305 if (!drmAvailable()) {
306 #if !defined(XFree86Server)
309 /* try to load the kernel module now */
310 if (!xf86LoadKernelModule(name
)) {
311 ErrorF("[drm] failed to load kernel module \"%s\"\n",
318 for (i
= 0; i
< DRM_MAX_MINOR
; i
++) {
319 if ((fd
= drmOpenMinor(i
, 1)) >= 0) {
320 if ((version
= drmGetVersion(fd
))) {
321 if (!strcmp(version
->name
, name
)) {
322 drmFreeVersion(version
);
326 id
= drmGetBusid(fd
);
327 drmMsg("drmGetBusid returned '%s'\n", id
? id
: "NULL");
337 drmFreeVersion(version
);
341 drmMsg("close %d\n", fd
);
350 * \brief Open the DRM device.
352 * Looks up the specified name and bus ID, and opens the device found. The
353 * entry in /dev/dri is created if necessary and if called by root.
355 * \param name driver name. Not referenced if bus ID is supplied.
356 * \param busid bus ID. Zero if not known.
358 * \return a file descriptor on success, or a negative value on error.
361 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
364 int drmOpen(const char *name
, const char *busid
)
367 if (busid
) return drmOpenByBusid(busid
);
368 return drmOpenByName(name
);
373 * \brief Free the version information returned by drmGetVersion().
375 * \param v pointer to the version information.
378 * It frees the memory pointed by \p %v as well as all the non-null strings
381 void drmFreeVersion(drmVersionPtr v
)
384 if (v
->name
) free(v
->name
);
385 if (v
->date
) free(v
->date
);
386 if (v
->desc
) free(v
->desc
);
392 * \brief Free the non-public version information returned by the kernel.
394 * \param v pointer to the version information.
397 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
398 * the non-null strings pointers in it.
400 static void drmFreeKernelVersion(drm_version_t
*v
)
403 if (v
->name
) free(v
->name
);
404 if (v
->date
) free(v
->date
);
405 if (v
->desc
) free(v
->desc
);
411 * \brief Copy version information.
413 * \param d destination pointer.
414 * \param s source pointer.
417 * Used by drmGetVersion() to translate the information returned by the ioctl
418 * interface in a private structure into the public structure counterpart.
420 static void drmCopyVersion(drmVersionPtr d
, const drm_version_t
*s
)
422 d
->version_major
= s
->version_major
;
423 d
->version_minor
= s
->version_minor
;
424 d
->version_patchlevel
= s
->version_patchlevel
;
425 d
->name_len
= s
->name_len
;
426 d
->name
= strdup(s
->name
);
427 d
->date_len
= s
->date_len
;
428 d
->date
= strdup(s
->date
);
429 d
->desc_len
= s
->desc_len
;
430 d
->desc
= strdup(s
->desc
);
435 * \brief Query the driver version information.
437 * \param fd file descriptor.
439 * \return pointer to a drmVersion structure which should be freed with
442 * \note Similar information is available via /proc/dri.
445 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
446 * first with zeros to get the string lengths, and then the actually strings.
447 * It also null-terminates them since they might not be already.
449 drmVersionPtr
drmGetVersion(int fd
)
451 drmVersionPtr retval
;
452 drm_version_t
*version
= malloc(sizeof(*version
));
454 /* First, get the lengths */
455 version
->name_len
= 0;
456 version
->name
= NULL
;
457 version
->date_len
= 0;
458 version
->date
= NULL
;
459 version
->desc_len
= 0;
460 version
->desc
= NULL
;
462 if (ioctl(fd
, DRM_IOCTL_VERSION
, version
)) {
463 drmFreeKernelVersion(version
);
467 /* Now, allocate space and get the data */
468 if (version
->name_len
)
469 version
->name
= malloc(version
->name_len
+ 1);
470 if (version
->date_len
)
471 version
->date
= malloc(version
->date_len
+ 1);
472 if (version
->desc_len
)
473 version
->desc
= malloc(version
->desc_len
+ 1);
475 if (ioctl(fd
, DRM_IOCTL_VERSION
, version
)) {
476 drmFreeKernelVersion(version
);
480 /* The results might not be null-terminated
481 strings, so terminate them. */
483 if (version
->name_len
) version
->name
[version
->name_len
] = '\0';
484 if (version
->date_len
) version
->date
[version
->date_len
] = '\0';
485 if (version
->desc_len
) version
->desc
[version
->desc_len
] = '\0';
487 /* Now, copy it all back into the
488 client-visible data structure... */
489 retval
= malloc(sizeof(*retval
));
490 drmCopyVersion(retval
, version
);
491 drmFreeKernelVersion(version
);
497 * \brief Get version information for the DRM user space library.
499 * This version number is driver independent.
501 * \param fd file descriptor.
503 * \return version information.
506 * This function allocates and fills a drm_version structure with a hard coded
509 drmVersionPtr
drmGetLibVersion(int fd
)
511 drm_version_t
*version
= malloc(sizeof(*version
));
514 * revision 1.0.x = original DRM interface with no drmGetLibVersion
515 * entry point and many drm<Device> extensions
516 * revision 1.1.x = added drmCommand entry points for device extensions
517 * added drmGetLibVersion to identify libdrm.a version
519 version
->version_major
= 1;
520 version
->version_minor
= 1;
521 version
->version_patchlevel
= 0;
523 return (drmVersionPtr
)version
;
528 * \brief Free the bus ID information.
530 * \param busid bus ID information string as given by drmGetBusid().
533 * This function is just frees the memory pointed by \p busid.
535 void drmFreeBusid(const char *busid
)
542 * \brief Get the bus ID of the device.
544 * \param fd file descriptor.
546 * \return bus ID string.
549 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
550 * get the string length and data, passing the arguments in a drm_unique
553 char *drmGetBusid(int fd
)
560 if (ioctl(fd
, DRM_IOCTL_GET_UNIQUE
, &u
)) return NULL
;
561 u
.unique
= malloc(u
.unique_len
+ 1);
562 if (ioctl(fd
, DRM_IOCTL_GET_UNIQUE
, &u
)) return NULL
;
563 u
.unique
[u
.unique_len
] = '\0';
569 * \brief Set the bus ID of the device.
571 * \param fd file descriptor.
572 * \param busid bus ID string.
574 * \return zero on success, negative on failure.
577 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
578 * the arguments in a drm_unique structure.
580 int drmSetBusid(int fd
, const char *busid
)
584 u
.unique
= (char *)busid
;
585 u
.unique_len
= strlen(busid
);
587 if (ioctl(fd
, DRM_IOCTL_SET_UNIQUE
, &u
)) {
595 * \brief Specifies a range of memory that is available for mapping by a
598 * \param fd file descriptor.
599 * \param offset usually the physical address. The actual meaning depends of
600 * the \p type parameter. See below.
601 * \param size of the memory in bytes.
602 * \param type type of the memory to be mapped.
603 * \param flags combination of several flags to modify the function actions.
604 * \param handle will be set to a value that may be used as the offset
605 * parameter for mmap().
607 * \return zero on success or a negative value on error.
609 * \par Mapping the frame buffer
610 * For the frame buffer
611 * - \p offset will be the physical address of the start of the frame buffer,
612 * - \p size will be the size of the frame buffer in bytes, and
613 * - \p type will be DRM_FRAME_BUFFER.
616 * The area mapped will be uncached. If MTRR support is available in the
617 * kernel, the frame buffer area will be set to write combining.
619 * \par Mapping the MMIO register area
620 * For the MMIO register area,
621 * - \p offset will be the physical address of the start of the register area,
622 * - \p size will be the size of the register area bytes, and
623 * - \p type will be DRM_REGISTERS.
625 * The area mapped will be uncached.
627 * \par Mapping the SAREA
629 * - \p offset will be ignored and should be set to zero,
630 * - \p size will be the desired size of the SAREA in bytes,
631 * - \p type will be DRM_SHM.
634 * A shared memory area of the requested size will be created and locked in
635 * kernel memory. This area may be mapped into client-space by using the handle
638 * \note May only be called by root.
641 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
642 * the arguments in a drm_map structure.
644 int drmAddMap(int fd
,
658 if (ioctl(fd
, DRM_IOCTL_ADD_MAP
, &map
)) return -errno
;
659 if (handle
) *handle
= (drmHandle
)map
.handle
;
665 * \brief Make buffers available for DMA transfers.
667 * \param fd file descriptor.
668 * \param count number of buffers.
669 * \param size size of each buffer.
670 * \param flags buffer allocation flags.
671 * \param agp_offset offset in the AGP aperture
673 * \return number of buffers allocated, negative on error.
676 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
680 int drmAddBufs(int fd
, int count
, int size
, drmBufDescFlags flags
,
683 drm_buf_desc_t request
;
685 request
.count
= count
;
687 request
.low_mark
= 0;
688 request
.high_mark
= 0;
689 request
.flags
= flags
;
690 request
.agp_start
= agp_offset
;
692 if (ioctl(fd
, DRM_IOCTL_ADD_BUFS
, &request
)) return -errno
;
693 return request
.count
;
698 * \brief Free buffers.
700 * \param fd file descriptor.
701 * \param count number of buffers to free.
702 * \param list list of buffers to be freed.
704 * \return zero on success, or a negative value on failure.
706 * \note This function is primarily used for debugging.
709 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
710 * the arguments in a drm_buf_free structure.
712 int drmFreeBufs(int fd
, int count
, int *list
)
714 drm_buf_free_t request
;
716 request
.count
= count
;
718 if (ioctl(fd
, DRM_IOCTL_FREE_BUFS
, &request
)) return -errno
;
724 * \brief Close the device.
726 * \param fd file descriptor.
729 * This function closes the file descriptor.
733 drmMsg("close %d\n", fd
);
739 * \brief Map a region of memory.
741 * \param fd file descriptor.
742 * \param handle handle returned by drmAddMap().
743 * \param size size in bytes. Must match the size used by drmAddMap().
744 * \param address will contain the user-space virtual address where the mapping
747 * \return zero on success, or a negative value on failure.
750 * This function is a wrapper for mmap().
755 drmAddressPtr address
)
757 static unsigned long pagesize_mask
= 0;
759 if (fd
< 0) return -EINVAL
;
762 pagesize_mask
= getpagesize() - 1;
764 size
= (size
+ pagesize_mask
) & ~pagesize_mask
;
766 *address
= mmap(0, size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, handle
);
767 if (*address
== MAP_FAILED
) return -errno
;
773 * \brief Unmap mappings obtained with drmMap().
775 * \param address address as given by drmMap().
776 * \param size size in bytes. Must match the size used by drmMap().
778 * \return zero on success, or a negative value on failure.
781 * This function is a wrapper for unmap().
783 int drmUnmap(drmAddress address
, drmSize size
)
785 return munmap(address
, size
);
790 * \brief Map all DMA buffers into client-virtual space.
792 * \param fd file descriptor.
794 * \return a pointer to a ::drmBufMap structure.
796 * \note The client may not use these buffers until obtaining buffer indices
800 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
801 * information about the buffers in a drm_buf_map structure into the
802 * client-visible data structures.
804 drmBufMapPtr
drmMapBufs(int fd
)
812 if (ioctl(fd
, DRM_IOCTL_MAP_BUFS
, &bufs
)) return NULL
;
815 if (!(bufs
.list
= malloc(bufs
.count
* sizeof(*bufs
.list
))))
818 if (ioctl(fd
, DRM_IOCTL_MAP_BUFS
, &bufs
)) {
822 /* Now, copy it all back into the
823 client-visible data structures... */
824 retval
= malloc(sizeof(*retval
));
825 retval
->count
= bufs
.count
;
826 retval
->list
= malloc(bufs
.count
* sizeof(*retval
->list
));
827 for (i
= 0; i
< bufs
.count
; i
++) {
828 retval
->list
[i
].idx
= bufs
.list
[i
].idx
;
829 retval
->list
[i
].total
= bufs
.list
[i
].total
;
830 retval
->list
[i
].used
= 0;
831 retval
->list
[i
].address
= bufs
.list
[i
].address
;
840 * \brief Unmap buffers allocated with drmMapBufs().
842 * \return zero on success, or negative value on failure.
845 * Calls munmap() for every buffer stored in \p bufs.
847 int drmUnmapBufs(drmBufMapPtr bufs
)
851 for (i
= 0; i
< bufs
->count
; i
++) {
852 munmap(bufs
->list
[i
].address
, bufs
->list
[i
].total
);
858 #define DRM_DMA_RETRY 16
861 * \brief Reserve DMA buffers.
863 * \param fd file descriptor.
866 * \return zero on success, or a negative value on failure.
869 * Assemble the arguments into a drm_dma structure and keeps issuing the
870 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
872 int drmDMA(int fd
, drmDMAReqPtr request
)
877 /* Copy to hidden structure */
878 dma
.context
= request
->context
;
879 dma
.send_count
= request
->send_count
;
880 dma
.send_indices
= request
->send_list
;
881 dma
.send_sizes
= request
->send_sizes
;
882 dma
.flags
= request
->flags
;
883 dma
.request_count
= request
->request_count
;
884 dma
.request_size
= request
->request_size
;
885 dma
.request_indices
= request
->request_list
;
886 dma
.request_sizes
= request
->request_sizes
;
889 ret
= ioctl( fd
, DRM_IOCTL_DMA
, &dma
);
890 } while ( ret
&& errno
== EAGAIN
&& i
++ < DRM_DMA_RETRY
);
893 request
->granted_count
= dma
.granted_count
;
902 * \brief Obtain heavyweight hardware lock.
904 * \param fd file descriptor.
905 * \param context context.
906 * \param flags flags that determine the sate of the hardware when the function
909 * \return always zero.
912 * This function translates the arguments into a drm_lock structure and issue
913 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
915 int drmGetLock(int fd
, drmContext context
, drmLockFlags flags
)
919 lock
.context
= context
;
921 if (flags
& DRM_LOCK_READY
) lock
.flags
|= _DRM_LOCK_READY
;
922 if (flags
& DRM_LOCK_QUIESCENT
) lock
.flags
|= _DRM_LOCK_QUIESCENT
;
923 if (flags
& DRM_LOCK_FLUSH
) lock
.flags
|= _DRM_LOCK_FLUSH
;
924 if (flags
& DRM_LOCK_FLUSH_ALL
) lock
.flags
|= _DRM_LOCK_FLUSH_ALL
;
925 if (flags
& DRM_HALT_ALL_QUEUES
) lock
.flags
|= _DRM_HALT_ALL_QUEUES
;
926 if (flags
& DRM_HALT_CUR_QUEUES
) lock
.flags
|= _DRM_HALT_CUR_QUEUES
;
928 while (ioctl(fd
, DRM_IOCTL_LOCK
, &lock
))
933 static void (*drm_unlock_callback
)( void ) = 0;
936 * \brief Release the hardware lock.
938 * \param fd file descriptor.
939 * \param context context.
941 * \return zero on success, or a negative value on failure.
944 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
945 * argument in a drm_lock structure.
947 int drmUnlock(int fd
, drmContext context
)
952 lock
.context
= context
;
954 ret
= ioctl(fd
, DRM_IOCTL_UNLOCK
, &lock
);
956 /* Need this to synchronize vt releasing. Could also teach fbdev
957 * about the drm lock...
959 if (drm_unlock_callback
) {
960 drm_unlock_callback();
968 * \brief Create context.
970 * Used by the X server during GLXContext initialization. This causes
971 * per-context kernel-level resources to be allocated.
973 * \param fd file descriptor.
974 * \param handle is set on success. To be used by the client when requesting DMA
975 * dispatch with drmDMA().
977 * \return zero on success, or a negative value on failure.
979 * \note May only be called by root.
982 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
983 * argument in a drm_ctx structure.
985 int drmCreateContext(int fd
, drmContextPtr handle
)
989 ctx
.flags
= 0; /* Modified with functions below */
990 if (ioctl(fd
, DRM_IOCTL_ADD_CTX
, &ctx
)) return -errno
;
991 *handle
= ctx
.handle
;
997 * \brief Destroy context.
999 * Free any kernel-level resources allocated with drmCreateContext() associated
1002 * \param fd file descriptor.
1003 * \param handle handle given by drmCreateContext().
1005 * \return zero on success, or a negative value on failure.
1007 * \note May only be called by root.
1010 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1011 * argument in a drm_ctx structure.
1013 int drmDestroyContext(int fd
, drmContext handle
)
1016 ctx
.handle
= handle
;
1017 if (ioctl(fd
, DRM_IOCTL_RM_CTX
, &ctx
)) return -errno
;
1023 * \brief Acquire the AGP device.
1025 * Must be called before any of the other AGP related calls.
1027 * \param fd file descriptor.
1029 * \return zero on success, or a negative value on failure.
1032 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1034 int drmAgpAcquire(int fd
)
1036 if (ioctl(fd
, DRM_IOCTL_AGP_ACQUIRE
, NULL
)) return -errno
;
1042 * \brief Release the AGP device.
1044 * \param fd file descriptor.
1046 * \return zero on success, or a negative value on failure.
1049 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1051 int drmAgpRelease(int fd
)
1053 if (ioctl(fd
, DRM_IOCTL_AGP_RELEASE
, NULL
)) return -errno
;
1059 * \brief Set the AGP mode.
1061 * \param fd file descriptor.
1062 * \param mode AGP mode.
1064 * \return zero on success, or a negative value on failure.
1067 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1068 * argument in a drm_agp_mode structure.
1070 int drmAgpEnable(int fd
, unsigned long mode
)
1075 if (ioctl(fd
, DRM_IOCTL_AGP_ENABLE
, &m
)) return -errno
;
1081 * \brief Allocate a chunk of AGP memory.
1083 * \param fd file descriptor.
1084 * \param size requested memory size in bytes. Will be rounded to page boundary.
1085 * \param type type of memory to allocate.
1086 * \param address if not zero, will be set to the physical address of the
1088 * \param handle on success will be set to a handle of the allocated memory.
1090 * \return zero on success, or a negative value on failure.
1093 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1094 * arguments in a drm_agp_buffer structure.
1096 int drmAgpAlloc(int fd
, unsigned long size
, unsigned long type
,
1097 unsigned long *address
, unsigned long *handle
)
1104 if (ioctl(fd
, DRM_IOCTL_AGP_ALLOC
, &b
)) return -errno
;
1105 if (address
!= 0UL) *address
= b
.physical
;
1112 * \brief Free a chunk of AGP memory.
1114 * \param fd file descriptor.
1115 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1117 * \return zero on success, or a negative value on failure.
1120 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1121 * argument in a drm_agp_buffer structure.
1123 int drmAgpFree(int fd
, unsigned long handle
)
1129 if (ioctl(fd
, DRM_IOCTL_AGP_FREE
, &b
)) return -errno
;
1135 * \brief Bind a chunk of AGP memory.
1137 * \param fd file descriptor.
1138 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1139 * \param offset offset in bytes. It will round to page boundary.
1141 * \return zero on success, or a negative value on failure.
1144 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1145 * argument in a drm_agp_binding structure.
1147 int drmAgpBind(int fd
, unsigned long handle
, unsigned long offset
)
1149 drm_agp_binding_t b
;
1153 if (ioctl(fd
, DRM_IOCTL_AGP_BIND
, &b
)) return -errno
;
1159 * \brief Unbind a chunk of AGP memory.
1161 * \param fd file descriptor.
1162 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1164 * \return zero on success, or a negative value on failure.
1167 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1168 * the argument in a drm_agp_binding structure.
1170 int drmAgpUnbind(int fd
, unsigned long handle
)
1172 drm_agp_binding_t b
;
1176 if (ioctl(fd
, DRM_IOCTL_AGP_UNBIND
, &b
)) return -errno
;
1182 * \brief Get AGP driver major version number.
1184 * \param fd file descriptor.
1186 * \return major version number on success, or a negative value on failure..
1189 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1190 * necessary information in a drm_agp_info structure.
1192 int drmAgpVersionMajor(int fd
)
1196 if (ioctl(fd
, DRM_IOCTL_AGP_INFO
, &i
)) return -errno
;
1197 return i
.agp_version_major
;
1202 * \brief Get AGP driver minor version number.
1204 * \param fd file descriptor.
1206 * \return minor version number on success, or a negative value on failure.
1209 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1210 * necessary information in a drm_agp_info structure.
1212 int drmAgpVersionMinor(int fd
)
1216 if (ioctl(fd
, DRM_IOCTL_AGP_INFO
, &i
)) return -errno
;
1217 return i
.agp_version_minor
;
1222 * \brief Get AGP mode.
1224 * \param fd file descriptor.
1226 * \return mode on success, or zero on failure.
1229 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1230 * necessary information in a drm_agp_info structure.
1232 unsigned long drmAgpGetMode(int fd
)
1236 if (ioctl(fd
, DRM_IOCTL_AGP_INFO
, &i
)) return 0;
1242 * \brief Get AGP aperture base.
1244 * \param fd file descriptor.
1246 * \return aperture base on success, zero on failure.
1249 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1250 * necessary information in a drm_agp_info structure.
1252 unsigned long drmAgpBase(int fd
)
1256 if (ioctl(fd
, DRM_IOCTL_AGP_INFO
, &i
)) return 0;
1257 return i
.aperture_base
;
1262 * \brief Get AGP aperture size.
1264 * \param fd file descriptor.
1266 * \return aperture size on success, zero on failure.
1269 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1270 * necessary information in a drm_agp_info structure.
1272 unsigned long drmAgpSize(int fd
)
1276 if (ioctl(fd
, DRM_IOCTL_AGP_INFO
, &i
)) return 0;
1277 return i
.aperture_size
;
1282 * \brief Get used AGP memory.
1284 * \param fd file descriptor.
1286 * \return memory used on success, or zero on failure.
1289 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1290 * necessary information in a drm_agp_info structure.
1292 unsigned long drmAgpMemoryUsed(int fd
)
1296 if (ioctl(fd
, DRM_IOCTL_AGP_INFO
, &i
)) return 0;
1297 return i
.memory_used
;
1302 * \brief Get available AGP memory.
1304 * \param fd file descriptor.
1306 * \return memory available on success, or zero on failure.
1309 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1310 * necessary information in a drm_agp_info structure.
1312 unsigned long drmAgpMemoryAvail(int fd
)
1316 if (ioctl(fd
, DRM_IOCTL_AGP_INFO
, &i
)) return 0;
1317 return i
.memory_allowed
;
1322 * \brief Get hardware vendor ID.
1324 * \param fd file descriptor.
1326 * \return vendor ID on success, or zero on failure.
1329 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1330 * necessary information in a drm_agp_info structure.
1332 unsigned int drmAgpVendorId(int fd
)
1336 if (ioctl(fd
, DRM_IOCTL_AGP_INFO
, &i
)) return 0;
1342 * \brief Get hardware device ID.
1344 * \param fd file descriptor.
1346 * \return zero on success, or zero on failure.
1349 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1350 * necessary information in a drm_agp_info structure.
1352 unsigned int drmAgpDeviceId(int fd
)
1356 if (ioctl(fd
, DRM_IOCTL_AGP_INFO
, &i
)) return 0;
1360 int drmScatterGatherAlloc(int fd
, unsigned long size
, unsigned long *handle
)
1362 drm_scatter_gather_t sg
;
1367 if (ioctl(fd
, DRM_IOCTL_SG_ALLOC
, &sg
)) return -errno
;
1368 *handle
= sg
.handle
;
1372 int drmScatterGatherFree(int fd
, unsigned long handle
)
1374 drm_scatter_gather_t sg
;
1378 if (ioctl(fd
, DRM_IOCTL_SG_FREE
, &sg
)) return -errno
;
1383 * \brief Wait for VBLANK.
1385 * \param fd file descriptor.
1386 * \param vbl pointer to a drmVBlank structure.
1388 * \return zero on success, or a negative value on failure.
1391 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1393 int drmWaitVBlank(int fd
, drmVBlankPtr vbl
)
1398 ret
= ioctl(fd
, DRM_IOCTL_WAIT_VBLANK
, vbl
);
1399 } while (ret
&& errno
== EINTR
);
1406 * \brief Install IRQ handler.
1408 * \param fd file descriptor.
1409 * \param irq IRQ number.
1411 * \return zero on success, or a negative value on failure.
1414 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1415 * argument in a drm_control structure.
1417 int drmCtlInstHandler(int fd
, int irq
)
1421 ctl
.func
= DRM_INST_HANDLER
;
1423 if (ioctl(fd
, DRM_IOCTL_CONTROL
, &ctl
)) return -errno
;
1429 * \brief Uninstall IRQ handler.
1431 * \param fd file descriptor.
1433 * \return zero on success, or a negative value on failure.
1436 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1437 * argument in a drm_control structure.
1439 int drmCtlUninstHandler(int fd
)
1443 ctl
.func
= DRM_UNINST_HANDLER
;
1445 if (ioctl(fd
, DRM_IOCTL_CONTROL
, &ctl
)) return -errno
;
1451 * \brief Get IRQ from bus ID.
1453 * \param fd file descriptor.
1454 * \param busnum bus number.
1455 * \param devnum device number.
1456 * \param funcnum function number.
1458 * \return IRQ number on success, or a negative value on failure.
1461 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
1462 * arguments in a drm_irq_busid structure.
1464 int drmGetInterruptFromBusID(int fd
, int busnum
, int devnum
, int funcnum
)
1470 p
.funcnum
= funcnum
;
1471 if (ioctl(fd
, DRM_IOCTL_IRQ_BUSID
, &p
)) return -errno
;
1477 * \brief Send a device-specific command.
1479 * \param fd file descriptor.
1480 * \param drmCommandIndex command index
1482 * \return zero on success, or a negative value on failure.
1485 * It issues a ioctl given by
1486 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
1488 int drmCommandNone(int fd
, unsigned long drmCommandIndex
)
1490 void *data
= NULL
; /* dummy */
1491 unsigned long request
;
1493 request
= DRM_IO( DRM_COMMAND_BASE
+ drmCommandIndex
);
1495 if (ioctl(fd
, request
, data
)) {
1503 * \brief Send a device-specific read command.
1505 * \param fd file descriptor.
1506 * \param drmCommandIndex command index
1507 * \param data destination pointer of the data to be read.
1508 * \param size size of the data to be read.
1510 * \return zero on success, or a negative value on failure.
1513 * It issues a read ioctl given by
1514 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
1516 int drmCommandRead(int fd
, unsigned long drmCommandIndex
,
1517 void *data
, unsigned long size
)
1519 unsigned long request
;
1521 request
= DRM_IOC( DRM_IOC_READ
, DRM_IOCTL_BASE
,
1522 DRM_COMMAND_BASE
+ drmCommandIndex
, size
);
1524 if (ioctl(fd
, request
, data
)) {
1532 * \brief Send a device-specific write command.
1534 * \param fd file descriptor.
1535 * \param drmCommandIndex command index
1536 * \param data source pointer of the data to be written.
1537 * \param size size of the data to be written.
1539 * \return zero on success, or a negative value on failure.
1542 * It issues a write ioctl given by
1543 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
1545 int drmCommandWrite(int fd
, unsigned long drmCommandIndex
,
1546 void *data
, unsigned long size
)
1548 unsigned long request
;
1550 request
= DRM_IOC( DRM_IOC_WRITE
, DRM_IOCTL_BASE
,
1551 DRM_COMMAND_BASE
+ drmCommandIndex
, size
);
1553 if (ioctl(fd
, request
, data
)) {
1561 * \brief Send a device-specific read-write command.
1563 * \param fd file descriptor.
1564 * \param drmCommandIndex command index
1565 * \param data source pointer of the data to be read and written.
1566 * \param size size of the data to be read and written.
1568 * \return zero on success, or a negative value on failure.
1571 * It issues a read-write ioctl given by
1572 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
1574 int drmCommandWriteRead(int fd
, unsigned long drmCommandIndex
,
1575 void *data
, unsigned long size
)
1577 unsigned long request
;
1579 request
= DRM_IOC( DRM_IOC_READ
|DRM_IOC_WRITE
, DRM_IOCTL_BASE
,
1580 DRM_COMMAND_BASE
+ drmCommandIndex
, size
);
1582 if (ioctl(fd
, request
, data
)) {