drmP.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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/pci.h>
  49. #include <linux/platform_device.h>
  50. #include <linux/poll.h>
  51. #include <linux/ratelimit.h>
  52. #include <linux/sched.h>
  53. #include <linux/slab.h>
  54. #include <linux/types.h>
  55. #include <linux/vmalloc.h>
  56. #include <linux/workqueue.h>
  57. #include <asm/mman.h>
  58. #include <asm/pgalloc.h>
  59. #include <asm/uaccess.h>
  60. #include <uapi/drm/drm.h>
  61. #include <uapi/drm/drm_mode.h>
  62. #include <drm/drm_agpsupport.h>
  63. #include <drm/drm_crtc.h>
  64. #include <drm/drm_global.h>
  65. #include <drm/drm_hashtab.h>
  66. #include <drm/drm_mem_util.h>
  67. #include <drm/drm_mm.h>
  68. #include <drm/drm_os_linux.h>
  69. #include <drm/drm_sarea.h>
  70. #include <drm/drm_vma_manager.h>
  71. struct module;
  72. struct drm_file;
  73. struct drm_device;
  74. struct drm_agp_head;
  75. struct drm_local_map;
  76. struct drm_device_dma;
  77. struct drm_dma_handle;
  78. struct drm_gem_object;
  79. struct device_node;
  80. struct videomode;
  81. struct reservation_object;
  82. struct dma_buf_attachment;
  83. /*
  84. * 4 debug categories are defined:
  85. *
  86. * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
  87. * This is the category used by the DRM_DEBUG() macro.
  88. *
  89. * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
  90. * This is the category used by the DRM_DEBUG_DRIVER() macro.
  91. *
  92. * KMS: used in the modesetting code.
  93. * This is the category used by the DRM_DEBUG_KMS() macro.
  94. *
  95. * PRIME: used in the prime code.
  96. * This is the category used by the DRM_DEBUG_PRIME() macro.
  97. *
  98. * ATOMIC: used in the atomic code.
  99. * This is the category used by the DRM_DEBUG_ATOMIC() macro.
  100. *
  101. * VBL: used for verbose debug message in the vblank code
  102. * This is the category used by the DRM_DEBUG_VBL() macro.
  103. *
  104. * Enabling verbose debug messages is done through the drm.debug parameter,
  105. * each category being enabled by a bit.
  106. *
  107. * drm.debug=0x1 will enable CORE messages
  108. * drm.debug=0x2 will enable DRIVER messages
  109. * drm.debug=0x3 will enable CORE and DRIVER messages
  110. * ...
  111. * drm.debug=0x3f will enable all messages
  112. *
  113. * An interesting feature is that it's possible to enable verbose logging at
  114. * run-time by echoing the debug value in its sysfs node:
  115. * # echo 0xf > /sys/module/drm/parameters/debug
  116. */
  117. #define DRM_UT_CORE 0x01
  118. #define DRM_UT_DRIVER 0x02
  119. #define DRM_UT_KMS 0x04
  120. #define DRM_UT_PRIME 0x08
  121. #define DRM_UT_ATOMIC 0x10
  122. #define DRM_UT_VBL 0x20
  123. extern __printf(2, 3)
  124. void drm_ut_debug_printk(const char *function_name,
  125. const char *format, ...);
  126. extern __printf(1, 2)
  127. void drm_err(const char *format, ...);
  128. /***********************************************************************/
  129. /** \name DRM template customization defaults */
  130. /*@{*/
  131. /* driver capabilities and requirements mask */
  132. #define DRIVER_USE_AGP 0x1
  133. #define DRIVER_PCI_DMA 0x8
  134. #define DRIVER_SG 0x10
  135. #define DRIVER_HAVE_DMA 0x20
  136. #define DRIVER_HAVE_IRQ 0x40
  137. #define DRIVER_IRQ_SHARED 0x80
  138. #define DRIVER_GEM 0x1000
  139. #define DRIVER_MODESET 0x2000
  140. #define DRIVER_PRIME 0x4000
  141. #define DRIVER_RENDER 0x8000
  142. #define DRIVER_ATOMIC 0x10000
  143. #define DRIVER_KMS_LEGACY_CONTEXT 0x20000
  144. /***********************************************************************/
  145. /** \name Macros to make printk easier */
  146. /*@{*/
  147. /**
  148. * Error output.
  149. *
  150. * \param fmt printf() like format string.
  151. * \param arg arguments
  152. */
  153. #define DRM_ERROR(fmt, ...) \
  154. drm_err(fmt, ##__VA_ARGS__)
  155. /**
  156. * Rate limited error output. Like DRM_ERROR() but won't flood the log.
  157. *
  158. * \param fmt printf() like format string.
  159. * \param arg arguments
  160. */
  161. #define DRM_ERROR_RATELIMITED(fmt, ...) \
  162. ({ \
  163. static DEFINE_RATELIMIT_STATE(_rs, \
  164. DEFAULT_RATELIMIT_INTERVAL, \
  165. DEFAULT_RATELIMIT_BURST); \
  166. \
  167. if (__ratelimit(&_rs)) \
  168. drm_err(fmt, ##__VA_ARGS__); \
  169. })
  170. #define DRM_INFO(fmt, ...) \
  171. printk(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
  172. #define DRM_INFO_ONCE(fmt, ...) \
  173. printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
  174. /**
  175. * Debug output.
  176. *
  177. * \param fmt printf() like format string.
  178. * \param arg arguments
  179. */
  180. #define DRM_DEBUG(fmt, args...) \
  181. do { \
  182. if (unlikely(drm_debug & DRM_UT_CORE)) \
  183. drm_ut_debug_printk(__func__, fmt, ##args); \
  184. } while (0)
  185. #define DRM_DEBUG_DRIVER(fmt, args...) \
  186. do { \
  187. if (unlikely(drm_debug & DRM_UT_DRIVER)) \
  188. drm_ut_debug_printk(__func__, fmt, ##args); \
  189. } while (0)
  190. #define DRM_DEBUG_KMS(fmt, args...) \
  191. do { \
  192. if (unlikely(drm_debug & DRM_UT_KMS)) \
  193. drm_ut_debug_printk(__func__, fmt, ##args); \
  194. } while (0)
  195. #define DRM_DEBUG_PRIME(fmt, args...) \
  196. do { \
  197. if (unlikely(drm_debug & DRM_UT_PRIME)) \
  198. drm_ut_debug_printk(__func__, fmt, ##args); \
  199. } while (0)
  200. #define DRM_DEBUG_ATOMIC(fmt, args...) \
  201. do { \
  202. if (unlikely(drm_debug & DRM_UT_ATOMIC)) \
  203. drm_ut_debug_printk(__func__, fmt, ##args); \
  204. } while (0)
  205. #define DRM_DEBUG_VBL(fmt, args...) \
  206. do { \
  207. if (unlikely(drm_debug & DRM_UT_VBL)) \
  208. drm_ut_debug_printk(__func__, fmt, ##args); \
  209. } while (0)
  210. /*@}*/
  211. /***********************************************************************/
  212. /** \name Internal types and structures */
  213. /*@{*/
  214. #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
  215. /**
  216. * Ioctl function type.
  217. *
  218. * \param inode device inode.
  219. * \param file_priv DRM file private pointer.
  220. * \param cmd command.
  221. * \param arg argument.
  222. */
  223. typedef int drm_ioctl_t(struct drm_device *dev, void *data,
  224. struct drm_file *file_priv);
  225. typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
  226. unsigned long arg);
  227. #define DRM_IOCTL_NR(n) _IOC_NR(n)
  228. #define DRM_MAJOR 226
  229. #define DRM_AUTH 0x1
  230. #define DRM_MASTER 0x2
  231. #define DRM_ROOT_ONLY 0x4
  232. #define DRM_CONTROL_ALLOW 0x8
  233. #define DRM_UNLOCKED 0x10
  234. #define DRM_RENDER_ALLOW 0x20
  235. struct drm_ioctl_desc {
  236. unsigned int cmd;
  237. int flags;
  238. drm_ioctl_t *func;
  239. const char *name;
  240. };
  241. /**
  242. * Creates a driver or general drm_ioctl_desc array entry for the given
  243. * ioctl, for use by drm_ioctl().
  244. */
  245. #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags) \
  246. [DRM_IOCTL_NR(DRM_IOCTL_##ioctl) - DRM_COMMAND_BASE] = { \
  247. .cmd = DRM_IOCTL_##ioctl, \
  248. .func = _func, \
  249. .flags = _flags, \
  250. .name = #ioctl \
  251. }
  252. /* Event queued up for userspace to read */
  253. struct drm_pending_event {
  254. struct drm_event *event;
  255. struct list_head link;
  256. struct drm_file *file_priv;
  257. pid_t pid; /* pid of requester, no guarantee it's valid by the time
  258. we deliver the event, for tracing only */
  259. void (*destroy)(struct drm_pending_event *event);
  260. };
  261. /* initial implementaton using a linked list - todo hashtab */
  262. struct drm_prime_file_private {
  263. struct list_head head;
  264. struct mutex lock;
  265. };
  266. /** File private data */
  267. struct drm_file {
  268. unsigned authenticated :1;
  269. /* Whether we're master for a minor. Protected by master_mutex */
  270. unsigned is_master :1;
  271. /* true when the client has asked us to expose stereo 3D mode flags */
  272. unsigned stereo_allowed :1;
  273. /*
  274. * true if client understands CRTC primary planes and cursor planes
  275. * in the plane list
  276. */
  277. unsigned universal_planes:1;
  278. /* true if client understands atomic properties */
  279. unsigned atomic:1;
  280. /*
  281. * This client is allowed to gain master privileges for @master.
  282. * Protected by struct drm_device::master_mutex.
  283. */
  284. unsigned allowed_master:1;
  285. struct pid *pid;
  286. kuid_t uid;
  287. drm_magic_t magic;
  288. struct list_head lhead;
  289. struct drm_minor *minor;
  290. unsigned long lock_count;
  291. /** Mapping of mm object handles to object pointers. */
  292. struct idr object_idr;
  293. /** Lock for synchronization of access to object_idr. */
  294. spinlock_t table_lock;
  295. struct file *filp;
  296. void *driver_priv;
  297. struct drm_master *master; /* master this node is currently associated with
  298. N.B. not always minor->master */
  299. /**
  300. * fbs - List of framebuffers associated with this file.
  301. *
  302. * Protected by fbs_lock. Note that the fbs list holds a reference on
  303. * the fb object to prevent it from untimely disappearing.
  304. */
  305. struct list_head fbs;
  306. struct mutex fbs_lock;
  307. /** User-created blob properties; this retains a reference on the
  308. * property. */
  309. struct list_head blobs;
  310. wait_queue_head_t event_wait;
  311. struct list_head event_list;
  312. int event_space;
  313. struct mutex event_read_lock;
  314. struct drm_prime_file_private prime;
  315. };
  316. /**
  317. * Lock data.
  318. */
  319. struct drm_lock_data {
  320. struct drm_hw_lock *hw_lock; /**< Hardware lock */
  321. /** Private of lock holder's file (NULL=kernel) */
  322. struct drm_file *file_priv;
  323. wait_queue_head_t lock_queue; /**< Queue of blocked processes */
  324. unsigned long lock_time; /**< Time of last lock in jiffies */
  325. spinlock_t spinlock;
  326. uint32_t kernel_waiters;
  327. uint32_t user_waiters;
  328. int idle_has_lock;
  329. };
  330. /**
  331. * struct drm_master - drm master structure
  332. *
  333. * @refcount: Refcount for this master object.
  334. * @minor: Link back to minor char device we are master for. Immutable.
  335. * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
  336. * @unique_len: Length of unique field. Protected by drm_global_mutex.
  337. * @magic_map: Map of used authentication tokens. Protected by struct_mutex.
  338. * @lock: DRI lock information.
  339. * @driver_priv: Pointer to driver-private information.
  340. */
  341. struct drm_master {
  342. struct kref refcount;
  343. struct drm_minor *minor;
  344. char *unique;
  345. int unique_len;
  346. struct idr magic_map;
  347. struct drm_lock_data lock;
  348. void *driver_priv;
  349. };
  350. /* Size of ringbuffer for vblank timestamps. Just double-buffer
  351. * in initial implementation.
  352. */
  353. #define DRM_VBLANKTIME_RBSIZE 2
  354. /* Flags and return codes for get_vblank_timestamp() driver function. */
  355. #define DRM_CALLED_FROM_VBLIRQ 1
  356. #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
  357. #define DRM_VBLANKTIME_IN_VBLANK (1 << 1)
  358. /* get_scanout_position() return flags */
  359. #define DRM_SCANOUTPOS_VALID (1 << 0)
  360. #define DRM_SCANOUTPOS_IN_VBLANK (1 << 1)
  361. #define DRM_SCANOUTPOS_ACCURATE (1 << 2)
  362. /**
  363. * DRM driver structure. This structure represent the common code for
  364. * a family of cards. There will one drm_device for each card present
  365. * in this family
  366. */
  367. struct drm_driver {
  368. int (*load) (struct drm_device *, unsigned long flags);
  369. int (*firstopen) (struct drm_device *);
  370. int (*open) (struct drm_device *, struct drm_file *);
  371. void (*preclose) (struct drm_device *, struct drm_file *file_priv);
  372. void (*postclose) (struct drm_device *, struct drm_file *);
  373. void (*lastclose) (struct drm_device *);
  374. int (*unload) (struct drm_device *);
  375. int (*suspend) (struct drm_device *, pm_message_t state);
  376. int (*resume) (struct drm_device *);
  377. int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
  378. int (*dma_quiescent) (struct drm_device *);
  379. int (*context_dtor) (struct drm_device *dev, int context);
  380. int (*set_busid)(struct drm_device *dev, struct drm_master *master);
  381. /**
  382. * get_vblank_counter - get raw hardware vblank counter
  383. * @dev: DRM device
  384. * @pipe: counter to fetch
  385. *
  386. * Driver callback for fetching a raw hardware vblank counter for @crtc.
  387. * If a device doesn't have a hardware counter, the driver can simply
  388. * return the value of drm_vblank_count. The DRM core will account for
  389. * missed vblank events while interrupts where disabled based on system
  390. * timestamps.
  391. *
  392. * Wraparound handling and loss of events due to modesetting is dealt
  393. * with in the DRM core code.
  394. *
  395. * RETURNS
  396. * Raw vblank counter value.
  397. */
  398. u32 (*get_vblank_counter) (struct drm_device *dev, unsigned int pipe);
  399. /**
  400. * enable_vblank - enable vblank interrupt events
  401. * @dev: DRM device
  402. * @pipe: which irq to enable
  403. *
  404. * Enable vblank interrupts for @crtc. If the device doesn't have
  405. * a hardware vblank counter, this routine should be a no-op, since
  406. * interrupts will have to stay on to keep the count accurate.
  407. *
  408. * RETURNS
  409. * Zero on success, appropriate errno if the given @crtc's vblank
  410. * interrupt cannot be enabled.
  411. */
  412. int (*enable_vblank) (struct drm_device *dev, unsigned int pipe);
  413. /**
  414. * disable_vblank - disable vblank interrupt events
  415. * @dev: DRM device
  416. * @pipe: which irq to enable
  417. *
  418. * Disable vblank interrupts for @crtc. If the device doesn't have
  419. * a hardware vblank counter, this routine should be a no-op, since
  420. * interrupts will have to stay on to keep the count accurate.
  421. */
  422. void (*disable_vblank) (struct drm_device *dev, unsigned int pipe);
  423. /**
  424. * Called by \c drm_device_is_agp. Typically used to determine if a
  425. * card is really attached to AGP or not.
  426. *
  427. * \param dev DRM device handle
  428. *
  429. * \returns
  430. * One of three values is returned depending on whether or not the
  431. * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
  432. * (return of 1), or may or may not be AGP (return of 2).
  433. */
  434. int (*device_is_agp) (struct drm_device *dev);
  435. /**
  436. * Called by vblank timestamping code.
  437. *
  438. * Return the current display scanout position from a crtc, and an
  439. * optional accurate ktime_get timestamp of when position was measured.
  440. *
  441. * \param dev DRM device.
  442. * \param pipe Id of the crtc to query.
  443. * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
  444. * \param *vpos Target location for current vertical scanout position.
  445. * \param *hpos Target location for current horizontal scanout position.
  446. * \param *stime Target location for timestamp taken immediately before
  447. * scanout position query. Can be NULL to skip timestamp.
  448. * \param *etime Target location for timestamp taken immediately after
  449. * scanout position query. Can be NULL to skip timestamp.
  450. * \param mode Current display timings.
  451. *
  452. * Returns vpos as a positive number while in active scanout area.
  453. * Returns vpos as a negative number inside vblank, counting the number
  454. * of scanlines to go until end of vblank, e.g., -1 means "one scanline
  455. * until start of active scanout / end of vblank."
  456. *
  457. * \return Flags, or'ed together as follows:
  458. *
  459. * DRM_SCANOUTPOS_VALID = Query successful.
  460. * DRM_SCANOUTPOS_INVBL = Inside vblank.
  461. * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
  462. * this flag means that returned position may be offset by a constant
  463. * but unknown small number of scanlines wrt. real scanout position.
  464. *
  465. */
  466. int (*get_scanout_position) (struct drm_device *dev, unsigned int pipe,
  467. unsigned int flags, int *vpos, int *hpos,
  468. ktime_t *stime, ktime_t *etime,
  469. const struct drm_display_mode *mode);
  470. /**
  471. * Called by \c drm_get_last_vbltimestamp. Should return a precise
  472. * timestamp when the most recent VBLANK interval ended or will end.
  473. *
  474. * Specifically, the timestamp in @vblank_time should correspond as
  475. * closely as possible to the time when the first video scanline of
  476. * the video frame after the end of VBLANK will start scanning out,
  477. * the time immediately after end of the VBLANK interval. If the
  478. * @crtc is currently inside VBLANK, this will be a time in the future.
  479. * If the @crtc is currently scanning out a frame, this will be the
  480. * past start time of the current scanout. This is meant to adhere
  481. * to the OpenML OML_sync_control extension specification.
  482. *
  483. * \param dev dev DRM device handle.
  484. * \param pipe crtc for which timestamp should be returned.
  485. * \param *max_error Maximum allowable timestamp error in nanoseconds.
  486. * Implementation should strive to provide timestamp
  487. * with an error of at most *max_error nanoseconds.
  488. * Returns true upper bound on error for timestamp.
  489. * \param *vblank_time Target location for returned vblank timestamp.
  490. * \param flags 0 = Defaults, no special treatment needed.
  491. * \param DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
  492. * irq handler. Some drivers need to apply some workarounds
  493. * for gpu-specific vblank irq quirks if flag is set.
  494. *
  495. * \returns
  496. * Zero if timestamping isn't supported in current display mode or a
  497. * negative number on failure. A positive status code on success,
  498. * which describes how the vblank_time timestamp was computed.
  499. */
  500. int (*get_vblank_timestamp) (struct drm_device *dev, unsigned int pipe,
  501. int *max_error,
  502. struct timeval *vblank_time,
  503. unsigned flags);
  504. /* these have to be filled in */
  505. irqreturn_t(*irq_handler) (int irq, void *arg);
  506. void (*irq_preinstall) (struct drm_device *dev);
  507. int (*irq_postinstall) (struct drm_device *dev);
  508. void (*irq_uninstall) (struct drm_device *dev);
  509. /* Master routines */
  510. int (*master_create)(struct drm_device *dev, struct drm_master *master);
  511. void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
  512. /**
  513. * master_set is called whenever the minor master is set.
  514. * master_drop is called whenever the minor master is dropped.
  515. */
  516. int (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
  517. bool from_open);
  518. void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv,
  519. bool from_release);
  520. int (*debugfs_init)(struct drm_minor *minor);
  521. void (*debugfs_cleanup)(struct drm_minor *minor);
  522. /**
  523. * Driver-specific constructor for drm_gem_objects, to set up
  524. * obj->driver_private.
  525. *
  526. * Returns 0 on success.
  527. */
  528. void (*gem_free_object) (struct drm_gem_object *obj);
  529. int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
  530. void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
  531. /**
  532. * Hook for allocating the GEM object struct, for use by core
  533. * helpers.
  534. */
  535. struct drm_gem_object *(*gem_create_object)(struct drm_device *dev,
  536. size_t size);
  537. /* prime: */
  538. /* export handle -> fd (see drm_gem_prime_handle_to_fd() helper) */
  539. int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
  540. uint32_t handle, uint32_t flags, int *prime_fd);
  541. /* import fd -> handle (see drm_gem_prime_fd_to_handle() helper) */
  542. int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
  543. int prime_fd, uint32_t *handle);
  544. /* export GEM -> dmabuf */
  545. struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
  546. struct drm_gem_object *obj, int flags);
  547. /* import dmabuf -> GEM */
  548. struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
  549. struct dma_buf *dma_buf);
  550. /* low-level interface used by drm_gem_prime_{import,export} */
  551. int (*gem_prime_pin)(struct drm_gem_object *obj);
  552. void (*gem_prime_unpin)(struct drm_gem_object *obj);
  553. struct reservation_object * (*gem_prime_res_obj)(
  554. struct drm_gem_object *obj);
  555. struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj);
  556. struct drm_gem_object *(*gem_prime_import_sg_table)(
  557. struct drm_device *dev,
  558. struct dma_buf_attachment *attach,
  559. struct sg_table *sgt);
  560. void *(*gem_prime_vmap)(struct drm_gem_object *obj);
  561. void (*gem_prime_vunmap)(struct drm_gem_object *obj, void *vaddr);
  562. int (*gem_prime_mmap)(struct drm_gem_object *obj,
  563. struct vm_area_struct *vma);
  564. /* vga arb irq handler */
  565. void (*vgaarb_irq)(struct drm_device *dev, bool state);
  566. /* dumb alloc support */
  567. int (*dumb_create)(struct drm_file *file_priv,
  568. struct drm_device *dev,
  569. struct drm_mode_create_dumb *args);
  570. int (*dumb_map_offset)(struct drm_file *file_priv,
  571. struct drm_device *dev, uint32_t handle,
  572. uint64_t *offset);
  573. int (*dumb_destroy)(struct drm_file *file_priv,
  574. struct drm_device *dev,
  575. uint32_t handle);
  576. /* Driver private ops for this object */
  577. const struct vm_operations_struct *gem_vm_ops;
  578. int major;
  579. int minor;
  580. int patchlevel;
  581. char *name;
  582. char *desc;
  583. char *date;
  584. u32 driver_features;
  585. int dev_priv_size;
  586. const struct drm_ioctl_desc *ioctls;
  587. int num_ioctls;
  588. const struct file_operations *fops;
  589. /* List of devices hanging off this driver with stealth attach. */
  590. struct list_head legacy_dev_list;
  591. };
  592. enum drm_minor_type {
  593. DRM_MINOR_LEGACY,
  594. DRM_MINOR_CONTROL,
  595. DRM_MINOR_RENDER,
  596. DRM_MINOR_CNT,
  597. };
  598. /**
  599. * Info file list entry. This structure represents a debugfs or proc file to
  600. * be created by the drm core
  601. */
  602. struct drm_info_list {
  603. const char *name; /** file name */
  604. int (*show)(struct seq_file*, void*); /** show callback */
  605. u32 driver_features; /**< Required driver features for this entry */
  606. void *data;
  607. };
  608. /**
  609. * debugfs node structure. This structure represents a debugfs file.
  610. */
  611. struct drm_info_node {
  612. struct list_head list;
  613. struct drm_minor *minor;
  614. const struct drm_info_list *info_ent;
  615. struct dentry *dent;
  616. };
  617. /**
  618. * DRM minor structure. This structure represents a drm minor number.
  619. */
  620. struct drm_minor {
  621. int index; /**< Minor device number */
  622. int type; /**< Control or render */
  623. struct device *kdev; /**< Linux device */
  624. struct drm_device *dev;
  625. struct dentry *debugfs_root;
  626. struct list_head debugfs_list;
  627. struct mutex debugfs_lock; /* Protects debugfs_list. */
  628. /* currently active master for this node. Protected by master_mutex */
  629. struct drm_master *master;
  630. };
  631. struct drm_pending_vblank_event {
  632. struct drm_pending_event base;
  633. unsigned int pipe;
  634. struct drm_event_vblank event;
  635. };
  636. struct drm_vblank_crtc {
  637. struct drm_device *dev; /* pointer to the drm_device */
  638. wait_queue_head_t queue; /**< VBLANK wait queue */
  639. struct timer_list disable_timer; /* delayed disable timer */
  640. /* vblank counter, protected by dev->vblank_time_lock for writes */
  641. u32 count;
  642. /* vblank timestamps, protected by dev->vblank_time_lock for writes */
  643. struct timeval time[DRM_VBLANKTIME_RBSIZE];
  644. atomic_t refcount; /* number of users of vblank interruptsper crtc */
  645. u32 last; /* protected by dev->vbl_lock, used */
  646. /* for wraparound handling */
  647. u32 last_wait; /* Last vblank seqno waited per CRTC */
  648. unsigned int inmodeset; /* Display driver is setting mode */
  649. unsigned int pipe; /* crtc index */
  650. int framedur_ns; /* frame/field duration in ns */
  651. int linedur_ns; /* line duration in ns */
  652. bool enabled; /* so we don't call enable more than
  653. once per disable */
  654. };
  655. /**
  656. * DRM device structure. This structure represent a complete card that
  657. * may contain multiple heads.
  658. */
  659. struct drm_device {
  660. struct list_head legacy_dev_list;/**< list of devices per driver for stealth attach cleanup */
  661. int if_version; /**< Highest interface version set */
  662. /** \name Lifetime Management */
  663. /*@{ */
  664. struct kref ref; /**< Object ref-count */
  665. struct device *dev; /**< Device structure of bus-device */
  666. struct drm_driver *driver; /**< DRM driver managing the device */
  667. void *dev_private; /**< DRM driver private data */
  668. struct drm_minor *control; /**< Control node */
  669. struct drm_minor *primary; /**< Primary node */
  670. struct drm_minor *render; /**< Render node */
  671. atomic_t unplugged; /**< Flag whether dev is dead */
  672. struct inode *anon_inode; /**< inode for private address-space */
  673. char *unique; /**< unique name of the device */
  674. /*@} */
  675. /** \name Locks */
  676. /*@{ */
  677. struct mutex struct_mutex; /**< For others */
  678. struct mutex master_mutex; /**< For drm_minor::master and drm_file::is_master */
  679. /*@} */
  680. /** \name Usage Counters */
  681. /*@{ */
  682. int open_count; /**< Outstanding files open, protected by drm_global_mutex. */
  683. spinlock_t buf_lock; /**< For drm_device::buf_use and a few other things. */
  684. int buf_use; /**< Buffers in use -- cannot alloc */
  685. atomic_t buf_alloc; /**< Buffer allocation in progress */
  686. /*@} */
  687. struct list_head filelist;
  688. /** \name Memory management */
  689. /*@{ */
  690. struct list_head maplist; /**< Linked list of regions */
  691. struct drm_open_hash map_hash; /**< User token hash table for maps */
  692. /** \name Context handle management */
  693. /*@{ */
  694. struct list_head ctxlist; /**< Linked list of context handles */
  695. struct mutex ctxlist_mutex; /**< For ctxlist */
  696. struct idr ctx_idr;
  697. struct list_head vmalist; /**< List of vmas (for debugging) */
  698. /*@} */
  699. /** \name DMA support */
  700. /*@{ */
  701. struct drm_device_dma *dma; /**< Optional pointer for DMA support */
  702. /*@} */
  703. /** \name Context support */
  704. /*@{ */
  705. __volatile__ long context_flag; /**< Context swapping flag */
  706. int last_context; /**< Last current context */
  707. /*@} */
  708. /** \name VBLANK IRQ support */
  709. /*@{ */
  710. bool irq_enabled;
  711. int irq;
  712. /*
  713. * At load time, disabling the vblank interrupt won't be allowed since
  714. * old clients may not call the modeset ioctl and therefore misbehave.
  715. * Once the modeset ioctl *has* been called though, we can safely
  716. * disable them when unused.
  717. */
  718. bool vblank_disable_allowed;
  719. /*
  720. * If true, vblank interrupt will be disabled immediately when the
  721. * refcount drops to zero, as opposed to via the vblank disable
  722. * timer.
  723. * This can be set to true it the hardware has a working vblank
  724. * counter and the driver uses drm_vblank_on() and drm_vblank_off()
  725. * appropriately.
  726. */
  727. bool vblank_disable_immediate;
  728. /* array of size num_crtcs */
  729. struct drm_vblank_crtc *vblank;
  730. spinlock_t vblank_time_lock; /**< Protects vblank count and time updates during vblank enable/disable */
  731. spinlock_t vbl_lock;
  732. u32 max_vblank_count; /**< size of vblank counter register */
  733. /**
  734. * List of events
  735. */
  736. struct list_head vblank_event_list;
  737. spinlock_t event_lock;
  738. /*@} */
  739. struct drm_agp_head *agp; /**< AGP data */
  740. struct pci_dev *pdev; /**< PCI device structure */
  741. #ifdef __alpha__
  742. struct pci_controller *hose;
  743. #endif
  744. struct platform_device *platformdev; /**< Platform device struture */
  745. struct virtio_device *virtdev;
  746. struct drm_sg_mem *sg; /**< Scatter gather memory */
  747. unsigned int num_crtcs; /**< Number of CRTCs on this device */
  748. struct {
  749. int context;
  750. struct drm_hw_lock *lock;
  751. } sigdata;
  752. struct drm_local_map *agp_buffer_map;
  753. unsigned int agp_buffer_token;
  754. struct drm_mode_config mode_config; /**< Current mode config */
  755. /** \name GEM information */
  756. /*@{ */
  757. struct mutex object_name_lock;
  758. struct idr object_name_idr;
  759. struct drm_vma_offset_manager *vma_offset_manager;
  760. /*@} */
  761. int switch_power_state;
  762. };
  763. #define DRM_SWITCH_POWER_ON 0
  764. #define DRM_SWITCH_POWER_OFF 1
  765. #define DRM_SWITCH_POWER_CHANGING 2
  766. #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
  767. static __inline__ int drm_core_check_feature(struct drm_device *dev,
  768. int feature)
  769. {
  770. return ((dev->driver->driver_features & feature) ? 1 : 0);
  771. }
  772. static inline void drm_device_set_unplugged(struct drm_device *dev)
  773. {
  774. smp_wmb();
  775. atomic_set(&dev->unplugged, 1);
  776. }
  777. static inline int drm_device_is_unplugged(struct drm_device *dev)
  778. {
  779. int ret = atomic_read(&dev->unplugged);
  780. smp_rmb();
  781. return ret;
  782. }
  783. static inline bool drm_is_render_client(const struct drm_file *file_priv)
  784. {
  785. return file_priv->minor->type == DRM_MINOR_RENDER;
  786. }
  787. static inline bool drm_is_control_client(const struct drm_file *file_priv)
  788. {
  789. return file_priv->minor->type == DRM_MINOR_CONTROL;
  790. }
  791. static inline bool drm_is_primary_client(const struct drm_file *file_priv)
  792. {
  793. return file_priv->minor->type == DRM_MINOR_LEGACY;
  794. }
  795. /******************************************************************/
  796. /** \name Internal function definitions */
  797. /*@{*/
  798. /* Driver support (drm_drv.h) */
  799. extern int drm_ioctl_permit(u32 flags, struct drm_file *file_priv);
  800. extern long drm_ioctl(struct file *filp,
  801. unsigned int cmd, unsigned long arg);
  802. extern long drm_compat_ioctl(struct file *filp,
  803. unsigned int cmd, unsigned long arg);
  804. extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
  805. /* Device support (drm_fops.h) */
  806. extern int drm_open(struct inode *inode, struct file *filp);
  807. extern ssize_t drm_read(struct file *filp, char __user *buffer,
  808. size_t count, loff_t *offset);
  809. extern int drm_release(struct inode *inode, struct file *filp);
  810. extern int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv);
  811. /* Mapping support (drm_vm.h) */
  812. extern unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
  813. /* Misc. IOCTL support (drm_ioctl.c) */
  814. int drm_noop(struct drm_device *dev, void *data,
  815. struct drm_file *file_priv);
  816. int drm_invalid_op(struct drm_device *dev, void *data,
  817. struct drm_file *file_priv);
  818. /* Cache management (drm_cache.c) */
  819. void drm_clflush_pages(struct page *pages[], unsigned long num_pages);
  820. void drm_clflush_sg(struct sg_table *st);
  821. void drm_clflush_virt_range(void *addr, unsigned long length);
  822. /*
  823. * These are exported to drivers so that they can implement fencing using
  824. * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
  825. */
  826. /* IRQ support (drm_irq.h) */
  827. extern int drm_irq_install(struct drm_device *dev, int irq);
  828. extern int drm_irq_uninstall(struct drm_device *dev);
  829. extern int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
  830. extern int drm_wait_vblank(struct drm_device *dev, void *data,
  831. struct drm_file *filp);
  832. extern u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe);
  833. extern u32 drm_crtc_vblank_count(struct drm_crtc *crtc);
  834. extern u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
  835. struct timeval *vblanktime);
  836. extern u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
  837. struct timeval *vblanktime);
  838. extern void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
  839. struct drm_pending_vblank_event *e);
  840. extern void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
  841. struct drm_pending_vblank_event *e);
  842. extern void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
  843. struct drm_pending_vblank_event *e);
  844. extern void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
  845. struct drm_pending_vblank_event *e);
  846. extern bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
  847. extern bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
  848. extern int drm_vblank_get(struct drm_device *dev, unsigned int pipe);
  849. extern void drm_vblank_put(struct drm_device *dev, unsigned int pipe);
  850. extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
  851. extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
  852. extern void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
  853. extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
  854. extern void drm_vblank_off(struct drm_device *dev, unsigned int pipe);
  855. extern void drm_vblank_on(struct drm_device *dev, unsigned int pipe);
  856. extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
  857. extern void drm_crtc_vblank_reset(struct drm_crtc *crtc);
  858. extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
  859. extern void drm_vblank_cleanup(struct drm_device *dev);
  860. extern u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe);
  861. extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
  862. unsigned int pipe, int *max_error,
  863. struct timeval *vblank_time,
  864. unsigned flags,
  865. const struct drm_display_mode *mode);
  866. extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
  867. const struct drm_display_mode *mode);
  868. /**
  869. * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
  870. * @crtc: which CRTC's vblank waitqueue to retrieve
  871. *
  872. * This function returns a pointer to the vblank waitqueue for the CRTC.
  873. * Drivers can use this to implement vblank waits using wait_event() & co.
  874. */
  875. static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
  876. {
  877. return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
  878. }
  879. /* Modesetting support */
  880. extern void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe);
  881. extern void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe);
  882. /* Stub support (drm_stub.h) */
  883. extern struct drm_master *drm_master_get(struct drm_master *master);
  884. extern void drm_master_put(struct drm_master **master);
  885. extern void drm_put_dev(struct drm_device *dev);
  886. extern void drm_unplug_dev(struct drm_device *dev);
  887. extern unsigned int drm_debug;
  888. extern bool drm_atomic;
  889. /* Debugfs support */
  890. #if defined(CONFIG_DEBUG_FS)
  891. extern int drm_debugfs_create_files(const struct drm_info_list *files,
  892. int count, struct dentry *root,
  893. struct drm_minor *minor);
  894. extern int drm_debugfs_remove_files(const struct drm_info_list *files,
  895. int count, struct drm_minor *minor);
  896. #else
  897. static inline int drm_debugfs_create_files(const struct drm_info_list *files,
  898. int count, struct dentry *root,
  899. struct drm_minor *minor)
  900. {
  901. return 0;
  902. }
  903. static inline int drm_debugfs_remove_files(const struct drm_info_list *files,
  904. int count, struct drm_minor *minor)
  905. {
  906. return 0;
  907. }
  908. #endif
  909. extern struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
  910. struct drm_gem_object *obj, int flags);
  911. extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
  912. struct drm_file *file_priv, uint32_t handle, uint32_t flags,
  913. int *prime_fd);
  914. extern struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
  915. struct dma_buf *dma_buf);
  916. extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
  917. struct drm_file *file_priv, int prime_fd, uint32_t *handle);
  918. extern void drm_gem_dmabuf_release(struct dma_buf *dma_buf);
  919. extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
  920. dma_addr_t *addrs, int max_pages);
  921. extern struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages);
  922. extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
  923. extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
  924. size_t align);
  925. extern void drm_pci_free(struct drm_device *dev, struct drm_dma_handle * dmah);
  926. /* sysfs support (drm_sysfs.c) */
  927. extern void drm_sysfs_hotplug_event(struct drm_device *dev);
  928. struct drm_device *drm_dev_alloc(struct drm_driver *driver,
  929. struct device *parent);
  930. void drm_dev_ref(struct drm_device *dev);
  931. void drm_dev_unref(struct drm_device *dev);
  932. int drm_dev_register(struct drm_device *dev, unsigned long flags);
  933. void drm_dev_unregister(struct drm_device *dev);
  934. int drm_dev_set_unique(struct drm_device *dev, const char *name);
  935. struct drm_minor *drm_minor_acquire(unsigned int minor_id);
  936. void drm_minor_release(struct drm_minor *minor);
  937. /*@}*/
  938. /* PCI section */
  939. static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
  940. {
  941. if (dev->driver->device_is_agp != NULL) {
  942. int err = (*dev->driver->device_is_agp) (dev);
  943. if (err != 2) {
  944. return err;
  945. }
  946. }
  947. return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
  948. }
  949. void drm_pci_agp_destroy(struct drm_device *dev);
  950. extern int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver);
  951. extern void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver);
  952. #ifdef CONFIG_PCI
  953. extern int drm_get_pci_dev(struct pci_dev *pdev,
  954. const struct pci_device_id *ent,
  955. struct drm_driver *driver);
  956. extern int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master);
  957. #else
  958. static inline int drm_get_pci_dev(struct pci_dev *pdev,
  959. const struct pci_device_id *ent,
  960. struct drm_driver *driver)
  961. {
  962. return -ENOSYS;
  963. }
  964. static inline int drm_pci_set_busid(struct drm_device *dev,
  965. struct drm_master *master)
  966. {
  967. return -ENOSYS;
  968. }
  969. #endif
  970. #define DRM_PCIE_SPEED_25 1
  971. #define DRM_PCIE_SPEED_50 2
  972. #define DRM_PCIE_SPEED_80 4
  973. extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
  974. extern int drm_pcie_get_max_link_width(struct drm_device *dev, u32 *mlw);
  975. /* platform section */
  976. extern int drm_platform_init(struct drm_driver *driver, struct platform_device *platform_device);
  977. extern int drm_platform_set_busid(struct drm_device *d, struct drm_master *m);
  978. /* returns true if currently okay to sleep */
  979. static __inline__ bool drm_can_sleep(void)
  980. {
  981. if (in_atomic() || in_dbg_master() || irqs_disabled())
  982. return false;
  983. return true;
  984. }
  985. /* helper for handling conditionals in various for_each macros */
  986. #define for_each_if(condition) if (!(condition)) {} else
  987. #endif