drm_atomic.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. };
  137. struct __drm_connnectors_state {
  138. struct drm_connector *ptr;
  139. struct drm_connector_state *state;
  140. };
  141. /**
  142. * struct drm_atomic_state - the global state object for atomic updates
  143. * @dev: parent DRM device
  144. * @allow_modeset: allow full modeset
  145. * @legacy_cursor_update: hint to enforce legacy cursor IOCTL semantics
  146. * @legacy_set_config: Disable conflicting encoders instead of failing with -EINVAL.
  147. * @planes: pointer to array of structures with per-plane data
  148. * @crtcs: pointer to array of CRTC pointers
  149. * @num_connector: size of the @connectors and @connector_states arrays
  150. * @connectors: pointer to array of structures with per-connector data
  151. * @acquire_ctx: acquire context for this atomic modeset state update
  152. */
  153. struct drm_atomic_state {
  154. struct drm_device *dev;
  155. bool allow_modeset : 1;
  156. bool legacy_cursor_update : 1;
  157. bool legacy_set_config : 1;
  158. struct __drm_planes_state *planes;
  159. struct __drm_crtcs_state *crtcs;
  160. int num_connector;
  161. struct __drm_connnectors_state *connectors;
  162. struct drm_modeset_acquire_ctx *acquire_ctx;
  163. /**
  164. * @commit_work:
  165. *
  166. * Work item which can be used by the driver or helpers to execute the
  167. * commit without blocking.
  168. */
  169. struct work_struct commit_work;
  170. };
  171. void drm_crtc_commit_put(struct drm_crtc_commit *commit);
  172. static inline void drm_crtc_commit_get(struct drm_crtc_commit *commit)
  173. {
  174. kref_get(&commit->ref);
  175. }
  176. struct drm_atomic_state * __must_check
  177. drm_atomic_state_alloc(struct drm_device *dev);
  178. void drm_atomic_state_clear(struct drm_atomic_state *state);
  179. void drm_atomic_state_free(struct drm_atomic_state *state);
  180. int __must_check
  181. drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state);
  182. void drm_atomic_state_default_clear(struct drm_atomic_state *state);
  183. void drm_atomic_state_default_release(struct drm_atomic_state *state);
  184. struct drm_crtc_state * __must_check
  185. drm_atomic_get_crtc_state(struct drm_atomic_state *state,
  186. struct drm_crtc *crtc);
  187. int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
  188. struct drm_crtc_state *state, struct drm_property *property,
  189. uint64_t val);
  190. struct drm_plane_state * __must_check
  191. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  192. struct drm_plane *plane);
  193. int drm_atomic_plane_set_property(struct drm_plane *plane,
  194. struct drm_plane_state *state, struct drm_property *property,
  195. uint64_t val);
  196. struct drm_connector_state * __must_check
  197. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  198. struct drm_connector *connector);
  199. int drm_atomic_connector_set_property(struct drm_connector *connector,
  200. struct drm_connector_state *state, struct drm_property *property,
  201. uint64_t val);
  202. /**
  203. * drm_atomic_get_existing_crtc_state - get crtc state, if it exists
  204. * @state: global atomic state object
  205. * @crtc: crtc to grab
  206. *
  207. * This function returns the crtc state for the given crtc, or NULL
  208. * if the crtc is not part of the global atomic state.
  209. */
  210. static inline struct drm_crtc_state *
  211. drm_atomic_get_existing_crtc_state(struct drm_atomic_state *state,
  212. struct drm_crtc *crtc)
  213. {
  214. return state->crtcs[drm_crtc_index(crtc)].state;
  215. }
  216. /**
  217. * drm_atomic_get_existing_plane_state - get plane state, if it exists
  218. * @state: global atomic state object
  219. * @plane: plane to grab
  220. *
  221. * This function returns the plane state for the given plane, or NULL
  222. * if the plane is not part of the global atomic state.
  223. */
  224. static inline struct drm_plane_state *
  225. drm_atomic_get_existing_plane_state(struct drm_atomic_state *state,
  226. struct drm_plane *plane)
  227. {
  228. return state->planes[drm_plane_index(plane)].state;
  229. }
  230. /**
  231. * drm_atomic_get_existing_connector_state - get connector state, if it exists
  232. * @state: global atomic state object
  233. * @connector: connector to grab
  234. *
  235. * This function returns the connector state for the given connector,
  236. * or NULL if the connector is not part of the global atomic state.
  237. */
  238. static inline struct drm_connector_state *
  239. drm_atomic_get_existing_connector_state(struct drm_atomic_state *state,
  240. struct drm_connector *connector)
  241. {
  242. int index = drm_connector_index(connector);
  243. if (index >= state->num_connector)
  244. return NULL;
  245. return state->connectors[index].state;
  246. }
  247. /**
  248. * __drm_atomic_get_current_plane_state - get current plane state
  249. * @state: global atomic state object
  250. * @plane: plane to grab
  251. *
  252. * This function returns the plane state for the given plane, either from
  253. * @state, or if the plane isn't part of the atomic state update, from @plane.
  254. * This is useful in atomic check callbacks, when drivers need to peek at, but
  255. * not change, state of other planes, since it avoids threading an error code
  256. * back up the call chain.
  257. *
  258. * WARNING:
  259. *
  260. * Note that this function is in general unsafe since it doesn't check for the
  261. * required locking for access state structures. Drivers must ensure that it is
  262. * safe to access the returned state structure through other means. One common
  263. * example is when planes are fixed to a single CRTC, and the driver knows that
  264. * the CRTC lock is held already. In that case holding the CRTC lock gives a
  265. * read-lock on all planes connected to that CRTC. But if planes can be
  266. * reassigned things get more tricky. In that case it's better to use
  267. * drm_atomic_get_plane_state and wire up full error handling.
  268. *
  269. * Returns:
  270. *
  271. * Read-only pointer to the current plane state.
  272. */
  273. static inline const struct drm_plane_state *
  274. __drm_atomic_get_current_plane_state(struct drm_atomic_state *state,
  275. struct drm_plane *plane)
  276. {
  277. if (state->planes[drm_plane_index(plane)].state)
  278. return state->planes[drm_plane_index(plane)].state;
  279. return plane->state;
  280. }
  281. int __must_check
  282. drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
  283. struct drm_display_mode *mode);
  284. int __must_check
  285. drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
  286. struct drm_property_blob *blob);
  287. int __must_check
  288. drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
  289. struct drm_crtc *crtc);
  290. void drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
  291. struct drm_framebuffer *fb);
  292. int __must_check
  293. drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
  294. struct drm_crtc *crtc);
  295. int __must_check
  296. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  297. struct drm_crtc *crtc);
  298. int __must_check
  299. drm_atomic_add_affected_planes(struct drm_atomic_state *state,
  300. struct drm_crtc *crtc);
  301. void drm_atomic_legacy_backoff(struct drm_atomic_state *state);
  302. void
  303. drm_atomic_clean_old_fb(struct drm_device *dev, unsigned plane_mask, int ret);
  304. int __must_check drm_atomic_check_only(struct drm_atomic_state *state);
  305. int __must_check drm_atomic_commit(struct drm_atomic_state *state);
  306. int __must_check drm_atomic_nonblocking_commit(struct drm_atomic_state *state);
  307. #define for_each_connector_in_state(__state, connector, connector_state, __i) \
  308. for ((__i) = 0; \
  309. (__i) < (__state)->num_connector && \
  310. ((connector) = (__state)->connectors[__i].ptr, \
  311. (connector_state) = (__state)->connectors[__i].state, 1); \
  312. (__i)++) \
  313. for_each_if (connector)
  314. #define for_each_crtc_in_state(__state, crtc, crtc_state, __i) \
  315. for ((__i) = 0; \
  316. (__i) < (__state)->dev->mode_config.num_crtc && \
  317. ((crtc) = (__state)->crtcs[__i].ptr, \
  318. (crtc_state) = (__state)->crtcs[__i].state, 1); \
  319. (__i)++) \
  320. for_each_if (crtc_state)
  321. #define for_each_plane_in_state(__state, plane, plane_state, __i) \
  322. for ((__i) = 0; \
  323. (__i) < (__state)->dev->mode_config.num_total_plane && \
  324. ((plane) = (__state)->planes[__i].ptr, \
  325. (plane_state) = (__state)->planes[__i].state, 1); \
  326. (__i)++) \
  327. for_each_if (plane_state)
  328. /**
  329. * drm_atomic_crtc_needs_modeset - compute combined modeset need
  330. * @state: &drm_crtc_state for the CRTC
  331. *
  332. * To give drivers flexibility struct &drm_crtc_state has 3 booleans to track
  333. * whether the state CRTC changed enough to need a full modeset cycle:
  334. * connectors_changed, mode_changed and active_change. This helper simply
  335. * combines these three to compute the overall need for a modeset for @state.
  336. */
  337. static inline bool
  338. drm_atomic_crtc_needs_modeset(struct drm_crtc_state *state)
  339. {
  340. return state->mode_changed || state->active_changed ||
  341. state->connectors_changed;
  342. }
  343. #endif /* DRM_ATOMIC_H_ */