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