3 * OS-independent header for DRM user-level library interface.
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
9 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
10 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
11 * All Rights Reserved.
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:
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
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.
34 /* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/xf86drm.h,v 1.26 2003/08/16 19:26:37 dawes Exp $ */
41 /* Defaults, if nothing set in xf86config */
44 /* Default /dev/dri directory permissions 0755 */
45 #define DRM_DEV_DIRMODE \
46 (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
47 #define DRM_DEV_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
49 #define DRM_DIR_NAME "/dev/dri"
50 #define DRM_DEV_NAME "%s/card%d"
51 #define DRM_PROC_NAME "/proc/dri/" /* For backward Linux compatibility */
53 #define DRM_ERR_NO_DEVICE (-1001)
54 #define DRM_ERR_NO_ACCESS (-1002)
55 #define DRM_ERR_NOT_ROOT (-1003)
56 #define DRM_ERR_INVALID (-1004)
57 #define DRM_ERR_NO_FD (-1005)
59 #define DRM_AGP_NO_HANDLE 0
61 typedef unsigned int drmSize, *drmSizePtr; /**< For mapped regions */
62 typedef void *drmAddress, **drmAddressPtr; /**< For mapped regions */
65 * Driver version information.
67 * \sa drmGetVersion() and drmSetVersion().
69 typedef struct _drmVersion {
70 int version_major; /**< Major version */
71 int version_minor; /**< Minor version */
72 int version_patchlevel; /**< Patch level */
73 int name_len; /**< Length of name buffer */
74 char *name; /**< Name of driver */
75 int date_len; /**< Length of date buffer */
76 char *date; /**< User-space buffer to hold date */
77 int desc_len; /**< Length of desc buffer */
78 char *desc; /**< User-space buffer to hold desc */
79 } drmVersion, *drmVersionPtr;
81 typedef struct _drmStats {
82 unsigned long count; /**< Number of data */
84 unsigned long value; /**< Value from kernel */
85 const char *long_format; /**< Suggested format for long_name */
86 const char *long_name; /**< Long name for value */
87 const char *rate_format; /**< Suggested format for rate_name */
88 const char *rate_name; /**< Short name for value per second */
89 int isvalue; /**< True if value (vs. counter) */
90 const char *mult_names; /**< Multiplier names (e.g., "KGM") */
91 int mult; /**< Multiplier value (e.g., 1024) */
92 int verbose; /**< Suggest only in verbose output */
97 /* All of these enums *MUST* match with the
98 kernel implementation -- so do *NOT*
99 change them! (The drmlib implementation
100 will just copy the flags instead of
101 translating them.) */
103 DRM_FRAME_BUFFER = 0, /**< WC, no caching, no core dump */
104 DRM_REGISTERS = 1, /**< no caching, no core dump */
105 DRM_SHM = 2, /**< shared, cached */
106 DRM_AGP = 3, /**< AGP/GART */
107 DRM_SCATTER_GATHER = 4, /**< PCI scatter/gather */
108 DRM_CONSISTENT = 5 /**< PCI consistent */
112 DRM_RESTRICTED = 0x0001, /**< Cannot be mapped to client-virtual */
113 DRM_READ_ONLY = 0x0002, /**< Read-only in client-virtual */
114 DRM_LOCKED = 0x0004, /**< Physical pages locked */
115 DRM_KERNEL = 0x0008, /**< Kernel requires access */
116 DRM_WRITE_COMBINING = 0x0010, /**< Use write-combining, if available */
117 DRM_CONTAINS_LOCK = 0x0020, /**< SHM page that contains lock */
118 DRM_REMOVABLE = 0x0040 /**< Removable mapping */
122 * \warning These values *MUST* match drm.h
125 /** \name Flags for DMA buffer dispatch */
127 DRM_DMA_BLOCK = 0x01, /**<
128 * Block until buffer dispatched.
130 * \note the buffer may not yet have been
131 * processed by the hardware -- getting a
132 * hardware lock with the hardware quiescent
133 * will ensure that the buffer has been
136 DRM_DMA_WHILE_LOCKED = 0x02, /**< Dispatch while lock held */
137 DRM_DMA_PRIORITY = 0x04, /**< High priority dispatch */
140 /** \name Flags for DMA buffer request */
142 DRM_DMA_WAIT = 0x10, /**< Wait for free buffers */
143 DRM_DMA_SMALLER_OK = 0x20, /**< Smaller-than-requested buffers OK */
144 DRM_DMA_LARGER_OK = 0x40 /**< Larger-than-requested buffers OK */
149 DRM_PAGE_ALIGN = 0x01,
150 DRM_AGP_BUFFER = 0x02,
151 DRM_SG_BUFFER = 0x04,
156 DRM_LOCK_READY = 0x01, /**< Wait until hardware is ready for DMA */
157 DRM_LOCK_QUIESCENT = 0x02, /**< Wait until hardware quiescent */
158 DRM_LOCK_FLUSH = 0x04, /**< Flush this context's DMA queue first */
159 DRM_LOCK_FLUSH_ALL = 0x08, /**< Flush all DMA queues first */
160 /* These *HALT* flags aren't supported yet
161 -- they will be used to support the
162 full-screen DGA-like mode. */
163 DRM_HALT_ALL_QUEUES = 0x10, /**< Halt all current and future queues */
164 DRM_HALT_CUR_QUEUES = 0x20 /**< Halt all current queues */
168 DRM_CONTEXT_PRESERVED = 0x01, /**< This context is preserved and
170 DRM_CONTEXT_2DONLY = 0x02 /**< This context is for 2D rendering only. */
171 } drm_context_tFlags, *drm_context_tFlagsPtr;
173 typedef struct _drmBufDesc {
174 int count; /**< Number of buffers of this size */
175 int size; /**< Size in bytes */
176 int low_mark; /**< Low water mark */
177 int high_mark; /**< High water mark */
178 } drmBufDesc, *drmBufDescPtr;
180 typedef struct _drmBufInfo {
181 int count; /**< Number of buffers described in list */
182 drmBufDescPtr list; /**< List of buffer descriptions */
183 } drmBufInfo, *drmBufInfoPtr;
185 typedef struct _drmBuf {
186 int idx; /**< Index into the master buffer list */
187 int total; /**< Buffer size */
188 int used; /**< Amount of buffer in use (for DMA) */
189 drmAddress address; /**< Address */
190 } drmBuf, *drmBufPtr;
193 * Buffer mapping information.
195 * Used by drmMapBufs() and drmUnmapBufs() to store information about the
198 typedef struct _drmBufMap {
199 int count; /**< Number of buffers mapped */
200 drmBufPtr list; /**< Buffers */
201 } drmBufMap, *drmBufMapPtr;
203 typedef struct _drmLock {
204 volatile unsigned int lock;
206 /* This is big enough for most current (and future?) architectures:
209 Intel P5/PPro/PII/PIII: 32 bytes
210 Intel StrongARM: 32 bytes
211 Intel i386/i486: 16 bytes
213 Motorola 68k: 16 bytes
214 Motorola PowerPC: 32 bytes
217 } drmLock, *drmLockPtr;
220 * Indices here refer to the offset into
223 typedef struct _drmDMAReq {
224 drm_context_t context; /**< Context handle */
225 int send_count; /**< Number of buffers to send */
226 int *send_list; /**< List of handles to buffers */
227 int *send_sizes; /**< Lengths of data to send, in bytes */
228 drmDMAFlags flags; /**< Flags */
229 int request_count; /**< Number of buffers requested */
230 int request_size; /**< Desired size of buffers requested */
231 int *request_list; /**< Buffer information */
232 int *request_sizes; /**< Minimum acceptable sizes */
233 int granted_count; /**< Number of buffers granted at this size */
234 } drmDMAReq, *drmDMAReqPtr;
236 typedef struct _drmRegion {
241 } drmRegion, *drmRegionPtr;
243 typedef struct _drmTextureRegion {
246 unsigned char in_use;
247 unsigned char padding; /**< Explicitly pad this out */
249 } drmTextureRegion, *drmTextureRegionPtr;
253 DRM_VBLANK_ABSOLUTE = 0x0, /**< Wait for specific vblank sequence number */
254 DRM_VBLANK_RELATIVE = 0x1, /**< Wait for given number of vblanks */
255 DRM_VBLANK_SIGNAL = 0x40000000 /* Send signal instead of blocking */
258 typedef struct _drmVBlankReq {
259 drmVBlankSeqType type;
260 unsigned int sequence;
261 unsigned long signal;
262 } drmVBlankReq, *drmVBlankReqPtr;
264 typedef struct _drmVBlankReply {
265 drmVBlankSeqType type;
266 unsigned int sequence;
269 } drmVBlankReply, *drmVBlankReplyPtr;
271 typedef union _drmVBlank {
272 drmVBlankReq request;
273 drmVBlankReply reply;
274 } drmVBlank, *drmVBlankPtr;
276 typedef struct _drmSetVersion {
281 } drmSetVersion, *drmSetVersionPtr;
283 typedef struct _drmFence{
290 typedef struct _drmTTM{
292 drm_handle_t user_token;
299 #define __drm_dummy_lock(lock) (*(__volatile__ unsigned int *)lock)
301 #define DRM_LOCK_HELD 0x80000000U /**< Hardware lock is held */
302 #define DRM_LOCK_CONT 0x40000000U /**< Hardware lock is contended */
304 #if defined(__GNUC__) && (__GNUC__ >= 2)
305 # if defined(__i386) || defined(__AMD64__) || defined(__x86_64__) || defined(__amd64__)
306 /* Reflect changes here to drmP.h */
307 #define DRM_CAS(lock,old,new,__ret) \
309 int __dummy; /* Can't mark eax as clobbered */ \
310 __asm__ __volatile__( \
311 "lock ; cmpxchg %4,%1\n\t" \
314 "=m" (__drm_dummy_lock(lock)), \
320 #elif defined(__alpha__)
322 #define DRM_CAS(lock, old, new, ret) \
326 __asm__ __volatile__( \
328 " zap %4, 0xF0, %0\n" \
330 " zap %1, 0xF0, %1\n" \
331 " cmpeq %0, %1, %1\n" \
333 " bis %5, %5, %1\n" \
335 "1: xor %1, 1, %1\n" \
339 "=m" (__drm_dummy_lock(lock)),\
345 #elif defined(__sparc__)
347 #define DRM_CAS(lock,old,new,__ret) \
348 do { register unsigned int __old __asm("o0"); \
349 register unsigned int __new __asm("o1"); \
350 register volatile unsigned int *__lock __asm("o2"); \
353 __lock = (volatile unsigned int *)lock; \
354 __asm__ __volatile__( \
355 /*"cas [%2], %3, %0"*/ \
356 ".word 0xd3e29008\n\t" \
357 /*"membar #StoreStore | #StoreLoad"*/ \
364 __ret = (__new != __old); \
367 #elif defined(__ia64__)
369 #ifdef __INTEL_COMPILER
370 /* this currently generates bad code (missing stop bits)... */
371 #include <ia64intrin.h>
373 #define DRM_CAS(lock,old,new,__ret) \
375 unsigned long __result, __old = (old) & 0xffffffff; \
377 __result = _InterlockedCompareExchange_acq(&__drm_dummy_lock(lock), (new), __old);\
378 __ret = (__result) != (__old); \
379 /* __ret = (__sync_val_compare_and_swap(&__drm_dummy_lock(lock), \
385 #define DRM_CAS(lock,old,new,__ret) \
387 unsigned int __result, __old = (old); \
388 __asm__ __volatile__( \
392 "cmpxchg4.acq %0=%1,%3,ar.ccv" \
393 : "=r" (__result), "=m" (__drm_dummy_lock(lock)) \
394 : "r" ((unsigned long)__old), "r" (new) \
396 __ret = (__result) != (__old); \
401 #elif defined(__powerpc__)
403 #define DRM_CAS(lock,old,new,__ret) \
405 __asm__ __volatile__( \
407 "0: lwarx %0,0,%1;" \
415 : "r"(lock), "r"(new), "r"(old) \
416 : "cr0", "memory"); \
419 #endif /* architecture */
420 #endif /* __GNUC__ >= 2 */
423 #define DRM_CAS(lock,old,new,ret) do { ret=1; } while (0) /* FAST LOCK FAILS */
426 #if defined(__alpha__) || defined(__powerpc__)
427 #define DRM_CAS_RESULT(_result) int _result
429 #define DRM_CAS_RESULT(_result) char _result
432 #define DRM_LIGHT_LOCK(fd,lock,context) \
434 DRM_CAS_RESULT(__ret); \
435 DRM_CAS(lock,context,DRM_LOCK_HELD|context,__ret); \
436 if (__ret) drmGetLock(fd,context,0); \
439 /* This one counts fast locks -- for
440 benchmarking only. */
441 #define DRM_LIGHT_LOCK_COUNT(fd,lock,context,count) \
443 DRM_CAS_RESULT(__ret); \
444 DRM_CAS(lock,context,DRM_LOCK_HELD|context,__ret); \
445 if (__ret) drmGetLock(fd,context,0); \
449 #define DRM_LOCK(fd,lock,context,flags) \
451 if (flags) drmGetLock(fd,context,flags); \
452 else DRM_LIGHT_LOCK(fd,lock,context); \
455 #define DRM_UNLOCK(fd,lock,context) \
457 DRM_CAS_RESULT(__ret); \
458 DRM_CAS(lock,DRM_LOCK_HELD|context,context,__ret); \
459 if (__ret) drmUnlock(fd,context); \
462 /* Simple spin locks */
463 #define DRM_SPINLOCK(spin,val) \
465 DRM_CAS_RESULT(__ret); \
467 DRM_CAS(spin,0,val,__ret); \
468 if (__ret) while ((spin)->lock); \
472 #define DRM_SPINLOCK_TAKE(spin,val) \
474 DRM_CAS_RESULT(__ret); \
477 cur = (*spin).lock; \
478 DRM_CAS(spin,cur,val,__ret); \
482 #define DRM_SPINLOCK_COUNT(spin,val,count,__ret) \
486 for (__i = 0; __ret && __i < count; __i++) { \
487 DRM_CAS(spin,0,val,__ret); \
488 if (__ret) for (;__i < count && (spin)->lock; __i++); \
492 #define DRM_SPINUNLOCK(spin,val) \
494 DRM_CAS_RESULT(__ret); \
495 if ((*spin).lock == val) { /* else server stole lock */ \
497 DRM_CAS(spin,val,0,__ret); \
504 /* General user-level programmer's API: unprivileged */
505 extern int drmAvailable(void);
506 extern int drmOpen(const char *name, const char *busid);
507 extern int drmClose(int fd);
508 extern drmVersionPtr drmGetVersion(int fd);
509 extern drmVersionPtr drmGetLibVersion(int fd);
510 extern void drmFreeVersion(drmVersionPtr);
511 extern int drmGetMagic(int fd, drm_magic_t * magic);
512 extern char *drmGetBusid(int fd);
513 extern int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
515 extern int drmGetMap(int fd, int idx, drm_handle_t *offset,
516 drmSize *size, drmMapType *type,
517 drmMapFlags *flags, drm_handle_t *handle,
519 extern int drmGetClient(int fd, int idx, int *auth, int *pid,
520 int *uid, unsigned long *magic,
521 unsigned long *iocs);
522 extern int drmGetStats(int fd, drmStatsT *stats);
523 extern int drmSetInterfaceVersion(int fd, drmSetVersion *version);
524 extern int drmCommandNone(int fd, unsigned long drmCommandIndex);
525 extern int drmCommandRead(int fd, unsigned long drmCommandIndex,
526 void *data, unsigned long size);
527 extern int drmCommandWrite(int fd, unsigned long drmCommandIndex,
528 void *data, unsigned long size);
529 extern int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
530 void *data, unsigned long size);
532 /* General user-level programmer's API: X server (root) only */
533 extern void drmFreeBusid(const char *busid);
534 extern int drmSetBusid(int fd, const char *busid);
535 extern int drmAuthMagic(int fd, drm_magic_t magic);
536 extern int drmAddMap(int fd,
541 drm_handle_t * handle);
542 extern int drmRmMap(int fd, drm_handle_t handle);
543 extern int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
544 drm_handle_t handle);
546 extern int drmAddBufs(int fd, int count, int size,
547 drmBufDescFlags flags,
549 extern int drmMarkBufs(int fd, double low, double high);
550 extern int drmCreateContext(int fd, drm_context_t * handle);
551 extern int drmSetContextFlags(int fd, drm_context_t context,
552 drm_context_tFlags flags);
553 extern int drmGetContextFlags(int fd, drm_context_t context,
554 drm_context_tFlagsPtr flags);
555 extern int drmAddContextTag(int fd, drm_context_t context, void *tag);
556 extern int drmDelContextTag(int fd, drm_context_t context);
557 extern void *drmGetContextTag(int fd, drm_context_t context);
558 extern drm_context_t * drmGetReservedContextList(int fd, int *count);
559 extern void drmFreeReservedContextList(drm_context_t *);
560 extern int drmSwitchToContext(int fd, drm_context_t context);
561 extern int drmDestroyContext(int fd, drm_context_t handle);
562 extern int drmCreateDrawable(int fd, drm_drawable_t * handle);
563 extern int drmDestroyDrawable(int fd, drm_drawable_t handle);
564 extern int drmCtlInstHandler(int fd, int irq);
565 extern int drmCtlUninstHandler(int fd);
567 /* General user-level programmer's API: authenticated client and/or X */
568 extern int drmMap(int fd,
571 drmAddressPtr address);
572 extern int drmUnmap(drmAddress address, drmSize size);
573 extern drmBufInfoPtr drmGetBufInfo(int fd);
574 extern drmBufMapPtr drmMapBufs(int fd);
575 extern int drmUnmapBufs(drmBufMapPtr bufs);
576 extern int drmDMA(int fd, drmDMAReqPtr request);
577 extern int drmFreeBufs(int fd, int count, int *list);
578 extern int drmGetLock(int fd,
579 drm_context_t context,
581 extern int drmUnlock(int fd, drm_context_t context);
582 extern int drmFinish(int fd, int context, drmLockFlags flags);
583 extern int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
584 drm_handle_t * handle);
586 /* AGP/GART support: X server (root) only */
587 extern int drmAgpAcquire(int fd);
588 extern int drmAgpRelease(int fd);
589 extern int drmAgpEnable(int fd, unsigned long mode);
590 extern int drmAgpAlloc(int fd, unsigned long size,
591 unsigned long type, unsigned long *address,
592 drm_handle_t *handle);
593 extern int drmAgpFree(int fd, drm_handle_t handle);
594 extern int drmAgpBind(int fd, drm_handle_t handle,
595 unsigned long offset);
596 extern int drmAgpUnbind(int fd, drm_handle_t handle);
598 /* AGP/GART info: authenticated client and/or X */
599 extern int drmAgpVersionMajor(int fd);
600 extern int drmAgpVersionMinor(int fd);
601 extern unsigned long drmAgpGetMode(int fd);
602 extern unsigned long drmAgpBase(int fd); /* Physical location */
603 extern unsigned long drmAgpSize(int fd); /* Bytes */
604 extern unsigned long drmAgpMemoryUsed(int fd);
605 extern unsigned long drmAgpMemoryAvail(int fd);
606 extern unsigned int drmAgpVendorId(int fd);
607 extern unsigned int drmAgpDeviceId(int fd);
609 /* PCI scatter/gather support: X server (root) only */
610 extern int drmScatterGatherAlloc(int fd, unsigned long size,
611 drm_handle_t *handle);
612 extern int drmScatterGatherFree(int fd, drm_handle_t handle);
614 extern int drmWaitVBlank(int fd, drmVBlankPtr vbl);
618 extern int drmFenceCreate(int fd, int shareable, int class,
619 unsigned type, int emit,
621 extern int drmFenceDestroy(int fd, const drmFence *fence);
622 extern int drmFenceReference(int fd, unsigned handle, drmFence *fence);
623 extern int drmFenceUnreference(int fd, const drmFence *fence);
624 extern int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type);
625 extern int drmFenceSignaled(int fd, drmFence *fence);
626 extern int drmFenceWait(int fd, drmFence *fence, unsigned flush_type,
627 int lazy, int ignore_signals);
628 extern int drmFenceEmit(int fd, drmFence *fence, unsigned emit_type);
631 extern int drmTTMCreate(int fd, drmTTM *ttm, unsigned long size,
633 extern int drmTTMDestroy(int fd, const drmTTM *ttm);
634 extern int drmTTMReference(int fd, unsigned handle, drmTTM *ttm);
635 extern int drmTTMUnreference(int fd, const drmTTM *ttm);
636 extern drm_handle_t drmTTMMapHandle(int fd, const drmTTM *ttm);
639 /* Support routines */
640 extern int drmError(int err, const char *label);
641 extern void *drmMalloc(int size);
642 extern void drmFree(void *pt);
644 /* Hash table routines */
645 extern void *drmHashCreate(void);
646 extern int drmHashDestroy(void *t);
647 extern int drmHashLookup(void *t, unsigned long key, void **value);
648 extern int drmHashInsert(void *t, unsigned long key, void *value);
649 extern int drmHashDelete(void *t, unsigned long key);
650 extern int drmHashFirst(void *t, unsigned long *key, void **value);
651 extern int drmHashNext(void *t, unsigned long *key, void **value);
654 extern void *drmRandomCreate(unsigned long seed);
655 extern int drmRandomDestroy(void *state);
656 extern unsigned long drmRandom(void *state);
657 extern double drmRandomDouble(void *state);
659 /* Skip list routines */
661 extern void *drmSLCreate(void);
662 extern int drmSLDestroy(void *l);
663 extern int drmSLLookup(void *l, unsigned long key, void **value);
664 extern int drmSLInsert(void *l, unsigned long key, void *value);
665 extern int drmSLDelete(void *l, unsigned long key);
666 extern int drmSLNext(void *l, unsigned long *key, void **value);
667 extern int drmSLFirst(void *l, unsigned long *key, void **value);
668 extern void drmSLDump(void *l);
669 extern int drmSLLookupNeighbors(void *l, unsigned long key,
670 unsigned long *prev_key, void **prev_value,
671 unsigned long *next_key, void **next_value);