1 /* drmP.h -- Private header for Direct Rendering Manager -*- linux-c -*-
2  * Created: Mon Jan  4 10:05:05 1999 by faith@precisioninsight.com
3  */
4 /*-
5  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
7  * All rights reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  *
28  * Authors:
29  *    Rickard E. (Rik) Faith <faith@valinux.com>
30  *    Gareth Hughes <gareth@valinux.com>
31  *
32  */
33 
34 #ifndef _DRM_P_H_
35 #define _DRM_P_H_
36 
37 #if defined(_KERNEL) || defined(__KERNEL__)
38 
39 struct drm_device;
40 struct drm_file;
41 
42 #if defined(__NetBSD__) && defined(_KERNEL_OPT)
43 #include "opt_drm.h"
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/malloc.h>
49 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/systm.h>
52 #include <sys/conf.h>
53 #include <sys/stat.h>
54 #if __FreeBSD_version >= 700000
55 #include <sys/priv.h>
56 #endif
57 #include <sys/proc.h>
58 #include <sys/lock.h>
59 #include <sys/fcntl.h>
60 #include <sys/uio.h>
61 #include <sys/filio.h>
62 #include <sys/sysctl.h>
63 #include <sys/bus.h>
64 #include <sys/signalvar.h>
65 #include <sys/poll.h>
66 #include <sys/tree.h>
67 #ifdef  __FreeBSD__
68 #include <vm/vm.h>
69 #include <vm/pmap.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_param.h>
73 #include <machine/resource.h>
74 #include <machine/param.h>
75 #endif
76 #include <machine/pmap.h>
77 #include <sys/bus.h>
78 #if defined(__i386__) || defined(__x86_64__)
79 #include <machine/specialreg.h>
80 #include <machine/sysarch.h>
81 #endif
82 #include <sys/endian.h>
83 #include <sys/mman.h>
84 #if defined( __FreeBSD__)
85 #include <sys/rman.h>
86 #include <sys/memrange.h>
87 #if __FreeBSD_version >= 800004
88 #include <dev/agp/agpvar.h>
89 #else /* __FreeBSD_version >= 800004 */
90 #include <pci/agpvar.h>
91 #endif /* __FreeBSD_version >= 800004 */
92 #include <sys/agpio.h>
93 #include <sys/mutex.h>
94 #include <dev/pci/pcivar.h>
95 #include <dev/pci/pcireg.h>
96 #include <sys/selinfo.h>
97 #include <sys/bus.h>
98 #elif   defined(__NetBSD__)
99 #if defined(__i386__) || defined(__x86_64__)
100 #include <machine/mtrr.h>
101 #endif
102 #include <sys/vnode.h>
103 #include <sys/select.h>
104 #include <sys/device.h>
105 #include <sys/resourcevar.h>
106 #include <sys/agpio.h>
107 #include <sys/ttycom.h>
108 #include <sys/mman.h>
109 #include <sys/kauth.h>
110 #include <sys/types.h>
111 #include <sys/file.h>
112 #include <sys/atomic.h>
113 #include <sys/workqueue.h>
114 #include <uvm/uvm.h>
115 #include <dev/pci/pcireg.h>
116 #include <dev/pci/pcivar.h>
117 #include <dev/pci/agpvar.h>
118 #endif
119 
120 #include "drm.h"
121 #include "drm_linux_list.h"
122 #include "drm_atomic.h"
123 #include "drm_internal.h"
124 
125 #ifdef  __FreeBSD__
126 #include <opt_drm.h>
127 #endif
128 
129 #ifdef DRM_DEBUG
130 #undef DRM_DEBUG
131 #define DRM_DEBUG_DEFAULT_ON 1
132 #endif /* DRM_DEBUG */
133 
134 #ifdef  __FreeBSD__
135 #define DRM_KASSERT(x,y)    KASSERT(x,y)
136 #else
137 #define DRM_KASSERT(x,y)    KASSERT(x)
138 #define device_get_softc(x)   device_private(x)
139 #define callout_deactivate(x) callout_stop(x)
140 #define callout_drain(x)      callout_stop(x)
141 #endif
142 
143 #if defined(DRM_LINUX) && DRM_LINUX && !defined(__amd64__)
144 #include <sys/file.h>
145 #include <sys/proc.h>
146 #include <machine/../linux/linux.h>
147 #include <machine/../linux/linux_proto.h>
148 #else
149 /* Either it was defined when it shouldn't be (FreeBSD amd64) or it isn't
150  * supported on this OS yet.
151  */
152 #undef DRM_LINUX
153 #define DRM_LINUX 0
154 #endif
155 
156 /* driver capabilities and requirements mask */
157 #define DRIVER_USE_AGP     0x1
158 #define DRIVER_REQUIRE_AGP 0x2
159 #define DRIVER_USE_MTRR    0x4
160 #define DRIVER_PCI_DMA     0x8
161 #define DRIVER_SG          0x10
162 #define DRIVER_HAVE_DMA    0x20
163 #define DRIVER_HAVE_IRQ    0x40
164 #define DRIVER_DMA_QUEUE   0x100
165 
166 
167 #define DRM_HASH_SIZE               16 /* Size of key hash table                  */
168 #define DRM_KERNEL_CONTEXT    0          /* Change drm_resctx if changed          */
169 #define DRM_RESERVED_CONTEXTS 1          /* Change drm_resctx if changed          */
170 
171 MALLOC_DECLARE(DRM_MEM_DMA);
172 MALLOC_DECLARE(DRM_MEM_SAREA);
173 MALLOC_DECLARE(DRM_MEM_DRIVER);
174 MALLOC_DECLARE(DRM_MEM_MAGIC);
175 MALLOC_DECLARE(DRM_MEM_IOCTLS);
176 MALLOC_DECLARE(DRM_MEM_MAPS);
177 MALLOC_DECLARE(DRM_MEM_BUFS);
178 MALLOC_DECLARE(DRM_MEM_SEGS);
179 MALLOC_DECLARE(DRM_MEM_PAGES);
180 MALLOC_DECLARE(DRM_MEM_FILES);
181 MALLOC_DECLARE(DRM_MEM_QUEUES);
182 MALLOC_DECLARE(DRM_MEM_CMDS);
183 MALLOC_DECLARE(DRM_MEM_MAPPINGS);
184 MALLOC_DECLARE(DRM_MEM_BUFLISTS);
185 MALLOC_DECLARE(DRM_MEM_AGPLISTS);
186 MALLOC_DECLARE(DRM_MEM_CTXBITMAP);
187 MALLOC_DECLARE(DRM_MEM_SGLISTS);
188 MALLOC_DECLARE(DRM_MEM_DRAWABLE);
189 MALLOC_DECLARE(DRM_MEM_MM);
190 
191 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
192 
193                                         /* Internal types and structures */
194 #define DRM_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
195 #define DRM_MIN(a,b) ((a)<(b)?(a):(b))
196 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
197 
198 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
199 
200 #define __OS_HAS_AGP          1
201 
202 #define DRM_DEV_MODE          (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
203 #define DRM_DEV_UID 0
204 #define DRM_DEV_GID 0
205 
206 #define wait_queue_head_t     atomic_t
207 #define DRM_WAKEUP(w)                   wakeup((void *)w)
208 #define DRM_WAKEUP_INT(w)     wakeup(w)
209 #define DRM_INIT_WAITQUEUE(queue) do {(void)(queue);} while (0)
210 
211 #if defined(__FreeBSD__)
212 #define DRM_CDEV    struct cdev *
213 #define DRM_CURPROC           curthread
214 #define DRM_STRUCTPROC                  struct thread
215 #define DRM_STRUCTCDEVPROC    struct thread
216 #define DRM_CDEVPROC_REAL(p)  (p)
217 #define DRM_IOCTL_DATA  caddr_t
218 #define DRM_SPINTYPE                    struct mtx
219 #define DRM_SPININIT(l,name)  mtx_init(l, name, NULL, MTX_DEF)
220 #define DRM_SPINUNINIT(l)     mtx_destroy(l)
221 #define DRM_SPINLOCK(l)                 mtx_lock(l)
222 #define DRM_SPINUNLOCK(u)     mtx_unlock(u)
223 #define DRM_SPINLOCK_IRQSAVE(l, irqflags) do {              \
224           mtx_lock(l);                                                \
225           (void)irqflags;                                             \
226 } while (0)
227 #define DRM_SPINUNLOCK_IRQRESTORE(u, irqflags) mtx_unlock(u)
228 #define DRM_SPINLOCK_ASSERT(l)          mtx_assert(l, MA_OWNED)
229 #define DRM_CURRENTPID                  curthread->td_proc->p_pid
230 #define DRM_LOCK()            mtx_lock(&dev->dev_lock)
231 #define DRM_UNLOCK()                    mtx_unlock(&dev->dev_lock)
232 #define DRM_SYSCTL_HANDLER_ARGS         (SYSCTL_HANDLER_ARGS)
233 #elif   defined(__NetBSD__)
234 #define DRM_CDEV    dev_t
235 #define DRM_CURPROC     curproc
236 #define DRM_STRUCTPROC  struct proc
237 #define DRM_STRUCTCDEVPROC    struct lwp
238 #define DRM_CDEVPROC_REAL(l)  (l)->l_proc
239 #define DRM_IOCTL_DATA                  void *
240 #define DRM_SPINTYPE                    kmutex_t
241 #define DRM_SPININIT(l,name)  mutex_init(l, MUTEX_DEFAULT, /*IPL_NONE*/ IPL_VM)
242 #define DRM_SPINUNINIT(l)     mutex_destroy(l)
243 #define DRM_SPINLOCK(l)                 mutex_enter(l)
244 #define DRM_SPINUNLOCK(u)     mutex_exit(u)
245 #define DRM_SPINLOCK_IRQSAVE(l, irqflags) do {              \
246           mutex_enter(l);                                             \
247           (void)irqflags;                                             \
248 } while (0)
249 #define DRM_SPINUNLOCK_IRQRESTORE(u, irqflags) mutex_exit(u)
250 #define DRM_SPINLOCK_ASSERT(l)          mutex_owned(l)
251 #define DRM_CURRENTPID                  curproc->p_pid
252 #define DRM_LOCK()            DRM_SPINLOCK(&dev->dev_lock)
253 #define DRM_UNLOCK()                    DRM_SPINUNLOCK(&dev->dev_lock)
254 #define DRM_SYSCTL_HANDLER_ARGS         (SYSCTLFN_ARGS)
255 #define vslock(addr, len)   uvm_vslock(curproc->p_vmspace, addr, len, VM_PROT_READ | VM_PROT_WRITE)
256 #define vsunlock(addr, len) uvm_vsunlock(curproc->p_vmspace, addr, len)
257 #endif
258 
259 #define DRM_IRQ_ARGS                    void *arg
260 #if defined(__FreeBSD__)
261 typedef void                            irqreturn_t;
262 #define IRQ_HANDLED           /* nothing */
263 #define IRQ_NONE              /* nothing */
264 #elif   defined(__NetBSD__)
265 typedef int                             irqreturn_t;
266 #define IRQ_HANDLED           1
267 #define IRQ_NONE              0
268 #endif
269 
270 enum {
271           DRM_IS_NOT_AGP,
272           DRM_IS_AGP,
273           DRM_MIGHT_BE_AGP
274 };
275 #define DRM_AGP_MEM           struct agp_memory_info
276 
277 #if defined(__FreeBSD__)
278 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
279 #elif   defined(__NetBSD__)
280 #define drm_get_device_from_kdev(_kdev) ((minor(_kdev) < DRM_MAXUNITS) ? \
281           drm_units[minor(_kdev)] : NULL)
282 #endif
283 
284 #if defined(__FreeBSD__)
285 #define PAGE_ALIGN(addr) round_page(addr)
286 /* DRM_SUSER returns true if the user is superuser */
287 #if __FreeBSD_version >= 700000
288 #define DRM_SUSER(p)                    (priv_check(p, PRIV_DRIVER) == 0)
289 #else
290 #define DRM_SUSER(p)                    (suser(p) == 0)
291 #endif
292 #define DRM_AGP_FIND_DEVICE() agp_find_device()
293 #define DRM_MTRR_WC           MDF_WRITECOMBINE
294 #define jiffies                         ticks
295 
296 #elif   defined(__NetBSD__)
297 
298 #define PAGE_ALIGN(addr)      round_page(addr)
299 #define DRM_SUSER(p)    (kauth_cred_getsvuid((p)->p_cred) == 0)
300 #define DRM_AGP_FIND_DEVICE() agp_find_device(0)
301 #ifdef MTRR_TYPE_WC
302 #define DRM_MTRR_WC           MTRR_TYPE_WC
303 #else
304 #define DRM_MTRR_WC           0
305 #endif
306 #define jiffies                         getticks()
307 
308 #define DRM_MAXUNITS    128
309 extern struct drm_device *drm_units[];
310 
311 #endif
312 
313 /* Capabilities taken from src/sys/dev/pci/pcireg.h. */
314 #ifndef PCIY_AGP
315 #define PCIY_AGP    0x02
316 #endif
317 
318 #ifndef PCIY_EXPRESS
319 #define PCIY_EXPRESS          0x10
320 #endif
321 
322 typedef unsigned long dma_addr_t;
323 #if defined(__FreeBSD__)
324 typedef u_int64_t u64;
325 typedef u_int32_t u32;
326 typedef u_int16_t u16;
327 typedef u_int8_t u8;
328 #else
329 typedef uint64_t u64;
330 typedef uint32_t u32;
331 typedef uint16_t u16;
332 typedef uint8_t u8;
333 #endif
334 
335 /* DRM_READMEMORYBARRIER() prevents reordering of reads.
336  * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
337  * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
338  */
339 #if defined(__NetBSD__)
340 #define DRM_READMEMORYBARRIER()                   membar_consumer()
341 #define DRM_WRITEMEMORYBARRIER()        membar_producer()
342 #define DRM_MEMORYBARRIER()             membar_sync()
343 #elif defined(__i386__)
344 #define DRM_READMEMORYBARRIER()                   __asm __volatile( \
345                                                   "lock; addl $0,0(%%esp)" : : : "memory");
346 #define DRM_WRITEMEMORYBARRIER()        __asm __volatile("" : : : "memory");
347 #define DRM_MEMORYBARRIER()             __asm __volatile( \
348                                                   "lock; addl $0,0(%%esp)" : : : "memory");
349 #elif defined(__alpha__)
350 #define DRM_READMEMORYBARRIER()                   alpha_mb();
351 #define DRM_WRITEMEMORYBARRIER()        alpha_wmb();
352 #define DRM_MEMORYBARRIER()             alpha_mb();
353 #elif defined(__amd64__)
354 #define DRM_READMEMORYBARRIER()                   __asm __volatile( \
355                                                   "lock; addl $0,0(%%rsp)" : : : "memory");
356 #define DRM_WRITEMEMORYBARRIER()        __asm __volatile("" : : : "memory");
357 #define DRM_MEMORYBARRIER()             __asm __volatile( \
358                                                   "lock; addl $0,0(%%rsp)" : : : "memory");
359 #endif
360 
361 #define DRM_COPY_TO_USER(user, kern, size) \
362           copyout(kern, user, size)
363 #define DRM_COPY_FROM_USER(kern, user, size) \
364           copyin(user, kern, size)
365 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3)      \
366           copyin(arg2, arg1, arg3)
367 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)        \
368           copyout(arg2, arg1, arg3)
369 #if defined(__NetBSD__)
370 #define DRM_GET_USER_UNCHECKED(val, uaddr)                  \
371           (ufetch_32((uint32_t *)(uaddr), &(val)), 0)
372 #elif defined(__FreeBSD__)
373 #define DRM_GET_USER_UNCHECKED(val, uaddr)                  \
374           ((val) = fuword32(uaddr), 0)
375 #else
376 #define DRM_GET_USER_UNCHECKED(val, uaddr)                  \
377           ((val) = fuword(uaddr), 0)
378 #endif
379 
380 #define cpu_to_le32(x) htole32(x)
381 #define le32_to_cpu(x) le32toh(x)
382 
383 #define DRM_HZ                          hz
384 #define DRM_UDELAY(udelay)    DELAY(udelay)
385 #define DRM_TIME_SLICE                  (hz/20)  /* Time slice for GLXContexts    */
386 
387 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do { \
388           (_map) = (_dev)->context_sareas[_ctx];            \
389 } while(0)
390 
391 #define LOCK_TEST_WITH_RETURN(dev, file_priv)                                   \
392 do {                                                                                      \
393           if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||                    \
394                dev->lock.file_priv != file_priv) {                              \
395                     DRM_ERROR("%s called without lock held\n",                  \
396                                  __FUNCTION__);                                 \
397                     return EINVAL;                                                        \
398           }                                                                               \
399 } while (0)
400 
401 /* Returns -errno to shared code */
402 #if defined(__FreeBSD__)
403 #define DRM_WAIT_ON( ret, queue, timeout, condition )                 \
404 for ( ret = 0 ; !ret && !(condition) ; ) {                            \
405           DRM_UNLOCK();                                                         \
406           mtx_lock(&dev->irq_lock);                                   \
407           if (!(condition))                                           \
408               ret = -mtx_sleep(&(queue), &dev->irq_lock,              \
409                     PCATCH, "drmwtq", (timeout));                     \
410           mtx_unlock(&dev->irq_lock);                                 \
411           DRM_LOCK();                                                           \
412 }
413 #elif   defined(__NetBSD__)
414 /* Returns -errno to shared code */
415 #define DRM_WAIT_ON( ret, queue, timeout, condition )                 \
416 for ( ret = 0 ; !ret && !(condition) ; ) {                            \
417           DRM_UNLOCK();                                                         \
418           mutex_enter(&dev->irq_lock);                                \
419           if (!(condition))                                           \
420              ret = -mtsleep(&(queue), PCATCH,               \
421                                "drmwtq", (timeout), &dev->irq_lock);  \
422           mutex_exit(&dev->irq_lock);                                 \
423           DRM_LOCK();                                                           \
424 }
425 #endif
426 
427 #define DRM_ERROR(fmt, ...) \
428           printf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,                 \
429               DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
430 
431 #define DRM_INFO(fmt, ...)  printf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
432 
433 #define DRM_DEBUG(fmt, ...) do {                                                \
434           if (drm_debug_flag)                                                   \
435                     printf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,      \
436                               __func__ , ##__VA_ARGS__);                        \
437 } while (0)
438 
439 typedef struct drm_pci_id_list
440 {
441           int vendor;
442           int device;
443           long driver_private;
444 #if defined(__NetBSD__)
445           const char *name;
446 #else
447           char *name;
448 #endif
449 } drm_pci_id_list_t;
450 
451 struct drm_msi_blacklist_entry
452 {
453           int vendor;
454           int device;
455 };
456 
457 #define DRM_AUTH    0x1
458 #define DRM_MASTER  0x2
459 #define DRM_ROOT_ONLY         0x4
460 typedef struct drm_ioctl_desc {
461           unsigned long cmd;
462           int (*func)(struct drm_device *dev, void *data,
463                         struct drm_file *file_priv);
464           int flags;
465 } drm_ioctl_desc_t;
466 /**
467  * Creates a driver or general drm_ioctl_desc array entry for the given
468  * ioctl, for use by drm_ioctl().
469  */
470 #define DRM_IOCTL_DEF(ioctl, func, flags) \
471           [DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
472 
473 typedef struct drm_magic_entry {
474           drm_magic_t                magic;
475           struct drm_file            *priv;
476           struct drm_magic_entry *next;
477 } drm_magic_entry_t;
478 
479 typedef struct drm_magic_head {
480           struct drm_magic_entry *head;
481           struct drm_magic_entry *tail;
482 } drm_magic_head_t;
483 
484 typedef struct drm_buf {
485           int                   idx;           /* Index into master buflist          */
486           int                   total;       /* Buffer size                          */
487           int                   order;       /* log-base-2(total)                    */
488           int                   used;          /* Amount of buffer in use (for DMA)  */
489           unsigned long         offset;      /* Byte offset (used internally)        */
490           void                  *address;    /* Address of buffer                    */
491           unsigned long         bus_address; /* Bus address of buffer                */
492           struct drm_buf        *next;       /* Kernel-only: used for free list    */
493           __volatile__ int  pending;     /* On hardware DMA queue                    */
494           struct drm_file   *file_priv;  /* Unique identifier of holding process */
495           int                   context;     /* Kernel queue for this buffer         */
496           enum {
497                     DRM_LIST_NONE        = 0,
498                     DRM_LIST_FREE        = 1,
499                     DRM_LIST_WAIT        = 2,
500                     DRM_LIST_PEND        = 3,
501                     DRM_LIST_PRIO        = 4,
502                     DRM_LIST_RECLAIM = 5
503           }                     list;          /* Which list we're on                */
504 
505           int                   dev_priv_size; /* Size of buffer private stoarge   */
506           void                  *dev_private;  /* Per-buffer private storage       */
507 } drm_buf_t;
508 
509 typedef struct drm_freelist {
510           int                   initialized; /* Freelist in use                    */
511           atomic_t    count;       /* Number of free buffers             */
512           drm_buf_t   *next;       /* End pointer                        */
513 
514           int                   low_mark;    /* Low water mark                     */
515           int                   high_mark;   /* High water mark                    */
516 } drm_freelist_t;
517 
518 typedef struct drm_dma_handle {
519           void *vaddr;
520           bus_addr_t busaddr;
521           bus_dma_tag_t tag;
522           bus_dmamap_t map;
523 #if defined(__NetBSD__)
524           size_t size;
525           size_t nsegs;
526           bus_dma_segment_t segs[1];
527 #endif
528 } drm_dma_handle_t;
529 
530 typedef struct drm_buf_entry {
531           int                   buf_size;
532           int                   buf_count;
533           drm_buf_t   *buflist;
534           int                   seg_count;
535           drm_dma_handle_t  **seglist;
536           int                   page_order;
537 
538           drm_freelist_t        freelist;
539 } drm_buf_entry_t;
540 
541 typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
542 struct drm_file {
543           TAILQ_ENTRY(drm_file) link;
544           struct drm_device *dev;
545           int                   authenticated;
546           int                   master;
547           int                   minor;
548           pid_t                 pid;
549           uid_t                 uid;
550           drm_magic_t           magic;
551           unsigned long         ioctl_count;
552           void                 *driver_priv;
553 };
554 
555 typedef struct drm_lock_data {
556           struct drm_hw_lock  *hw_lock; /* Hardware lock                 */
557           struct drm_file   *file_priv;   /* Unique identifier of holding process (NULL is kernel)*/
558           int                   lock_queue;       /* Queue of blocked processes    */
559           unsigned long         lock_time;        /* Time of last lock in jiffies            */
560 } drm_lock_data_t;
561 
562 /* This structure, in the struct drm_device, is always initialized while the
563  * device
564  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
565  * when set marks that no further bufs may be allocated until device teardown
566  * occurs (when the last open of the device has closed).  The high/low
567  * watermarks of bufs are only touched by the X Server, and thus not
568  * concurrently accessed, so no locking is needed.
569  */
570 typedef struct drm_device_dma {
571           drm_buf_entry_t       bufs[DRM_MAX_ORDER+1];
572           int                   buf_count;
573           drm_buf_t   **buflist;        /* Vector of pointers info bufs            */
574           int                   seg_count;
575           int                   page_count;
576           unsigned long         *pagelist;
577           unsigned long         byte_count;
578           enum {
579                     _DRM_DMA_USE_AGP = 0x01,
580                     _DRM_DMA_USE_SG  = 0x02
581           } flags;
582 } drm_device_dma_t;
583 
584 typedef struct drm_agp_mem {
585           void               *handle;
586           unsigned long      bound; /* address */
587           int                pages;
588           struct drm_agp_mem *prev;
589           struct drm_agp_mem *next;
590 } drm_agp_mem_t;
591 
592 typedef struct drm_agp_head {
593           device_t     agpdev;
594           struct agp_info    info;
595           const char         *chipset;
596           drm_agp_mem_t      *memory;
597           unsigned long      mode;
598           int                enabled;
599           int                acquired;
600           unsigned long      base;
601           int                    mtrr;
602           int                    cant_use_aperture;
603           unsigned long          page_mask;
604 } drm_agp_head_t;
605 
606 typedef struct drm_sg_mem {
607           unsigned long                   handle;
608           void                           *virtual;
609           int                             pages;
610           dma_addr_t                     *busaddr;
611           struct drm_dma_handle          *sg_dmah;          /* Handle for sg_pages   */
612           struct drm_dma_handle          *dmah;             /* Handle to PCI memory  */
613                                                             /* for ATI PCIGART table */
614 } drm_sg_mem_t;
615 
616 typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
617 
618 #if defined(__NetBSD__)
619 typedef struct {
620           int                           mapped;
621           int                           maptype;
622           bus_addr_t                    base;
623           bus_size_t                    size;
624           bus_space_handle_t  bsh;
625           int                           flags;
626           void *                        vaddr;
627 } pci_map_data_t;
628 
629 /* XXX */
630 #define readl(va) (*(volatile u_int32_t *) (va))
631 #endif
632 
633 typedef struct drm_local_map {
634           unsigned long       offset;    /* Physical address (0 for SAREA)*/
635           unsigned long       size;      /* Physical size (bytes)         */
636           enum drm_map_type   type;      /* Type of memory mapped                   */
637           enum drm_map_flags  flags;     /* Flags                                   */
638           void                *handle; /* User-space: "Handle" to pass to mmap    */
639                                          /* Kernel-space: kernel-virtual address    */
640           int                 mtrr;      /* Boolean: MTRR used */
641                                          /* Private data                            */
642           int                 rid;       /* PCI resource ID for bus_space */
643 #if defined(__FreeBSD__)
644           struct resource *bsr;
645 #endif
646           bus_space_tag_t bst;
647           bus_space_handle_t bsh;
648           drm_dma_handle_t *dmah;
649 #if defined(__NetBSD__)
650           pci_map_data_t *fullmap;
651           bus_size_t mapsize;
652 #endif
653           TAILQ_ENTRY(drm_local_map) link;
654 } drm_local_map_t;
655 
656 TAILQ_HEAD(drm_vbl_sig_list, drm_vbl_sig);
657 typedef struct drm_vbl_sig {
658           TAILQ_ENTRY(drm_vbl_sig) link;
659           unsigned int        sequence;
660           int                 signo;
661           int                 pid;
662 } drm_vbl_sig_t;
663 
664 struct drm_vblank_info {
665           wait_queue_head_t queue;      /* vblank wait queue */
666           atomic_t count;                         /* number of VBLANK interrupts */
667                                                   /* (driver must alloc the right number of counters) */
668           struct drm_vbl_sig_list sigs; /* signal list to send on VBLANK */
669           atomic_t refcount;            /* number of users of vblank interrupts */
670           u32 last;                     /* protected by dev->vbl_lock, used */
671                                                   /* for wraparound handling */
672           int enabled;                            /* so we don't call enable more than */
673                                                   /* once per disable */
674           int inmodeset;                          /* Display driver is setting mode */
675 };
676 
677 /* location of GART table */
678 #define DRM_ATI_GART_MAIN 1
679 #define DRM_ATI_GART_FB   2
680 
681 #define DRM_ATI_GART_PCI  1
682 #define DRM_ATI_GART_PCIE 2
683 #define DRM_ATI_GART_IGP  3
684 
685 struct drm_ati_pcigart_info {
686           int gart_table_location;
687           int gart_reg_if;
688           void *addr;
689           dma_addr_t bus_addr;
690           dma_addr_t table_mask;
691           dma_addr_t member_mask;
692           struct drm_dma_handle *table_handle;
693           drm_local_map_t mapping;
694           int table_size;
695 };
696 
697 #ifndef DMA_BIT_MASK
698 #define DMA_BIT_MASK(n) ((dma_addr_t)(((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1))
699 #endif
700 
701 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
702 
703 struct drm_driver_info {
704           int       (*load)(struct drm_device *, unsigned long flags);
705           int       (*firstopen)(struct drm_device *);
706           int       (*open)(struct drm_device *, struct drm_file *);
707           void      (*preclose)(struct drm_device *, struct drm_file *file_priv);
708           void      (*postclose)(struct drm_device *, struct drm_file *);
709           void      (*lastclose)(struct drm_device *);
710           int       (*unload)(struct drm_device *);
711           void      (*reclaim_buffers_locked)(struct drm_device *,
712                                                     struct drm_file *file_priv);
713           int       (*dma_ioctl)(struct drm_device *dev, void *data,
714                                    struct drm_file *file_priv);
715           void      (*dma_ready)(struct drm_device *);
716           int       (*dma_quiescent)(struct drm_device *);
717           int       (*dma_flush_block_and_flush)(struct drm_device *, int context,
718                                                        enum drm_lock_flags flags);
719           int       (*dma_flush_unblock)(struct drm_device *, int context,
720                                              enum drm_lock_flags flags);
721           int       (*context_ctor)(struct drm_device *dev, int context);
722           int       (*context_dtor)(struct drm_device *dev, int context);
723           int       (*kernel_context_switch)(struct drm_device *dev, int old,
724                                                    int new);
725           int       (*kernel_context_switch_unlock)(struct drm_device *dev);
726           void      (*irq_preinstall)(struct drm_device *dev);
727           int       (*irq_postinstall)(struct drm_device *dev);
728           void      (*irq_uninstall)(struct drm_device *dev);
729           irqreturn_t         (*irq_handler)(DRM_IRQ_ARGS);
730           u32       (*get_vblank_counter)(struct drm_device *dev, unsigned int crtc);
731           int       (*enable_vblank)(struct drm_device *dev, unsigned int crtc);
732           void      (*disable_vblank)(struct drm_device *dev, unsigned int crtc);
733 
734           drm_pci_id_list_t *id_entry;  /* PCI ID, name, and chipset private */
735 
736           /**
737            * Called by \c drm_device_is_agp.  Typically used to determine if a
738            * card is really attached to AGP or not.
739            *
740            * \param dev  DRM device handle
741            *
742            * \returns
743            * One of three values is returned depending on whether or not the
744            * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
745            * (return of 1), or may or may not be AGP (return of 2).
746            */
747           int       (*device_is_agp) (struct drm_device * dev);
748 
749           drm_ioctl_desc_t *ioctls;
750           int       max_ioctl;
751 
752           int       buf_priv_size;
753 
754           int       major;
755           int       minor;
756           int       patchlevel;
757           const char *name;             /* Simple driver name                      */
758           const char *desc;             /* Longer driver name                      */
759           const char *date;             /* Date of last major changes.             */
760 
761           u32 driver_features;
762 };
763 
764 /* Length for the array of resource pointers for drm_get_resource_*. */
765 #define DRM_MAX_PCI_RESOURCE  6
766 
767 /**
768  * DRM device functions structure
769  */
770 struct drm_device {
771           struct drm_driver_info *driver;
772           drm_pci_id_list_t *id_entry;  /* PCI ID, name, and chipset private */
773 
774           u_int16_t pci_device;                   /* PCI device id */
775           u_int16_t pci_vendor;                   /* PCI vendor id */
776 
777           char                  *unique;          /* Unique identifier: e.g., busid  */
778           int                   unique_len;       /* Length of unique field        */
779           device_t    device; /* Device instance from newbus     */
780 #if defined(__FreeBSD__)
781           struct cdev           *devnode;         /* Device number for mknod       */
782 #endif
783           int                   if_version;       /* Highest interface version set */
784 
785           int                   flags;  /* Flags to open(2)              */
786 
787                                         /* Locks */
788           DRM_SPINTYPE          vbl_lock;         /* protects vblank operations */
789           DRM_SPINTYPE          dma_lock;         /* protects dev->dma */
790           DRM_SPINTYPE          irq_lock;         /* protects irq condition checks */
791           DRM_SPINTYPE          dev_lock;         /* protects everything else */
792           DRM_SPINTYPE          drw_lock;
793 
794                                         /* Usage Counters */
795           int                   open_count;       /* Outstanding files open        */
796           int                   buf_use;          /* Buffers in use -- cannot alloc  */
797 
798                                         /* Performance counters */
799           unsigned long     counters;
800           enum drm_stat_type  types[15];
801           atomic_t          counts[15];
802 
803                                         /* Authentication */
804           drm_file_list_t   files;
805           drm_magic_head_t  magiclist[DRM_HASH_SIZE];
806 
807           /* Linked list of mappable regions. Protected by dev_lock */
808           drm_map_list_t        maplist;
809 
810           drm_local_map_t       **context_sareas;
811           int                   max_context;
812 
813           drm_lock_data_t       lock;             /* Information on hardware lock            */
814 
815                                         /* DMA queues (contexts) */
816           drm_device_dma_t  *dma;                 /* Optional pointer for DMA support */
817 
818                                         /* Context support */
819           int                   irq;              /* Interrupt used by board       */
820           int                   irq_enabled;      /* True if the irq handler is enabled */
821           int                   msi_enabled;      /* MSI enabled */
822 #if defined(__FreeBSD__)
823           int                   irqrid; /* Interrupt used by board */
824           struct resource   *irqr;      /* Resource for interrupt used by board    */
825 
826           /* Storage of resource pointers for drm_get_resource_* */
827           struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
828           int                   pcirid[DRM_MAX_PCI_RESOURCE];
829 #elif   defined(__NetBSD__)
830           struct pci_attach_args  pa;
831           pci_map_data_t        pci_map_data[DRM_MAX_PCI_RESOURCE];
832           pci_map_data_t        agp_map_data[DRM_MAX_PCI_RESOURCE];
833           dev_t       kdev;
834 #endif
835           void                  *irqh;  /* Handle from bus_setup_intr      */
836 
837           int                   pci_domain;
838           int                   pci_bus;
839           int                   pci_slot;
840           int                   pci_func;
841 
842           atomic_t    context_flag;     /* Context swapping flag         */
843           int                   last_context;     /* Last current context                    */
844 
845           int                   vblank_disable_allowed;
846           atomic_t    vbl_signal_pending;         /* number of signals pending on all crtcs */
847           struct callout        vblank_disable_timer;
848           u32                   max_vblank_count; /* size of vblank counter register */
849           struct drm_vblank_info *vblank;                   /* per crtc vblank info */
850           int                   num_crtcs;
851 
852 #if defined(__FreeBSD__)
853           struct sigio      *buf_sigio; /* Processes waiting for SIGIO     */
854 #elif   defined(__NetBSD__)
855           pid_t   buf_pgid;
856 #endif
857 
858                                         /* Sysctl support */
859           struct drm_sysctl_info *sysctl;
860 
861           drm_agp_head_t    *agp;
862           drm_sg_mem_t      *sg;  /* Scatter gather memory */
863           atomic_t          *ctx_bitmap;
864           void                  *dev_private;
865           unsigned int          agp_buffer_token;
866           drm_local_map_t   *agp_buffer_map;
867 
868 #if defined(__FreeBSD__)
869           struct unrhdr         *drw_unrhdr;
870 #elif   defined(__NetBSD__)
871           int drw_no;
872 #endif
873           /* RB tree of drawable infos */
874           RB_HEAD(drawable_tree, bsd_drm_drawable_info) drw_head;
875 };
876 
drm_core_check_feature(struct drm_device * dev,int feature)877 static __inline__ int drm_core_check_feature(struct drm_device *dev,
878                                                        int feature)
879 {
880           return ((dev->driver->driver_features & feature) ? 1 : 0);
881 }
882 
883 #if __OS_HAS_AGP
drm_core_has_AGP(struct drm_device * dev)884 static inline int drm_core_has_AGP(struct drm_device *dev)
885 {
886           return drm_core_check_feature(dev, DRIVER_USE_AGP);
887 }
888 #else
889 #define drm_core_has_AGP(dev) (0)
890 #endif
891 
892 #if defined(__FreeBSD__)
893 
894 #define DRM_READ8(map, offset)                                                            \
895           *(volatile u_int8_t *)(((vm_offset_t)(map)->handle) +                 \
896               (vm_offset_t)(offset))
897 #define DRM_READ16(map, offset)                                                           \
898           *(volatile u_int16_t *)(((vm_offset_t)(map)->handle) +                \
899               (vm_offset_t)(offset))
900 #define DRM_READ32(map, offset)                                                           \
901           *(volatile u_int32_t *)(((vm_offset_t)(map)->handle) +                \
902               (vm_offset_t)(offset))
903 #define DRM_WRITE8(map, offset, val)                                            \
904           *(volatile u_int8_t *)(((vm_offset_t)(map)->handle) +                 \
905               (vm_offset_t)(offset)) = val
906 #define DRM_WRITE16(map, offset, val)                                           \
907           *(volatile u_int16_t *)(((vm_offset_t)(map)->handle) +                \
908               (vm_offset_t)(offset)) = val
909 #define DRM_WRITE32(map, offset, val)                                           \
910           *(volatile u_int32_t *)(((vm_offset_t)(map)->handle) +                \
911               (vm_offset_t)(offset)) = val
912 
913 #define DRM_VERIFYAREA_READ( uaddr, size )                  \
914           (!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
915 
916 #elif   defined(__NetBSD__)
917 
918 typedef vaddr_t vm_offset_t;
919 
920 #define DRM_IS_BUS_SPACE(map) ((map)->type == _DRM_REGISTERS || \
921                                          (map)->type == _DRM_CONSISTENT)
922 
923 static __inline__ u_int8_t
DRM_READ8(drm_local_map_t * map,bus_size_t offset)924 DRM_READ8(drm_local_map_t *map, bus_size_t offset)
925 {
926           if (DRM_IS_BUS_SPACE(map))
927                     return bus_space_read_1(map->bst, map->bsh, offset);
928           else
929                     return *(volatile u_int8_t *)((vaddr_t)map->handle + offset);
930 }
931 
932 static __inline__ u_int16_t
DRM_READ16(drm_local_map_t * map,bus_size_t offset)933 DRM_READ16(drm_local_map_t *map, bus_size_t offset)
934 {
935           if (DRM_IS_BUS_SPACE(map))
936                     return bus_space_read_2(map->bst, map->bsh, offset);
937           else
938                     return *(volatile u_int16_t *)((vaddr_t)map->handle + offset);
939 }
940 
941 static __inline__ u_int32_t
DRM_READ32(drm_local_map_t * map,bus_size_t offset)942 DRM_READ32(drm_local_map_t *map, bus_size_t offset)
943 {
944           if (DRM_IS_BUS_SPACE(map))
945                     return bus_space_read_4(map->bst, map->bsh, offset);
946           else
947                     return *(volatile u_int32_t *)((vaddr_t)map->handle + offset);
948 }
949 
950 static __inline__ void
DRM_WRITE8(drm_local_map_t * map,bus_size_t offset,u_int8_t val)951 DRM_WRITE8(drm_local_map_t *map, bus_size_t offset, u_int8_t val)
952 {
953           if (DRM_IS_BUS_SPACE(map))
954                     bus_space_write_1(map->bst, map->bsh, offset, val);
955           else
956                     *(volatile u_int8_t *)((vaddr_t)map->handle + offset) = val;
957 }
958 
959 static __inline__ void
DRM_WRITE16(drm_local_map_t * map,bus_size_t offset,u_int16_t val)960 DRM_WRITE16(drm_local_map_t *map, bus_size_t offset, u_int16_t val)
961 {
962           if (DRM_IS_BUS_SPACE(map))
963                     bus_space_write_2(map->bst, map->bsh, offset, val);
964           else
965                     *(volatile u_int16_t *)((vaddr_t)map->handle + offset) = val;
966 }
967 
968 static __inline__ void
DRM_WRITE32(drm_local_map_t * map,bus_size_t offset,u_int32_t val)969 DRM_WRITE32(drm_local_map_t *map, bus_size_t offset, u_int32_t val)
970 {
971           if (DRM_IS_BUS_SPACE(map))
972                     bus_space_write_4(map->bst, map->bsh, offset, val);
973           else
974                     *(volatile u_int32_t *)((vaddr_t)map->handle + offset) = val;
975 }
976 
977 #define DRM_VERIFYAREA_READ( uaddr, size )                  \
978           (!uvm_map_checkprot(&(curproc->p_vmspace->vm_map),              \
979                     (vaddr_t)uaddr, (vaddr_t)uaddr+size, UVM_PROT_READ))
980 
981 #endif
982 
983 extern int          drm_debug_flag;
984 
985 /* Device setup support (drm_drv.c) */
986 #if defined(__FreeBSD__)
987 int       drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
988 int       drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
989 void      drm_close(void *data);
990 int       drm_detach(device_t kdev);
991 d_ioctl_t drm_ioctl;
992 d_open_t drm_open;
993 d_read_t drm_read;
994 d_poll_t drm_poll;
995 d_mmap_t drm_mmap;
996 #elif   defined(__NetBSD__)
997 int       drm_probe(struct pci_attach_args *pa, drm_pci_id_list_t *idlist);
998 void      drm_attach(device_t kdev, struct pci_attach_args *pa, drm_pci_id_list_t *idlist);
999 int     drm_detach(device_t self, int flags);
1000 dev_type_ioctl(drm_ioctl);
1001 dev_type_open(drm_open);
1002 dev_type_close(drm_close);
1003 dev_type_read(drm_read);
1004 dev_type_poll(drm_poll);
1005 dev_type_mmap(drm_mmap);
1006 #endif
1007 extern drm_local_map_t        *drm_getsarea(struct drm_device *dev);
1008 
1009 int drm_close_pid(struct drm_device *dev, struct drm_file *priv, pid_t pid);
1010 
1011 /* File operations helpers (drm_fops.c) */
1012 #if defined(__FreeBSD__)
1013 extern int                    drm_open_helper(struct cdev *kdev, int flags, int fmt,
1014                                                    DRM_STRUCTPROC *p,
1015                                                   struct drm_device *dev);
1016 #elif   defined(__NetBSD__)
1017 extern struct drm_file *drm_find_file_by_proc(struct drm_device *dev,
1018         struct proc *p);
1019 extern int                    drm_open_helper(dev_t kdev, int flags, int fmt,
1020                                         struct proc *p, struct drm_device *dev);
1021 #endif
1022 
1023 /* Memory management support (drm_memory.c) */
1024 void      drm_mem_init(void);
1025 void      drm_mem_uninit(void);
1026 void      *drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map);
1027 void      *drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
1028 void      drm_ioremapfree(drm_local_map_t *map);
1029 int       drm_mtrr_add(unsigned long offset, size_t size, int flags);
1030 int       drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1031 
1032 int       drm_context_switch(struct drm_device *dev, int old, int new);
1033 int       drm_context_switch_complete(struct drm_device *dev, int new);
1034 
1035 int       drm_ctxbitmap_init(struct drm_device *dev);
1036 void      drm_ctxbitmap_cleanup(struct drm_device *dev);
1037 void      drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
1038 int       drm_ctxbitmap_next(struct drm_device *dev);
1039 
1040 /* Locking IOCTL support (drm_lock.c) */
1041 int       drm_lock_take(struct drm_lock_data *lock_data,
1042                           unsigned int context);
1043 int       drm_lock_transfer(struct drm_lock_data *lock_data,
1044                                 unsigned int context);
1045 int       drm_lock_free(struct drm_lock_data *lock_data,
1046                           unsigned int context);
1047 
1048 /* Buffer management support (drm_bufs.c) */
1049 unsigned long drm_get_resource_start(struct drm_device *dev,
1050                                              unsigned int resource);
1051 unsigned long drm_get_resource_len(struct drm_device *dev,
1052                                            unsigned int resource);
1053 void      drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
1054 int       drm_order(unsigned long size);
1055 int       drm_addmap(struct drm_device *dev, unsigned long offset,
1056                        unsigned long size,
1057                        enum drm_map_type type, enum drm_map_flags flags,
1058                        drm_local_map_t **map_ptr);
1059 int       drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request);
1060 int       drm_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request);
1061 int       drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request);
1062 
1063 /* DMA support (drm_dma.c) */
1064 int       drm_dma_setup(struct drm_device *dev);
1065 void      drm_dma_takedown(struct drm_device *dev);
1066 void      drm_free_buffer(struct drm_device *dev, drm_buf_t *buf);
1067 void      drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv);
1068 #define drm_core_reclaim_buffers drm_reclaim_buffers
1069 
1070 /* IRQ support (drm_irq.c) */
1071 int       drm_irq_install(struct drm_device *dev);
1072 int       drm_irq_uninstall(struct drm_device *dev);
1073 irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
1074 void      drm_driver_irq_preinstall(struct drm_device *dev);
1075 void      drm_driver_irq_postinstall(struct drm_device *dev);
1076 void      drm_driver_irq_uninstall(struct drm_device *dev);
1077 void      drm_handle_vblank(struct drm_device *dev, unsigned int crtc);
1078 u32       drm_vblank_count(struct drm_device *dev, unsigned int crtc);
1079 int       drm_vblank_get(struct drm_device *dev, unsigned int crtc);
1080 void      drm_vblank_put(struct drm_device *dev, unsigned int crtc);
1081 void      drm_vblank_cleanup(struct drm_device *dev);
1082 int       drm_vblank_wait(struct drm_device *dev, unsigned int *vbl_seq);
1083 int       drm_vblank_init(struct drm_device *dev, int num_crtcs);
1084 void      drm_vbl_send_signals(struct drm_device *dev, unsigned int crtc);
1085 int       drm_modeset_ctl(struct drm_device *dev, void *data,
1086                               struct drm_file *file_priv);
1087 
1088 /* AGP/PCI Express/GART support (drm_agpsupport.c) */
1089 int       drm_device_is_agp(struct drm_device *dev);
1090 int       drm_device_is_pcie(struct drm_device *dev);
1091 drm_agp_head_t *drm_agp_init(struct drm_device *dev);
1092 int       drm_agp_acquire(struct drm_device *dev);
1093 int       drm_agp_release(struct drm_device *dev);
1094 int       drm_agp_info(struct drm_device * dev, struct drm_agp_info *info);
1095 int       drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
1096 void      *drm_agp_allocate_memory(size_t pages, u32 type);
1097 int       drm_agp_free_memory(void *handle);
1098 int       drm_agp_bind_memory(void *handle, off_t start);
1099 int       drm_agp_unbind_memory(void *handle);
1100 int       drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
1101 int       drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
1102 int       drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
1103 int       drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
1104 
1105 /* Scatter Gather Support (drm_scatter.c) */
1106 void      drm_sg_cleanup(drm_sg_mem_t *entry);
1107 int       drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
1108 
1109 /* sysctl support (drm_sysctl.h) */
1110 extern int                    drm_sysctl_init(struct drm_device *dev);
1111 extern int                    drm_sysctl_cleanup(struct drm_device *dev);
1112 
1113 /* ATI PCIGART support (ati_pcigart.c) */
1114 int       drm_ati_pcigart_init(struct drm_device *dev,
1115                                         struct drm_ati_pcigart_info *gart_info);
1116 int       drm_ati_pcigart_cleanup(struct drm_device *dev,
1117                                         struct drm_ati_pcigart_info *gart_info);
1118 
1119 /* Locking IOCTL support (drm_drv.c) */
1120 int       drm_lock(struct drm_device *dev, void *data,
1121                      struct drm_file *file_priv);
1122 int       drm_unlock(struct drm_device *dev, void *data,
1123                        struct drm_file *file_priv);
1124 int       drm_version(struct drm_device *dev, void *data,
1125                         struct drm_file *file_priv);
1126 int       drm_setversion(struct drm_device *dev, void *data,
1127                            struct drm_file *file_priv);
1128 
1129 /* Misc. IOCTL support (drm_ioctl.c) */
1130 int       drm_irq_by_busid(struct drm_device *dev, void *data,
1131                                struct drm_file *file_priv);
1132 int       drm_getunique(struct drm_device *dev, void *data,
1133                           struct drm_file *file_priv);
1134 int       drm_setunique(struct drm_device *dev, void *data,
1135                           struct drm_file *file_priv);
1136 int       drm_getmap(struct drm_device *dev, void *data,
1137                        struct drm_file *file_priv);
1138 int       drm_getclient(struct drm_device *dev, void *data,
1139                           struct drm_file *file_priv);
1140 int       drm_getstats(struct drm_device *dev, void *data,
1141                          struct drm_file *file_priv);
1142 int       drm_noop(struct drm_device *dev, void *data,
1143                      struct drm_file *file_priv);
1144 
1145 /* Context IOCTL support (drm_context.c) */
1146 int       drm_resctx(struct drm_device *dev, void *data,
1147                        struct drm_file *file_priv);
1148 int       drm_addctx(struct drm_device *dev, void *data,
1149                        struct drm_file *file_priv);
1150 int       drm_modctx(struct drm_device *dev, void *data,
1151                        struct drm_file *file_priv);
1152 int       drm_getctx(struct drm_device *dev, void *data,
1153                        struct drm_file *file_priv);
1154 int       drm_switchctx(struct drm_device *dev, void *data,
1155                           struct drm_file *file_priv);
1156 int       drm_newctx(struct drm_device *dev, void *data,
1157                        struct drm_file *file_priv);
1158 int       drm_rmctx(struct drm_device *dev, void *data,
1159                       struct drm_file *file_priv);
1160 int       drm_setsareactx(struct drm_device *dev, void *data,
1161                               struct drm_file *file_priv);
1162 int       drm_getsareactx(struct drm_device *dev, void *data,
1163                               struct drm_file *file_priv);
1164 
1165 /* Drawable IOCTL support (drm_drawable.c) */
1166 int       drm_adddraw(struct drm_device *dev, void *data,
1167                         struct drm_file *file_priv);
1168 int       drm_rmdraw(struct drm_device *dev, void *data,
1169                        struct drm_file *file_priv);
1170 int       drm_update_draw(struct drm_device *dev, void *data,
1171                               struct drm_file *file_priv);
1172 struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
1173                                                             int handle);
1174 
1175 /* Drawable support (drm_drawable.c) */
1176 void drm_drawable_free_all(struct drm_device *dev);
1177 
1178 /* Authentication IOCTL support (drm_auth.c) */
1179 int       drm_getmagic(struct drm_device *dev, void *data,
1180                          struct drm_file *file_priv);
1181 int       drm_authmagic(struct drm_device *dev, void *data,
1182                           struct drm_file *file_priv);
1183 
1184 /* Buffer management support (drm_bufs.c) */
1185 int       drm_addmap_ioctl(struct drm_device *dev, void *data,
1186                                struct drm_file *file_priv);
1187 int       drm_rmmap_ioctl(struct drm_device *dev, void *data,
1188                               struct drm_file *file_priv);
1189 int       drm_addbufs(struct drm_device *dev, void *data,
1190                         struct drm_file *file_priv);
1191 int       drm_infobufs(struct drm_device *dev, void *data,
1192                          struct drm_file *file_priv);
1193 int       drm_markbufs(struct drm_device *dev, void *data,
1194                          struct drm_file *file_priv);
1195 int       drm_freebufs(struct drm_device *dev, void *data,
1196                          struct drm_file *file_priv);
1197 int       drm_mapbufs(struct drm_device *dev, void *data,
1198                         struct drm_file *file_priv);
1199 
1200 /* DMA support (drm_dma.c) */
1201 int       drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1202 
1203 /* IRQ support (drm_irq.c) */
1204 int       drm_control(struct drm_device *dev, void *data,
1205                         struct drm_file *file_priv);
1206 int       drm_wait_vblank(struct drm_device *dev, void *data,
1207                               struct drm_file *file_priv);
1208 
1209 /* AGP/GART support (drm_agpsupport.c) */
1210 int       drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
1211                                     struct drm_file *file_priv);
1212 int       drm_agp_release_ioctl(struct drm_device *dev, void *data,
1213                                     struct drm_file *file_priv);
1214 int       drm_agp_enable_ioctl(struct drm_device *dev, void *data,
1215                                    struct drm_file *file_priv);
1216 int       drm_agp_info_ioctl(struct drm_device *dev, void *data,
1217                                  struct drm_file *file_priv);
1218 int       drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
1219                                   struct drm_file *file_priv);
1220 int       drm_agp_free_ioctl(struct drm_device *dev, void *data,
1221                                  struct drm_file *file_priv);
1222 int       drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
1223                                    struct drm_file *file_priv);
1224 int       drm_agp_bind_ioctl(struct drm_device *dev, void *data,
1225                                  struct drm_file *file_priv);
1226 
1227 /* Scatter Gather Support (drm_scatter.c) */
1228 int       drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1229                                  struct drm_file *file_priv);
1230 int       drm_sg_free(struct drm_device *dev, void *data,
1231                         struct drm_file *file_priv);
1232 
1233 /* consistent PCI memory functions (drm_pci.c) */
1234 drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1235                                         size_t align, dma_addr_t maxaddr);
1236 void      drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1237 
1238 /* Inline replacements for drm_alloc and friends */
1239 static __inline__ void *
drm_alloc(size_t size,struct malloc_type * area)1240 drm_alloc(size_t size, struct malloc_type *area)
1241 {
1242           return malloc(size, area, M_NOWAIT);
1243 }
1244 
1245 static __inline__ void *
drm_calloc(size_t nmemb,size_t size,struct malloc_type * area)1246 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1247 {
1248           return malloc(size * nmemb, area, M_NOWAIT | M_ZERO);
1249 }
1250 
1251 static __inline__ void *
drm_realloc(void * oldpt,size_t oldsize,size_t size,struct malloc_type * area)1252 drm_realloc(void *oldpt, size_t oldsize, size_t size,
1253     struct malloc_type *area)
1254 {
1255 #if defined(__FreeBSD__)
1256           return reallocf(oldpt, size, area, M_NOWAIT);
1257 #elif   defined(__NetBSD__)
1258           void *pt;
1259 
1260           pt = malloc(size, area, M_NOWAIT);
1261           if (pt == NULL)
1262                     return NULL;
1263           if (oldpt && oldsize) {
1264                     memcpy(pt, oldpt, oldsize);
1265                     free(oldpt, area);
1266           }
1267           return pt;
1268 #endif
1269 }
1270 
1271 static __inline__ void
drm_free(void * pt,size_t size,struct malloc_type * area)1272 drm_free(void *pt, size_t size, struct malloc_type *area)
1273 {
1274           free(pt, area);
1275 }
1276 
1277 /* Inline replacements for DRM_IOREMAP macros */
1278 static __inline__ void
drm_core_ioremap_wc(struct drm_local_map * map,struct drm_device * dev)1279 drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
1280 {
1281           map->handle = drm_ioremap_wc(dev, map);
1282 }
1283 static __inline__ void
drm_core_ioremap(struct drm_local_map * map,struct drm_device * dev)1284 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
1285 {
1286           map->handle = drm_ioremap(dev, map);
1287 }
1288 static __inline__ void
drm_core_ioremapfree(struct drm_local_map * map,struct drm_device * dev)1289 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
1290 {
1291           if ( map->handle && map->size )
1292                     drm_ioremapfree(map);
1293 }
1294 
1295 static __inline__ struct drm_local_map *
drm_core_findmap(struct drm_device * dev,unsigned long offset)1296 drm_core_findmap(struct drm_device *dev, unsigned long offset)
1297 {
1298           drm_local_map_t *map;
1299 
1300           DRM_SPINLOCK_ASSERT(&dev->dev_lock);
1301           TAILQ_FOREACH(map, &dev->maplist, link) {
1302                     if (map->offset == offset)
1303                               return map;
1304           }
1305           return NULL;
1306 }
1307 
drm_core_dropmap(struct drm_map * map)1308 static __inline__ void drm_core_dropmap(struct drm_map *map)
1309 {
1310 }
1311 
1312 #endif /* __KERNEL__ */
1313 #endif /* _DRM_P_H_ */
1314