drm_atomic.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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_put(struct drm_crtc_commit *commit);
  176. static inline void drm_crtc_commit_get(struct drm_crtc_commit *commit)
  177. {
  178. kref_get(&commit->ref);
  179. }
  180. struct drm_atomic_state * __must_check
  181. drm_atomic_state_alloc(struct drm_device *dev);
  182. void drm_atomic_state_clear(struct drm_atomic_state *state);
  183. /**
  184. * drm_atomic_state_get - acquire a reference to the atomic state
  185. * @state: The atomic state
  186. *
  187. * Returns a new reference to the @state
  188. */
  189. static inline struct drm_atomic_state *
  190. drm_atomic_state_get(struct drm_atomic_state *state)
  191. {
  192. kref_get(&state->ref);
  193. return state;
  194. }
  195. void __drm_atomic_state_free(struct kref *ref);
  196. /**
  197. * drm_atomic_state_put - release a reference to the atomic state
  198. * @state: The atomic state
  199. *
  200. * This releases a reference to @state which is freed after removing the
  201. * final reference. No locking required and callable from any context.
  202. */
  203. static inline void drm_atomic_state_put(struct drm_atomic_state *state)
  204. {
  205. kref_put(&state->ref, __drm_atomic_state_free);
  206. }
  207. int __must_check
  208. drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state);
  209. void drm_atomic_state_default_clear(struct drm_atomic_state *state);
  210. void drm_atomic_state_default_release(struct drm_atomic_state *state);
  211. struct drm_crtc_state * __must_check
  212. drm_atomic_get_crtc_state(struct drm_atomic_state *state,
  213. struct drm_crtc *crtc);
  214. int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
  215. struct drm_crtc_state *state, struct drm_property *property,
  216. uint64_t val);
  217. struct drm_plane_state * __must_check
  218. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  219. struct drm_plane *plane);
  220. int drm_atomic_plane_set_property(struct drm_plane *plane,
  221. struct drm_plane_state *state, struct drm_property *property,
  222. uint64_t val);
  223. struct drm_connector_state * __must_check
  224. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  225. struct drm_connector *connector);
  226. int drm_atomic_connector_set_property(struct drm_connector *connector,
  227. struct drm_connector_state *state, struct drm_property *property,
  228. uint64_t val);
  229. /**
  230. * drm_atomic_get_existing_crtc_state - get crtc state, if it exists
  231. * @state: global atomic state object
  232. * @crtc: crtc to grab
  233. *
  234. * This function returns the crtc state for the given crtc, or NULL
  235. * if the crtc is not part of the global atomic state.
  236. */
  237. static inline struct drm_crtc_state *
  238. drm_atomic_get_existing_crtc_state(struct drm_atomic_state *state,
  239. struct drm_crtc *crtc)
  240. {
  241. return state->crtcs[drm_crtc_index(crtc)].state;
  242. }
  243. /**
  244. * drm_atomic_get_existing_plane_state - get plane state, if it exists
  245. * @state: global atomic state object
  246. * @plane: plane to grab
  247. *
  248. * This function returns the plane state for the given plane, or NULL
  249. * if the plane is not part of the global atomic state.
  250. */
  251. static inline struct drm_plane_state *
  252. drm_atomic_get_existing_plane_state(struct drm_atomic_state *state,
  253. struct drm_plane *plane)
  254. {
  255. return state->planes[drm_plane_index(plane)].state;
  256. }
  257. /**
  258. * drm_atomic_get_existing_connector_state - get connector state, if it exists
  259. * @state: global atomic state object
  260. * @connector: connector to grab
  261. *
  262. * This function returns the connector state for the given connector,
  263. * or NULL if the connector is not part of the global atomic state.
  264. */
  265. static inline struct drm_connector_state *
  266. drm_atomic_get_existing_connector_state(struct drm_atomic_state *state,
  267. struct drm_connector *connector)
  268. {
  269. int index = drm_connector_index(connector);
  270. if (index >= state->num_connector)
  271. return NULL;
  272. return state->connectors[index].state;
  273. }
  274. /**
  275. * __drm_atomic_get_current_plane_state - get current plane state
  276. * @state: global atomic state object
  277. * @plane: plane to grab
  278. *
  279. * This function returns the plane state for the given plane, either from
  280. * @state, or if the plane isn't part of the atomic state update, from @plane.
  281. * This is useful in atomic check callbacks, when drivers need to peek at, but
  282. * not change, state of other planes, since it avoids threading an error code
  283. * back up the call chain.
  284. *
  285. * WARNING:
  286. *
  287. * Note that this function is in general unsafe since it doesn't check for the
  288. * required locking for access state structures. Drivers must ensure that it is
  289. * safe to access the returned state structure through other means. One common
  290. * example is when planes are fixed to a single CRTC, and the driver knows that
  291. * the CRTC lock is held already. In that case holding the CRTC lock gives a
  292. * read-lock on all planes connected to that CRTC. But if planes can be
  293. * reassigned things get more tricky. In that case it's better to use
  294. * drm_atomic_get_plane_state and wire up full error handling.
  295. *
  296. * Returns:
  297. *
  298. * Read-only pointer to the current plane state.
  299. */
  300. static inline const struct drm_plane_state *
  301. __drm_atomic_get_current_plane_state(struct drm_atomic_state *state,
  302. struct drm_plane *plane)
  303. {
  304. if (state->planes[drm_plane_index(plane)].state)
  305. return state->planes[drm_plane_index(plane)].state;
  306. return plane->state;
  307. }
  308. int __must_check
  309. drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
  310. struct drm_display_mode *mode);
  311. int __must_check
  312. drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
  313. struct drm_property_blob *blob);
  314. int __must_check
  315. drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
  316. struct drm_crtc *crtc);
  317. void drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
  318. struct drm_framebuffer *fb);
  319. void drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
  320. struct dma_fence *fence);
  321. int __must_check
  322. drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
  323. struct drm_crtc *crtc);
  324. int __must_check
  325. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  326. struct drm_crtc *crtc);
  327. int __must_check
  328. drm_atomic_add_affected_planes(struct drm_atomic_state *state,
  329. struct drm_crtc *crtc);
  330. void drm_atomic_legacy_backoff(struct drm_atomic_state *state);
  331. void
  332. drm_atomic_clean_old_fb(struct drm_device *dev, unsigned plane_mask, int ret);
  333. int __must_check drm_atomic_check_only(struct drm_atomic_state *state);
  334. int __must_check drm_atomic_commit(struct drm_atomic_state *state);
  335. int __must_check drm_atomic_nonblocking_commit(struct drm_atomic_state *state);
  336. void drm_state_dump(struct drm_device *dev, struct drm_printer *p);
  337. #define for_each_connector_in_state(__state, connector, connector_state, __i) \
  338. for ((__i) = 0; \
  339. (__i) < (__state)->num_connector && \
  340. ((connector) = (__state)->connectors[__i].ptr, \
  341. (connector_state) = (__state)->connectors[__i].state, 1); \
  342. (__i)++) \
  343. for_each_if (connector)
  344. #define for_each_crtc_in_state(__state, crtc, crtc_state, __i) \
  345. for ((__i) = 0; \
  346. (__i) < (__state)->dev->mode_config.num_crtc && \
  347. ((crtc) = (__state)->crtcs[__i].ptr, \
  348. (crtc_state) = (__state)->crtcs[__i].state, 1); \
  349. (__i)++) \
  350. for_each_if (crtc_state)
  351. #define for_each_plane_in_state(__state, plane, plane_state, __i) \
  352. for ((__i) = 0; \
  353. (__i) < (__state)->dev->mode_config.num_total_plane && \
  354. ((plane) = (__state)->planes[__i].ptr, \
  355. (plane_state) = (__state)->planes[__i].state, 1); \
  356. (__i)++) \
  357. for_each_if (plane_state)
  358. /**
  359. * drm_atomic_crtc_needs_modeset - compute combined modeset need
  360. * @state: &drm_crtc_state for the CRTC
  361. *
  362. * To give drivers flexibility &struct drm_crtc_state has 3 booleans to track
  363. * whether the state CRTC changed enough to need a full modeset cycle:
  364. * connectors_changed, mode_changed and active_changed. This helper simply
  365. * combines these three to compute the overall need for a modeset for @state.
  366. *
  367. * The atomic helper code sets these booleans, but drivers can and should
  368. * change them appropriately to accurately represent whether a modeset is
  369. * really needed. In general, drivers should avoid full modesets whenever
  370. * possible.
  371. *
  372. * For example if the CRTC mode has changed, and the hardware is able to enact
  373. * the requested mode change without going through a full modeset, the driver
  374. * should clear mode_changed during its ->atomic_check.
  375. */
  376. static inline bool
  377. drm_atomic_crtc_needs_modeset(const struct drm_crtc_state *state)
  378. {
  379. return state->mode_changed || state->active_changed ||
  380. state->connectors_changed;
  381. }
  382. #endif /* DRM_ATOMIC_H_ */