drm_file.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  3. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  4. * Copyright (c) 2009-2010, Code Aurora Forum.
  5. * All rights reserved.
  6. *
  7. * Author: Rickard E. (Rik) Faith <faith@valinux.com>
  8. * Author: Gareth Hughes <gareth@valinux.com>
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a
  11. * copy of this software and associated documentation files (the "Software"),
  12. * to deal in the Software without restriction, including without limitation
  13. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  14. * and/or sell copies of the Software, and to permit persons to whom the
  15. * Software is furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice (including the next
  18. * paragraph) shall be included in all copies or substantial portions of the
  19. * Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  24. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  25. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  26. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. * OTHER DEALINGS IN THE SOFTWARE.
  28. */
  29. #ifndef _DRM_FILE_H_
  30. #define _DRM_FILE_H_
  31. #include <linux/types.h>
  32. #include <linux/completion.h>
  33. #include <linux/idr.h>
  34. #include <uapi/drm/drm.h>
  35. #include <drm/drm_prime.h>
  36. struct dma_fence;
  37. struct drm_file;
  38. struct drm_device;
  39. struct drm_printer;
  40. struct device;
  41. struct file;
  42. extern struct xarray drm_minors_xa;
  43. /*
  44. * FIXME: Not sure we want to have drm_minor here in the end, but to avoid
  45. * header include loops we need it here for now.
  46. */
  47. /* Note that the values of this enum are ABI (it determines
  48. * /dev/dri/renderD* numbers).
  49. *
  50. * Setting DRM_MINOR_ACCEL to 32 gives enough space for more drm minors to
  51. * be implemented before we hit any future
  52. */
  53. enum drm_minor_type {
  54. DRM_MINOR_PRIMARY = 0,
  55. DRM_MINOR_CONTROL = 1,
  56. DRM_MINOR_RENDER = 2,
  57. DRM_MINOR_ACCEL = 32,
  58. };
  59. /**
  60. * struct drm_minor - DRM device minor structure
  61. *
  62. * This structure represents a DRM minor number for device nodes in /dev.
  63. * Entirely opaque to drivers and should never be inspected directly by drivers.
  64. * Drivers instead should only interact with &struct drm_file and of course
  65. * &struct drm_device, which is also where driver-private data and resources can
  66. * be attached to.
  67. */
  68. struct drm_minor {
  69. /* private: */
  70. int index; /* Minor device number */
  71. int type; /* Control or render or accel */
  72. struct device *kdev; /* Linux device */
  73. struct drm_device *dev;
  74. struct dentry *debugfs_symlink;
  75. struct dentry *debugfs_root;
  76. };
  77. /**
  78. * struct drm_pending_event - Event queued up for userspace to read
  79. *
  80. * This represents a DRM event. Drivers can use this as a generic completion
  81. * mechanism, which supports kernel-internal &struct completion, &struct dma_fence
  82. * and also the DRM-specific &struct drm_event delivery mechanism.
  83. */
  84. struct drm_pending_event {
  85. /**
  86. * @completion:
  87. *
  88. * Optional pointer to a kernel internal completion signalled when
  89. * drm_send_event() is called, useful to internally synchronize with
  90. * nonblocking operations.
  91. */
  92. struct completion *completion;
  93. /**
  94. * @completion_release:
  95. *
  96. * Optional callback currently only used by the atomic modeset helpers
  97. * to clean up the reference count for the structure @completion is
  98. * stored in.
  99. */
  100. void (*completion_release)(struct completion *completion);
  101. /**
  102. * @event:
  103. *
  104. * Pointer to the actual event that should be sent to userspace to be
  105. * read using drm_read(). Can be optional, since nowadays events are
  106. * also used to signal kernel internal threads with @completion or DMA
  107. * transactions using @fence.
  108. */
  109. struct drm_event *event;
  110. /**
  111. * @fence:
  112. *
  113. * Optional DMA fence to unblock other hardware transactions which
  114. * depend upon the nonblocking DRM operation this event represents.
  115. */
  116. struct dma_fence *fence;
  117. /**
  118. * @file_priv:
  119. *
  120. * &struct drm_file where @event should be delivered to. Only set when
  121. * @event is set.
  122. */
  123. struct drm_file *file_priv;
  124. /**
  125. * @link:
  126. *
  127. * Double-linked list to keep track of this event. Can be used by the
  128. * driver up to the point when it calls drm_send_event(), after that
  129. * this list entry is owned by the core for its own book-keeping.
  130. */
  131. struct list_head link;
  132. /**
  133. * @pending_link:
  134. *
  135. * Entry on &drm_file.pending_event_list, to keep track of all pending
  136. * events for @file_priv, to allow correct unwinding of them when
  137. * userspace closes the file before the event is delivered.
  138. */
  139. struct list_head pending_link;
  140. };
  141. /**
  142. * struct drm_file - DRM file private data
  143. *
  144. * This structure tracks DRM state per open file descriptor.
  145. */
  146. struct drm_file {
  147. /**
  148. * @authenticated:
  149. *
  150. * Whether the client is allowed to submit rendering, which for legacy
  151. * nodes means it must be authenticated.
  152. *
  153. * See also the :ref:`section on primary nodes and authentication
  154. * <drm_primary_node>`.
  155. */
  156. bool authenticated;
  157. /**
  158. * @stereo_allowed:
  159. *
  160. * True when the client has asked us to expose stereo 3D mode flags.
  161. */
  162. bool stereo_allowed;
  163. /**
  164. * @universal_planes:
  165. *
  166. * True if client understands CRTC primary planes and cursor planes
  167. * in the plane list. Automatically set when @atomic is set.
  168. */
  169. bool universal_planes;
  170. /** @atomic: True if client understands atomic properties. */
  171. bool atomic;
  172. /**
  173. * @aspect_ratio_allowed:
  174. *
  175. * True, if client can handle picture aspect ratios, and has requested
  176. * to pass this information along with the mode.
  177. */
  178. bool aspect_ratio_allowed;
  179. /**
  180. * @writeback_connectors:
  181. *
  182. * True if client understands writeback connectors
  183. */
  184. bool writeback_connectors;
  185. /**
  186. * @was_master:
  187. *
  188. * This client has or had, master capability. Protected by struct
  189. * &drm_device.master_mutex.
  190. *
  191. * This is used to ensure that CAP_SYS_ADMIN is not enforced, if the
  192. * client is or was master in the past.
  193. */
  194. bool was_master;
  195. /**
  196. * @is_master:
  197. *
  198. * This client is the creator of @master. Protected by struct
  199. * &drm_device.master_mutex.
  200. *
  201. * See also the :ref:`section on primary nodes and authentication
  202. * <drm_primary_node>`.
  203. */
  204. bool is_master;
  205. /**
  206. * @supports_virtualized_cursor_plane:
  207. *
  208. * This client is capable of handling the cursor plane with the
  209. * restrictions imposed on it by the virtualized drivers.
  210. *
  211. * This implies that the cursor plane has to behave like a cursor
  212. * i.e. track cursor movement. It also requires setting of the
  213. * hotspot properties by the client on the cursor plane.
  214. */
  215. bool supports_virtualized_cursor_plane;
  216. /**
  217. * @master:
  218. *
  219. * Master this node is currently associated with. Protected by struct
  220. * &drm_device.master_mutex, and serialized by @master_lookup_lock.
  221. *
  222. * Only relevant if drm_is_primary_client() returns true. Note that
  223. * this only matches &drm_device.master if the master is the currently
  224. * active one.
  225. *
  226. * To update @master, both &drm_device.master_mutex and
  227. * @master_lookup_lock need to be held, therefore holding either of
  228. * them is safe and enough for the read side.
  229. *
  230. * When dereferencing this pointer, either hold struct
  231. * &drm_device.master_mutex for the duration of the pointer's use, or
  232. * use drm_file_get_master() if struct &drm_device.master_mutex is not
  233. * currently held and there is no other need to hold it. This prevents
  234. * @master from being freed during use.
  235. *
  236. * See also @authentication and @is_master and the :ref:`section on
  237. * primary nodes and authentication <drm_primary_node>`.
  238. */
  239. struct drm_master *master;
  240. /** @master_lookup_lock: Serializes @master. */
  241. spinlock_t master_lookup_lock;
  242. /**
  243. * @pid: Process that is using this file.
  244. *
  245. * Must only be dereferenced under a rcu_read_lock or equivalent.
  246. *
  247. * Updates are guarded with dev->filelist_mutex and reference must be
  248. * dropped after a RCU grace period to accommodate lockless readers.
  249. */
  250. struct pid __rcu *pid;
  251. /** @client_id: A unique id for fdinfo */
  252. u64 client_id;
  253. /** @magic: Authentication magic, see @authenticated. */
  254. drm_magic_t magic;
  255. /**
  256. * @lhead:
  257. *
  258. * List of all open files of a DRM device, linked into
  259. * &drm_device.filelist. Protected by &drm_device.filelist_mutex.
  260. */
  261. struct list_head lhead;
  262. /** @minor: &struct drm_minor for this file. */
  263. struct drm_minor *minor;
  264. /**
  265. * @object_idr:
  266. *
  267. * Mapping of mm object handles to object pointers. Used by the GEM
  268. * subsystem. Protected by @table_lock.
  269. *
  270. * Note that allocated entries might be NULL as a transient state when
  271. * creating or deleting a handle.
  272. */
  273. struct idr object_idr;
  274. /** @table_lock: Protects @object_idr. */
  275. spinlock_t table_lock;
  276. /** @syncobj_idr: Mapping of sync object handles to object pointers. */
  277. struct idr syncobj_idr;
  278. /** @syncobj_table_lock: Protects @syncobj_idr. */
  279. spinlock_t syncobj_table_lock;
  280. /** @filp: Pointer to the core file structure. */
  281. struct file *filp;
  282. /**
  283. * @driver_priv:
  284. *
  285. * Optional pointer for driver private data. Can be allocated in
  286. * &drm_driver.open and should be freed in &drm_driver.postclose.
  287. */
  288. void *driver_priv;
  289. /**
  290. * @fbs:
  291. *
  292. * List of &struct drm_framebuffer associated with this file, using the
  293. * &drm_framebuffer.filp_head entry.
  294. *
  295. * Protected by @fbs_lock. Note that the @fbs list holds a reference on
  296. * the framebuffer object to prevent it from untimely disappearing.
  297. */
  298. struct list_head fbs;
  299. /** @fbs_lock: Protects @fbs. */
  300. struct mutex fbs_lock;
  301. /**
  302. * @blobs:
  303. *
  304. * User-created blob properties; this retains a reference on the
  305. * property.
  306. *
  307. * Protected by @drm_mode_config.blob_lock;
  308. */
  309. struct list_head blobs;
  310. /** @event_wait: Waitqueue for new events added to @event_list. */
  311. wait_queue_head_t event_wait;
  312. /**
  313. * @pending_event_list:
  314. *
  315. * List of pending &struct drm_pending_event, used to clean up pending
  316. * events in case this file gets closed before the event is signalled.
  317. * Uses the &drm_pending_event.pending_link entry.
  318. *
  319. * Protect by &drm_device.event_lock.
  320. */
  321. struct list_head pending_event_list;
  322. /**
  323. * @event_list:
  324. *
  325. * List of &struct drm_pending_event, ready for delivery to userspace
  326. * through drm_read(). Uses the &drm_pending_event.link entry.
  327. *
  328. * Protect by &drm_device.event_lock.
  329. */
  330. struct list_head event_list;
  331. /**
  332. * @event_space:
  333. *
  334. * Available event space to prevent userspace from
  335. * exhausting kernel memory. Currently limited to the fairly arbitrary
  336. * value of 4KB.
  337. */
  338. int event_space;
  339. /** @event_read_lock: Serializes drm_read(). */
  340. struct mutex event_read_lock;
  341. /**
  342. * @prime:
  343. *
  344. * Per-file buffer caches used by the PRIME buffer sharing code.
  345. */
  346. struct drm_prime_file_private prime;
  347. };
  348. /**
  349. * drm_is_primary_client - is this an open file of the primary node
  350. * @file_priv: DRM file
  351. *
  352. * Returns true if this is an open file of the primary node, i.e.
  353. * &drm_file.minor of @file_priv is a primary minor.
  354. *
  355. * See also the :ref:`section on primary nodes and authentication
  356. * <drm_primary_node>`.
  357. */
  358. static inline bool drm_is_primary_client(const struct drm_file *file_priv)
  359. {
  360. return file_priv->minor->type == DRM_MINOR_PRIMARY;
  361. }
  362. /**
  363. * drm_is_render_client - is this an open file of the render node
  364. * @file_priv: DRM file
  365. *
  366. * Returns true if this is an open file of the render node, i.e.
  367. * &drm_file.minor of @file_priv is a render minor.
  368. *
  369. * See also the :ref:`section on render nodes <drm_render_node>`.
  370. */
  371. static inline bool drm_is_render_client(const struct drm_file *file_priv)
  372. {
  373. return file_priv->minor->type == DRM_MINOR_RENDER;
  374. }
  375. /**
  376. * drm_is_accel_client - is this an open file of the compute acceleration node
  377. * @file_priv: DRM file
  378. *
  379. * Returns true if this is an open file of the compute acceleration node, i.e.
  380. * &drm_file.minor of @file_priv is a accel minor.
  381. *
  382. * See also :doc:`Introduction to compute accelerators subsystem
  383. * </accel/introduction>`.
  384. */
  385. static inline bool drm_is_accel_client(const struct drm_file *file_priv)
  386. {
  387. return file_priv->minor->type == DRM_MINOR_ACCEL;
  388. }
  389. void drm_file_update_pid(struct drm_file *);
  390. struct drm_minor *drm_minor_acquire(struct xarray *minors_xa, unsigned int minor_id);
  391. void drm_minor_release(struct drm_minor *minor);
  392. int drm_open(struct inode *inode, struct file *filp);
  393. int drm_open_helper(struct file *filp, struct drm_minor *minor);
  394. ssize_t drm_read(struct file *filp, char __user *buffer,
  395. size_t count, loff_t *offset);
  396. int drm_release(struct inode *inode, struct file *filp);
  397. int drm_release_noglobal(struct inode *inode, struct file *filp);
  398. __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait);
  399. int drm_event_reserve_init_locked(struct drm_device *dev,
  400. struct drm_file *file_priv,
  401. struct drm_pending_event *p,
  402. struct drm_event *e);
  403. int drm_event_reserve_init(struct drm_device *dev,
  404. struct drm_file *file_priv,
  405. struct drm_pending_event *p,
  406. struct drm_event *e);
  407. void drm_event_cancel_free(struct drm_device *dev,
  408. struct drm_pending_event *p);
  409. void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e);
  410. void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
  411. void drm_send_event_timestamp_locked(struct drm_device *dev,
  412. struct drm_pending_event *e,
  413. ktime_t timestamp);
  414. /**
  415. * struct drm_memory_stats - GEM object stats associated
  416. * @shared: Total size of GEM objects shared between processes
  417. * @private: Total size of GEM objects
  418. * @resident: Total size of GEM objects backing pages
  419. * @purgeable: Total size of GEM objects that can be purged (resident and not active)
  420. * @active: Total size of GEM objects active on one or more engines
  421. *
  422. * Used by drm_print_memory_stats()
  423. */
  424. struct drm_memory_stats {
  425. u64 shared;
  426. u64 private;
  427. u64 resident;
  428. u64 purgeable;
  429. u64 active;
  430. };
  431. enum drm_gem_object_status;
  432. void drm_print_memory_stats(struct drm_printer *p,
  433. const struct drm_memory_stats *stats,
  434. enum drm_gem_object_status supported_status,
  435. const char *region);
  436. void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file);
  437. void drm_show_fdinfo(struct seq_file *m, struct file *f);
  438. struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags);
  439. #endif /* _DRM_FILE_H_ */