• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * \file xf86drm.h
3  * OS-independent header for DRM user-level library interface.
4  *
5  * \author Rickard E. (Rik) Faith <faith@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_PRIMARY_MINOR_NAME  "drm"
82 #define DRM_CONTROL_MINOR_NAME  "drmC"
83 #define DRM_RENDER_MINOR_NAME   "drmR"
84 #else
85 #define DRM_DIR_NAME  "/dev/dri"
86 #define DRM_PRIMARY_MINOR_NAME  "card"
87 #define DRM_CONTROL_MINOR_NAME  "controlD"
88 #define DRM_RENDER_MINOR_NAME   "renderD"
89 #define DRM_PROC_NAME "/proc/dri/" /* For backward Linux compatibility */
90 #endif
91 
92 #define DRM_DEV_NAME          "%s/" DRM_PRIMARY_MINOR_NAME "%d"
93 #define DRM_CONTROL_DEV_NAME  "%s/" DRM_CONTROL_MINOR_NAME "%d"
94 #define DRM_RENDER_DEV_NAME   "%s/" DRM_RENDER_MINOR_NAME  "%d"
95 
96 #define DRM_NODE_NAME_MAX \
97     (sizeof(DRM_DIR_NAME) + 1 /* slash */ \
98      + MAX3(sizeof(DRM_PRIMARY_MINOR_NAME), \
99             sizeof(DRM_CONTROL_MINOR_NAME), \
100             sizeof(DRM_RENDER_MINOR_NAME)) \
101      + sizeof("144") /* highest possible node number */ \
102      + 1) /* NULL-terminator */
103 
104 #define DRM_ERR_NO_DEVICE  (-1001)
105 #define DRM_ERR_NO_ACCESS  (-1002)
106 #define DRM_ERR_NOT_ROOT   (-1003)
107 #define DRM_ERR_INVALID    (-1004)
108 #define DRM_ERR_NO_FD      (-1005)
109 
110 #define DRM_AGP_NO_HANDLE 0
111 
112 typedef unsigned int  drmSize,     *drmSizePtr;	    /**< For mapped regions */
113 typedef void          *drmAddress, **drmAddressPtr; /**< For mapped regions */
114 
115 #if (__GNUC__ >= 3)
116 #define DRM_PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
117 #else
118 #define DRM_PRINTFLIKE(f, a)
119 #endif
120 
121 typedef struct _drmServerInfo {
122   int (*debug_print)(const char *format, va_list ap) DRM_PRINTFLIKE(1,0);
123   int (*load_module)(const char *name);
124   void (*get_perms)(gid_t *, mode_t *);
125 } drmServerInfo, *drmServerInfoPtr;
126 
127 typedef struct drmHashEntry {
128     int      fd;
129     void     (*f)(int, void *, void *);
130     void     *tagTable;
131 } drmHashEntry;
132 
133 extern int drmIoctl(int fd, unsigned long request, void *arg);
134 extern void *drmGetHashTable(void);
135 extern drmHashEntry *drmGetEntry(int fd);
136 
137 /**
138  * Driver version information.
139  *
140  * \sa drmGetVersion() and drmSetVersion().
141  */
142 typedef struct _drmVersion {
143     int     version_major;        /**< Major version */
144     int     version_minor;        /**< Minor version */
145     int     version_patchlevel;   /**< Patch level */
146     int     name_len; 	          /**< Length of name buffer */
147     char    *name;	          /**< Name of driver */
148     int     date_len;             /**< Length of date buffer */
149     char    *date;                /**< User-space buffer to hold date */
150     int     desc_len;	          /**< Length of desc buffer */
151     char    *desc;                /**< User-space buffer to hold desc */
152 } drmVersion, *drmVersionPtr;
153 
154 typedef struct _drmStats {
155     unsigned long count;	     /**< Number of data */
156     struct {
157 	unsigned long value;	     /**< Value from kernel */
158 	const char    *long_format;  /**< Suggested format for long_name */
159 	const char    *long_name;    /**< Long name for value */
160 	const char    *rate_format;  /**< Suggested format for rate_name */
161 	const char    *rate_name;    /**< Short name for value per second */
162 	int           isvalue;       /**< True if value (vs. counter) */
163 	const char    *mult_names;   /**< Multiplier names (e.g., "KGM") */
164 	int           mult;          /**< Multiplier value (e.g., 1024) */
165 	int           verbose;       /**< Suggest only in verbose output */
166     } data[15];
167 } drmStatsT;
168 
169 
170 				/* All of these enums *MUST* match with the
171                                    kernel implementation -- so do *NOT*
172                                    change them!  (The drmlib implementation
173                                    will just copy the flags instead of
174                                    translating them.) */
175 typedef enum {
176     DRM_FRAME_BUFFER    = 0,      /**< WC, no caching, no core dump */
177     DRM_REGISTERS       = 1,      /**< no caching, no core dump */
178     DRM_SHM             = 2,      /**< shared, cached */
179     DRM_AGP             = 3,	  /**< AGP/GART */
180     DRM_SCATTER_GATHER  = 4,	  /**< PCI scatter/gather */
181     DRM_CONSISTENT      = 5	  /**< PCI consistent */
182 } drmMapType;
183 
184 typedef enum {
185     DRM_RESTRICTED      = 0x0001, /**< Cannot be mapped to client-virtual */
186     DRM_READ_ONLY       = 0x0002, /**< Read-only in client-virtual */
187     DRM_LOCKED          = 0x0004, /**< Physical pages locked */
188     DRM_KERNEL          = 0x0008, /**< Kernel requires access */
189     DRM_WRITE_COMBINING = 0x0010, /**< Use write-combining, if available */
190     DRM_CONTAINS_LOCK   = 0x0020, /**< SHM page that contains lock */
191     DRM_REMOVABLE	= 0x0040  /**< Removable mapping */
192 } drmMapFlags;
193 
194 /**
195  * \warning These values *MUST* match drm.h
196  */
197 typedef enum {
198     /** \name Flags for DMA buffer dispatch */
199     /*@{*/
200     DRM_DMA_BLOCK        = 0x01, /**<
201 				  * Block until buffer dispatched.
202 				  *
203 				  * \note the buffer may not yet have been
204 				  * processed by the hardware -- getting a
205 				  * hardware lock with the hardware quiescent
206 				  * will ensure that the buffer has been
207 				  * processed.
208 				  */
209     DRM_DMA_WHILE_LOCKED = 0x02, /**< Dispatch while lock held */
210     DRM_DMA_PRIORITY     = 0x04, /**< High priority dispatch */
211     /*@}*/
212 
213     /** \name Flags for DMA buffer request */
214     /*@{*/
215     DRM_DMA_WAIT         = 0x10, /**< Wait for free buffers */
216     DRM_DMA_SMALLER_OK   = 0x20, /**< Smaller-than-requested buffers OK */
217     DRM_DMA_LARGER_OK    = 0x40  /**< Larger-than-requested buffers OK */
218     /*@}*/
219 } drmDMAFlags;
220 
221 typedef enum {
222     DRM_PAGE_ALIGN       = 0x01,
223     DRM_AGP_BUFFER       = 0x02,
224     DRM_SG_BUFFER        = 0x04,
225     DRM_FB_BUFFER        = 0x08,
226     DRM_PCI_BUFFER_RO    = 0x10
227 } drmBufDescFlags;
228 
229 typedef enum {
230     DRM_LOCK_READY      = 0x01, /**< Wait until hardware is ready for DMA */
231     DRM_LOCK_QUIESCENT  = 0x02, /**< Wait until hardware quiescent */
232     DRM_LOCK_FLUSH      = 0x04, /**< Flush this context's DMA queue first */
233     DRM_LOCK_FLUSH_ALL  = 0x08, /**< Flush all DMA queues first */
234 				/* These *HALT* flags aren't supported yet
235                                    -- they will be used to support the
236                                    full-screen DGA-like mode. */
237     DRM_HALT_ALL_QUEUES = 0x10, /**< Halt all current and future queues */
238     DRM_HALT_CUR_QUEUES = 0x20  /**< Halt all current queues */
239 } drmLockFlags;
240 
241 typedef enum {
242     DRM_CONTEXT_PRESERVED = 0x01, /**< This context is preserved and
243 				     never swapped. */
244     DRM_CONTEXT_2DONLY    = 0x02  /**< This context is for 2D rendering only. */
245 } drm_context_tFlags, *drm_context_tFlagsPtr;
246 
247 typedef struct _drmBufDesc {
248     int              count;	  /**< Number of buffers of this size */
249     int              size;	  /**< Size in bytes */
250     int              low_mark;	  /**< Low water mark */
251     int              high_mark;	  /**< High water mark */
252 } drmBufDesc, *drmBufDescPtr;
253 
254 typedef struct _drmBufInfo {
255     int              count;	  /**< Number of buffers described in list */
256     drmBufDescPtr    list;	  /**< List of buffer descriptions */
257 } drmBufInfo, *drmBufInfoPtr;
258 
259 typedef struct _drmBuf {
260     int              idx;	  /**< Index into the master buffer list */
261     int              total;	  /**< Buffer size */
262     int              used;	  /**< Amount of buffer in use (for DMA) */
263     drmAddress       address;	  /**< Address */
264 } drmBuf, *drmBufPtr;
265 
266 /**
267  * Buffer mapping information.
268  *
269  * Used by drmMapBufs() and drmUnmapBufs() to store information about the
270  * mapped buffers.
271  */
272 typedef struct _drmBufMap {
273     int              count;	  /**< Number of buffers mapped */
274     drmBufPtr        list;	  /**< Buffers */
275 } drmBufMap, *drmBufMapPtr;
276 
277 typedef struct _drmLock {
278     volatile unsigned int lock;
279     char                      padding[60];
280     /* This is big enough for most current (and future?) architectures:
281        DEC Alpha:              32 bytes
282        Intel Merced:           ?
283        Intel P5/PPro/PII/PIII: 32 bytes
284        Intel StrongARM:        32 bytes
285        Intel i386/i486:        16 bytes
286        MIPS:                   32 bytes (?)
287        Motorola 68k:           16 bytes
288        Motorola PowerPC:       32 bytes
289        Sun SPARC:              32 bytes
290     */
291 } drmLock, *drmLockPtr;
292 
293 /**
294  * Indices here refer to the offset into
295  * list in drmBufInfo
296  */
297 typedef struct _drmDMAReq {
298     drm_context_t    context;  	  /**< Context handle */
299     int           send_count;     /**< Number of buffers to send */
300     int           *send_list;     /**< List of handles to buffers */
301     int           *send_sizes;    /**< Lengths of data to send, in bytes */
302     drmDMAFlags   flags;          /**< Flags */
303     int           request_count;  /**< Number of buffers requested */
304     int           request_size;	  /**< Desired size of buffers requested */
305     int           *request_list;  /**< Buffer information */
306     int           *request_sizes; /**< Minimum acceptable sizes */
307     int           granted_count;  /**< Number of buffers granted at this size */
308 } drmDMAReq, *drmDMAReqPtr;
309 
310 typedef struct _drmRegion {
311     drm_handle_t     handle;
312     unsigned int  offset;
313     drmSize       size;
314     drmAddress    map;
315 } drmRegion, *drmRegionPtr;
316 
317 typedef struct _drmTextureRegion {
318     unsigned char next;
319     unsigned char prev;
320     unsigned char in_use;
321     unsigned char padding;	/**< Explicitly pad this out */
322     unsigned int  age;
323 } drmTextureRegion, *drmTextureRegionPtr;
324 
325 
326 typedef enum {
327     DRM_VBLANK_ABSOLUTE = 0x0,	/**< Wait for specific vblank sequence number */
328     DRM_VBLANK_RELATIVE = 0x1,	/**< Wait for given number of vblanks */
329     /* bits 1-6 are reserved for high crtcs */
330     DRM_VBLANK_HIGH_CRTC_MASK = 0x0000003e,
331     DRM_VBLANK_EVENT = 0x4000000,	/**< Send event instead of blocking */
332     DRM_VBLANK_FLIP = 0x8000000,	/**< Scheduled buffer swap should flip */
333     DRM_VBLANK_NEXTONMISS = 0x10000000,	/**< If missed, wait for next vblank */
334     DRM_VBLANK_SECONDARY = 0x20000000,	/**< Secondary display controller */
335     DRM_VBLANK_SIGNAL   = 0x40000000	/* Send signal instead of blocking */
336 } drmVBlankSeqType;
337 #define DRM_VBLANK_HIGH_CRTC_SHIFT 1
338 
339 typedef struct _drmVBlankReq {
340 	drmVBlankSeqType type;
341 	unsigned int sequence;
342 	unsigned long signal;
343 } drmVBlankReq, *drmVBlankReqPtr;
344 
345 typedef struct _drmVBlankReply {
346 	drmVBlankSeqType type;
347 	unsigned int sequence;
348 	long tval_sec;
349 	long tval_usec;
350 } drmVBlankReply, *drmVBlankReplyPtr;
351 
352 typedef union _drmVBlank {
353 	drmVBlankReq request;
354 	drmVBlankReply reply;
355 } drmVBlank, *drmVBlankPtr;
356 
357 typedef struct _drmSetVersion {
358 	int drm_di_major;
359 	int drm_di_minor;
360 	int drm_dd_major;
361 	int drm_dd_minor;
362 } drmSetVersion, *drmSetVersionPtr;
363 
364 #define __drm_dummy_lock(lock) (*(__volatile__ unsigned int *)lock)
365 
366 #define DRM_LOCK_HELD  0x80000000U /**< Hardware lock is held */
367 #define DRM_LOCK_CONT  0x40000000U /**< Hardware lock is contended */
368 
369 #if defined(__GNUC__) && (__GNUC__ >= 2)
370 # if defined(__i386) || defined(__AMD64__) || defined(__x86_64__) || defined(__amd64__)
371 				/* Reflect changes here to drmP.h */
372 #define DRM_CAS(lock,old,new,__ret)                                    \
373 	do {                                                           \
374                 int __dummy;	/* Can't mark eax as clobbered */      \
375 		__asm__ __volatile__(                                  \
376 			"lock ; cmpxchg %4,%1\n\t"                     \
377                         "setnz %0"                                     \
378 			: "=d" (__ret),                                \
379    			  "=m" (__drm_dummy_lock(lock)),               \
380                           "=a" (__dummy)                               \
381 			: "2" (old),                                   \
382 			  "r" (new));                                  \
383 	} while (0)
384 
385 #elif defined(__alpha__)
386 
387 #define	DRM_CAS(lock, old, new, ret)		\
388 	do {					\
389 		int tmp, old32;			\
390 		__asm__ __volatile__(		\
391 		"	addl	$31, %5, %3\n"	\
392 		"1:	ldl_l	%0, %2\n"	\
393 		"	cmpeq	%0, %3, %1\n"	\
394 		"	beq	%1, 2f\n"	\
395 		"	mov	%4, %0\n"	\
396 		"	stl_c	%0, %2\n"	\
397 		"	beq	%0, 3f\n"	\
398 		"	mb\n"			\
399 		"2:	cmpeq	%1, 0, %1\n"	\
400 		".subsection 2\n"		\
401 		"3:	br	1b\n"		\
402 		".previous"			\
403 		: "=&r"(tmp), "=&r"(ret),	\
404 		  "=m"(__drm_dummy_lock(lock)),	\
405 		  "=&r"(old32)			\
406 		: "r"(new), "r"(old)		\
407 		: "memory");			\
408 	} while (0)
409 
410 #elif defined(__sparc__)
411 
412 #define DRM_CAS(lock,old,new,__ret)				\
413 do {	register unsigned int __old __asm("o0");		\
414 	register unsigned int __new __asm("o1");		\
415 	register volatile unsigned int *__lock __asm("o2");	\
416 	__old = old;						\
417 	__new = new;						\
418 	__lock = (volatile unsigned int *)lock;			\
419 	__asm__ __volatile__(					\
420 		/*"cas [%2], %3, %0"*/				\
421 		".word 0xd3e29008\n\t"				\
422 		/*"membar #StoreStore | #StoreLoad"*/		\
423 		".word 0x8143e00a"				\
424 		: "=&r" (__new)					\
425 		: "0" (__new),					\
426 		  "r" (__lock),					\
427 		  "r" (__old)					\
428 		: "memory");					\
429 	__ret = (__new != __old);				\
430 } while(0)
431 
432 #elif defined(__ia64__)
433 
434 #ifdef __INTEL_COMPILER
435 /* this currently generates bad code (missing stop bits)... */
436 #include <ia64intrin.h>
437 
438 #define DRM_CAS(lock,old,new,__ret)					      \
439 	do {								      \
440 		unsigned long __result, __old = (old) & 0xffffffff;		\
441 		__mf();							      	\
442 		__result = _InterlockedCompareExchange_acq(&__drm_dummy_lock(lock), (new), __old);\
443 		__ret = (__result) != (__old);					\
444 /*		__ret = (__sync_val_compare_and_swap(&__drm_dummy_lock(lock), \
445 						     (old), (new))	      \
446 			 != (old));					      */\
447 	} while (0)
448 
449 #else
450 #define DRM_CAS(lock,old,new,__ret)					  \
451 	do {								  \
452 		unsigned int __result, __old = (old);			  \
453 		__asm__ __volatile__(					  \
454 			"mf\n"						  \
455 			"mov ar.ccv=%2\n"				  \
456 			";;\n"						  \
457 			"cmpxchg4.acq %0=%1,%3,ar.ccv"			  \
458 			: "=r" (__result), "=m" (__drm_dummy_lock(lock))  \
459 			: "r" ((unsigned long)__old), "r" (new)			  \
460 			: "memory");					  \
461 		__ret = (__result) != (__old);				  \
462 	} while (0)
463 
464 #endif
465 
466 #elif defined(__powerpc__)
467 
468 #define DRM_CAS(lock,old,new,__ret)			\
469 	do {						\
470 		__asm__ __volatile__(			\
471 			"sync;"				\
472 			"0:    lwarx %0,0,%1;"		\
473 			"      xor. %0,%3,%0;"		\
474 			"      bne 1f;"			\
475 			"      stwcx. %2,0,%1;"		\
476 			"      bne- 0b;"		\
477 			"1:    "			\
478 			"sync;"				\
479 		: "=&r"(__ret)				\
480 		: "r"(lock), "r"(new), "r"(old)		\
481 		: "cr0", "memory");			\
482 	} while (0)
483 
484 # elif defined (__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) \
485 	|| defined (__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) \
486 	|| defined (__ARM_ARCH_6K__) || defined(__ARM_ARCH_6T2__) \
487 	|| defined (__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) \
488 	|| defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) \
489 	|| defined(__ARM_ARCH_7EM__)
490        /* excluding ARMv4/ARMv5 and lower (lacking ldrex/strex support) */
491        #undef DRM_DEV_MODE
492        #define DRM_DEV_MODE     (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
493 
494        #define DRM_CAS(lock,old,new,__ret)             \
495        do {                                            \
496                __asm__ __volatile__ (                  \
497                        "1: ldrex %0, [%1]\n"           \
498                        "   teq %0, %2\n"               \
499                        "   ite eq\n"                   \
500                        "   strexeq %0, %3, [%1]\n"     \
501                        "   movne   %0, #1\n"           \
502                : "=&r" (__ret)                         \
503                : "r" (lock), "r" (old), "r" (new)      \
504                : "cc","memory");                       \
505        } while (0)
506 
507 #endif /* architecture */
508 #endif /* __GNUC__ >= 2 */
509 
510 #ifndef DRM_CAS
511 #define DRM_CAS(lock,old,new,ret) do { ret=1; } while (0) /* FAST LOCK FAILS */
512 #endif
513 
514 #if defined(__alpha__)
515 #define DRM_CAS_RESULT(_result)		long _result
516 #elif defined(__powerpc__)
517 #define DRM_CAS_RESULT(_result)		int _result
518 #else
519 #define DRM_CAS_RESULT(_result)		char _result
520 #endif
521 
522 #define DRM_LIGHT_LOCK(fd,lock,context)                                \
523 	do {                                                           \
524                 DRM_CAS_RESULT(__ret);                                 \
525 		DRM_CAS(lock,context,DRM_LOCK_HELD|context,__ret);     \
526                 if (__ret) drmGetLock(fd,context,0);                   \
527         } while(0)
528 
529 				/* This one counts fast locks -- for
530                                    benchmarking only. */
531 #define DRM_LIGHT_LOCK_COUNT(fd,lock,context,count)                    \
532 	do {                                                           \
533                 DRM_CAS_RESULT(__ret);                                 \
534 		DRM_CAS(lock,context,DRM_LOCK_HELD|context,__ret);     \
535                 if (__ret) drmGetLock(fd,context,0);                   \
536                 else       ++count;                                    \
537         } while(0)
538 
539 #define DRM_LOCK(fd,lock,context,flags)                                \
540 	do {                                                           \
541 		if (flags) drmGetLock(fd,context,flags);               \
542 		else       DRM_LIGHT_LOCK(fd,lock,context);            \
543 	} while(0)
544 
545 #define DRM_UNLOCK(fd,lock,context)                                    \
546 	do {                                                           \
547                 DRM_CAS_RESULT(__ret);                                 \
548 		DRM_CAS(lock,DRM_LOCK_HELD|context,context,__ret);     \
549                 if (__ret) drmUnlock(fd,context);                      \
550         } while(0)
551 
552 				/* Simple spin locks */
553 #define DRM_SPINLOCK(spin,val)                                         \
554 	do {                                                           \
555             DRM_CAS_RESULT(__ret);                                     \
556 	    do {                                                       \
557 		DRM_CAS(spin,0,val,__ret);                             \
558 		if (__ret) while ((spin)->lock);                       \
559 	    } while (__ret);                                           \
560 	} while(0)
561 
562 #define DRM_SPINLOCK_TAKE(spin,val)                                    \
563 	do {                                                           \
564             DRM_CAS_RESULT(__ret);                                     \
565             int  cur;                                                  \
566 	    do {                                                       \
567                 cur = (*spin).lock;                                    \
568 		DRM_CAS(spin,cur,val,__ret);                           \
569 	    } while (__ret);                                           \
570 	} while(0)
571 
572 #define DRM_SPINLOCK_COUNT(spin,val,count,__ret)                       \
573 	do {                                                           \
574             int  __i;                                                  \
575             __ret = 1;                                                 \
576             for (__i = 0; __ret && __i < count; __i++) {               \
577 		DRM_CAS(spin,0,val,__ret);                             \
578 		if (__ret) for (;__i < count && (spin)->lock; __i++);  \
579 	    }                                                          \
580 	} while(0)
581 
582 #define DRM_SPINUNLOCK(spin,val)                                       \
583 	do {                                                           \
584             DRM_CAS_RESULT(__ret);                                     \
585             if ((*spin).lock == val) { /* else server stole lock */    \
586 	        do {                                                   \
587 		    DRM_CAS(spin,val,0,__ret);                         \
588 	        } while (__ret);                                       \
589             }                                                          \
590 	} while(0)
591 
592 
593 
594 /* General user-level programmer's API: unprivileged */
595 extern int           drmAvailable(void);
596 extern int           drmOpen(const char *name, const char *busid);
597 
598 #define DRM_NODE_PRIMARY 0
599 #define DRM_NODE_CONTROL 1
600 #define DRM_NODE_RENDER  2
601 #define DRM_NODE_MAX     3
602 
603 extern int           drmOpenWithType(const char *name, const char *busid,
604                                      int type);
605 
606 extern int           drmOpenControl(int minor);
607 extern int           drmOpenRender(int minor);
608 extern int           drmClose(int fd);
609 extern drmVersionPtr drmGetVersion(int fd);
610 extern drmVersionPtr drmGetLibVersion(int fd);
611 extern int           drmGetCap(int fd, uint64_t capability, uint64_t *value);
612 extern void          drmFreeVersion(drmVersionPtr);
613 extern int           drmGetMagic(int fd, drm_magic_t * magic);
614 extern char          *drmGetBusid(int fd);
615 extern int           drmGetInterruptFromBusID(int fd, int busnum, int devnum,
616 					      int funcnum);
617 extern int           drmGetMap(int fd, int idx, drm_handle_t *offset,
618 			       drmSize *size, drmMapType *type,
619 			       drmMapFlags *flags, drm_handle_t *handle,
620 			       int *mtrr);
621 extern int           drmGetClient(int fd, int idx, int *auth, int *pid,
622 				  int *uid, unsigned long *magic,
623 				  unsigned long *iocs);
624 extern int           drmGetStats(int fd, drmStatsT *stats);
625 extern int           drmSetInterfaceVersion(int fd, drmSetVersion *version);
626 extern int           drmCommandNone(int fd, unsigned long drmCommandIndex);
627 extern int           drmCommandRead(int fd, unsigned long drmCommandIndex,
628                                     void *data, unsigned long size);
629 extern int           drmCommandWrite(int fd, unsigned long drmCommandIndex,
630                                      void *data, unsigned long size);
631 extern int           drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
632                                          void *data, unsigned long size);
633 
634 /* General user-level programmer's API: X server (root) only  */
635 extern void          drmFreeBusid(const char *busid);
636 extern int           drmSetBusid(int fd, const char *busid);
637 extern int           drmAuthMagic(int fd, drm_magic_t magic);
638 extern int           drmAddMap(int fd,
639 			       drm_handle_t offset,
640 			       drmSize size,
641 			       drmMapType type,
642 			       drmMapFlags flags,
643 			       drm_handle_t * handle);
644 extern int	     drmRmMap(int fd, drm_handle_t handle);
645 extern int	     drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
646 						 drm_handle_t handle);
647 
648 extern int           drmAddBufs(int fd, int count, int size,
649 				drmBufDescFlags flags,
650 				int agp_offset);
651 extern int           drmMarkBufs(int fd, double low, double high);
652 extern int           drmCreateContext(int fd, drm_context_t * handle);
653 extern int           drmSetContextFlags(int fd, drm_context_t context,
654 					drm_context_tFlags flags);
655 extern int           drmGetContextFlags(int fd, drm_context_t context,
656 					drm_context_tFlagsPtr flags);
657 extern int           drmAddContextTag(int fd, drm_context_t context, void *tag);
658 extern int           drmDelContextTag(int fd, drm_context_t context);
659 extern void          *drmGetContextTag(int fd, drm_context_t context);
660 extern drm_context_t * drmGetReservedContextList(int fd, int *count);
661 extern void          drmFreeReservedContextList(drm_context_t *);
662 extern int           drmSwitchToContext(int fd, drm_context_t context);
663 extern int           drmDestroyContext(int fd, drm_context_t handle);
664 extern int           drmCreateDrawable(int fd, drm_drawable_t * handle);
665 extern int           drmDestroyDrawable(int fd, drm_drawable_t handle);
666 extern int           drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
667 					   drm_drawable_info_type_t type,
668 					   unsigned int num, void *data);
669 extern int           drmCtlInstHandler(int fd, int irq);
670 extern int           drmCtlUninstHandler(int fd);
671 extern int           drmSetClientCap(int fd, uint64_t capability,
672 				     uint64_t value);
673 
674 extern int           drmCrtcGetSequence(int fd, uint32_t crtcId,
675 					uint64_t *sequence, uint64_t *ns);
676 extern int           drmCrtcQueueSequence(int fd, uint32_t crtcId,
677 					  uint32_t flags, uint64_t sequence,
678 					  uint64_t *sequence_queued,
679 					  uint64_t user_data);
680 /* General user-level programmer's API: authenticated client and/or X */
681 extern int           drmMap(int fd,
682 			    drm_handle_t handle,
683 			    drmSize size,
684 			    drmAddressPtr address);
685 extern int           drmUnmap(drmAddress address, drmSize size);
686 extern drmBufInfoPtr drmGetBufInfo(int fd);
687 extern drmBufMapPtr  drmMapBufs(int fd);
688 extern int           drmUnmapBufs(drmBufMapPtr bufs);
689 extern int           drmDMA(int fd, drmDMAReqPtr request);
690 extern int           drmFreeBufs(int fd, int count, int *list);
691 extern int           drmGetLock(int fd,
692 			        drm_context_t context,
693 			        drmLockFlags flags);
694 extern int           drmUnlock(int fd, drm_context_t context);
695 extern int           drmFinish(int fd, int context, drmLockFlags flags);
696 extern int	     drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
697 						 drm_handle_t * handle);
698 
699 /* AGP/GART support: X server (root) only */
700 extern int           drmAgpAcquire(int fd);
701 extern int           drmAgpRelease(int fd);
702 extern int           drmAgpEnable(int fd, unsigned long mode);
703 extern int           drmAgpAlloc(int fd, unsigned long size,
704 				 unsigned long type, unsigned long *address,
705 				 drm_handle_t *handle);
706 extern int           drmAgpFree(int fd, drm_handle_t handle);
707 extern int 	     drmAgpBind(int fd, drm_handle_t handle,
708 				unsigned long offset);
709 extern int           drmAgpUnbind(int fd, drm_handle_t handle);
710 
711 /* AGP/GART info: authenticated client and/or X */
712 extern int           drmAgpVersionMajor(int fd);
713 extern int           drmAgpVersionMinor(int fd);
714 extern unsigned long drmAgpGetMode(int fd);
715 extern unsigned long drmAgpBase(int fd); /* Physical location */
716 extern unsigned long drmAgpSize(int fd); /* Bytes */
717 extern unsigned long drmAgpMemoryUsed(int fd);
718 extern unsigned long drmAgpMemoryAvail(int fd);
719 extern unsigned int  drmAgpVendorId(int fd);
720 extern unsigned int  drmAgpDeviceId(int fd);
721 
722 /* PCI scatter/gather support: X server (root) only */
723 extern int           drmScatterGatherAlloc(int fd, unsigned long size,
724 					   drm_handle_t *handle);
725 extern int           drmScatterGatherFree(int fd, drm_handle_t handle);
726 
727 extern int           drmWaitVBlank(int fd, drmVBlankPtr vbl);
728 
729 /* Support routines */
730 extern void          drmSetServerInfo(drmServerInfoPtr info);
731 extern int           drmError(int err, const char *label);
732 extern void          *drmMalloc(int size);
733 extern void          drmFree(void *pt);
734 
735 /* Hash table routines */
736 extern void *drmHashCreate(void);
737 extern int  drmHashDestroy(void *t);
738 extern int  drmHashLookup(void *t, unsigned long key, void **value);
739 extern int  drmHashInsert(void *t, unsigned long key, void *value);
740 extern int  drmHashDelete(void *t, unsigned long key);
741 extern int  drmHashFirst(void *t, unsigned long *key, void **value);
742 extern int  drmHashNext(void *t, unsigned long *key, void **value);
743 
744 /* PRNG routines */
745 extern void          *drmRandomCreate(unsigned long seed);
746 extern int           drmRandomDestroy(void *state);
747 extern unsigned long drmRandom(void *state);
748 extern double        drmRandomDouble(void *state);
749 
750 /* Skip list routines */
751 
752 extern void *drmSLCreate(void);
753 extern int  drmSLDestroy(void *l);
754 extern int  drmSLLookup(void *l, unsigned long key, void **value);
755 extern int  drmSLInsert(void *l, unsigned long key, void *value);
756 extern int  drmSLDelete(void *l, unsigned long key);
757 extern int  drmSLNext(void *l, unsigned long *key, void **value);
758 extern int  drmSLFirst(void *l, unsigned long *key, void **value);
759 extern void drmSLDump(void *l);
760 extern int  drmSLLookupNeighbors(void *l, unsigned long key,
761 				 unsigned long *prev_key, void **prev_value,
762 				 unsigned long *next_key, void **next_value);
763 
764 extern int drmOpenOnce(void *unused, const char *BusID, int *newlyopened);
765 extern int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type);
766 extern void drmCloseOnce(int fd);
767 extern void drmMsg(const char *format, ...) DRM_PRINTFLIKE(1, 2);
768 
769 extern int drmSetMaster(int fd);
770 extern int drmDropMaster(int fd);
771 extern int drmIsMaster(int fd);
772 
773 #define DRM_EVENT_CONTEXT_VERSION 4
774 
775 typedef struct _drmEventContext {
776 
777 	/* This struct is versioned so we can add more pointers if we
778 	 * add more events. */
779 	int version;
780 
781 	void (*vblank_handler)(int fd,
782 			       unsigned int sequence,
783 			       unsigned int tv_sec,
784 			       unsigned int tv_usec,
785 			       void *user_data);
786 
787 	void (*page_flip_handler)(int fd,
788 				  unsigned int sequence,
789 				  unsigned int tv_sec,
790 				  unsigned int tv_usec,
791 				  void *user_data);
792 
793 	void (*page_flip_handler2)(int fd,
794 				   unsigned int sequence,
795 				   unsigned int tv_sec,
796 				   unsigned int tv_usec,
797 				   unsigned int crtc_id,
798 				   void *user_data);
799 
800 	void (*sequence_handler)(int fd,
801 				 uint64_t sequence,
802 				 uint64_t ns,
803 				 uint64_t user_data);
804 } drmEventContext, *drmEventContextPtr;
805 
806 extern int drmHandleEvent(int fd, drmEventContextPtr evctx);
807 
808 extern char *drmGetDeviceNameFromFd(int fd);
809 
810 /* Improved version of drmGetDeviceNameFromFd which attributes for any type of
811  * device/node - card, control or renderD.
812  */
813 extern char *drmGetDeviceNameFromFd2(int fd);
814 extern int drmGetNodeTypeFromFd(int fd);
815 
816 /* Convert between GEM handles and DMA-BUF file descriptors.
817  *
818  * Warning: since GEM handles are not reference-counted and are unique per
819  * DRM file description, the caller is expected to perform its own reference
820  * counting. drmPrimeFDToHandle is guaranteed to return the same handle for
821  * different FDs if they reference the same underlying buffer object. This
822  * could even be a buffer object originally created on the same DRM FD.
823  *
824  * When sharing a DRM FD with an API such as EGL or GBM, the caller must not
825  * use drmPrimeHandleToFD nor drmPrimeFDToHandle. A single user-space
826  * reference-counting implementation is necessary to avoid double-closing GEM
827  * handles.
828  *
829  * Two processes can't share the same DRM FD and both use it to create or
830  * import GEM handles, even when using a single user-space reference-counting
831  * implementation like GBM, because GBM doesn't share its state between
832  * processes.
833  */
834 extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
835 extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
836 
837 extern char *drmGetPrimaryDeviceNameFromFd(int fd);
838 extern char *drmGetRenderDeviceNameFromFd(int fd);
839 
840 #define DRM_BUS_PCI       0
841 #define DRM_BUS_USB       1
842 #define DRM_BUS_PLATFORM  2
843 #define DRM_BUS_HOST1X    3
844 
845 typedef struct _drmPciBusInfo {
846     uint16_t domain;
847     uint8_t bus;
848     uint8_t dev;
849     uint8_t func;
850 } drmPciBusInfo, *drmPciBusInfoPtr;
851 
852 typedef struct _drmPciDeviceInfo {
853     uint16_t vendor_id;
854     uint16_t device_id;
855     uint16_t subvendor_id;
856     uint16_t subdevice_id;
857     uint8_t revision_id;
858 } drmPciDeviceInfo, *drmPciDeviceInfoPtr;
859 
860 typedef struct _drmUsbBusInfo {
861     uint8_t bus;
862     uint8_t dev;
863 } drmUsbBusInfo, *drmUsbBusInfoPtr;
864 
865 typedef struct _drmUsbDeviceInfo {
866     uint16_t vendor;
867     uint16_t product;
868 } drmUsbDeviceInfo, *drmUsbDeviceInfoPtr;
869 
870 #define DRM_PLATFORM_DEVICE_NAME_LEN 512
871 
872 typedef struct _drmPlatformBusInfo {
873     char fullname[DRM_PLATFORM_DEVICE_NAME_LEN];
874 } drmPlatformBusInfo, *drmPlatformBusInfoPtr;
875 
876 typedef struct _drmPlatformDeviceInfo {
877     char **compatible; /* NULL terminated list of compatible strings */
878 } drmPlatformDeviceInfo, *drmPlatformDeviceInfoPtr;
879 
880 #define DRM_HOST1X_DEVICE_NAME_LEN 512
881 
882 typedef struct _drmHost1xBusInfo {
883     char fullname[DRM_HOST1X_DEVICE_NAME_LEN];
884 } drmHost1xBusInfo, *drmHost1xBusInfoPtr;
885 
886 typedef struct _drmHost1xDeviceInfo {
887     char **compatible; /* NULL terminated list of compatible strings */
888 } drmHost1xDeviceInfo, *drmHost1xDeviceInfoPtr;
889 
890 typedef struct _drmDevice {
891     char **nodes; /* DRM_NODE_MAX sized array */
892     int available_nodes; /* DRM_NODE_* bitmask */
893     int bustype;
894     union {
895         drmPciBusInfoPtr pci;
896         drmUsbBusInfoPtr usb;
897         drmPlatformBusInfoPtr platform;
898         drmHost1xBusInfoPtr host1x;
899     } businfo;
900     union {
901         drmPciDeviceInfoPtr pci;
902         drmUsbDeviceInfoPtr usb;
903         drmPlatformDeviceInfoPtr platform;
904         drmHost1xDeviceInfoPtr host1x;
905     } deviceinfo;
906 } drmDevice, *drmDevicePtr;
907 
908 extern int drmGetDevice(int fd, drmDevicePtr *device);
909 extern void drmFreeDevice(drmDevicePtr *device);
910 
911 extern int drmGetDevices(drmDevicePtr devices[], int max_devices);
912 extern void drmFreeDevices(drmDevicePtr devices[], int count);
913 
914 #define DRM_DEVICE_GET_PCI_REVISION (1 << 0)
915 extern int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device);
916 extern int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices);
917 
918 extern int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b);
919 
920 extern int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle);
921 extern int drmSyncobjDestroy(int fd, uint32_t handle);
922 extern int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd);
923 extern int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle);
924 
925 extern int drmSyncobjImportSyncFile(int fd, uint32_t handle, int sync_file_fd);
926 extern int drmSyncobjExportSyncFile(int fd, uint32_t handle, int *sync_file_fd);
927 extern int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles,
928 			  int64_t timeout_nsec, unsigned flags,
929 			  uint32_t *first_signaled);
930 extern int drmSyncobjReset(int fd, const uint32_t *handles, uint32_t handle_count);
931 extern int drmSyncobjSignal(int fd, const uint32_t *handles, uint32_t handle_count);
932 extern int drmSyncobjTimelineSignal(int fd, const uint32_t *handles,
933 				    uint64_t *points, uint32_t handle_count);
934 extern int drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points,
935 				  unsigned num_handles,
936 				  int64_t timeout_nsec, unsigned flags,
937 				  uint32_t *first_signaled);
938 extern int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points,
939 			   uint32_t handle_count);
940 extern int drmSyncobjQuery2(int fd, uint32_t *handles, uint64_t *points,
941 			    uint32_t handle_count, uint32_t flags);
942 extern int drmSyncobjTransfer(int fd,
943 			      uint32_t dst_handle, uint64_t dst_point,
944 			      uint32_t src_handle, uint64_t src_point,
945 			      uint32_t flags);
946 
947 #if defined(__cplusplus)
948 }
949 #endif
950 
951 #endif
952