Home | History | Annotate | Download | only in libdrm
      1 /**
      2  * \file xf86drm.h
      3  * OS-independent header for DRM user-level library interface.
      4  *
      5  * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
      6  */
      7 
      8 /*
      9  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
     10  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
     11  * All Rights Reserved.
     12  *
     13  * Permission is hereby granted, free of charge, to any person obtaining a
     14  * copy of this software and associated documentation files (the "Software"),
     15  * to deal in the Software without restriction, including without limitation
     16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     17  * and/or sell copies of the Software, and to permit persons to whom the
     18  * Software is furnished to do so, subject to the following conditions:
     19  *
     20  * The above copyright notice and this permission notice (including the next
     21  * paragraph) shall be included in all copies or substantial portions of the
     22  * Software.
     23  *
     24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     27  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     28  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     29  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     30  * DEALINGS IN THE SOFTWARE.
     31  *
     32  */
     33 
     34 #ifndef _XF86DRM_H_
     35 #define _XF86DRM_H_
     36 
     37 #include <stdarg.h>
     38 #include <sys/types.h>
     39 #include <stdint.h>
     40 #include <drm.h>
     41 
     42 #if defined(__cplusplus)
     43 extern "C" {
     44 #endif
     45 
     46 #ifndef DRM_MAX_MINOR
     47 #define DRM_MAX_MINOR   16
     48 #endif
     49 
     50 #if defined(__linux__)
     51 
     52 #define DRM_IOCTL_NR(n)		_IOC_NR(n)
     53 #define DRM_IOC_VOID		_IOC_NONE
     54 #define DRM_IOC_READ		_IOC_READ
     55 #define DRM_IOC_WRITE		_IOC_WRITE
     56 #define DRM_IOC_READWRITE	_IOC_READ|_IOC_WRITE
     57 #define DRM_IOC(dir, group, nr, size) _IOC(dir, group, nr, size)
     58 
     59 #else /* One of the *BSDs */
     60 
     61 #include <sys/ioccom.h>
     62 #define DRM_IOCTL_NR(n)         ((n) & 0xff)
     63 #define DRM_IOC_VOID            IOC_VOID
     64 #define DRM_IOC_READ            IOC_OUT
     65 #define DRM_IOC_WRITE           IOC_IN
     66 #define DRM_IOC_READWRITE       IOC_INOUT
     67 #define DRM_IOC(dir, group, nr, size) _IOC(dir, group, nr, size)
     68 
     69 #endif
     70 
     71 				/* Defaults, if nothing set in xf86config */
     72 #define DRM_DEV_UID	 0
     73 #define DRM_DEV_GID	 0
     74 /* Default /dev/dri directory permissions 0755 */
     75 #define DRM_DEV_DIRMODE	 	\
     76 	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
     77 #define DRM_DEV_MODE	 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
     78 
     79 #ifdef __OpenBSD__
     80 #define DRM_DIR_NAME  "/dev"
     81 #define DRM_DEV_NAME  "%s/drm%d"
     82 #define DRM_CONTROL_DEV_NAME  "%s/drmC%d"
     83 #define DRM_RENDER_DEV_NAME  "%s/drmR%d"
     84 #else
     85 #define DRM_DIR_NAME  "/dev/dri"
     86 #define DRM_DEV_NAME  "%s/card%d"
     87 #define DRM_CONTROL_DEV_NAME  "%s/controlD%d"
     88 #define DRM_RENDER_DEV_NAME  "%s/renderD%d"
     89 #define DRM_PROC_NAME "/proc/dri/" /* For backward Linux compatibility */
     90 #endif
     91 
     92 #define DRM_ERR_NO_DEVICE  (-1001)
     93 #define DRM_ERR_NO_ACCESS  (-1002)
     94 #define DRM_ERR_NOT_ROOT   (-1003)
     95 #define DRM_ERR_INVALID    (-1004)
     96 #define DRM_ERR_NO_FD      (-1005)
     97 
     98 #define DRM_AGP_NO_HANDLE 0
     99 
    100 typedef unsigned int  drmSize,     *drmSizePtr;	    /**< For mapped regions */
    101 typedef void          *drmAddress, **drmAddressPtr; /**< For mapped regions */
    102 
    103 #if (__GNUC__ >= 3)
    104 #define DRM_PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
    105 #else
    106 #define DRM_PRINTFLIKE(f, a)
    107 #endif
    108 
    109 typedef struct _drmServerInfo {
    110   int (*debug_print)(const char *format, va_list ap) DRM_PRINTFLIKE(1,0);
    111   int (*load_module)(const char *name);
    112   void (*get_perms)(gid_t *, mode_t *);
    113 } drmServerInfo, *drmServerInfoPtr;
    114 
    115 typedef struct drmHashEntry {
    116     int      fd;
    117     void     (*f)(int, void *, void *);
    118     void     *tagTable;
    119 } drmHashEntry;
    120 
    121 extern int drmIoctl(int fd, unsigned long request, void *arg);
    122 extern void *drmGetHashTable(void);
    123 extern drmHashEntry *drmGetEntry(int fd);
    124 
    125 /**
    126  * Driver version information.
    127  *
    128  * \sa drmGetVersion() and drmSetVersion().
    129  */
    130 typedef struct _drmVersion {
    131     int     version_major;        /**< Major version */
    132     int     version_minor;        /**< Minor version */
    133     int     version_patchlevel;   /**< Patch level */
    134     int     name_len; 	          /**< Length of name buffer */
    135     char    *name;	          /**< Name of driver */
    136     int     date_len;             /**< Length of date buffer */
    137     char    *date;                /**< User-space buffer to hold date */
    138     int     desc_len;	          /**< Length of desc buffer */
    139     char    *desc;                /**< User-space buffer to hold desc */
    140 } drmVersion, *drmVersionPtr;
    141 
    142 typedef struct _drmStats {
    143     unsigned long count;	     /**< Number of data */
    144     struct {
    145 	unsigned long value;	     /**< Value from kernel */
    146 	const char    *long_format;  /**< Suggested format for long_name */
    147 	const char    *long_name;    /**< Long name for value */
    148 	const char    *rate_format;  /**< Suggested format for rate_name */
    149 	const char    *rate_name;    /**< Short name for value per second */
    150 	int           isvalue;       /**< True if value (vs. counter) */
    151 	const char    *mult_names;   /**< Multiplier names (e.g., "KGM") */
    152 	int           mult;          /**< Multiplier value (e.g., 1024) */
    153 	int           verbose;       /**< Suggest only in verbose output */
    154     } data[15];
    155 } drmStatsT;
    156 
    157 
    158 				/* All of these enums *MUST* match with the
    159                                    kernel implementation -- so do *NOT*
    160                                    change them!  (The drmlib implementation
    161                                    will just copy the flags instead of
    162                                    translating them.) */
    163 typedef enum {
    164     DRM_FRAME_BUFFER    = 0,      /**< WC, no caching, no core dump */
    165     DRM_REGISTERS       = 1,      /**< no caching, no core dump */
    166     DRM_SHM             = 2,      /**< shared, cached */
    167     DRM_AGP             = 3,	  /**< AGP/GART */
    168     DRM_SCATTER_GATHER  = 4,	  /**< PCI scatter/gather */
    169     DRM_CONSISTENT      = 5	  /**< PCI consistent */
    170 } drmMapType;
    171 
    172 typedef enum {
    173     DRM_RESTRICTED      = 0x0001, /**< Cannot be mapped to client-virtual */
    174     DRM_READ_ONLY       = 0x0002, /**< Read-only in client-virtual */
    175     DRM_LOCKED          = 0x0004, /**< Physical pages locked */
    176     DRM_KERNEL          = 0x0008, /**< Kernel requires access */
    177     DRM_WRITE_COMBINING = 0x0010, /**< Use write-combining, if available */
    178     DRM_CONTAINS_LOCK   = 0x0020, /**< SHM page that contains lock */
    179     DRM_REMOVABLE	= 0x0040  /**< Removable mapping */
    180 } drmMapFlags;
    181 
    182 /**
    183  * \warning These values *MUST* match drm.h
    184  */
    185 typedef enum {
    186     /** \name Flags for DMA buffer dispatch */
    187     /*@{*/
    188     DRM_DMA_BLOCK        = 0x01, /**<
    189 				  * Block until buffer dispatched.
    190 				  *
    191 				  * \note the buffer may not yet have been
    192 				  * processed by the hardware -- getting a
    193 				  * hardware lock with the hardware quiescent
    194 				  * will ensure that the buffer has been
    195 				  * processed.
    196 				  */
    197     DRM_DMA_WHILE_LOCKED = 0x02, /**< Dispatch while lock held */
    198     DRM_DMA_PRIORITY     = 0x04, /**< High priority dispatch */
    199     /*@}*/
    200 
    201     /** \name Flags for DMA buffer request */
    202     /*@{*/
    203     DRM_DMA_WAIT         = 0x10, /**< Wait for free buffers */
    204     DRM_DMA_SMALLER_OK   = 0x20, /**< Smaller-than-requested buffers OK */
    205     DRM_DMA_LARGER_OK    = 0x40  /**< Larger-than-requested buffers OK */
    206     /*@}*/
    207 } drmDMAFlags;
    208 
    209 typedef enum {
    210     DRM_PAGE_ALIGN       = 0x01,
    211     DRM_AGP_BUFFER       = 0x02,
    212     DRM_SG_BUFFER        = 0x04,
    213     DRM_FB_BUFFER        = 0x08,
    214     DRM_PCI_BUFFER_RO    = 0x10
    215 } drmBufDescFlags;
    216 
    217 typedef enum {
    218     DRM_LOCK_READY      = 0x01, /**< Wait until hardware is ready for DMA */
    219     DRM_LOCK_QUIESCENT  = 0x02, /**< Wait until hardware quiescent */
    220     DRM_LOCK_FLUSH      = 0x04, /**< Flush this context's DMA queue first */
    221     DRM_LOCK_FLUSH_ALL  = 0x08, /**< Flush all DMA queues first */
    222 				/* These *HALT* flags aren't supported yet
    223                                    -- they will be used to support the
    224                                    full-screen DGA-like mode. */
    225     DRM_HALT_ALL_QUEUES = 0x10, /**< Halt all current and future queues */
    226     DRM_HALT_CUR_QUEUES = 0x20  /**< Halt all current queues */
    227 } drmLockFlags;
    228 
    229 typedef enum {
    230     DRM_CONTEXT_PRESERVED = 0x01, /**< This context is preserved and
    231 				     never swapped. */
    232     DRM_CONTEXT_2DONLY    = 0x02  /**< This context is for 2D rendering only. */
    233 } drm_context_tFlags, *drm_context_tFlagsPtr;
    234 
    235 typedef struct _drmBufDesc {
    236     int              count;	  /**< Number of buffers of this size */
    237     int              size;	  /**< Size in bytes */
    238     int              low_mark;	  /**< Low water mark */
    239     int              high_mark;	  /**< High water mark */
    240 } drmBufDesc, *drmBufDescPtr;
    241 
    242 typedef struct _drmBufInfo {
    243     int              count;	  /**< Number of buffers described in list */
    244     drmBufDescPtr    list;	  /**< List of buffer descriptions */
    245 } drmBufInfo, *drmBufInfoPtr;
    246 
    247 typedef struct _drmBuf {
    248     int              idx;	  /**< Index into the master buffer list */
    249     int              total;	  /**< Buffer size */
    250     int              used;	  /**< Amount of buffer in use (for DMA) */
    251     drmAddress       address;	  /**< Address */
    252 } drmBuf, *drmBufPtr;
    253 
    254 /**
    255  * Buffer mapping information.
    256  *
    257  * Used by drmMapBufs() and drmUnmapBufs() to store information about the
    258  * mapped buffers.
    259  */
    260 typedef struct _drmBufMap {
    261     int              count;	  /**< Number of buffers mapped */
    262     drmBufPtr        list;	  /**< Buffers */
    263 } drmBufMap, *drmBufMapPtr;
    264 
    265 typedef struct _drmLock {
    266     volatile unsigned int lock;
    267     char                      padding[60];
    268     /* This is big enough for most current (and future?) architectures:
    269        DEC Alpha:              32 bytes
    270        Intel Merced:           ?
    271        Intel P5/PPro/PII/PIII: 32 bytes
    272        Intel StrongARM:        32 bytes
    273        Intel i386/i486:        16 bytes
    274        MIPS:                   32 bytes (?)
    275        Motorola 68k:           16 bytes
    276        Motorola PowerPC:       32 bytes
    277        Sun SPARC:              32 bytes
    278     */
    279 } drmLock, *drmLockPtr;
    280 
    281 /**
    282  * Indices here refer to the offset into
    283  * list in drmBufInfo
    284  */
    285 typedef struct _drmDMAReq {
    286     drm_context_t    context;  	  /**< Context handle */
    287     int           send_count;     /**< Number of buffers to send */
    288     int           *send_list;     /**< List of handles to buffers */
    289     int           *send_sizes;    /**< Lengths of data to send, in bytes */
    290     drmDMAFlags   flags;          /**< Flags */
    291     int           request_count;  /**< Number of buffers requested */
    292     int           request_size;	  /**< Desired size of buffers requested */
    293     int           *request_list;  /**< Buffer information */
    294     int           *request_sizes; /**< Minimum acceptable sizes */
    295     int           granted_count;  /**< Number of buffers granted at this size */
    296 } drmDMAReq, *drmDMAReqPtr;
    297 
    298 typedef struct _drmRegion {
    299     drm_handle_t     handle;
    300     unsigned int  offset;
    301     drmSize       size;
    302     drmAddress    map;
    303 } drmRegion, *drmRegionPtr;
    304 
    305 typedef struct _drmTextureRegion {
    306     unsigned char next;
    307     unsigned char prev;
    308     unsigned char in_use;
    309     unsigned char padding;	/**< Explicitly pad this out */
    310     unsigned int  age;
    311 } drmTextureRegion, *drmTextureRegionPtr;
    312 
    313 
    314 typedef enum {
    315     DRM_VBLANK_ABSOLUTE = 0x0,	/**< Wait for specific vblank sequence number */
    316     DRM_VBLANK_RELATIVE = 0x1,	/**< Wait for given number of vblanks */
    317     /* bits 1-6 are reserved for high crtcs */
    318     DRM_VBLANK_HIGH_CRTC_MASK = 0x0000003e,
    319     DRM_VBLANK_EVENT = 0x4000000,	/**< Send event instead of blocking */
    320     DRM_VBLANK_FLIP = 0x8000000,	/**< Scheduled buffer swap should flip */
    321     DRM_VBLANK_NEXTONMISS = 0x10000000,	/**< If missed, wait for next vblank */
    322     DRM_VBLANK_SECONDARY = 0x20000000,	/**< Secondary display controller */
    323     DRM_VBLANK_SIGNAL   = 0x40000000	/* Send signal instead of blocking */
    324 } drmVBlankSeqType;
    325 #define DRM_VBLANK_HIGH_CRTC_SHIFT 1
    326 
    327 typedef struct _drmVBlankReq {
    328 	drmVBlankSeqType type;
    329 	unsigned int sequence;
    330 	unsigned long signal;
    331 } drmVBlankReq, *drmVBlankReqPtr;
    332 
    333 typedef struct _drmVBlankReply {
    334 	drmVBlankSeqType type;
    335 	unsigned int sequence;
    336 	long tval_sec;
    337 	long tval_usec;
    338 } drmVBlankReply, *drmVBlankReplyPtr;
    339 
    340 typedef union _drmVBlank {
    341 	drmVBlankReq request;
    342 	drmVBlankReply reply;
    343 } drmVBlank, *drmVBlankPtr;
    344 
    345 typedef struct _drmSetVersion {
    346 	int drm_di_major;
    347 	int drm_di_minor;
    348 	int drm_dd_major;
    349 	int drm_dd_minor;
    350 } drmSetVersion, *drmSetVersionPtr;
    351 
    352 #define __drm_dummy_lock(lock) (*(__volatile__ unsigned int *)lock)
    353 
    354 #define DRM_LOCK_HELD  0x80000000U /**< Hardware lock is held */
    355 #define DRM_LOCK_CONT  0x40000000U /**< Hardware lock is contended */
    356 
    357 #if defined(__GNUC__) && (__GNUC__ >= 2)
    358 # if defined(__i386) || defined(__AMD64__) || defined(__x86_64__) || defined(__amd64__)
    359 				/* Reflect changes here to drmP.h */
    360 #define DRM_CAS(lock,old,new,__ret)                                    \
    361 	do {                                                           \
    362                 int __dummy;	/* Can't mark eax as clobbered */      \
    363 		__asm__ __volatile__(                                  \
    364 			"lock ; cmpxchg %4,%1\n\t"                     \
    365                         "setnz %0"                                     \
    366 			: "=d" (__ret),                                \
    367    			  "=m" (__drm_dummy_lock(lock)),               \
    368                           "=a" (__dummy)                               \
    369 			: "2" (old),                                   \
    370 			  "r" (new));                                  \
    371 	} while (0)
    372 
    373 #elif defined(__alpha__)
    374 
    375 #define	DRM_CAS(lock, old, new, ret)		\
    376 	do {					\
    377 		int tmp, old32;			\
    378 		__asm__ __volatile__(		\
    379 		"	addl	$31, %5, %3\n"	\
    380 		"1:	ldl_l	%0, %2\n"	\
    381 		"	cmpeq	%0, %3, %1\n"	\
    382 		"	beq	%1, 2f\n"	\
    383 		"	mov	%4, %0\n"	\
    384 		"	stl_c	%0, %2\n"	\
    385 		"	beq	%0, 3f\n"	\
    386 		"	mb\n"			\
    387 		"2:	cmpeq	%1, 0, %1\n"	\
    388 		".subsection 2\n"		\
    389 		"3:	br	1b\n"		\
    390 		".previous"			\
    391 		: "=&r"(tmp), "=&r"(ret),	\
    392 		  "=m"(__drm_dummy_lock(lock)),	\
    393 		  "=&r"(old32)			\
    394 		: "r"(new), "r"(old)		\
    395 		: "memory");			\
    396 	} while (0)
    397 
    398 #elif defined(__sparc__)
    399 
    400 #define DRM_CAS(lock,old,new,__ret)				\
    401 do {	register unsigned int __old __asm("o0");		\
    402 	register unsigned int __new __asm("o1");		\
    403 	register volatile unsigned int *__lock __asm("o2");	\
    404 	__old = old;						\
    405 	__new = new;						\
    406 	__lock = (volatile unsigned int *)lock;			\
    407 	__asm__ __volatile__(					\
    408 		/*"cas [%2], %3, %0"*/				\
    409 		".word 0xd3e29008\n\t"				\
    410 		/*"membar #StoreStore | #StoreLoad"*/		\
    411 		".word 0x8143e00a"				\
    412 		: "=&r" (__new)					\
    413 		: "0" (__new),					\
    414 		  "r" (__lock),					\
    415 		  "r" (__old)					\
    416 		: "memory");					\
    417 	__ret = (__new != __old);				\
    418 } while(0)
    419 
    420 #elif defined(__ia64__)
    421 
    422 #ifdef __INTEL_COMPILER
    423 /* this currently generates bad code (missing stop bits)... */
    424 #include <ia64intrin.h>
    425 
    426 #define DRM_CAS(lock,old,new,__ret)					      \
    427 	do {								      \
    428 		unsigned long __result, __old = (old) & 0xffffffff;		\
    429 		__mf();							      	\
    430 		__result = _InterlockedCompareExchange_acq(&__drm_dummy_lock(lock), (new), __old);\
    431 		__ret = (__result) != (__old);					\
    432 /*		__ret = (__sync_val_compare_and_swap(&__drm_dummy_lock(lock), \
    433 						     (old), (new))	      \
    434 			 != (old));					      */\
    435 	} while (0)
    436 
    437 #else
    438 #define DRM_CAS(lock,old,new,__ret)					  \
    439 	do {								  \
    440 		unsigned int __result, __old = (old);			  \
    441 		__asm__ __volatile__(					  \
    442 			"mf\n"						  \
    443 			"mov ar.ccv=%2\n"				  \
    444 			";;\n"						  \
    445 			"cmpxchg4.acq %0=%1,%3,ar.ccv"			  \
    446 			: "=r" (__result), "=m" (__drm_dummy_lock(lock))  \
    447 			: "r" ((unsigned long)__old), "r" (new)			  \
    448 			: "memory");					  \
    449 		__ret = (__result) != (__old);				  \
    450 	} while (0)
    451 
    452 #endif
    453 
    454 #elif defined(__powerpc__)
    455 
    456 #define DRM_CAS(lock,old,new,__ret)			\
    457 	do {						\
    458 		__asm__ __volatile__(			\
    459 			"sync;"				\
    460 			"0:    lwarx %0,0,%1;"		\
    461 			"      xor. %0,%3,%0;"		\
    462 			"      bne 1f;"			\
    463 			"      stwcx. %2,0,%1;"		\
    464 			"      bne- 0b;"		\
    465 			"1:    "			\
    466 			"sync;"				\
    467 		: "=&r"(__ret)				\
    468 		: "r"(lock), "r"(new), "r"(old)		\
    469 		: "cr0", "memory");			\
    470 	} while (0)
    471 
    472 #endif /* architecture */
    473 #endif /* __GNUC__ >= 2 */
    474 
    475 #ifndef DRM_CAS
    476 #define DRM_CAS(lock,old,new,ret) do { ret=1; } while (0) /* FAST LOCK FAILS */
    477 #endif
    478 
    479 #if defined(__alpha__)
    480 #define DRM_CAS_RESULT(_result)		long _result
    481 #elif defined(__powerpc__)
    482 #define DRM_CAS_RESULT(_result)		int _result
    483 #else
    484 #define DRM_CAS_RESULT(_result)		char _result
    485 #endif
    486 
    487 #define DRM_LIGHT_LOCK(fd,lock,context)                                \
    488 	do {                                                           \
    489                 DRM_CAS_RESULT(__ret);                                 \
    490 		DRM_CAS(lock,context,DRM_LOCK_HELD|context,__ret);     \
    491                 if (__ret) drmGetLock(fd,context,0);                   \
    492         } while(0)
    493 
    494 				/* This one counts fast locks -- for
    495                                    benchmarking only. */
    496 #define DRM_LIGHT_LOCK_COUNT(fd,lock,context,count)                    \
    497 	do {                                                           \
    498                 DRM_CAS_RESULT(__ret);                                 \
    499 		DRM_CAS(lock,context,DRM_LOCK_HELD|context,__ret);     \
    500                 if (__ret) drmGetLock(fd,context,0);                   \
    501                 else       ++count;                                    \
    502         } while(0)
    503 
    504 #define DRM_LOCK(fd,lock,context,flags)                                \
    505 	do {                                                           \
    506 		if (flags) drmGetLock(fd,context,flags);               \
    507 		else       DRM_LIGHT_LOCK(fd,lock,context);            \
    508 	} while(0)
    509 
    510 #define DRM_UNLOCK(fd,lock,context)                                    \
    511 	do {                                                           \
    512                 DRM_CAS_RESULT(__ret);                                 \
    513 		DRM_CAS(lock,DRM_LOCK_HELD|context,context,__ret);     \
    514                 if (__ret) drmUnlock(fd,context);                      \
    515         } while(0)
    516 
    517 				/* Simple spin locks */
    518 #define DRM_SPINLOCK(spin,val)                                         \
    519 	do {                                                           \
    520             DRM_CAS_RESULT(__ret);                                     \
    521 	    do {                                                       \
    522 		DRM_CAS(spin,0,val,__ret);                             \
    523 		if (__ret) while ((spin)->lock);                       \
    524 	    } while (__ret);                                           \
    525 	} while(0)
    526 
    527 #define DRM_SPINLOCK_TAKE(spin,val)                                    \
    528 	do {                                                           \
    529             DRM_CAS_RESULT(__ret);                                     \
    530             int  cur;                                                  \
    531 	    do {                                                       \
    532                 cur = (*spin).lock;                                    \
    533 		DRM_CAS(spin,cur,val,__ret);                           \
    534 	    } while (__ret);                                           \
    535 	} while(0)
    536 
    537 #define DRM_SPINLOCK_COUNT(spin,val,count,__ret)                       \
    538 	do {                                                           \
    539             int  __i;                                                  \
    540             __ret = 1;                                                 \
    541             for (__i = 0; __ret && __i < count; __i++) {               \
    542 		DRM_CAS(spin,0,val,__ret);                             \
    543 		if (__ret) for (;__i < count && (spin)->lock; __i++);  \
    544 	    }                                                          \
    545 	} while(0)
    546 
    547 #define DRM_SPINUNLOCK(spin,val)                                       \
    548 	do {                                                           \
    549             DRM_CAS_RESULT(__ret);                                     \
    550             if ((*spin).lock == val) { /* else server stole lock */    \
    551 	        do {                                                   \
    552 		    DRM_CAS(spin,val,0,__ret);                         \
    553 	        } while (__ret);                                       \
    554             }                                                          \
    555 	} while(0)
    556 
    557 
    558 
    559 /* General user-level programmer's API: unprivileged */
    560 extern int           drmAvailable(void);
    561 extern int           drmOpen(const char *name, const char *busid);
    562 
    563 #define DRM_NODE_PRIMARY 0
    564 #define DRM_NODE_CONTROL 1
    565 #define DRM_NODE_RENDER  2
    566 #define DRM_NODE_MAX     3
    567 
    568 extern int           drmOpenWithType(const char *name, const char *busid,
    569                                      int type);
    570 
    571 extern int           drmOpenControl(int minor);
    572 extern int           drmOpenRender(int minor);
    573 extern int           drmClose(int fd);
    574 extern drmVersionPtr drmGetVersion(int fd);
    575 extern drmVersionPtr drmGetLibVersion(int fd);
    576 extern int           drmGetCap(int fd, uint64_t capability, uint64_t *value);
    577 extern void          drmFreeVersion(drmVersionPtr);
    578 extern int           drmGetMagic(int fd, drm_magic_t * magic);
    579 extern char          *drmGetBusid(int fd);
    580 extern int           drmGetInterruptFromBusID(int fd, int busnum, int devnum,
    581 					      int funcnum);
    582 extern int           drmGetMap(int fd, int idx, drm_handle_t *offset,
    583 			       drmSize *size, drmMapType *type,
    584 			       drmMapFlags *flags, drm_handle_t *handle,
    585 			       int *mtrr);
    586 extern int           drmGetClient(int fd, int idx, int *auth, int *pid,
    587 				  int *uid, unsigned long *magic,
    588 				  unsigned long *iocs);
    589 extern int           drmGetStats(int fd, drmStatsT *stats);
    590 extern int           drmSetInterfaceVersion(int fd, drmSetVersion *version);
    591 extern int           drmCommandNone(int fd, unsigned long drmCommandIndex);
    592 extern int           drmCommandRead(int fd, unsigned long drmCommandIndex,
    593                                     void *data, unsigned long size);
    594 extern int           drmCommandWrite(int fd, unsigned long drmCommandIndex,
    595                                      void *data, unsigned long size);
    596 extern int           drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
    597                                          void *data, unsigned long size);
    598 
    599 /* General user-level programmer's API: X server (root) only  */
    600 extern void          drmFreeBusid(const char *busid);
    601 extern int           drmSetBusid(int fd, const char *busid);
    602 extern int           drmAuthMagic(int fd, drm_magic_t magic);
    603 extern int           drmAddMap(int fd,
    604 			       drm_handle_t offset,
    605 			       drmSize size,
    606 			       drmMapType type,
    607 			       drmMapFlags flags,
    608 			       drm_handle_t * handle);
    609 extern int	     drmRmMap(int fd, drm_handle_t handle);
    610 extern int	     drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
    611 						 drm_handle_t handle);
    612 
    613 extern int           drmAddBufs(int fd, int count, int size,
    614 				drmBufDescFlags flags,
    615 				int agp_offset);
    616 extern int           drmMarkBufs(int fd, double low, double high);
    617 extern int           drmCreateContext(int fd, drm_context_t * handle);
    618 extern int           drmSetContextFlags(int fd, drm_context_t context,
    619 					drm_context_tFlags flags);
    620 extern int           drmGetContextFlags(int fd, drm_context_t context,
    621 					drm_context_tFlagsPtr flags);
    622 extern int           drmAddContextTag(int fd, drm_context_t context, void *tag);
    623 extern int           drmDelContextTag(int fd, drm_context_t context);
    624 extern void          *drmGetContextTag(int fd, drm_context_t context);
    625 extern drm_context_t * drmGetReservedContextList(int fd, int *count);
    626 extern void          drmFreeReservedContextList(drm_context_t *);
    627 extern int           drmSwitchToContext(int fd, drm_context_t context);
    628 extern int           drmDestroyContext(int fd, drm_context_t handle);
    629 extern int           drmCreateDrawable(int fd, drm_drawable_t * handle);
    630 extern int           drmDestroyDrawable(int fd, drm_drawable_t handle);
    631 extern int           drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
    632 					   drm_drawable_info_type_t type,
    633 					   unsigned int num, void *data);
    634 extern int           drmCtlInstHandler(int fd, int irq);
    635 extern int           drmCtlUninstHandler(int fd);
    636 extern int           drmSetClientCap(int fd, uint64_t capability,
    637 				     uint64_t value);
    638 
    639 /* General user-level programmer's API: authenticated client and/or X */
    640 extern int           drmMap(int fd,
    641 			    drm_handle_t handle,
    642 			    drmSize size,
    643 			    drmAddressPtr address);
    644 extern int           drmUnmap(drmAddress address, drmSize size);
    645 extern drmBufInfoPtr drmGetBufInfo(int fd);
    646 extern drmBufMapPtr  drmMapBufs(int fd);
    647 extern int           drmUnmapBufs(drmBufMapPtr bufs);
    648 extern int           drmDMA(int fd, drmDMAReqPtr request);
    649 extern int           drmFreeBufs(int fd, int count, int *list);
    650 extern int           drmGetLock(int fd,
    651 			        drm_context_t context,
    652 			        drmLockFlags flags);
    653 extern int           drmUnlock(int fd, drm_context_t context);
    654 extern int           drmFinish(int fd, int context, drmLockFlags flags);
    655 extern int	     drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
    656 						 drm_handle_t * handle);
    657 
    658 /* AGP/GART support: X server (root) only */
    659 extern int           drmAgpAcquire(int fd);
    660 extern int           drmAgpRelease(int fd);
    661 extern int           drmAgpEnable(int fd, unsigned long mode);
    662 extern int           drmAgpAlloc(int fd, unsigned long size,
    663 				 unsigned long type, unsigned long *address,
    664 				 drm_handle_t *handle);
    665 extern int           drmAgpFree(int fd, drm_handle_t handle);
    666 extern int 	     drmAgpBind(int fd, drm_handle_t handle,
    667 				unsigned long offset);
    668 extern int           drmAgpUnbind(int fd, drm_handle_t handle);
    669 
    670 /* AGP/GART info: authenticated client and/or X */
    671 extern int           drmAgpVersionMajor(int fd);
    672 extern int           drmAgpVersionMinor(int fd);
    673 extern unsigned long drmAgpGetMode(int fd);
    674 extern unsigned long drmAgpBase(int fd); /* Physical location */
    675 extern unsigned long drmAgpSize(int fd); /* Bytes */
    676 extern unsigned long drmAgpMemoryUsed(int fd);
    677 extern unsigned long drmAgpMemoryAvail(int fd);
    678 extern unsigned int  drmAgpVendorId(int fd);
    679 extern unsigned int  drmAgpDeviceId(int fd);
    680 
    681 /* PCI scatter/gather support: X server (root) only */
    682 extern int           drmScatterGatherAlloc(int fd, unsigned long size,
    683 					   drm_handle_t *handle);
    684 extern int           drmScatterGatherFree(int fd, drm_handle_t handle);
    685 
    686 extern int           drmWaitVBlank(int fd, drmVBlankPtr vbl);
    687 
    688 /* Support routines */
    689 extern void          drmSetServerInfo(drmServerInfoPtr info);
    690 extern int           drmError(int err, const char *label);
    691 extern void          *drmMalloc(int size);
    692 extern void          drmFree(void *pt);
    693 
    694 /* Hash table routines */
    695 extern void *drmHashCreate(void);
    696 extern int  drmHashDestroy(void *t);
    697 extern int  drmHashLookup(void *t, unsigned long key, void **value);
    698 extern int  drmHashInsert(void *t, unsigned long key, void *value);
    699 extern int  drmHashDelete(void *t, unsigned long key);
    700 extern int  drmHashFirst(void *t, unsigned long *key, void **value);
    701 extern int  drmHashNext(void *t, unsigned long *key, void **value);
    702 
    703 /* PRNG routines */
    704 extern void          *drmRandomCreate(unsigned long seed);
    705 extern int           drmRandomDestroy(void *state);
    706 extern unsigned long drmRandom(void *state);
    707 extern double        drmRandomDouble(void *state);
    708 
    709 /* Skip list routines */
    710 
    711 extern void *drmSLCreate(void);
    712 extern int  drmSLDestroy(void *l);
    713 extern int  drmSLLookup(void *l, unsigned long key, void **value);
    714 extern int  drmSLInsert(void *l, unsigned long key, void *value);
    715 extern int  drmSLDelete(void *l, unsigned long key);
    716 extern int  drmSLNext(void *l, unsigned long *key, void **value);
    717 extern int  drmSLFirst(void *l, unsigned long *key, void **value);
    718 extern void drmSLDump(void *l);
    719 extern int  drmSLLookupNeighbors(void *l, unsigned long key,
    720 				 unsigned long *prev_key, void **prev_value,
    721 				 unsigned long *next_key, void **next_value);
    722 
    723 extern int drmOpenOnce(void *unused, const char *BusID, int *newlyopened);
    724 extern int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type);
    725 extern void drmCloseOnce(int fd);
    726 extern void drmMsg(const char *format, ...) DRM_PRINTFLIKE(1, 2);
    727 
    728 extern int drmSetMaster(int fd);
    729 extern int drmDropMaster(int fd);
    730 
    731 #define DRM_EVENT_CONTEXT_VERSION 2
    732 
    733 typedef struct _drmEventContext {
    734 
    735 	/* This struct is versioned so we can add more pointers if we
    736 	 * add more events. */
    737 	int version;
    738 
    739 	void (*vblank_handler)(int fd,
    740 			       unsigned int sequence,
    741 			       unsigned int tv_sec,
    742 			       unsigned int tv_usec,
    743 			       void *user_data);
    744 
    745 	void (*page_flip_handler)(int fd,
    746 				  unsigned int sequence,
    747 				  unsigned int tv_sec,
    748 				  unsigned int tv_usec,
    749 				  void *user_data);
    750 
    751 } drmEventContext, *drmEventContextPtr;
    752 
    753 extern int drmHandleEvent(int fd, drmEventContextPtr evctx);
    754 
    755 extern char *drmGetDeviceNameFromFd(int fd);
    756 extern int drmGetNodeTypeFromFd(int fd);
    757 
    758 extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
    759 extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
    760 
    761 extern char *drmGetPrimaryDeviceNameFromFd(int fd);
    762 extern char *drmGetRenderDeviceNameFromFd(int fd);
    763 
    764 #define DRM_BUS_PCI   0
    765 
    766 typedef struct _drmPciBusInfo {
    767     uint16_t domain;
    768     uint8_t bus;
    769     uint8_t dev;
    770     uint8_t func;
    771 } drmPciBusInfo, *drmPciBusInfoPtr;
    772 
    773 typedef struct _drmPciDeviceInfo {
    774     uint16_t vendor_id;
    775     uint16_t device_id;
    776     uint16_t subvendor_id;
    777     uint16_t subdevice_id;
    778     uint8_t revision_id;
    779 } drmPciDeviceInfo, *drmPciDeviceInfoPtr;
    780 
    781 typedef struct _drmDevice {
    782     char **nodes; /* DRM_NODE_MAX sized array */
    783     int available_nodes; /* DRM_NODE_* bitmask */
    784     int bustype;
    785     union {
    786         drmPciBusInfoPtr pci;
    787     } businfo;
    788     union {
    789         drmPciDeviceInfoPtr pci;
    790     } deviceinfo;
    791 } drmDevice, *drmDevicePtr;
    792 
    793 extern int drmGetDevice(int fd, drmDevicePtr *device);
    794 extern void drmFreeDevice(drmDevicePtr *device);
    795 
    796 extern int drmGetDevices(drmDevicePtr devices[], int max_devices);
    797 extern void drmFreeDevices(drmDevicePtr devices[], int count);
    798 
    799 #if defined(__cplusplus)
    800 }
    801 #endif
    802 
    803 #endif
    804