drm_atomic.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Copyright (C) 2014 Red Hat
  3. * Copyright (C) 2014 Intel Corp.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Rob Clark <robdclark@gmail.com>
  25. * Daniel Vetter <daniel.vetter@ffwll.ch>
  26. */
  27. #ifndef DRM_ATOMIC_H_
  28. #define DRM_ATOMIC_H_
  29. #include <drm/drm_crtc.h>
  30. /**
  31. * struct drm_crtc_commit - track modeset commits on a CRTC
  32. *
  33. * This structure is used to track pending modeset changes and atomic commit on
  34. * a per-CRTC basis. Since updating the list should never block this structure
  35. * is reference counted to allow waiters to safely wait on an event to complete,
  36. * without holding any locks.
  37. *
  38. * It has 3 different events in total to allow a fine-grained synchronization
  39. * between outstanding updates::
  40. *
  41. * atomic commit thread hardware
  42. *
  43. * write new state into hardware ----> ...
  44. * signal hw_done
  45. * switch to new state on next
  46. * ... v/hblank
  47. *
  48. * wait for buffers to show up ...
  49. *
  50. * ... send completion irq
  51. * irq handler signals flip_done
  52. * cleanup old buffers
  53. *
  54. * signal cleanup_done
  55. *
  56. * wait for flip_done <----
  57. * clean up atomic state
  58. *
  59. * The important bit to know is that cleanup_done is the terminal event, but the
  60. * ordering between flip_done and hw_done is entirely up to the specific driver
  61. * and modeset state change.
  62. *
  63. * For an implementation of how to use this look at
  64. * drm_atomic_helper_setup_commit() from the atomic helper library.
  65. */
  66. struct drm_crtc_commit {
  67. /**
  68. * @crtc:
  69. *
  70. * DRM CRTC for this commit.
  71. */
  72. struct drm_crtc *crtc;
  73. /**
  74. * @ref:
  75. *
  76. * Reference count for this structure. Needed to allow blocking on
  77. * completions without the risk of the completion disappearing
  78. * meanwhile.
  79. */
  80. struct kref ref;
  81. /**
  82. * @flip_done:
  83. *
  84. * Will be signaled when the hardware has flipped to the new set of
  85. * buffers. Signals at the same time as when the drm event for this
  86. * commit is sent to userspace, or when an out-fence is singalled. Note
  87. * that for most hardware, in most cases this happens after @hw_done is
  88. * signalled.
  89. */
  90. struct completion flip_done;
  91. /**
  92. * @hw_done:
  93. *
  94. * Will be signalled when all hw register changes for this commit have
  95. * been written out. Especially when disabling a pipe this can be much
  96. * later than than @flip_done, since that can signal already when the
  97. * screen goes black, whereas to fully shut down a pipe more register
  98. * I/O is required.
  99. *
  100. * Note that this does not need to include separately reference-counted
  101. * resources like backing storage buffer pinning, or runtime pm
  102. * management.
  103. */
  104. struct completion hw_done;
  105. /**
  106. * @cleanup_done:
  107. *
  108. * Will be signalled after old buffers have been cleaned up by calling
  109. * drm_atomic_helper_cleanup_planes(). Since this can only happen after
  110. * a vblank wait completed it might be a bit later. This completion is
  111. * useful to throttle updates and avoid hardware updates getting ahead
  112. * of the buffer cleanup too much.
  113. */
  114. struct completion cleanup_done;
  115. /**
  116. * @commit_entry:
  117. *
  118. * Entry on the per-CRTC commit_list. Protected by crtc->commit_lock.
  119. */
  120. struct list_head commit_entry;
  121. /**
  122. * @event:
  123. *
  124. * &drm_pending_vblank_event pointer to clean up private events.
  125. */
  126. struct drm_pending_vblank_event *event;
  127. };
  128. struct __drm_planes_state {
  129. struct drm_plane *ptr;
  130. struct drm_plane_state *state;
  131. };
  132. struct __drm_crtcs_state {
  133. struct drm_crtc *ptr;
  134. struct drm_crtc_state *state;
  135. struct drm_crtc_commit *commit;
  136. s64 __user *out_fence_ptr;
  137. unsigned last_vblank_count;
  138. };
  139. struct __drm_connnectors_state {
  140. struct drm_connector *ptr;
  141. struct drm_connector_state *state;
  142. };
  143. /**
  144. * struct drm_atomic_state - the global state object for atomic updates
  145. * @ref: count of all references to this state (will not be freed until zero)
  146. * @dev: parent DRM device
  147. * @allow_modeset: allow full modeset
  148. * @legacy_cursor_update: hint to enforce legacy cursor IOCTL semantics
  149. * @legacy_set_config: Disable conflicting encoders instead of failing with -EINVAL.
  150. * @planes: pointer to array of structures with per-plane data
  151. * @crtcs: pointer to array of CRTC pointers
  152. * @num_connector: size of the @connectors and @connector_states arrays
  153. * @connectors: pointer to array of structures with per-connector data
  154. * @acquire_ctx: acquire context for this atomic modeset state update
  155. */
  156. struct drm_atomic_state {
  157. struct kref ref;
  158. struct drm_device *dev;
  159. bool allow_modeset : 1;
  160. bool legacy_cursor_update : 1;
  161. bool legacy_set_config : 1;
  162. struct __drm_planes_state *planes;
  163. struct __drm_crtcs_state *crtcs;
  164. int num_connector;
  165. struct __drm_connnectors_state *connectors;
  166. struct drm_modeset_acquire_ctx *acquire_ctx;
  167. /**
  168. * @commit_work:
  169. *
  170. * Work item which can be used by the driver or helpers to execute the
  171. * commit without blocking.
  172. */
  173. struct work_struct commit_work;
  174. };
  175. void __drm_crtc_commit_free(struct kref *kref);
  176. /**
  177. * drm_crtc_commit_get - acquire a reference to the CRTC commit
  178. * @commit: CRTC commit
  179. *
  180. * Increases the reference of @commit.
  181. */
  182. static inline void drm_crtc_commit_get(struct drm_crtc_commit *commit)
  183. {
  184. kref_get(&commit->ref);
  185. }
  186. /**
  187. * drm_crtc_commit_put - release a reference to the CRTC commmit
  188. * @commit: CRTC commit
  189. *
  190. * This releases a reference to @commit which is freed after removing the
  191. * final reference. No locking required and callable from any context.
  192. */
  193. static inline void drm_crtc_commit_put(struct drm_crtc_commit *commit)
  194. {
  195. kref_put(&commit->ref, __drm_crtc_commit_free);
  196. }
  197. struct drm_atomic_state * __must_check
  198. drm_atomic_state_alloc(struct drm_device *dev);
  199. void drm_atomic_state_clear(struct drm_atomic_state *state);
  200. /**
  201. * drm_atomic_state_get - acquire a reference to the atomic state
  202. * @state: The atomic state
  203. *
  204. * Returns a new reference to the @state
  205. */
  206. static inline struct drm_atomic_state *
  207. drm_atomic_state_get(struct drm_atomic_state *state)
  208. {
  209. kref_get(&state->ref);
  210. return state;
  211. }
  212. void __drm_atomic_state_free(struct kref *ref);
  213. /**
  214. * drm_atomic_state_put - release a reference to the atomic state
  215. * @state: The atomic state
  216. *
  217. * This releases a reference to @state which is freed after removing the
  218. * final reference. No locking required and callable from any context.
  219. */
  220. static inline void drm_atomic_state_put(struct drm_atomic_state *state)
  221. {
  222. kref_put(&state->ref, __drm_atomic_state_free);
  223. }
  224. int __must_check
  225. drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state);
  226. void drm_atomic_state_default_clear(struct drm_atomic_state *state);
  227. void drm_atomic_state_default_release(struct drm_atomic_state *state);
  228. struct drm_crtc_state * __must_check
  229. drm_atomic_get_crtc_state(struct drm_atomic_state *state,
  230. struct drm_crtc *crtc);
  231. int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
  232. struct drm_crtc_state *state, struct drm_property *property,
  233. uint64_t val);
  234. struct drm_plane_state * __must_check
  235. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  236. struct drm_plane *plane);
  237. int drm_atomic_plane_set_property(struct drm_plane *plane,
  238. struct drm_plane_state *state, struct drm_property *property,
  239. uint64_t val);
  240. struct drm_connector_state * __must_check
  241. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  242. struct drm_connector *connector);
  243. int drm_atomic_connector_set_property(struct drm_connector *connector,
  244. struct drm_connector_state *state, struct drm_property *property,
  245. uint64_t val);
  246. /**
  247. * drm_atomic_get_existing_crtc_state - get crtc state, if it exists
  248. * @state: global atomic state object
  249. * @crtc: crtc to grab
  250. *
  251. * This function returns the crtc state for the given crtc, or NULL
  252. * if the crtc is not part of the global atomic state.
  253. */
  254. static inline struct drm_crtc_state *
  255. drm_atomic_get_existing_crtc_state(struct drm_atomic_state *state,
  256. struct drm_crtc *crtc)
  257. {
  258. return state->crtcs[drm_crtc_index(crtc)].state;
  259. }
  260. /**
  261. * drm_atomic_get_existing_plane_state - get plane state, if it exists
  262. * @state: global atomic state object
  263. * @plane: plane to grab
  264. *
  265. * This function returns the plane state for the given plane, or NULL
  266. * if the plane is not part of the global atomic state.
  267. */
  268. static inline struct drm_plane_state *
  269. drm_atomic_get_existing_plane_state(struct drm_atomic_state *state,
  270. struct drm_plane *plane)
  271. {
  272. return state->planes[drm_plane_index(plane)].state;
  273. }
  274. /**
  275. * drm_atomic_get_existing_connector_state - get connector state, if it exists
  276. * @state: global atomic state object
  277. * @connector: connector to grab
  278. *
  279. * This function returns the connector state for the given connector,
  280. * or NULL if the connector is not part of the global atomic state.
  281. */
  282. static inline struct drm_connector_state *
  283. drm_atomic_get_existing_connector_state(struct drm_atomic_state *state,
  284. struct drm_connector *connector)
  285. {
  286. int index = drm_connector_index(connector);
  287. if (index >= state->num_connector)
  288. return NULL;
  289. return state->connectors[index].state;
  290. }
  291. /**
  292. * __drm_atomic_get_current_plane_state - get current plane state
  293. * @state: global atomic state object
  294. * @plane: plane to grab
  295. *
  296. * This function returns the plane state for the given plane, either from
  297. * @state, or if the plane isn't part of the atomic state update, from @plane.
  298. * This is useful in atomic check callbacks, when drivers need to peek at, but
  299. * not change, state of other planes, since it avoids threading an error code
  300. * back up the call chain.
  301. *
  302. * WARNING:
  303. *
  304. * Note that this function is in general unsafe since it doesn't check for the
  305. * required locking for access state structures. Drivers must ensure that it is
  306. * safe to access the returned state structure through other means. One common
  307. * example is when planes are fixed to a single CRTC, and the driver knows that
  308. * the CRTC lock is held already. In that case holding the CRTC lock gives a
  309. * read-lock on all planes connected to that CRTC. But if planes can be
  310. * reassigned things get more tricky. In that case it's better to use
  311. * drm_atomic_get_plane_state and wire up full error handling.
  312. *
  313. * Returns:
  314. *
  315. * Read-only pointer to the current plane state.
  316. */
  317. static inline const struct drm_plane_state *
  318. __drm_atomic_get_current_plane_state(struct drm_atomic_state *state,
  319. struct drm_plane *plane)
  320. {
  321. if (state->planes[drm_plane_index(plane)].state)
  322. return state->planes[drm_plane_index(plane)].state;
  323. return plane->state;
  324. }
  325. int __must_check
  326. drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
  327. struct drm_display_mode *mode);
  328. int __must_check
  329. drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
  330. struct drm_property_blob *blob);
  331. int __must_check
  332. drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
  333. struct drm_crtc *crtc);
  334. void drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
  335. struct drm_framebuffer *fb);
  336. void drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
  337. struct dma_fence *fence);
  338. int __must_check
  339. drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
  340. struct drm_crtc *crtc);
  341. int __must_check
  342. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  343. struct drm_crtc *crtc);
  344. int __must_check
  345. drm_atomic_add_affected_planes(struct drm_atomic_state *state,
  346. struct drm_crtc *crtc);
  347. void drm_atomic_legacy_backoff(struct drm_atomic_state *state);
  348. void
  349. drm_atomic_clean_old_fb(struct drm_device *dev, unsigned plane_mask, int ret);
  350. int __must_check drm_atomic_check_only(struct drm_atomic_state *state);
  351. int __must_check drm_atomic_commit(struct drm_atomic_state *state);
  352. int __must_check drm_atomic_nonblocking_commit(struct drm_atomic_state *state);
  353. void drm_state_dump(struct drm_device *dev, struct drm_printer *p);
  354. #define for_each_connector_in_state(__state, connector, connector_state, __i) \
  355. for ((__i) = 0; \
  356. (__i) < (__state)->num_connector && \
  357. ((connector) = (__state)->connectors[__i].ptr, \
  358. (connector_state) = (__state)->connectors[__i].state, 1); \
  359. (__i)++) \
  360. for_each_if (connector)
  361. #define for_each_crtc_in_state(__state, crtc, crtc_state, __i) \
  362. for ((__i) = 0; \
  363. (__i) < (__state)->dev->mode_config.num_crtc && \
  364. ((crtc) = (__state)->crtcs[__i].ptr, \
  365. (crtc_state) = (__state)->crtcs[__i].state, 1); \
  366. (__i)++) \
  367. for_each_if (crtc_state)
  368. #define for_each_plane_in_state(__state, plane, plane_state, __i) \
  369. for ((__i) = 0; \
  370. (__i) < (__state)->dev->mode_config.num_total_plane && \
  371. ((plane) = (__state)->planes[__i].ptr, \
  372. (plane_state) = (__state)->planes[__i].state, 1); \
  373. (__i)++) \
  374. for_each_if (plane_state)
  375. /**
  376. * drm_atomic_crtc_needs_modeset - compute combined modeset need
  377. * @state: &drm_crtc_state for the CRTC
  378. *
  379. * To give drivers flexibility &struct drm_crtc_state has 3 booleans to track
  380. * whether the state CRTC changed enough to need a full modeset cycle:
  381. * connectors_changed, mode_changed and active_changed. This helper simply
  382. * combines these three to compute the overall need for a modeset for @state.
  383. *
  384. * The atomic helper code sets these booleans, but drivers can and should
  385. * change them appropriately to accurately represent whether a modeset is
  386. * really needed. In general, drivers should avoid full modesets whenever
  387. * possible.
  388. *
  389. * For example if the CRTC mode has changed, and the hardware is able to enact
  390. * the requested mode change without going through a full modeset, the driver
  391. * should clear mode_changed during its ->atomic_check.
  392. */
  393. static inline bool
  394. drm_atomic_crtc_needs_modeset(const struct drm_crtc_state *state)
  395. {
  396. return state->mode_changed || state->active_changed ||
  397. state->connectors_changed;
  398. }
  399. #endif /* DRM_ATOMIC_H_ */