2 * Internal Header for the Direct Rendering Manager
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * Copyright (c) 2009-2010, Code Aurora Forum.
9 * Author: Rickard E. (Rik) Faith <faith@valinux.com>
10 * Author: Gareth Hughes <gareth@valinux.com>
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice (including the next
20 * paragraph) shall be included in all copies or substantial portions of the
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29 * OTHER DEALINGS IN THE SOFTWARE.
35 #if defined(_KERNEL) || defined(__KERNEL__)
37 #include <linux/agp_backend.h>
38 #include <linux/cdev.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/file.h>
42 #include <linux/highmem.h>
43 #include <linux/idr.h>
44 #include <linux/init.h>
46 #include <linux/jiffies.h>
47 #include <linux/kernel.h>
48 #include <linux/kref.h>
49 #include <linux/miscdevice.h>
51 #include <linux/mutex.h>
52 #include <linux/pci.h>
53 #include <linux/platform_device.h>
54 #include <linux/poll.h>
55 #include <linux/ratelimit.h>
56 #include <linux/sched.h>
57 #include <linux/slab.h>
58 #include <linux/seq_file.h>
59 #include <linux/types.h>
60 #include <linux/vmalloc.h>
61 #include <linux/workqueue.h>
63 #include <asm/pgalloc.h>
64 #include <asm/uaccess.h>
66 #include <uapi/drm/drm.h>
67 #include <uapi/drm/drm_mode.h>
69 #include <drm/drm_agpsupport.h>
70 #include <drm/drm_crtc.h>
71 #include <drm/drm_global.h>
72 #include <drm/drm_hashtab.h>
73 #include <drm/drm_mem_util.h>
74 #include <drm/drm_mm.h>
75 #include <drm/drm_os_linux.h>
76 #include <drm/drm_sarea.h>
77 #include <drm/drm_vma_manager.h>
79 #include <sys/device.h>
81 #include <vm/vm_extern.h>
82 #include <vm/vm_pager.h>
90 struct drm_device_dma;
91 struct drm_dma_handle;
92 struct drm_gem_object;
96 struct reservation_object;
97 struct dma_buf_attachment;
100 * The following categories are defined:
102 * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
103 * This is the category used by the DRM_DEBUG() macro.
105 * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
106 * This is the category used by the DRM_DEBUG_DRIVER() macro.
108 * KMS: used in the modesetting code.
109 * This is the category used by the DRM_DEBUG_KMS() macro.
111 * PRIME: used in the prime code.
112 * This is the category used by the DRM_DEBUG_PRIME() macro.
114 * ATOMIC: used in the atomic code.
115 * This is the category used by the DRM_DEBUG_ATOMIC() macro.
117 * PID: used as modifier to include PID number in messages.
118 * This is the category used by the all debug macros.
120 * FIOCTL: used in failed ioctl debugging.
121 * This is the category used by the DRM_DEBUG_FIOCTL() macro.
123 * IOCTL: used in ioctl debugging.
124 * This is the category used by the DRM_DEBUG_IOCTL() macro.
126 * VBLANK: used in vblank debugging.
127 * This is the category used by the DRM_DEBUG_VBLANK() macro.
129 * VBL: used for verbose debug message in the vblank code
130 * This is the category used by the DRM_DEBUG_VBL() macro.
132 * Enabling verbose debug messages is done through the drm.debug parameter,
133 * each category being enabled by a bit.
135 * drm.debug=0x1 will enable CORE messages
136 * drm.debug=0x2 will enable DRIVER messages
137 * drm.debug=0x3 will enable CORE and DRIVER messages
139 * drm.debug=0x3f will enable all messages
141 * An interesting feature is that it's possible to enable verbose logging at
142 * run-time by using hw.drm.debug sysctl variable:
143 * # sysctl hw.drm.debug=0xfff
145 #define DRM_UT_CORE 0x01
146 #define DRM_UT_DRIVER 0x02
147 #define DRM_UT_KMS 0x04
148 #define DRM_UT_PRIME 0x08
149 #define DRM_UT_ATOMIC 0x10
150 #define DRM_UT_VBL 0x20
151 /* Extra DragonFly debug categories */
153 #define DRM_UT_RES7 0x40 /* reserved for future expansion */
154 #define DRM_UT_RES8 0x80 /* reserved for future expansion */
155 #define DRM_UT_PID 0x100
156 #define DRM_UT_FIOCTL 0x200
157 #define DRM_UT_IOCTL 0x400
158 #define DRM_UT_VBLANK 0x800
161 extern __printflike(2, 3)
162 void drm_ut_debug_printk(const char *function_name,
163 const char *format, ...);
164 extern __printflike(2, 3)
165 void drm_err(const char *func, const char *format, ...);
167 /***********************************************************************/
168 /** \name DRM template customization defaults */
171 /* driver capabilities and requirements mask */
172 #define DRIVER_USE_AGP 0x1
173 #define DRIVER_PCI_DMA 0x8
174 #define DRIVER_SG 0x10
175 #define DRIVER_HAVE_DMA 0x20
176 #define DRIVER_HAVE_IRQ 0x40
177 #define DRIVER_IRQ_SHARED 0x80
178 #define DRIVER_GEM 0x1000
179 #define DRIVER_MODESET 0x2000
180 #define DRIVER_PRIME 0x4000
181 #define DRIVER_RENDER 0x8000
182 #define DRIVER_ATOMIC 0x10000
183 #define DRIVER_KMS_LEGACY_CONTEXT 0x20000
185 /***********************************************************************/
186 /** \name Macros to make printk easier */
192 * \param fmt printf() like format string.
193 * \param arg arguments
195 #define DRM_ERROR(fmt, ...) \
196 drm_err(__func__, fmt, ##__VA_ARGS__)
199 * Rate limited error output. Like DRM_ERROR() but won't flood the log.
201 * \param fmt printf() like format string.
202 * \param arg arguments
204 #define DRM_ERROR_RATELIMITED(fmt, ...) \
206 static struct krate krate_derr = { .freq = 1, .count = -16 }; \
208 krateprintf(&krate_derr, "error: [" DRM_NAME ":%s] *ERROR* " \
209 fmt, __func__, ##__VA_ARGS__); \
212 #define DRM_INFO(fmt, ...) \
213 printk(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
215 #define DRM_INFO_ONCE(fmt, ...) \
216 printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
221 * \param fmt printf() like format string.
222 * \param arg arguments
224 #define DRM_DEBUG(fmt, args...) \
226 if (unlikely(drm_debug & DRM_UT_CORE)) \
227 drm_ut_debug_printk(__func__, fmt, ##args); \
230 #define DRM_DEBUG_DRIVER(fmt, args...) \
232 if (unlikely(drm_debug & DRM_UT_DRIVER)) \
233 drm_ut_debug_printk(__func__, fmt, ##args); \
235 #define DRM_DEBUG_KMS(fmt, args...) \
237 if (unlikely(drm_debug & DRM_UT_KMS)) \
238 drm_ut_debug_printk(__func__, fmt, ##args); \
240 #define DRM_DEBUG_PRIME(fmt, args...) \
242 if (unlikely(drm_debug & DRM_UT_PRIME)) \
243 drm_ut_debug_printk(__func__, fmt, ##args); \
245 #define DRM_DEBUG_ATOMIC(fmt, args...) \
247 if (unlikely(drm_debug & DRM_UT_ATOMIC)) \
248 drm_ut_debug_printk(__func__, fmt, ##args); \
250 #define DRM_DEBUG_VBL(fmt, args...) \
252 if (unlikely(drm_debug & DRM_UT_VBL)) \
253 drm_ut_debug_printk(__func__, fmt, ##args); \
256 #define DRM_DEBUG_FIOCTL(fmt, args...) \
258 if (unlikely(drm_debug & DRM_UT_FIOCTL)) \
259 drm_ut_debug_printk(__func__, fmt, ##args); \
261 #define DRM_DEBUG_IOCTL(fmt, args...) \
263 if (unlikely(drm_debug & DRM_UT_IOCTL)) \
264 drm_ut_debug_printk(__func__, fmt, ##args); \
266 #define DRM_DEBUG_VBLANK(fmt, args...) \
268 if (unlikely(drm_debug & DRM_UT_VBLANK)) \
269 drm_ut_debug_printk(__func__, fmt, ##args); \
275 /***********************************************************************/
276 /** \name Internal types and structures */
279 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
281 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
283 #define DRM_DEV_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
284 #define DRM_DEV_UID UID_ROOT
285 #define DRM_DEV_GID GID_WHEEL
287 #define DRM_CURPROC curthread
288 #define DRM_STRUCTPROC struct thread
289 #define DRM_LOCK(dev) lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
290 #define DRM_UNLOCK(dev) lockmgr(&(dev)->struct_mutex, LK_RELEASE)
292 #if defined(INVARIANTS)
293 #define DRM_LOCK_ASSERT(dev) KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) != 0);
294 #define DRM_UNLOCK_ASSERT(dev) KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) == 0);
296 #define DRM_LOCK_ASSERT(d)
297 #define DRM_UNLOCK_ASSERT(d)
300 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
302 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
304 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
305 vm_memattr_t memattr);
306 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
307 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
309 typedef struct drm_pci_id_list
318 * Ioctl function type.
320 * \param inode device inode.
321 * \param file_priv DRM file private pointer.
322 * \param cmd command.
323 * \param arg argument.
325 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
326 struct drm_file *file_priv);
328 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
331 #define DRM_IOCTL_NR(n) ((n) & 0xff)
334 #define DRM_MASTER 0x2
335 #define DRM_ROOT_ONLY 0x4
336 #define DRM_CONTROL_ALLOW 0x8
337 #define DRM_UNLOCKED 0x10
338 #define DRM_RENDER_ALLOW 0x20
340 struct drm_ioctl_desc {
344 unsigned int cmd_drv;
349 * Creates a driver or general drm_ioctl_desc array entry for the given
350 * ioctl, for use by drm_ioctl().
353 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags) \
354 [DRM_IOCTL_NR(DRM_##ioctl)] = { \
355 .cmd = DRM_##ioctl, \
358 .cmd_drv = DRM_IOCTL_##ioctl, \
362 /* Event queued up for userspace to read */
363 struct drm_pending_event {
364 struct drm_event *event;
365 struct list_head link;
366 struct list_head pending_link;
367 struct drm_file *file_priv;
368 pid_t pid; /* pid of requester, no guarantee it's valid by the time
369 we deliver the event, for tracing only */
370 void (*destroy)(struct drm_pending_event *event);
373 /* initial implementaton using a linked list - todo hashtab */
374 struct drm_prime_file_private {
375 struct list_head head;
379 /** File private data */
381 unsigned authenticated :1;
382 /* Whether we're master for a minor. Protected by master_mutex */
383 unsigned is_master :1;
384 /* true when the client has asked us to expose stereo 3D mode flags */
385 unsigned stereo_allowed :1;
387 * true if client understands CRTC primary planes and cursor planes
390 unsigned universal_planes:1;
391 /* true if client understands atomic properties */
394 * This client is allowed to gain master privileges for @master.
395 * Protected by struct drm_device::master_mutex.
397 unsigned allowed_master:1;
402 struct list_head lhead;
403 struct drm_minor *minor;
404 unsigned long lock_count;
406 /** Mapping of mm object handles to object pointers. */
407 struct idr object_idr;
408 /** Lock for synchronization of access to object_idr. */
409 struct lock table_lock;
414 struct drm_master *masterp;
416 * fbs - List of framebuffers associated with this file.
418 * Protected by fbs_lock. Note that the fbs list holds a reference on
419 * the fb object to prevent it from untimely disappearing.
421 struct list_head fbs;
422 struct lock fbs_lock;
424 /** User-created blob properties; this retains a reference on the
426 struct list_head blobs;
428 wait_queue_head_t event_wait;
429 struct list_head pending_event_list;
430 struct list_head event_list;
433 struct lock event_read_lock;
435 struct drm_prime_file_private prime;
438 struct drm_device *dev;
440 unsigned long ioctl_count;
448 struct drm_lock_data {
449 struct drm_hw_lock *hw_lock; /**< Hardware lock */
450 /** Private of lock holder's file (NULL=kernel) */
451 struct drm_file *file_priv;
452 wait_queue_head_t lock_queue; /**< Queue of blocked processes */
453 unsigned long lock_time; /**< Time of last lock in jiffies */
454 struct spinlock spinlock;
455 uint32_t kernel_waiters;
456 uint32_t user_waiters;
461 * GEM specific mm private for tracking GEM objects
464 struct drm_vma_offset_manager vma_manager;
465 struct drm_mm offset_manager; /**< Offset mgmt for buffer objects */
466 struct drm_open_hash offset_hash; /**< User token hash table for maps */
467 struct unrhdr *idxunr;
471 * struct drm_master - drm master structure
473 * @refcount: Refcount for this master object.
474 * @minor: Link back to minor char device we are master for. Immutable.
475 * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
476 * @unique_len: Length of unique field. Protected by drm_global_mutex.
477 * @magic_map: Map of used authentication tokens. Protected by struct_mutex.
478 * @lock: DRI lock information.
479 * @driver_priv: Pointer to driver-private information.
482 struct kref refcount;
483 struct drm_minor *minor;
486 struct idr magic_map;
487 struct drm_lock_data lock;
491 /* Size of ringbuffer for vblank timestamps. Just double-buffer
492 * in initial implementation.
494 #define DRM_VBLANKTIME_RBSIZE 2
496 /* Flags and return codes for get_vblank_timestamp() driver function. */
497 #define DRM_CALLED_FROM_VBLIRQ 1
498 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
499 #define DRM_VBLANKTIME_IN_VBLANK (1 << 1)
501 /* get_scanout_position() return flags */
502 #define DRM_SCANOUTPOS_VALID (1 << 0)
503 #define DRM_SCANOUTPOS_IN_VBLANK (1 << 1)
504 #define DRM_SCANOUTPOS_ACCURATE (1 << 2)
507 * DRM driver structure. This structure represent the common code for
508 * a family of cards. There will one drm_device for each card present
512 int (*load) (struct drm_device *, unsigned long flags);
513 int (*firstopen) (struct drm_device *);
514 int (*open) (struct drm_device *, struct drm_file *);
515 void (*preclose) (struct drm_device *, struct drm_file *file_priv);
516 void (*postclose) (struct drm_device *, struct drm_file *);
517 void (*lastclose) (struct drm_device *);
518 int (*unload) (struct drm_device *);
519 int (*suspend) (struct drm_device *, pm_message_t state);
520 int (*resume) (struct drm_device *);
521 int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
522 int (*dma_quiescent) (struct drm_device *);
523 int (*context_dtor) (struct drm_device *dev, int context);
524 int (*set_busid)(struct drm_device *dev, struct drm_master *master);
527 * get_vblank_counter - get raw hardware vblank counter
529 * @pipe: counter to fetch
531 * Driver callback for fetching a raw hardware vblank counter for @crtc.
532 * If a device doesn't have a hardware counter, the driver can simply
533 * return the value of drm_vblank_count. The DRM core will account for
534 * missed vblank events while interrupts where disabled based on system
537 * Wraparound handling and loss of events due to modesetting is dealt
538 * with in the DRM core code.
541 * Raw vblank counter value.
543 u32 (*get_vblank_counter) (struct drm_device *dev, unsigned int pipe);
546 * enable_vblank - enable vblank interrupt events
548 * @pipe: which irq to enable
550 * Enable vblank interrupts for @crtc. If the device doesn't have
551 * a hardware vblank counter, this routine should be a no-op, since
552 * interrupts will have to stay on to keep the count accurate.
555 * Zero on success, appropriate errno if the given @crtc's vblank
556 * interrupt cannot be enabled.
558 int (*enable_vblank) (struct drm_device *dev, unsigned int pipe);
561 * disable_vblank - disable vblank interrupt events
563 * @pipe: which irq to enable
565 * Disable vblank interrupts for @crtc. If the device doesn't have
566 * a hardware vblank counter, this routine should be a no-op, since
567 * interrupts will have to stay on to keep the count accurate.
569 void (*disable_vblank) (struct drm_device *dev, unsigned int pipe);
572 * Called by \c drm_device_is_agp. Typically used to determine if a
573 * card is really attached to AGP or not.
575 * \param dev DRM device handle
578 * One of three values is returned depending on whether or not the
579 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
580 * (return of 1), or may or may not be AGP (return of 2).
582 int (*device_is_agp) (struct drm_device *dev);
585 * Called by vblank timestamping code.
587 * Return the current display scanout position from a crtc, and an
588 * optional accurate ktime_get timestamp of when position was measured.
590 * \param dev DRM device.
591 * \param pipe Id of the crtc to query.
592 * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
593 * \param *vpos Target location for current vertical scanout position.
594 * \param *hpos Target location for current horizontal scanout position.
595 * \param *stime Target location for timestamp taken immediately before
596 * scanout position query. Can be NULL to skip timestamp.
597 * \param *etime Target location for timestamp taken immediately after
598 * scanout position query. Can be NULL to skip timestamp.
599 * \param mode Current display timings.
601 * Returns vpos as a positive number while in active scanout area.
602 * Returns vpos as a negative number inside vblank, counting the number
603 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
604 * until start of active scanout / end of vblank."
606 * \return Flags, or'ed together as follows:
608 * DRM_SCANOUTPOS_VALID = Query successful.
609 * DRM_SCANOUTPOS_INVBL = Inside vblank.
610 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
611 * this flag means that returned position may be offset by a constant
612 * but unknown small number of scanlines wrt. real scanout position.
615 int (*get_scanout_position) (struct drm_device *dev, unsigned int pipe,
616 unsigned int flags, int *vpos, int *hpos,
617 ktime_t *stime, ktime_t *etime,
618 const struct drm_display_mode *mode);
621 * Called by \c drm_get_last_vbltimestamp. Should return a precise
622 * timestamp when the most recent VBLANK interval ended or will end.
624 * Specifically, the timestamp in @vblank_time should correspond as
625 * closely as possible to the time when the first video scanline of
626 * the video frame after the end of VBLANK will start scanning out,
627 * the time immediately after end of the VBLANK interval. If the
628 * @crtc is currently inside VBLANK, this will be a time in the future.
629 * If the @crtc is currently scanning out a frame, this will be the
630 * past start time of the current scanout. This is meant to adhere
631 * to the OpenML OML_sync_control extension specification.
633 * \param dev dev DRM device handle.
634 * \param pipe crtc for which timestamp should be returned.
635 * \param *max_error Maximum allowable timestamp error in nanoseconds.
636 * Implementation should strive to provide timestamp
637 * with an error of at most *max_error nanoseconds.
638 * Returns true upper bound on error for timestamp.
639 * \param *vblank_time Target location for returned vblank timestamp.
640 * \param flags 0 = Defaults, no special treatment needed.
641 * \param DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
642 * irq handler. Some drivers need to apply some workarounds
643 * for gpu-specific vblank irq quirks if flag is set.
646 * Zero if timestamping isn't supported in current display mode or a
647 * negative number on failure. A positive status code on success,
648 * which describes how the vblank_time timestamp was computed.
650 int (*get_vblank_timestamp) (struct drm_device *dev, unsigned int pipe,
652 struct timeval *vblank_time,
655 /* these have to be filled in */
657 irqreturn_t(*irq_handler) (int irq, void *arg);
658 void (*irq_preinstall) (struct drm_device *dev);
659 int (*irq_postinstall) (struct drm_device *dev);
660 void (*irq_uninstall) (struct drm_device *dev);
662 /* Master routines */
663 int (*master_create)(struct drm_device *dev, struct drm_master *master);
664 void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
666 * master_set is called whenever the minor master is set.
667 * master_drop is called whenever the minor master is dropped.
670 int (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
672 void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv,
675 int (*debugfs_init)(struct drm_minor *minor);
676 void (*debugfs_cleanup)(struct drm_minor *minor);
679 * @gem_free_object: deconstructor for drm_gem_objects
681 * This is deprecated and should not be used by new drivers. Use
682 * @gem_free_object_unlocked instead.
684 void (*gem_free_object) (struct drm_gem_object *obj);
687 * @gem_free_object_unlocked: deconstructor for drm_gem_objects
689 * This is for drivers which are not encumbered with dev->struct_mutex
690 * legacy locking schemes. Use this hook instead of @gem_free_object.
692 void (*gem_free_object_unlocked) (struct drm_gem_object *obj);
694 int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
695 void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
698 * Hook for allocating the GEM object struct, for use by core
701 struct drm_gem_object *(*gem_create_object)(struct drm_device *dev,
705 /* export handle -> fd (see drm_gem_prime_handle_to_fd() helper) */
706 int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
707 uint32_t handle, uint32_t flags, int *prime_fd);
708 /* import fd -> handle (see drm_gem_prime_fd_to_handle() helper) */
709 int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
710 int prime_fd, uint32_t *handle);
711 /* export GEM -> dmabuf */
712 struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
713 struct drm_gem_object *obj, int flags);
714 /* import dmabuf -> GEM */
715 struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
716 struct dma_buf *dma_buf);
717 /* low-level interface used by drm_gem_prime_{import,export} */
718 int (*gem_prime_pin)(struct drm_gem_object *obj);
719 void (*gem_prime_unpin)(struct drm_gem_object *obj);
720 struct reservation_object * (*gem_prime_res_obj)(
721 struct drm_gem_object *obj);
722 struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj);
723 struct drm_gem_object *(*gem_prime_import_sg_table)(
724 struct drm_device *dev,
725 struct dma_buf_attachment *attach,
726 struct sg_table *sgt);
727 void *(*gem_prime_vmap)(struct drm_gem_object *obj);
728 void (*gem_prime_vunmap)(struct drm_gem_object *obj, void *vaddr);
729 int (*gem_prime_mmap)(struct drm_gem_object *obj,
730 struct vm_area_struct *vma);
732 /* vga arb irq handler */
733 void (*vgaarb_irq)(struct drm_device *dev, bool state);
735 /* dumb alloc support */
736 int (*dumb_create)(struct drm_file *file_priv,
737 struct drm_device *dev,
738 struct drm_mode_create_dumb *args);
739 int (*dumb_map_offset)(struct drm_file *file_priv,
740 struct drm_device *dev, uint32_t handle,
742 int (*dumb_destroy)(struct drm_file *file_priv,
743 struct drm_device *dev,
746 /* Driver private ops for this object */
747 struct cdev_pager_ops *gem_vm_ops;
758 const struct drm_ioctl_desc *ioctls;
760 const struct file_operations *fops;
762 /* List of devices hanging off this driver with stealth attach. */
763 struct list_head legacy_dev_list;
765 int (*sysctl_init) (struct drm_device *dev,
766 struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
767 void (*sysctl_cleanup) (struct drm_device *dev);
768 #endif /* __DragonFly__ */
771 enum drm_minor_type {
779 * Info file list entry. This structure represents a debugfs or proc file to
780 * be created by the drm core
782 struct drm_info_list {
783 const char *name; /** file name */
784 int (*show)(struct seq_file*, void*); /** show callback */
785 u32 driver_features; /**< Required driver features for this entry */
790 * debugfs node structure. This structure represents a debugfs file.
792 struct drm_info_node {
793 struct list_head list;
794 struct drm_minor *minor;
795 const struct drm_info_list *info_ent;
800 * DRM minor structure. This structure represents a drm minor number.
803 int index; /**< Minor device number */
804 int type; /**< Control or render */
805 struct device *kdev; /**< Linux device */
806 struct drm_device *dev;
808 struct dentry *debugfs_root;
810 struct list_head debugfs_list;
811 struct lock debugfs_lock; /* Protects debugfs_list. */
813 /* currently active master for this node. Protected by master_mutex */
814 struct drm_master *master;
818 struct drm_pending_vblank_event {
819 struct drm_pending_event base;
821 struct drm_event_vblank event;
824 struct drm_vblank_crtc {
825 struct drm_device *dev; /* pointer to the drm_device */
826 wait_queue_head_t queue; /**< VBLANK wait queue */
827 struct timer_list disable_timer; /* delayed disable timer */
829 /* vblank counter, protected by dev->vblank_time_lock for writes */
831 /* vblank timestamps, protected by dev->vblank_time_lock for writes */
832 struct timeval time[DRM_VBLANKTIME_RBSIZE];
834 atomic_t refcount; /* number of users of vblank interruptsper crtc */
835 u32 last; /* protected by dev->vbl_lock, used */
836 /* for wraparound handling */
837 u32 last_wait; /* Last vblank seqno waited per CRTC */
838 unsigned int inmodeset; /* Display driver is setting mode */
839 unsigned int pipe; /* crtc index */
840 int framedur_ns; /* frame/field duration in ns */
841 int linedur_ns; /* line duration in ns */
842 bool enabled; /* so we don't call enable more than
846 struct drm_sysctl_info {
847 struct sysctl_ctx_list ctx;
851 /* Length for the array of resource pointers for drm_get_resource_*. */
852 #define DRM_MAX_PCI_RESOURCE 6
855 * DRM device structure. This structure represent a complete card that
856 * may contain multiple heads.
859 struct list_head legacy_dev_list;/**< list of devices per driver for stealth attach cleanup */
860 int if_version; /**< Highest interface version set */
862 int unique_len; /* Length of unique field */
863 struct cdev *devnode; /* Device number for mknod */
865 int flags; /* Flags to open(2) */
867 struct lwkt_serialize irq_lock; /* protects irq condition checks */
868 struct lock dev_lock; /* protects everything else */
870 /** \name Lifetime Management */
872 struct kref ref; /**< Object ref-count */
873 struct device *dev; /**< Device structure of bus-device */
874 struct drm_driver *driver; /**< DRM driver managing the device */
875 void *dev_private; /**< DRM driver private data */
876 struct drm_minor *control; /**< Control node */
877 struct drm_minor *primary; /**< Primary node */
878 struct drm_minor *render; /**< Render node */
879 atomic_t unplugged; /**< Flag whether dev is dead */
880 struct inode *anon_inode; /**< inode for private address-space */
881 char *unique; /**< unique name of the device */
886 struct lock struct_mutex; /**< For others */
887 struct lock master_mutex; /**< For drm_minor::master and drm_file::is_master */
890 /** \name Usage Counters */
892 int open_count; /**< Outstanding files open, protected by drm_global_mutex. */
893 struct spinlock buf_lock; /**< For drm_device::buf_use and a few other things. */
894 int buf_use; /**< Buffers in use -- cannot alloc */
895 atomic_t buf_alloc; /**< Buffer allocation in progress */
898 /** \name Performance counters */
900 unsigned long counters;
901 enum drm_stat_type types[15];
905 struct idr magic_map;
907 struct lock filelist_mutex;
908 struct list_head filelist;
910 /** \name Memory management */
912 struct list_head maplist; /**< Linked list of regions */
913 struct drm_open_hash map_hash; /**< User token hash table for maps */
915 /** \name Context handle management */
917 struct list_head ctxlist; /**< Linked list of context handles */
918 struct lock ctxlist_mutex; /**< For ctxlist */
922 struct list_head vmalist; /**< List of vmas (for debugging) */
926 struct drm_lock_data lock; /* Information on hardware lock */
928 /** \name DMA support */
930 struct drm_device_dma *dma; /**< Optional pointer for DMA support */
933 /** \name Context support */
936 __volatile__ long context_flag; /**< Context swapping flag */
937 int last_context; /**< Last current context */
940 /** \name VBLANK IRQ support */
946 * If true, vblank interrupt will be disabled immediately when the
947 * refcount drops to zero, as opposed to via the vblank disable
949 * This can be set to true it the hardware has a working vblank
950 * counter and the driver uses drm_vblank_on() and drm_vblank_off()
953 bool vblank_disable_immediate;
955 /* array of size num_crtcs */
956 struct drm_vblank_crtc *vblank;
958 struct lock vblank_time_lock; /**< Protects vblank count and time updates during vblank enable/disable */
959 struct lock vbl_lock;
961 u32 max_vblank_count; /**< size of vblank counter register */
966 struct list_head vblank_event_list;
967 struct lock event_lock;
971 struct sigio *buf_sigio; /* Processes waiting for SIGIO */
972 struct drm_agp_head *agp; /**< AGP data */
974 struct pci_dev *pdev; /**< PCI device structure */
976 struct pci_controller *hose;
979 struct platform_device *platformdev; /**< Platform device struture */
980 struct virtio_device *virtdev;
982 struct drm_sg_mem *sg; /**< Scatter gather memory */
983 unsigned int num_crtcs; /**< Number of CRTCs on this device */
989 struct drm_hw_lock *lock;
992 struct drm_local_map *agp_buffer_map;
993 unsigned int agp_buffer_token;
995 struct drm_mode_config mode_config; /**< Current mode config */
997 /** \name GEM information */
999 struct lock object_name_lock;
1000 struct idr object_name_idr;
1001 struct drm_vma_offset_manager *vma_offset_manager;
1003 int switch_power_state;
1004 #ifdef __DragonFly__
1005 /* Storage of resource pointers for drm_get_resource_* */
1006 struct resource *pcir[DRM_MAX_PCI_RESOURCE];
1007 int pcirid[DRM_MAX_PCI_RESOURCE];
1013 drm_pci_id_list_t *id_entry; /* PCI ID, name, and chipset private */
1014 char busid_str[128];
1017 struct drm_sysctl_info *sysctl;
1021 #define DRM_SWITCH_POWER_ON 0
1022 #define DRM_SWITCH_POWER_OFF 1
1023 #define DRM_SWITCH_POWER_CHANGING 2
1024 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
1026 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1029 return ((dev->driver->driver_features & feature) ? 1 : 0);
1032 static inline void drm_device_set_unplugged(struct drm_device *dev)
1035 atomic_set(&dev->unplugged, 1);
1038 static inline int drm_device_is_unplugged(struct drm_device *dev)
1040 int ret = atomic_read(&dev->unplugged);
1045 static inline bool drm_is_render_client(const struct drm_file *file_priv)
1047 return file_priv->minor->type == DRM_MINOR_RENDER;
1050 static inline bool drm_is_control_client(const struct drm_file *file_priv)
1052 return file_priv->minor->type == DRM_MINOR_CONTROL;
1055 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
1057 return file_priv->minor->type == DRM_MINOR_LEGACY;
1060 /******************************************************************/
1061 /** \name Internal function definitions */
1064 /* Driver support (drm_drv.h) */
1065 extern int drm_ioctl_permit(u32 flags, struct drm_file *file_priv);
1066 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1067 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1068 int drm_create_cdevs(device_t kdev);
1069 d_ioctl_t drm_ioctl;
1070 extern long drm_compat_ioctl(struct file *filp,
1071 unsigned int cmd, unsigned long arg);
1072 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
1074 /* File Operations (drm_fops.c) */
1076 d_close_t drm_close;
1078 d_kqfilter_t drm_kqfilter;
1079 int drm_release(device_t kdev);
1082 d_mmap_single_t drm_mmap_single;
1084 void drm_cdevpriv_dtor(void *cd);
1086 int drm_add_busid_modesetting(struct drm_device *dev,
1087 struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1089 /* File operations helpers (drm_fops.c) */
1090 extern int drm_open_helper(struct cdev *kdev, int flags, int fmt,
1092 struct drm_device *dev,
1095 int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv);
1096 int drm_event_reserve_init_locked(struct drm_device *dev,
1097 struct drm_file *file_priv,
1098 struct drm_pending_event *p,
1099 struct drm_event *e);
1100 int drm_event_reserve_init(struct drm_device *dev,
1101 struct drm_file *file_priv,
1102 struct drm_pending_event *p,
1103 struct drm_event *e);
1104 void drm_event_cancel_free(struct drm_device *dev,
1105 struct drm_pending_event *p);
1106 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e);
1107 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
1109 /* Misc. IOCTL support (drm_ioctl.c) */
1110 int drm_noop(struct drm_device *dev, void *data,
1111 struct drm_file *file_priv);
1112 int drm_invalid_op(struct drm_device *dev, void *data,
1113 struct drm_file *file_priv);
1115 /* Cache management (drm_cache.c) */
1116 void drm_clflush_pages(struct page *pages[], unsigned long num_pages);
1117 void drm_clflush_sg(struct sg_table *st);
1118 void drm_clflush_virt_range(void *addr, unsigned long length);
1121 * These are exported to drivers so that they can implement fencing using
1122 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1125 unsigned long drm_get_resource_start(struct drm_device *dev,
1126 unsigned int resource);
1127 unsigned long drm_get_resource_len(struct drm_device *dev,
1128 unsigned int resource);
1130 /* IRQ support (drm_irq.h) */
1131 extern int drm_irq_install(struct drm_device *dev, int irq);
1132 extern int drm_irq_uninstall(struct drm_device *dev);
1134 extern int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
1135 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1136 struct drm_file *filp);
1137 extern u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe);
1138 extern u32 drm_crtc_vblank_count(struct drm_crtc *crtc);
1139 extern u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
1140 struct timeval *vblanktime);
1141 extern u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
1142 struct timeval *vblanktime);
1143 extern void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
1144 struct drm_pending_vblank_event *e);
1145 extern void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1146 struct drm_pending_vblank_event *e);
1147 extern void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
1148 struct drm_pending_vblank_event *e);
1149 extern void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
1150 struct drm_pending_vblank_event *e);
1151 extern bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
1152 extern bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
1153 extern int drm_vblank_get(struct drm_device *dev, unsigned int pipe);
1154 extern void drm_vblank_put(struct drm_device *dev, unsigned int pipe);
1155 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1156 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1157 extern void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
1158 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1159 extern void drm_vblank_off(struct drm_device *dev, unsigned int pipe);
1160 extern void drm_vblank_on(struct drm_device *dev, unsigned int pipe);
1161 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1162 extern void drm_crtc_vblank_reset(struct drm_crtc *crtc);
1163 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1164 extern void drm_vblank_cleanup(struct drm_device *dev);
1165 extern u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe);
1167 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1168 unsigned int pipe, int *max_error,
1169 struct timeval *vblank_time,
1171 const struct drm_display_mode *mode);
1172 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1173 const struct drm_display_mode *mode);
1176 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1177 * @crtc: which CRTC's vblank waitqueue to retrieve
1179 * This function returns a pointer to the vblank waitqueue for the CRTC.
1180 * Drivers can use this to implement vblank waits using wait_event() & co.
1182 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1184 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1187 /* Modesetting support */
1188 extern void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe);
1189 extern void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe);
1191 /* Stub support (drm_stub.h) */
1192 extern struct drm_master *drm_master_get(struct drm_master *master);
1193 extern void drm_master_put(struct drm_master **master);
1195 extern void drm_put_dev(struct drm_device *dev);
1196 extern void drm_unplug_dev(struct drm_device *dev);
1197 extern unsigned int drm_debug;
1198 extern bool drm_atomic;
1200 /* Debugfs support */
1201 #if defined(CONFIG_DEBUG_FS)
1202 extern int drm_debugfs_create_files(const struct drm_info_list *files,
1203 int count, struct dentry *root,
1204 struct drm_minor *minor);
1205 extern int drm_debugfs_remove_files(const struct drm_info_list *files,
1206 int count, struct drm_minor *minor);
1208 static inline int drm_debugfs_create_files(const struct drm_info_list *files,
1209 int count, struct dentry *root,
1210 struct drm_minor *minor)
1215 static inline int drm_debugfs_remove_files(const struct drm_info_list *files,
1216 int count, struct drm_minor *minor)
1222 /* sysctl support (drm_sysctl.h) */
1223 extern int drm_sysctl_init(struct drm_device *dev);
1224 extern int drm_sysctl_cleanup(struct drm_device *dev);
1226 /* XXX: These are here only because of drm_sysctl.c */
1227 extern int drm_vblank_offdelay;
1228 extern unsigned int drm_timestamp_precision;
1230 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1231 vm_size_t size, struct vm_object **obj_res, int nprot);
1233 extern struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
1234 struct drm_gem_object *obj, int flags);
1235 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1236 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1238 extern struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
1239 struct dma_buf *dma_buf);
1240 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1241 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1242 extern void drm_gem_dmabuf_release(struct dma_buf *dma_buf);
1244 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
1245 dma_addr_t *addrs, int max_pages);
1246 extern struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages);
1247 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1250 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
1252 extern void drm_pci_free(struct drm_device *dev, struct drm_dma_handle * dmah);
1254 /* sysfs support (drm_sysfs.c) */
1255 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1257 struct ttm_bo_device;
1258 int ttm_bo_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1259 vm_size_t size, struct vm_object **obj_res, int nprot);
1260 struct ttm_buffer_object;
1261 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1263 /* simplified version of kvasnprintf() for drm needs. */
1264 char *drm_vasprintf(int flags, const char *format, __va_list ap) __printflike(2, 0);
1265 char *drm_asprintf(int flags, const char *format, ...) __printflike(2, 3);
1267 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1268 void drm_init_pdev(device_t dev, struct pci_dev **pdev);
1269 void drm_fini_pdev(struct pci_dev **pdev);
1270 void drm_print_pdev(struct pci_dev *pdev);
1272 /* Inline drm_free() helper for area kfree() */
1273 static __inline__ void
1274 drm_free(void *pt, struct malloc_type *area)
1276 /* kfree is special!!! */
1281 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1282 struct device *parent);
1283 void drm_dev_ref(struct drm_device *dev);
1284 void drm_dev_unref(struct drm_device *dev);
1285 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1286 void drm_dev_unregister(struct drm_device *dev);
1287 int drm_dev_set_unique(struct drm_device *dev, const char *name);
1289 struct drm_minor *drm_minor_acquire(unsigned int minor_id);
1290 void drm_minor_release(struct drm_minor *minor);
1295 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1297 if (dev->driver->device_is_agp != NULL) {
1298 int err = (*dev->driver->device_is_agp) (dev);
1305 return (pci_find_extcap(dev->pdev->dev.bsddev, PCIY_AGP, NULL) == 0);
1307 void drm_pci_agp_destroy(struct drm_device *dev);
1309 extern int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver);
1310 extern void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver);
1312 extern int drm_get_pci_dev(struct pci_dev *pdev,
1313 const struct pci_device_id *ent,
1314 struct drm_driver *driver);
1315 extern int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master);
1317 static inline int drm_get_pci_dev(struct pci_dev *pdev,
1318 const struct pci_device_id *ent,
1319 struct drm_driver *driver)
1324 static inline int drm_pci_set_busid(struct drm_device *dev,
1325 struct drm_master *master)
1331 #define DRM_PCIE_SPEED_25 1
1332 #define DRM_PCIE_SPEED_50 2
1333 #define DRM_PCIE_SPEED_80 4
1335 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1336 extern int drm_pcie_get_max_link_width(struct drm_device *dev, u32 *mlw);
1338 /* platform section */
1339 extern int drm_platform_init(struct drm_driver *driver, struct platform_device *platform_device);
1340 extern int drm_platform_set_busid(struct drm_device *d, struct drm_master *m);
1343 #define drm_can_sleep() (HZ & 1)
1345 #endif /* __KERNEL__ */
1347 /* helper for handling conditionals in various for_each macros */
1348 #define for_each_if(condition) if (!(condition)) {} else