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 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: stable/10/sys/dev/drm/drmP.h 248084 2013-03-09 02:32:23Z attilio $");
36 
37 #ifndef _DRM_P_H_
38 #define _DRM_P_H_
39 
40 #if defined(_KERNEL) || defined(__KERNEL__)
41 
42 struct drm_device;
43 struct drm_file;
44 
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/module.h>
50 #include <sys/systm.h>
51 #include <sys/conf.h>
52 #include <sys/sglist.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/rwlock.h>
63 #include <sys/sysctl.h>
64 #include <sys/bus.h>
65 #include <sys/queue.h>
66 #include <sys/signalvar.h>
67 #include <sys/poll.h>
68 #include <sys/taskqueue.h>
69 #include <sys/tree.h>
70 #include <vm/vm.h>
71 #include <vm/pmap.h>
72 #include <vm/vm_extern.h>
73 #include <vm/vm_kern.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vm_param.h>
79 #include <vm/vm_phys.h>
80 #include <machine/param.h>
81 #include <machine/pmap.h>
82 #include <machine/bus.h>
83 #include <machine/resource.h>
84 #if defined(__i386__) || defined(__amd64__)
85 #include <machine/specialreg.h>
86 #endif
87 #include <machine/sysarch.h>
88 #include <sys/endian.h>
89 #if _BYTE_ORDER == _BIG_ENDIAN
90 #define __BIG_ENDIAN 1
91 #else
92 #define __LITTLE_ENDIAN 1
93 #endif
94 #include <sys/mman.h>
95 #include <sys/rman.h>
96 #include <sys/memrange.h>
97 #if __FreeBSD_version >= 800004
98 #include <dev/agp/agpvar.h>
99 #else /* __FreeBSD_version >= 800004 */
100 #include <pci/agpvar.h>
101 #endif /* __FreeBSD_version >= 800004 */
102 #include <sys/agpio.h>
103 #include <sys/mutex.h>
104 #include <dev/pci/pcivar.h>
105 #include <dev/pci/pcireg.h>
106 #include <sys/selinfo.h>
107 #include <sys/bus.h>
108 
109 #include "dev/drm/drm.h"
110 #include "dev/drm/drm_atomic.h"
111 #include "dev/drm/drm_internal.h"
112 #include "dev/drm/drm_linux_list.h"
113 
114 #include <opt_drm.h>
115 #ifdef DRM_DEBUG
116 #undef DRM_DEBUG
117 #define DRM_DEBUG_DEFAULT_ON 1
118 #endif /* DRM_DEBUG */
119 
120 #if defined(DRM_LINUX) && DRM_LINUX && !defined(__amd64__)
121 #include <sys/file.h>
122 #include <sys/proc.h>
123 #include <machine/../linux/linux.h>
124 #include <machine/../linux/linux_proto.h>
125 #else
126 /* Either it was defined when it shouldn't be (FreeBSD amd64) or it isn't
127  * supported on this OS yet.
128  */
129 #undef DRM_LINUX
130 #define DRM_LINUX 0
131 #endif
132 
133 /* driver capabilities and requirements mask */
134 #define DRIVER_USE_AGP     0x1
135 #define DRIVER_REQUIRE_AGP 0x2
136 #define DRIVER_USE_MTRR    0x4
137 #define DRIVER_PCI_DMA     0x8
138 #define DRIVER_SG          0x10
139 #define DRIVER_HAVE_DMA    0x20
140 #define DRIVER_HAVE_IRQ    0x40
141 #define DRIVER_DMA_QUEUE   0x100
142 
143 
144 #define DRM_HASH_SIZE	      16 /* Size of key hash table		  */
145 #define DRM_KERNEL_CONTEXT    0	 /* Change drm_resctx if changed	  */
146 #define DRM_RESERVED_CONTEXTS 1	 /* Change drm_resctx if changed	  */
147 
148 MALLOC_DECLARE(DRM_MEM_DMA);
149 MALLOC_DECLARE(DRM_MEM_SAREA);
150 MALLOC_DECLARE(DRM_MEM_DRIVER);
151 MALLOC_DECLARE(DRM_MEM_MAGIC);
152 MALLOC_DECLARE(DRM_MEM_IOCTLS);
153 MALLOC_DECLARE(DRM_MEM_MAPS);
154 MALLOC_DECLARE(DRM_MEM_BUFS);
155 MALLOC_DECLARE(DRM_MEM_SEGS);
156 MALLOC_DECLARE(DRM_MEM_PAGES);
157 MALLOC_DECLARE(DRM_MEM_FILES);
158 MALLOC_DECLARE(DRM_MEM_QUEUES);
159 MALLOC_DECLARE(DRM_MEM_CMDS);
160 MALLOC_DECLARE(DRM_MEM_MAPPINGS);
161 MALLOC_DECLARE(DRM_MEM_BUFLISTS);
162 MALLOC_DECLARE(DRM_MEM_AGPLISTS);
163 MALLOC_DECLARE(DRM_MEM_CTXBITMAP);
164 MALLOC_DECLARE(DRM_MEM_SGLISTS);
165 MALLOC_DECLARE(DRM_MEM_DRAWABLE);
166 MALLOC_DECLARE(DRM_MEM_MM);
167 MALLOC_DECLARE(DRM_MEM_HASHTAB);
168 
169 SYSCTL_DECL(_hw_drm);
170 
171 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
172 
173 				/* Internal types and structures */
174 #define DRM_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
175 #define DRM_MIN(a,b) ((a)<(b)?(a):(b))
176 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
177 
178 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
179 
180 #define __OS_HAS_AGP	1
181 
182 #define DRM_DEV_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
183 #define DRM_DEV_UID	0
184 #define DRM_DEV_GID	0
185 
186 #define wait_queue_head_t	atomic_t
187 #define DRM_WAKEUP(w)		wakeup((void *)w)
188 #define DRM_WAKEUP_INT(w)	wakeup(w)
189 #define DRM_INIT_WAITQUEUE(queue) do {(void)(queue);} while (0)
190 
191 #define DRM_CURPROC		curthread
192 #define DRM_STRUCTPROC		struct thread
193 #define DRM_SPINTYPE		struct mtx
194 #define DRM_SPININIT(l,name)	mtx_init(l, name, NULL, MTX_DEF)
195 #define DRM_SPINUNINIT(l)	mtx_destroy(l)
196 #define DRM_SPINLOCK(l)		mtx_lock(l)
197 #define DRM_SPINUNLOCK(u)	mtx_unlock(u)
198 #define DRM_SPINLOCK_IRQSAVE(l, irqflags) do {		\
199 	mtx_lock(l);					\
200 	(void)irqflags;					\
201 } while (0)
202 #define DRM_SPINUNLOCK_IRQRESTORE(u, irqflags) mtx_unlock(u)
203 #define DRM_SPINLOCK_ASSERT(l)	mtx_assert(l, MA_OWNED)
204 #define DRM_CURRENTPID		curthread->td_proc->p_pid
205 #define DRM_LOCK()		mtx_lock(&dev->dev_lock)
206 #define DRM_UNLOCK() 		mtx_unlock(&dev->dev_lock)
207 #define DRM_SYSCTL_HANDLER_ARGS	(SYSCTL_HANDLER_ARGS)
208 
209 #define DRM_IRQ_ARGS		void *arg
210 typedef void			irqreturn_t;
211 #define IRQ_HANDLED		/* nothing */
212 #define IRQ_NONE		/* nothing */
213 
214 #define unlikely(x)            __builtin_expect(!!(x), 0)
215 #define container_of(ptr, type, member) ({			\
216 	__typeof( ((type *)0)->member ) *__mptr = (ptr);	\
217 	(type *)( (char *)__mptr - offsetof(type,member) );})
218 
219 enum {
220 	DRM_IS_NOT_AGP,
221 	DRM_IS_AGP,
222 	DRM_MIGHT_BE_AGP
223 };
224 #define DRM_AGP_MEM		struct agp_memory_info
225 
226 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
227 
228 #define PAGE_ALIGN(addr) round_page(addr)
229 /* DRM_SUSER returns true if the user is superuser */
230 #if __FreeBSD_version >= 700000
231 #define DRM_SUSER(p)		(priv_check(p, PRIV_DRIVER) == 0)
232 #else
233 #define DRM_SUSER(p)		(suser(p) == 0)
234 #endif
235 #define DRM_AGP_FIND_DEVICE()	agp_find_device()
236 #define DRM_MTRR_WC		MDF_WRITECOMBINE
237 #define jiffies			ticks
238 
239 typedef vm_paddr_t dma_addr_t;
240 typedef u_int64_t u64;
241 typedef u_int32_t u32;
242 typedef u_int16_t u16;
243 typedef u_int8_t u8;
244 
245 /* DRM_READMEMORYBARRIER() prevents reordering of reads.
246  * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
247  * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
248  */
249 #define DRM_READMEMORYBARRIER()		rmb()
250 #define DRM_WRITEMEMORYBARRIER()	wmb()
251 #define DRM_MEMORYBARRIER()		mb()
252 
253 #define DRM_READ8(map, offset)						\
254 	*(volatile u_int8_t *)(((vm_offset_t)(map)->virtual) +		\
255 	    (vm_offset_t)(offset))
256 #define DRM_READ16(map, offset)						\
257 	le16toh(*(volatile u_int16_t *)(((vm_offset_t)(map)->virtual) +	\
258 	    (vm_offset_t)(offset)))
259 #define DRM_READ32(map, offset)						\
260 	le32toh(*(volatile u_int32_t *)(((vm_offset_t)(map)->virtual) +	\
261 	    (vm_offset_t)(offset)))
262 #define DRM_WRITE8(map, offset, val)					\
263 	*(volatile u_int8_t *)(((vm_offset_t)(map)->virtual) +		\
264 	    (vm_offset_t)(offset)) = val
265 #define DRM_WRITE16(map, offset, val)					\
266 	*(volatile u_int16_t *)(((vm_offset_t)(map)->virtual) +		\
267 	    (vm_offset_t)(offset)) = htole16(val)
268 #define DRM_WRITE32(map, offset, val)					\
269 	*(volatile u_int32_t *)(((vm_offset_t)(map)->virtual) +		\
270 	    (vm_offset_t)(offset)) = htole32(val)
271 
272 #define DRM_VERIFYAREA_READ( uaddr, size )		\
273 	(!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
274 
275 #define DRM_COPY_TO_USER(user, kern, size) \
276 	copyout(kern, user, size)
277 #define DRM_COPY_FROM_USER(kern, user, size) \
278 	copyin(user, kern, size)
279 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3) 	\
280 	copyin(arg2, arg1, arg3)
281 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)	\
282 	copyout(arg2, arg1, arg3)
283 #define DRM_GET_USER_UNCHECKED(val, uaddr)		\
284 	((val) = fuword32(uaddr), 0)
285 
286 #define cpu_to_le32(x) htole32(x)
287 #define le32_to_cpu(x) le32toh(x)
288 
289 #define DRM_HZ			hz
290 #define DRM_UDELAY(udelay)	DELAY(udelay)
291 #define DRM_TIME_SLICE		(hz/20)  /* Time slice for GLXContexts	  */
292 
293 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {	\
294 	(_map) = (_dev)->context_sareas[_ctx];		\
295 } while(0)
296 
297 #define LOCK_TEST_WITH_RETURN(dev, file_priv)				\
298 do {									\
299 	if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||		\
300 	     dev->lock.file_priv != file_priv) {			\
301 		DRM_ERROR("%s called without lock held\n",		\
302 			   __FUNCTION__);				\
303 		return EINVAL;						\
304 	}								\
305 } while (0)
306 
307 /* Returns -errno to shared code */
308 #define DRM_WAIT_ON( ret, queue, timeout, condition )		\
309 for ( ret = 0 ; !ret && !(condition) ; ) {			\
310 	DRM_UNLOCK();						\
311 	mtx_lock(&dev->irq_lock);				\
312 	if (!(condition))					\
313 	    ret = -mtx_sleep(&(queue), &dev->irq_lock, 		\
314 		PCATCH, "drmwtq", (timeout));			\
315 	mtx_unlock(&dev->irq_lock);				\
316 	DRM_LOCK();						\
317 }
318 
319 #define DRM_ERROR(fmt, ...) \
320 	printf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,		\
321 	    DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
322 
323 #define DRM_INFO(fmt, ...)  printf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
324 
325 #define DRM_DEBUG(fmt, ...) do {					\
326 	if (drm_debug_flag)						\
327 		printf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,	\
328 			__func__ , ##__VA_ARGS__);			\
329 } while (0)
330 
331 typedef struct drm_pci_id_list
332 {
333 	int vendor;
334 	int device;
335 	long driver_private;
336 	char *name;
337 } drm_pci_id_list_t;
338 
339 struct drm_msi_blacklist_entry
340 {
341 	int vendor;
342 	int device;
343 };
344 
345 #define DRM_AUTH	0x1
346 #define DRM_MASTER	0x2
347 #define DRM_ROOT_ONLY	0x4
348 typedef struct drm_ioctl_desc {
349 	unsigned long cmd;
350 	int (*func)(struct drm_device *dev, void *data,
351 		    struct drm_file *file_priv);
352 	int flags;
353 } drm_ioctl_desc_t;
354 /**
355  * Creates a driver or general drm_ioctl_desc array entry for the given
356  * ioctl, for use by drm_ioctl().
357  */
358 #define DRM_IOCTL_DEF(ioctl, func, flags) \
359 	[DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
360 
361 typedef struct drm_magic_entry {
362 	drm_magic_t	       magic;
363 	struct drm_file	       *priv;
364 	struct drm_magic_entry *next;
365 } drm_magic_entry_t;
366 
367 typedef struct drm_magic_head {
368 	struct drm_magic_entry *head;
369 	struct drm_magic_entry *tail;
370 } drm_magic_head_t;
371 
372 typedef struct drm_buf {
373 	int		  idx;	       /* Index into master buflist	     */
374 	int		  total;       /* Buffer size			     */
375 	int		  order;       /* log-base-2(total)		     */
376 	int		  used;	       /* Amount of buffer in use (for DMA)  */
377 	unsigned long	  offset;      /* Byte offset (used internally)	     */
378 	void		  *address;    /* Address of buffer		     */
379 	unsigned long	  bus_address; /* Bus address of buffer		     */
380 	struct drm_buf	  *next;       /* Kernel-only: used for free list    */
381 	__volatile__ int  pending;     /* On hardware DMA queue		     */
382 	struct drm_file   *file_priv;  /* Unique identifier of holding process */
383 	int		  context;     /* Kernel queue for this buffer	     */
384 	enum {
385 		DRM_LIST_NONE	 = 0,
386 		DRM_LIST_FREE	 = 1,
387 		DRM_LIST_WAIT	 = 2,
388 		DRM_LIST_PEND	 = 3,
389 		DRM_LIST_PRIO	 = 4,
390 		DRM_LIST_RECLAIM = 5
391 	}		  list;	       /* Which list we're on		     */
392 
393 	int		  dev_priv_size; /* Size of buffer private stoarge   */
394 	void		  *dev_private;  /* Per-buffer private storage       */
395 } drm_buf_t;
396 
397 typedef struct drm_freelist {
398 	int		  initialized; /* Freelist in use		   */
399 	atomic_t	  count;       /* Number of free buffers	   */
400 	drm_buf_t	  *next;       /* End pointer			   */
401 
402 	int		  low_mark;    /* Low water mark		   */
403 	int		  high_mark;   /* High water mark		   */
404 } drm_freelist_t;
405 
406 typedef struct drm_dma_handle {
407 	void *vaddr;
408 	bus_addr_t busaddr;
409 	bus_dma_tag_t tag;
410 	bus_dmamap_t map;
411 } drm_dma_handle_t;
412 
413 typedef struct drm_buf_entry {
414 	int		  buf_size;
415 	int		  buf_count;
416 	drm_buf_t	  *buflist;
417 	int		  seg_count;
418 	drm_dma_handle_t  **seglist;
419 	int		  page_order;
420 
421 	drm_freelist_t	  freelist;
422 } drm_buf_entry_t;
423 
424 typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
425 struct drm_file {
426 	TAILQ_ENTRY(drm_file) link;
427 	struct drm_device *dev;
428 	int		  authenticated;
429 	int		  master;
430 	pid_t		  pid;
431 	uid_t		  uid;
432 	drm_magic_t	  magic;
433 	unsigned long	  ioctl_count;
434 	void		 *driver_priv;
435 };
436 
437 typedef struct drm_lock_data {
438 	struct drm_hw_lock	*hw_lock;	/* Hardware lock		   */
439 	struct drm_file   *file_priv;   /* Unique identifier of holding process (NULL is kernel)*/
440 	int		  lock_queue;	/* Queue of blocked processes	   */
441 	unsigned long	  lock_time;	/* Time of last lock in jiffies	   */
442 } drm_lock_data_t;
443 
444 /* This structure, in the struct drm_device, is always initialized while the
445  * device
446  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
447  * when set marks that no further bufs may be allocated until device teardown
448  * occurs (when the last open of the device has closed).  The high/low
449  * watermarks of bufs are only touched by the X Server, and thus not
450  * concurrently accessed, so no locking is needed.
451  */
452 typedef struct drm_device_dma {
453 	drm_buf_entry_t	  bufs[DRM_MAX_ORDER+1];
454 	int		  buf_count;
455 	drm_buf_t	  **buflist;	/* Vector of pointers info bufs	   */
456 	int		  seg_count;
457 	int		  page_count;
458 	unsigned long	  *pagelist;
459 	unsigned long	  byte_count;
460 	enum {
461 		_DRM_DMA_USE_AGP = 0x01,
462 		_DRM_DMA_USE_SG  = 0x02
463 	} flags;
464 } drm_device_dma_t;
465 
466 typedef struct drm_agp_mem {
467 	void               *handle;
468 	unsigned long      bound; /* address */
469 	int                pages;
470 	struct drm_agp_mem *prev;
471 	struct drm_agp_mem *next;
472 } drm_agp_mem_t;
473 
474 typedef struct drm_agp_head {
475 	device_t	   agpdev;
476 	struct agp_info    info;
477 	const char         *chipset;
478 	drm_agp_mem_t      *memory;
479 	unsigned long      mode;
480 	int                enabled;
481 	int                acquired;
482 	unsigned long      base;
483    	int 		   mtrr;
484 	int		   cant_use_aperture;
485 	unsigned long	   page_mask;
486 } drm_agp_head_t;
487 
488 typedef struct drm_sg_mem {
489 	vm_offset_t vaddr;
490 	vm_paddr_t *busaddr;
491 	vm_pindex_t pages;
492 } drm_sg_mem_t;
493 
494 #define DRM_MAP_HANDLE_BITS	(sizeof(void *) == 4 ? 4 : 24)
495 #define DRM_MAP_HANDLE_SHIFT	(sizeof(void *) * 8 - DRM_MAP_HANDLE_BITS)
496 typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
497 
498 typedef struct drm_local_map {
499 	unsigned long offset;	  /* Physical address (0 for SAREA)       */
500 	unsigned long size;	  /* Physical size (bytes)                */
501 	enum drm_map_type type;	  /* Type of memory mapped                */
502 	enum drm_map_flags flags; /* Flags                                */
503 	void *handle;		  /* User-space: "Handle" to pass to mmap */
504 				  /* Kernel-space: kernel-virtual address */
505 	int mtrr;		  /* Boolean: MTRR used                   */
506 				  /* Private data                         */
507 	int rid;		  /* PCI resource ID for bus_space        */
508 	void *virtual;		  /* Kernel-space: kernel-virtual address */
509 	struct resource *bsr;
510 	bus_space_tag_t bst;
511 	bus_space_handle_t bsh;
512 	drm_dma_handle_t *dmah;
513 	TAILQ_ENTRY(drm_local_map) link;
514 } drm_local_map_t;
515 
516 struct drm_vblank_info {
517 	wait_queue_head_t queue;	/* vblank wait queue */
518 	atomic_t count;			/* number of VBLANK interrupts */
519 					/* (driver must alloc the right number of counters) */
520 	atomic_t refcount;		/* number of users of vblank interrupts */
521 	u32 last;			/* protected by dev->vbl_lock, used */
522 					/* for wraparound handling */
523 	int enabled;			/* so we don't call enable more than */
524 					/* once per disable */
525 	int inmodeset;			/* Display driver is setting mode */
526 };
527 
528 /* location of GART table */
529 #define DRM_ATI_GART_MAIN 1
530 #define DRM_ATI_GART_FB   2
531 
532 #define DRM_ATI_GART_PCI  1
533 #define DRM_ATI_GART_PCIE 2
534 #define DRM_ATI_GART_IGP  3
535 
536 struct drm_ati_pcigart_info {
537 	int gart_table_location;
538 	int gart_reg_if;
539 	void *addr;
540 	dma_addr_t bus_addr;
541 	dma_addr_t table_mask;
542 	dma_addr_t member_mask;
543 	struct drm_dma_handle *table_handle;
544 	drm_local_map_t mapping;
545 	int table_size;
546 	struct drm_dma_handle *dmah; /* handle for ATI PCIGART table */
547 };
548 
549 #ifndef DMA_BIT_MASK
550 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
551 #endif
552 
553 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
554 
555 struct drm_driver_info {
556 	int	(*load)(struct drm_device *, unsigned long flags);
557 	int	(*firstopen)(struct drm_device *);
558 	int	(*open)(struct drm_device *, struct drm_file *);
559 	void	(*preclose)(struct drm_device *, struct drm_file *file_priv);
560 	void	(*postclose)(struct drm_device *, struct drm_file *);
561 	void	(*lastclose)(struct drm_device *);
562 	int	(*unload)(struct drm_device *);
563 	void	(*reclaim_buffers_locked)(struct drm_device *,
564 					  struct drm_file *file_priv);
565 	int	(*dma_ioctl)(struct drm_device *dev, void *data,
566 			     struct drm_file *file_priv);
567 	void	(*dma_ready)(struct drm_device *);
568 	int	(*dma_quiescent)(struct drm_device *);
569 	int	(*dma_flush_block_and_flush)(struct drm_device *, int context,
570 					     enum drm_lock_flags flags);
571 	int	(*dma_flush_unblock)(struct drm_device *, int context,
572 				     enum drm_lock_flags flags);
573 	int	(*context_ctor)(struct drm_device *dev, int context);
574 	int	(*context_dtor)(struct drm_device *dev, int context);
575 	int	(*kernel_context_switch)(struct drm_device *dev, int old,
576 					 int new);
577 	int	(*kernel_context_switch_unlock)(struct drm_device *dev);
578 	void	(*irq_preinstall)(struct drm_device *dev);
579 	int	(*irq_postinstall)(struct drm_device *dev);
580 	void	(*irq_uninstall)(struct drm_device *dev);
581 	void	(*irq_handler)(DRM_IRQ_ARGS);
582 	u32	(*get_vblank_counter)(struct drm_device *dev, int crtc);
583 	int	(*enable_vblank)(struct drm_device *dev, int crtc);
584 	void	(*disable_vblank)(struct drm_device *dev, int crtc);
585 
586 	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
587 
588 	/**
589 	 * Called by \c drm_device_is_agp.  Typically used to determine if a
590 	 * card is really attached to AGP or not.
591 	 *
592 	 * \param dev  DRM device handle
593 	 *
594 	 * \returns
595 	 * One of three values is returned depending on whether or not the
596 	 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
597 	 * (return of 1), or may or may not be AGP (return of 2).
598 	 */
599 	int	(*device_is_agp) (struct drm_device * dev);
600 
601 	drm_ioctl_desc_t *ioctls;
602 	int	max_ioctl;
603 
604 	int	buf_priv_size;
605 
606 	int	major;
607 	int	minor;
608 	int	patchlevel;
609 	const char *name;		/* Simple driver name		   */
610 	const char *desc;		/* Longer driver name		   */
611 	const char *date;		/* Date of last major changes.	   */
612 
613 	u32 driver_features;
614 };
615 
616 /* Length for the array of resource pointers for drm_get_resource_*. */
617 #define DRM_MAX_PCI_RESOURCE	6
618 
619 /**
620  * DRM device functions structure
621  */
622 struct drm_device {
623 	struct drm_driver_info *driver;
624 	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
625 
626 	u_int16_t pci_device;		/* PCI device id */
627 	u_int16_t pci_vendor;		/* PCI vendor id */
628 
629 	char		  *unique;	/* Unique identifier: e.g., busid  */
630 	int		  unique_len;	/* Length of unique field	   */
631 	device_t	  device;	/* Device instance from newbus     */
632 	struct cdev	  *devnode;	/* Device number for mknod	   */
633 	int		  if_version;	/* Highest interface version set */
634 
635 	int		  flags;	/* Flags to open(2)		   */
636 
637 				/* Locks */
638 	struct mtx	  vbl_lock;	/* protects vblank operations */
639 	struct mtx	  dma_lock;	/* protects dev->dma */
640 	struct mtx	  irq_lock;	/* protects irq condition checks */
641 	struct mtx	  dev_lock;	/* protects everything else */
642 	DRM_SPINTYPE	  drw_lock;
643 
644 				/* Usage Counters */
645 	int		  open_count;	/* Outstanding files open	   */
646 	int		  buf_use;	/* Buffers in use -- cannot alloc  */
647 
648 				/* Performance counters */
649 	unsigned long     counters;
650 	enum drm_stat_type	types[15];
651 	atomic_t          counts[15];
652 
653 				/* Authentication */
654 	drm_file_list_t   files;
655 	drm_magic_head_t  magiclist[DRM_HASH_SIZE];
656 
657 	/* Linked list of mappable regions. Protected by dev_lock */
658 	drm_map_list_t	  maplist;
659 	struct unrhdr	  *map_unrhdr;
660 
661 	drm_local_map_t	  **context_sareas;
662 	int		  max_context;
663 
664 	drm_lock_data_t	  lock;		/* Information on hardware lock	   */
665 
666 				/* DMA queues (contexts) */
667 	drm_device_dma_t  *dma;		/* Optional pointer for DMA support */
668 
669 				/* Context support */
670 	int		  irq;		/* Interrupt used by board	   */
671 	int		  irq_enabled;	/* True if the irq handler is enabled */
672 	int		  msi_enabled;	/* MSI enabled */
673 	int		  irqrid;	/* Interrupt used by board */
674 	struct resource   *irqr;	/* Resource for interrupt used by board	   */
675 	void		  *irqh;	/* Handle from bus_setup_intr      */
676 
677 	/* Storage of resource pointers for drm_get_resource_* */
678 	struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
679 	int		  pcirid[DRM_MAX_PCI_RESOURCE];
680 
681 	int		  pci_domain;
682 	int		  pci_bus;
683 	int		  pci_slot;
684 	int		  pci_func;
685 
686 	atomic_t	  context_flag;	/* Context swapping flag	   */
687 	int		  last_context;	/* Last current context		   */
688 
689 	int		  vblank_disable_allowed;
690 	struct callout	  vblank_disable_timer;
691 	u32		  max_vblank_count;	/* size of vblank counter register */
692 	struct drm_vblank_info *vblank;		/* per crtc vblank info */
693 	int		  num_crtcs;
694 
695 	struct sigio      *buf_sigio;	/* Processes waiting for SIGIO     */
696 
697 				/* Sysctl support */
698 	struct drm_sysctl_info *sysctl;
699 
700 	drm_agp_head_t    *agp;
701 	drm_sg_mem_t      *sg;  /* Scatter gather memory */
702 	atomic_t          *ctx_bitmap;
703 	void		  *dev_private;
704 	unsigned int	  agp_buffer_token;
705 	drm_local_map_t   *agp_buffer_map;
706 
707 	struct unrhdr	  *drw_unrhdr;
708 	/* RB tree of drawable infos */
709 	RB_HEAD(drawable_tree, bsd_drm_drawable_info) drw_head;
710 };
711 
drm_core_check_feature(struct drm_device * dev,int feature)712 static __inline__ int drm_core_check_feature(struct drm_device *dev,
713 					     int feature)
714 {
715 	return ((dev->driver->driver_features & feature) ? 1 : 0);
716 }
717 
718 #if __OS_HAS_AGP
drm_core_has_AGP(struct drm_device * dev)719 static inline int drm_core_has_AGP(struct drm_device *dev)
720 {
721 	return drm_core_check_feature(dev, DRIVER_USE_AGP);
722 }
723 #else
724 #define drm_core_has_AGP(dev) (0)
725 #endif
726 
727 extern int	drm_debug_flag;
728 
729 /* Device setup support (drm_drv.c) */
730 int	drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
731 int	drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
732 void	drm_close(void *data);
733 int	drm_detach(device_t kdev);
734 d_ioctl_t drm_ioctl;
735 d_open_t drm_open;
736 d_read_t drm_read;
737 d_poll_t drm_poll;
738 d_mmap_t drm_mmap;
739 extern drm_local_map_t	*drm_getsarea(struct drm_device *dev);
740 
741 /* File operations helpers (drm_fops.c) */
742 extern int		drm_open_helper(struct cdev *kdev, int flags, int fmt,
743 					 DRM_STRUCTPROC *p,
744 					struct drm_device *dev);
745 
746 /* Memory management support (drm_memory.c) */
747 void	drm_mem_init(void);
748 void	drm_mem_uninit(void);
749 void	*drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map);
750 void	*drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
751 void	drm_ioremapfree(drm_local_map_t *map);
752 int	drm_mtrr_add(unsigned long offset, size_t size, int flags);
753 int	drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
754 
755 int	drm_context_switch(struct drm_device *dev, int old, int new);
756 int	drm_context_switch_complete(struct drm_device *dev, int new);
757 
758 int	drm_ctxbitmap_init(struct drm_device *dev);
759 void	drm_ctxbitmap_cleanup(struct drm_device *dev);
760 void	drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
761 int	drm_ctxbitmap_next(struct drm_device *dev);
762 
763 /* Locking IOCTL support (drm_lock.c) */
764 int	drm_lock_take(struct drm_lock_data *lock_data,
765 		      unsigned int context);
766 int	drm_lock_transfer(struct drm_lock_data *lock_data,
767 			  unsigned int context);
768 int	drm_lock_free(struct drm_lock_data *lock_data,
769 		      unsigned int context);
770 
771 /* Buffer management support (drm_bufs.c) */
772 unsigned long drm_get_resource_start(struct drm_device *dev,
773 				     unsigned int resource);
774 unsigned long drm_get_resource_len(struct drm_device *dev,
775 				   unsigned int resource);
776 void	drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
777 int	drm_order(unsigned long size);
778 int	drm_addmap(struct drm_device *dev, unsigned long offset,
779 		   unsigned long size,
780 		   enum drm_map_type type, enum drm_map_flags flags,
781 		   drm_local_map_t **map_ptr);
782 int	drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request);
783 int	drm_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request);
784 int	drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request);
785 
786 /* DMA support (drm_dma.c) */
787 int	drm_dma_setup(struct drm_device *dev);
788 void	drm_dma_takedown(struct drm_device *dev);
789 void	drm_free_buffer(struct drm_device *dev, drm_buf_t *buf);
790 void	drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv);
791 #define drm_core_reclaim_buffers drm_reclaim_buffers
792 
793 /* IRQ support (drm_irq.c) */
794 int	drm_irq_install(struct drm_device *dev);
795 int	drm_irq_uninstall(struct drm_device *dev);
796 irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
797 void	drm_driver_irq_preinstall(struct drm_device *dev);
798 void	drm_driver_irq_postinstall(struct drm_device *dev);
799 void	drm_driver_irq_uninstall(struct drm_device *dev);
800 void	drm_handle_vblank(struct drm_device *dev, int crtc);
801 u32	drm_vblank_count(struct drm_device *dev, int crtc);
802 int	drm_vblank_get(struct drm_device *dev, int crtc);
803 void	drm_vblank_put(struct drm_device *dev, int crtc);
804 void	drm_vblank_cleanup(struct drm_device *dev);
805 int	drm_vblank_wait(struct drm_device *dev, unsigned int *vbl_seq);
806 int	drm_vblank_init(struct drm_device *dev, int num_crtcs);
807 int 	drm_modeset_ctl(struct drm_device *dev, void *data,
808 			struct drm_file *file_priv);
809 
810 /* AGP/PCI Express/GART support (drm_agpsupport.c) */
811 int	drm_device_is_agp(struct drm_device *dev);
812 int	drm_device_is_pcie(struct drm_device *dev);
813 drm_agp_head_t *drm_agp_init(void);
814 int	drm_agp_acquire(struct drm_device *dev);
815 int	drm_agp_release(struct drm_device *dev);
816 int	drm_agp_info(struct drm_device * dev, struct drm_agp_info *info);
817 int	drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
818 void	*drm_agp_allocate_memory(size_t pages, u32 type);
819 int	drm_agp_free_memory(void *handle);
820 int	drm_agp_bind_memory(void *handle, off_t start);
821 int	drm_agp_unbind_memory(void *handle);
822 int	drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
823 int	drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
824 int	drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
825 int	drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
826 
827 /* Scatter Gather Support (drm_scatter.c) */
828 void	drm_sg_cleanup(drm_sg_mem_t *entry);
829 int	drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
830 
831 /* sysctl support (drm_sysctl.h) */
832 extern int		drm_sysctl_init(struct drm_device *dev);
833 extern int		drm_sysctl_cleanup(struct drm_device *dev);
834 
835 /* ATI PCIGART support (ati_pcigart.c) */
836 int	drm_ati_pcigart_init(struct drm_device *dev,
837 				struct drm_ati_pcigart_info *gart_info);
838 int	drm_ati_pcigart_cleanup(struct drm_device *dev,
839 				struct drm_ati_pcigart_info *gart_info);
840 
841 /* Locking IOCTL support (drm_drv.c) */
842 int	drm_lock(struct drm_device *dev, void *data,
843 		 struct drm_file *file_priv);
844 int	drm_unlock(struct drm_device *dev, void *data,
845 		   struct drm_file *file_priv);
846 int	drm_version(struct drm_device *dev, void *data,
847 		    struct drm_file *file_priv);
848 int	drm_setversion(struct drm_device *dev, void *data,
849 		       struct drm_file *file_priv);
850 
851 /* Misc. IOCTL support (drm_ioctl.c) */
852 int	drm_irq_by_busid(struct drm_device *dev, void *data,
853 			 struct drm_file *file_priv);
854 int	drm_getunique(struct drm_device *dev, void *data,
855 		      struct drm_file *file_priv);
856 int	drm_setunique(struct drm_device *dev, void *data,
857 		      struct drm_file *file_priv);
858 int	drm_getmap(struct drm_device *dev, void *data,
859 		   struct drm_file *file_priv);
860 int	drm_getclient(struct drm_device *dev, void *data,
861 		      struct drm_file *file_priv);
862 int	drm_getstats(struct drm_device *dev, void *data,
863 		     struct drm_file *file_priv);
864 int	drm_noop(struct drm_device *dev, void *data,
865 		 struct drm_file *file_priv);
866 
867 /* Context IOCTL support (drm_context.c) */
868 int	drm_resctx(struct drm_device *dev, void *data,
869 		   struct drm_file *file_priv);
870 int	drm_addctx(struct drm_device *dev, void *data,
871 		   struct drm_file *file_priv);
872 int	drm_modctx(struct drm_device *dev, void *data,
873 		   struct drm_file *file_priv);
874 int	drm_getctx(struct drm_device *dev, void *data,
875 		   struct drm_file *file_priv);
876 int	drm_switchctx(struct drm_device *dev, void *data,
877 		      struct drm_file *file_priv);
878 int	drm_newctx(struct drm_device *dev, void *data,
879 		   struct drm_file *file_priv);
880 int	drm_rmctx(struct drm_device *dev, void *data,
881 		  struct drm_file *file_priv);
882 int	drm_setsareactx(struct drm_device *dev, void *data,
883 			struct drm_file *file_priv);
884 int	drm_getsareactx(struct drm_device *dev, void *data,
885 			struct drm_file *file_priv);
886 
887 /* Drawable IOCTL support (drm_drawable.c) */
888 int	drm_adddraw(struct drm_device *dev, void *data,
889 		    struct drm_file *file_priv);
890 int	drm_rmdraw(struct drm_device *dev, void *data,
891 		   struct drm_file *file_priv);
892 int	drm_update_draw(struct drm_device *dev, void *data,
893 			struct drm_file *file_priv);
894 struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
895 						int handle);
896 
897 /* Drawable support (drm_drawable.c) */
898 void drm_drawable_free_all(struct drm_device *dev);
899 
900 /* Authentication IOCTL support (drm_auth.c) */
901 int	drm_getmagic(struct drm_device *dev, void *data,
902 		     struct drm_file *file_priv);
903 int	drm_authmagic(struct drm_device *dev, void *data,
904 		      struct drm_file *file_priv);
905 
906 /* Buffer management support (drm_bufs.c) */
907 int	drm_addmap_ioctl(struct drm_device *dev, void *data,
908 			 struct drm_file *file_priv);
909 int	drm_rmmap_ioctl(struct drm_device *dev, void *data,
910 			struct drm_file *file_priv);
911 int	drm_addbufs(struct drm_device *dev, void *data,
912 		    struct drm_file *file_priv);
913 int	drm_infobufs(struct drm_device *dev, void *data,
914 		     struct drm_file *file_priv);
915 int	drm_markbufs(struct drm_device *dev, void *data,
916 		     struct drm_file *file_priv);
917 int	drm_freebufs(struct drm_device *dev, void *data,
918 		     struct drm_file *file_priv);
919 int	drm_mapbufs(struct drm_device *dev, void *data,
920 		    struct drm_file *file_priv);
921 
922 /* DMA support (drm_dma.c) */
923 int	drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
924 
925 /* IRQ support (drm_irq.c) */
926 int	drm_control(struct drm_device *dev, void *data,
927 		    struct drm_file *file_priv);
928 int	drm_wait_vblank(struct drm_device *dev, void *data,
929 			struct drm_file *file_priv);
930 
931 /* AGP/GART support (drm_agpsupport.c) */
932 int	drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
933 			      struct drm_file *file_priv);
934 int	drm_agp_release_ioctl(struct drm_device *dev, void *data,
935 			      struct drm_file *file_priv);
936 int	drm_agp_enable_ioctl(struct drm_device *dev, void *data,
937 			     struct drm_file *file_priv);
938 int	drm_agp_info_ioctl(struct drm_device *dev, void *data,
939 			   struct drm_file *file_priv);
940 int	drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
941 			    struct drm_file *file_priv);
942 int	drm_agp_free_ioctl(struct drm_device *dev, void *data,
943 			   struct drm_file *file_priv);
944 int	drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
945 			     struct drm_file *file_priv);
946 int	drm_agp_bind_ioctl(struct drm_device *dev, void *data,
947 			   struct drm_file *file_priv);
948 
949 /* Scatter Gather Support (drm_scatter.c) */
950 int	drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
951 			   struct drm_file *file_priv);
952 int	drm_sg_free(struct drm_device *dev, void *data,
953 		    struct drm_file *file_priv);
954 
955 /* consistent PCI memory functions (drm_pci.c) */
956 drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
957 				size_t align, dma_addr_t maxaddr);
958 void	drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
959 
960 /* Inline replacements for drm_alloc and friends */
961 static __inline__ void *
drm_alloc(size_t size,struct malloc_type * area)962 drm_alloc(size_t size, struct malloc_type *area)
963 {
964 	return malloc(size, area, M_NOWAIT);
965 }
966 
967 static __inline__ void *
drm_calloc(size_t nmemb,size_t size,struct malloc_type * area)968 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
969 {
970 	return malloc(size * nmemb, area, M_NOWAIT | M_ZERO);
971 }
972 
973 static __inline__ void *
drm_realloc(void * oldpt,size_t oldsize,size_t size,struct malloc_type * area)974 drm_realloc(void *oldpt, size_t oldsize, size_t size,
975     struct malloc_type *area)
976 {
977 	return reallocf(oldpt, size, area, M_NOWAIT);
978 }
979 
980 static __inline__ void
drm_free(void * pt,size_t size,struct malloc_type * area)981 drm_free(void *pt, size_t size, struct malloc_type *area)
982 {
983 	free(pt, area);
984 }
985 
986 /* Inline replacements for DRM_IOREMAP macros */
987 static __inline__ void
drm_core_ioremap_wc(struct drm_local_map * map,struct drm_device * dev)988 drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
989 {
990 	map->virtual = drm_ioremap_wc(dev, map);
991 }
992 static __inline__ void
drm_core_ioremap(struct drm_local_map * map,struct drm_device * dev)993 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
994 {
995 	map->virtual = drm_ioremap(dev, map);
996 }
997 static __inline__ void
drm_core_ioremapfree(struct drm_local_map * map,struct drm_device * dev)998 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
999 {
1000 	if ( map->virtual && map->size )
1001 		drm_ioremapfree(map);
1002 }
1003 
1004 static __inline__ struct drm_local_map *
drm_core_findmap(struct drm_device * dev,unsigned long offset)1005 drm_core_findmap(struct drm_device *dev, unsigned long offset)
1006 {
1007 	drm_local_map_t *map;
1008 
1009 	DRM_SPINLOCK_ASSERT(&dev->dev_lock);
1010 	TAILQ_FOREACH(map, &dev->maplist, link) {
1011 		if (offset == (unsigned long)map->handle)
1012 			return map;
1013 	}
1014 	return NULL;
1015 }
1016 
drm_core_dropmap(struct drm_map * map)1017 static __inline__ void drm_core_dropmap(struct drm_map *map)
1018 {
1019 }
1020 
1021 #endif /* __KERNEL__ */
1022 #endif /* _DRM_P_H_ */
1023