drmP.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Internal Header for the Direct Rendering Manager
  3. *
  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.
  7. * All rights reserved.
  8. *
  9. * Author: Rickard E. (Rik) Faith <faith@valinux.com>
  10. * Author: Gareth Hughes <gareth@valinux.com>
  11. *
  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:
  18. *
  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
  21. * Software.
  22. *
  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.
  30. */
  31. #ifndef _DRM_P_H_
  32. #define _DRM_P_H_
  33. #include <linux/agp_backend.h>
  34. #include <linux/cdev.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/file.h>
  37. #include <linux/fs.h>
  38. #include <linux/highmem.h>
  39. #include <linux/idr.h>
  40. #include <linux/init.h>
  41. #include <linux/io.h>
  42. #include <linux/jiffies.h>
  43. #include <linux/kernel.h>
  44. #include <linux/kref.h>
  45. #include <linux/miscdevice.h>
  46. #include <linux/mm.h>
  47. #include <linux/mutex.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/poll.h>
  50. #include <linux/ratelimit.h>
  51. #include <linux/sched.h>
  52. #include <linux/slab.h>
  53. #include <linux/types.h>
  54. #include <linux/vmalloc.h>
  55. #include <linux/workqueue.h>
  56. #include <linux/dma-fence.h>
  57. #include <linux/module.h>
  58. #include <asm/mman.h>
  59. #include <asm/pgalloc.h>
  60. #include <linux/uaccess.h>
  61. #include <uapi/drm/drm.h>
  62. #include <uapi/drm/drm_mode.h>
  63. #include <drm/drm_agpsupport.h>
  64. #include <drm/drm_crtc.h>
  65. #include <drm/drm_fourcc.h>
  66. #include <drm/drm_global.h>
  67. #include <drm/drm_hashtab.h>
  68. #include <drm/drm_mem_util.h>
  69. #include <drm/drm_mm.h>
  70. #include <drm/drm_os_linux.h>
  71. #include <drm/drm_sarea.h>
  72. #include <drm/drm_drv.h>
  73. #include <drm/drm_prime.h>
  74. #include <drm/drm_pci.h>
  75. #include <drm/drm_file.h>
  76. #include <drm/drm_debugfs.h>
  77. #include <drm/drm_ioctl.h>
  78. #include <drm/drm_sysfs.h>
  79. struct module;
  80. struct drm_device;
  81. struct drm_agp_head;
  82. struct drm_local_map;
  83. struct drm_device_dma;
  84. struct drm_gem_object;
  85. struct drm_master;
  86. struct drm_vblank_crtc;
  87. struct drm_vma_offset_manager;
  88. struct device_node;
  89. struct videomode;
  90. struct reservation_object;
  91. struct dma_buf_attachment;
  92. struct pci_dev;
  93. struct pci_controller;
  94. /*
  95. * The following categories are defined:
  96. *
  97. * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
  98. * This is the category used by the DRM_DEBUG() macro.
  99. *
  100. * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
  101. * This is the category used by the DRM_DEBUG_DRIVER() macro.
  102. *
  103. * KMS: used in the modesetting code.
  104. * This is the category used by the DRM_DEBUG_KMS() macro.
  105. *
  106. * PRIME: used in the prime code.
  107. * This is the category used by the DRM_DEBUG_PRIME() macro.
  108. *
  109. * ATOMIC: used in the atomic code.
  110. * This is the category used by the DRM_DEBUG_ATOMIC() macro.
  111. *
  112. * VBL: used for verbose debug message in the vblank code
  113. * This is the category used by the DRM_DEBUG_VBL() macro.
  114. *
  115. * Enabling verbose debug messages is done through the drm.debug parameter,
  116. * each category being enabled by a bit.
  117. *
  118. * drm.debug=0x1 will enable CORE messages
  119. * drm.debug=0x2 will enable DRIVER messages
  120. * drm.debug=0x3 will enable CORE and DRIVER messages
  121. * ...
  122. * drm.debug=0x3f will enable all messages
  123. *
  124. * An interesting feature is that it's possible to enable verbose logging at
  125. * run-time by echoing the debug value in its sysfs node:
  126. * # echo 0xf > /sys/module/drm/parameters/debug
  127. */
  128. #define DRM_UT_NONE 0x00
  129. #define DRM_UT_CORE 0x01
  130. #define DRM_UT_DRIVER 0x02
  131. #define DRM_UT_KMS 0x04
  132. #define DRM_UT_PRIME 0x08
  133. #define DRM_UT_ATOMIC 0x10
  134. #define DRM_UT_VBL 0x20
  135. #define DRM_UT_STATE 0x40
  136. /***********************************************************************/
  137. /** \name DRM template customization defaults */
  138. /*@{*/
  139. /***********************************************************************/
  140. /** \name Macros to make printk easier */
  141. /*@{*/
  142. #define _DRM_PRINTK(once, level, fmt, ...) \
  143. do { \
  144. printk##once(KERN_##level "[" DRM_NAME "] " fmt, \
  145. ##__VA_ARGS__); \
  146. } while (0)
  147. #define DRM_INFO(fmt, ...) \
  148. _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
  149. #define DRM_NOTE(fmt, ...) \
  150. _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
  151. #define DRM_WARN(fmt, ...) \
  152. _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
  153. #define DRM_INFO_ONCE(fmt, ...) \
  154. _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
  155. #define DRM_NOTE_ONCE(fmt, ...) \
  156. _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
  157. #define DRM_WARN_ONCE(fmt, ...) \
  158. _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
  159. /**
  160. * Error output.
  161. *
  162. * \param fmt printf() like format string.
  163. * \param arg arguments
  164. */
  165. #define DRM_DEV_ERROR(dev, fmt, ...) \
  166. drm_dev_printk(dev, KERN_ERR, DRM_UT_NONE, __func__, " *ERROR*",\
  167. fmt, ##__VA_ARGS__)
  168. #define DRM_ERROR(fmt, ...) \
  169. drm_printk(KERN_ERR, DRM_UT_NONE, fmt, ##__VA_ARGS__)
  170. /**
  171. * Rate limited error output. Like DRM_ERROR() but won't flood the log.
  172. *
  173. * \param fmt printf() like format string.
  174. * \param arg arguments
  175. */
  176. #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...) \
  177. ({ \
  178. static DEFINE_RATELIMIT_STATE(_rs, \
  179. DEFAULT_RATELIMIT_INTERVAL, \
  180. DEFAULT_RATELIMIT_BURST); \
  181. \
  182. if (__ratelimit(&_rs)) \
  183. DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__); \
  184. })
  185. #define DRM_ERROR_RATELIMITED(fmt, ...) \
  186. DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
  187. #define DRM_DEV_INFO(dev, fmt, ...) \
  188. drm_dev_printk(dev, KERN_INFO, DRM_UT_NONE, __func__, "", fmt, \
  189. ##__VA_ARGS__)
  190. #define DRM_DEV_INFO_ONCE(dev, fmt, ...) \
  191. ({ \
  192. static bool __print_once __read_mostly; \
  193. if (!__print_once) { \
  194. __print_once = true; \
  195. DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__); \
  196. } \
  197. })
  198. /**
  199. * Debug output.
  200. *
  201. * \param fmt printf() like format string.
  202. * \param arg arguments
  203. */
  204. #define DRM_DEV_DEBUG(dev, fmt, args...) \
  205. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_CORE, __func__, "", fmt, \
  206. ##args)
  207. #define DRM_DEBUG(fmt, ...) \
  208. drm_printk(KERN_DEBUG, DRM_UT_CORE, fmt, ##__VA_ARGS__)
  209. #define DRM_DEV_DEBUG_DRIVER(dev, fmt, args...) \
  210. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_DRIVER, __func__, "", \
  211. fmt, ##args)
  212. #define DRM_DEBUG_DRIVER(fmt, ...) \
  213. drm_printk(KERN_DEBUG, DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
  214. #define DRM_DEV_DEBUG_KMS(dev, fmt, args...) \
  215. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_KMS, __func__, "", fmt, \
  216. ##args)
  217. #define DRM_DEBUG_KMS(fmt, ...) \
  218. drm_printk(KERN_DEBUG, DRM_UT_KMS, fmt, ##__VA_ARGS__)
  219. #define DRM_DEV_DEBUG_PRIME(dev, fmt, args...) \
  220. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_PRIME, __func__, "", \
  221. fmt, ##args)
  222. #define DRM_DEBUG_PRIME(fmt, ...) \
  223. drm_printk(KERN_DEBUG, DRM_UT_PRIME, fmt, ##__VA_ARGS__)
  224. #define DRM_DEV_DEBUG_ATOMIC(dev, fmt, args...) \
  225. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ATOMIC, __func__, "", \
  226. fmt, ##args)
  227. #define DRM_DEBUG_ATOMIC(fmt, ...) \
  228. drm_printk(KERN_DEBUG, DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
  229. #define DRM_DEV_DEBUG_VBL(dev, fmt, args...) \
  230. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_VBL, __func__, "", fmt, \
  231. ##args)
  232. #define DRM_DEBUG_VBL(fmt, ...) \
  233. drm_printk(KERN_DEBUG, DRM_UT_VBL, fmt, ##__VA_ARGS__)
  234. #define _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, level, fmt, args...) \
  235. ({ \
  236. static DEFINE_RATELIMIT_STATE(_rs, \
  237. DEFAULT_RATELIMIT_INTERVAL, \
  238. DEFAULT_RATELIMIT_BURST); \
  239. if (__ratelimit(&_rs)) \
  240. drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ ## level, \
  241. __func__, "", fmt, ##args); \
  242. })
  243. /**
  244. * Rate limited debug output. Like DRM_DEBUG() but won't flood the log.
  245. *
  246. * \param fmt printf() like format string.
  247. * \param arg arguments
  248. */
  249. #define DRM_DEV_DEBUG_RATELIMITED(dev, fmt, args...) \
  250. DEV__DRM_DEFINE_DEBUG_RATELIMITED(dev, CORE, fmt, ##args)
  251. #define DRM_DEBUG_RATELIMITED(fmt, args...) \
  252. DRM_DEV_DEBUG_RATELIMITED(NULL, fmt, ##args)
  253. #define DRM_DEV_DEBUG_DRIVER_RATELIMITED(dev, fmt, args...) \
  254. _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRIVER, fmt, ##args)
  255. #define DRM_DEBUG_DRIVER_RATELIMITED(fmt, args...) \
  256. DRM_DEV_DEBUG_DRIVER_RATELIMITED(NULL, fmt, ##args)
  257. #define DRM_DEV_DEBUG_KMS_RATELIMITED(dev, fmt, args...) \
  258. _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, KMS, fmt, ##args)
  259. #define DRM_DEBUG_KMS_RATELIMITED(fmt, args...) \
  260. DRM_DEV_DEBUG_KMS_RATELIMITED(NULL, fmt, ##args)
  261. #define DRM_DEV_DEBUG_PRIME_RATELIMITED(dev, fmt, args...) \
  262. _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, PRIME, fmt, ##args)
  263. #define DRM_DEBUG_PRIME_RATELIMITED(fmt, args...) \
  264. DRM_DEV_DEBUG_PRIME_RATELIMITED(NULL, fmt, ##args)
  265. /* Format strings and argument splitters to simplify printing
  266. * various "complex" objects
  267. */
  268. #define DRM_MODE_FMT "%d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x"
  269. #define DRM_MODE_ARG(m) \
  270. (m)->base.id, (m)->name, (m)->vrefresh, (m)->clock, \
  271. (m)->hdisplay, (m)->hsync_start, (m)->hsync_end, (m)->htotal, \
  272. (m)->vdisplay, (m)->vsync_start, (m)->vsync_end, (m)->vtotal, \
  273. (m)->type, (m)->flags
  274. #define DRM_RECT_FMT "%dx%d%+d%+d"
  275. #define DRM_RECT_ARG(r) drm_rect_width(r), drm_rect_height(r), (r)->x1, (r)->y1
  276. /* for rect's in fixed-point format: */
  277. #define DRM_RECT_FP_FMT "%d.%06ux%d.%06u%+d.%06u%+d.%06u"
  278. #define DRM_RECT_FP_ARG(r) \
  279. drm_rect_width(r) >> 16, ((drm_rect_width(r) & 0xffff) * 15625) >> 10, \
  280. drm_rect_height(r) >> 16, ((drm_rect_height(r) & 0xffff) * 15625) >> 10, \
  281. (r)->x1 >> 16, (((r)->x1 & 0xffff) * 15625) >> 10, \
  282. (r)->y1 >> 16, (((r)->y1 & 0xffff) * 15625) >> 10
  283. /*@}*/
  284. /***********************************************************************/
  285. /** \name Internal types and structures */
  286. /*@{*/
  287. #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
  288. /* Flags and return codes for get_vblank_timestamp() driver function. */
  289. #define DRM_CALLED_FROM_VBLIRQ 1
  290. #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
  291. /* get_scanout_position() return flags */
  292. #define DRM_SCANOUTPOS_VALID (1 << 0)
  293. #define DRM_SCANOUTPOS_IN_VBLANK (1 << 1)
  294. #define DRM_SCANOUTPOS_ACCURATE (1 << 2)
  295. /**
  296. * DRM device structure. This structure represent a complete card that
  297. * may contain multiple heads.
  298. */
  299. struct drm_device {
  300. struct list_head legacy_dev_list;/**< list of devices per driver for stealth attach cleanup */
  301. int if_version; /**< Highest interface version set */
  302. /** \name Lifetime Management */
  303. /*@{ */
  304. struct kref ref; /**< Object ref-count */
  305. struct device *dev; /**< Device structure of bus-device */
  306. struct drm_driver *driver; /**< DRM driver managing the device */
  307. void *dev_private; /**< DRM driver private data */
  308. struct drm_minor *control; /**< Control node */
  309. struct drm_minor *primary; /**< Primary node */
  310. struct drm_minor *render; /**< Render node */
  311. bool registered;
  312. /* currently active master for this device. Protected by master_mutex */
  313. struct drm_master *master;
  314. atomic_t unplugged; /**< Flag whether dev is dead */
  315. struct inode *anon_inode; /**< inode for private address-space */
  316. char *unique; /**< unique name of the device */
  317. /*@} */
  318. /** \name Locks */
  319. /*@{ */
  320. struct mutex struct_mutex; /**< For others */
  321. struct mutex master_mutex; /**< For drm_minor::master and drm_file::is_master */
  322. /*@} */
  323. /** \name Usage Counters */
  324. /*@{ */
  325. int open_count; /**< Outstanding files open, protected by drm_global_mutex. */
  326. spinlock_t buf_lock; /**< For drm_device::buf_use and a few other things. */
  327. int buf_use; /**< Buffers in use -- cannot alloc */
  328. atomic_t buf_alloc; /**< Buffer allocation in progress */
  329. /*@} */
  330. struct mutex filelist_mutex;
  331. struct list_head filelist;
  332. /** \name Memory management */
  333. /*@{ */
  334. struct list_head maplist; /**< Linked list of regions */
  335. struct drm_open_hash map_hash; /**< User token hash table for maps */
  336. /** \name Context handle management */
  337. /*@{ */
  338. struct list_head ctxlist; /**< Linked list of context handles */
  339. struct mutex ctxlist_mutex; /**< For ctxlist */
  340. struct idr ctx_idr;
  341. struct list_head vmalist; /**< List of vmas (for debugging) */
  342. /*@} */
  343. /** \name DMA support */
  344. /*@{ */
  345. struct drm_device_dma *dma; /**< Optional pointer for DMA support */
  346. /*@} */
  347. /** \name Context support */
  348. /*@{ */
  349. __volatile__ long context_flag; /**< Context swapping flag */
  350. int last_context; /**< Last current context */
  351. /*@} */
  352. /** \name VBLANK IRQ support */
  353. /*@{ */
  354. bool irq_enabled;
  355. int irq;
  356. /*
  357. * If true, vblank interrupt will be disabled immediately when the
  358. * refcount drops to zero, as opposed to via the vblank disable
  359. * timer.
  360. * This can be set to true it the hardware has a working vblank
  361. * counter and the driver uses drm_vblank_on() and drm_vblank_off()
  362. * appropriately.
  363. */
  364. bool vblank_disable_immediate;
  365. /* array of size num_crtcs */
  366. struct drm_vblank_crtc *vblank;
  367. spinlock_t vblank_time_lock; /**< Protects vblank count and time updates during vblank enable/disable */
  368. spinlock_t vbl_lock;
  369. u32 max_vblank_count; /**< size of vblank counter register */
  370. /**
  371. * List of events
  372. */
  373. struct list_head vblank_event_list;
  374. spinlock_t event_lock;
  375. /*@} */
  376. struct drm_agp_head *agp; /**< AGP data */
  377. struct pci_dev *pdev; /**< PCI device structure */
  378. #ifdef __alpha__
  379. struct pci_controller *hose;
  380. #endif
  381. struct virtio_device *virtdev;
  382. struct drm_sg_mem *sg; /**< Scatter gather memory */
  383. unsigned int num_crtcs; /**< Number of CRTCs on this device */
  384. struct {
  385. int context;
  386. struct drm_hw_lock *lock;
  387. } sigdata;
  388. struct drm_local_map *agp_buffer_map;
  389. unsigned int agp_buffer_token;
  390. struct drm_mode_config mode_config; /**< Current mode config */
  391. /** \name GEM information */
  392. /*@{ */
  393. struct mutex object_name_lock;
  394. struct idr object_name_idr;
  395. struct drm_vma_offset_manager *vma_offset_manager;
  396. /*@} */
  397. int switch_power_state;
  398. };
  399. /**
  400. * drm_drv_uses_atomic_modeset - check if the driver implements
  401. * atomic_commit()
  402. * @dev: DRM device
  403. *
  404. * This check is useful if drivers do not have DRIVER_ATOMIC set but
  405. * have atomic modesetting internally implemented.
  406. */
  407. static inline bool drm_drv_uses_atomic_modeset(struct drm_device *dev)
  408. {
  409. return dev->mode_config.funcs->atomic_commit != NULL;
  410. }
  411. #include <drm/drm_irq.h>
  412. #define DRM_SWITCH_POWER_ON 0
  413. #define DRM_SWITCH_POWER_OFF 1
  414. #define DRM_SWITCH_POWER_CHANGING 2
  415. #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
  416. static __inline__ int drm_core_check_feature(struct drm_device *dev,
  417. int feature)
  418. {
  419. return ((dev->driver->driver_features & feature) ? 1 : 0);
  420. }
  421. static inline void drm_device_set_unplugged(struct drm_device *dev)
  422. {
  423. smp_wmb();
  424. atomic_set(&dev->unplugged, 1);
  425. }
  426. static inline int drm_device_is_unplugged(struct drm_device *dev)
  427. {
  428. int ret = atomic_read(&dev->unplugged);
  429. smp_rmb();
  430. return ret;
  431. }
  432. /******************************************************************/
  433. /** \name Internal function definitions */
  434. /*@{*/
  435. /* Driver support (drm_drv.h) */
  436. /*
  437. * These are exported to drivers so that they can implement fencing using
  438. * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
  439. */
  440. /*@}*/
  441. /* returns true if currently okay to sleep */
  442. static __inline__ bool drm_can_sleep(void)
  443. {
  444. if (in_atomic() || in_dbg_master() || irqs_disabled())
  445. return false;
  446. return true;
  447. }
  448. /* helper for handling conditionals in various for_each macros */
  449. #define for_each_if(condition) if (!(condition)) {} else
  450. #endif