drm_atomic.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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. #include <drm/drm_util.h>
  31. /**
  32. * struct drm_crtc_commit - track modeset commits on a CRTC
  33. *
  34. * This structure is used to track pending modeset changes and atomic commit on
  35. * a per-CRTC basis. Since updating the list should never block, this structure
  36. * is reference counted to allow waiters to safely wait on an event to complete,
  37. * without holding any locks.
  38. *
  39. * It has 3 different events in total to allow a fine-grained synchronization
  40. * between outstanding updates::
  41. *
  42. * atomic commit thread hardware
  43. *
  44. * write new state into hardware ----> ...
  45. * signal hw_done
  46. * switch to new state on next
  47. * ... v/hblank
  48. *
  49. * wait for buffers to show up ...
  50. *
  51. * ... send completion irq
  52. * irq handler signals flip_done
  53. * cleanup old buffers
  54. *
  55. * signal cleanup_done
  56. *
  57. * wait for flip_done <----
  58. * clean up atomic state
  59. *
  60. * The important bit to know is that &cleanup_done is the terminal event, but the
  61. * ordering between &flip_done and &hw_done is entirely up to the specific driver
  62. * and modeset state change.
  63. *
  64. * For an implementation of how to use this look at
  65. * drm_atomic_helper_setup_commit() from the atomic helper library.
  66. */
  67. struct drm_crtc_commit {
  68. /**
  69. * @crtc:
  70. *
  71. * DRM CRTC for this commit.
  72. */
  73. struct drm_crtc *crtc;
  74. /**
  75. * @ref:
  76. *
  77. * Reference count for this structure. Needed to allow blocking on
  78. * completions without the risk of the completion disappearing
  79. * meanwhile.
  80. */
  81. struct kref ref;
  82. /**
  83. * @flip_done:
  84. *
  85. * Will be signaled when the hardware has flipped to the new set of
  86. * buffers. Signals at the same time as when the drm event for this
  87. * commit is sent to userspace, or when an out-fence is singalled. Note
  88. * that for most hardware, in most cases this happens after @hw_done is
  89. * signalled.
  90. *
  91. * Completion of this stage is signalled implicitly by calling
  92. * drm_crtc_send_vblank_event() on &drm_crtc_state.event.
  93. */
  94. struct completion flip_done;
  95. /**
  96. * @hw_done:
  97. *
  98. * Will be signalled when all hw register changes for this commit have
  99. * been written out. Especially when disabling a pipe this can be much
  100. * later than @flip_done, since that can signal already when the
  101. * screen goes black, whereas to fully shut down a pipe more register
  102. * I/O is required.
  103. *
  104. * Note that this does not need to include separately reference-counted
  105. * resources like backing storage buffer pinning, or runtime pm
  106. * management.
  107. *
  108. * Drivers should call drm_atomic_helper_commit_hw_done() to signal
  109. * completion of this stage.
  110. */
  111. struct completion hw_done;
  112. /**
  113. * @cleanup_done:
  114. *
  115. * Will be signalled after old buffers have been cleaned up by calling
  116. * drm_atomic_helper_cleanup_planes(). Since this can only happen after
  117. * a vblank wait completed it might be a bit later. This completion is
  118. * useful to throttle updates and avoid hardware updates getting ahead
  119. * of the buffer cleanup too much.
  120. *
  121. * Drivers should call drm_atomic_helper_commit_cleanup_done() to signal
  122. * completion of this stage.
  123. */
  124. struct completion cleanup_done;
  125. /**
  126. * @commit_entry:
  127. *
  128. * Entry on the per-CRTC &drm_crtc.commit_list. Protected by
  129. * $drm_crtc.commit_lock.
  130. */
  131. struct list_head commit_entry;
  132. /**
  133. * @event:
  134. *
  135. * &drm_pending_vblank_event pointer to clean up private events.
  136. */
  137. struct drm_pending_vblank_event *event;
  138. /**
  139. * @abort_completion:
  140. *
  141. * A flag that's set after drm_atomic_helper_setup_commit() takes a
  142. * second reference for the completion of $drm_crtc_state.event. It's
  143. * used by the free code to remove the second reference if commit fails.
  144. */
  145. bool abort_completion;
  146. };
  147. struct __drm_planes_state {
  148. struct drm_plane *ptr;
  149. struct drm_plane_state *state, *old_state, *new_state;
  150. };
  151. struct __drm_crtcs_state {
  152. struct drm_crtc *ptr;
  153. struct drm_crtc_state *state, *old_state, *new_state;
  154. /**
  155. * @commit:
  156. *
  157. * A reference to the CRTC commit object that is kept for use by
  158. * drm_atomic_helper_wait_for_flip_done() after
  159. * drm_atomic_helper_commit_hw_done() is called. This ensures that a
  160. * concurrent commit won't free a commit object that is still in use.
  161. */
  162. struct drm_crtc_commit *commit;
  163. s32 __user *out_fence_ptr;
  164. u64 last_vblank_count;
  165. };
  166. struct __drm_connnectors_state {
  167. struct drm_connector *ptr;
  168. struct drm_connector_state *state, *old_state, *new_state;
  169. /**
  170. * @out_fence_ptr:
  171. *
  172. * User-provided pointer which the kernel uses to return a sync_file
  173. * file descriptor. Used by writeback connectors to signal completion of
  174. * the writeback.
  175. */
  176. s32 __user *out_fence_ptr;
  177. };
  178. struct drm_private_obj;
  179. struct drm_private_state;
  180. /**
  181. * struct drm_private_state_funcs - atomic state functions for private objects
  182. *
  183. * These hooks are used by atomic helpers to create, swap and destroy states of
  184. * private objects. The structure itself is used as a vtable to identify the
  185. * associated private object type. Each private object type that needs to be
  186. * added to the atomic states is expected to have an implementation of these
  187. * hooks and pass a pointer to its drm_private_state_funcs struct to
  188. * drm_atomic_get_private_obj_state().
  189. */
  190. struct drm_private_state_funcs {
  191. /**
  192. * @atomic_duplicate_state:
  193. *
  194. * Duplicate the current state of the private object and return it. It
  195. * is an error to call this before obj->state has been initialized.
  196. *
  197. * RETURNS:
  198. *
  199. * Duplicated atomic state or NULL when obj->state is not
  200. * initialized or allocation failed.
  201. */
  202. struct drm_private_state *(*atomic_duplicate_state)(struct drm_private_obj *obj);
  203. /**
  204. * @atomic_destroy_state:
  205. *
  206. * Frees the private object state created with @atomic_duplicate_state.
  207. */
  208. void (*atomic_destroy_state)(struct drm_private_obj *obj,
  209. struct drm_private_state *state);
  210. };
  211. /**
  212. * struct drm_private_obj - base struct for driver private atomic object
  213. *
  214. * A driver private object is initialized by calling
  215. * drm_atomic_private_obj_init() and cleaned up by calling
  216. * drm_atomic_private_obj_fini().
  217. *
  218. * Currently only tracks the state update functions and the opaque driver
  219. * private state itself, but in the future might also track which
  220. * &drm_modeset_lock is required to duplicate and update this object's state.
  221. *
  222. * All private objects must be initialized before the DRM device they are
  223. * attached to is registered to the DRM subsystem (call to drm_dev_register())
  224. * and should stay around until this DRM device is unregistered (call to
  225. * drm_dev_unregister()). In other words, private objects lifetime is tied
  226. * to the DRM device lifetime. This implies that:
  227. *
  228. * 1/ all calls to drm_atomic_private_obj_init() must be done before calling
  229. * drm_dev_register()
  230. * 2/ all calls to drm_atomic_private_obj_fini() must be done after calling
  231. * drm_dev_unregister()
  232. */
  233. struct drm_private_obj {
  234. /**
  235. * @head: List entry used to attach a private object to a &drm_device
  236. * (queued to &drm_mode_config.privobj_list).
  237. */
  238. struct list_head head;
  239. /**
  240. * @lock: Modeset lock to protect the state object.
  241. */
  242. struct drm_modeset_lock lock;
  243. /**
  244. * @state: Current atomic state for this driver private object.
  245. */
  246. struct drm_private_state *state;
  247. /**
  248. * @funcs:
  249. *
  250. * Functions to manipulate the state of this driver private object, see
  251. * &drm_private_state_funcs.
  252. */
  253. const struct drm_private_state_funcs *funcs;
  254. };
  255. /**
  256. * drm_for_each_privobj() - private object iterator
  257. *
  258. * @privobj: pointer to the current private object. Updated after each
  259. * iteration
  260. * @dev: the DRM device we want get private objects from
  261. *
  262. * Allows one to iterate over all private objects attached to @dev
  263. */
  264. #define drm_for_each_privobj(privobj, dev) \
  265. list_for_each_entry(privobj, &(dev)->mode_config.privobj_list, head)
  266. /**
  267. * struct drm_private_state - base struct for driver private object state
  268. * @state: backpointer to global drm_atomic_state
  269. *
  270. * Currently only contains a backpointer to the overall atomic update, but in
  271. * the future also might hold synchronization information similar to e.g.
  272. * &drm_crtc.commit.
  273. */
  274. struct drm_private_state {
  275. struct drm_atomic_state *state;
  276. };
  277. struct __drm_private_objs_state {
  278. struct drm_private_obj *ptr;
  279. struct drm_private_state *state, *old_state, *new_state;
  280. };
  281. /**
  282. * struct drm_atomic_state - the global state object for atomic updates
  283. * @ref: count of all references to this state (will not be freed until zero)
  284. * @dev: parent DRM device
  285. * @legacy_cursor_update: hint to enforce legacy cursor IOCTL semantics
  286. * @async_update: hint for asynchronous plane update
  287. * @planes: pointer to array of structures with per-plane data
  288. * @crtcs: pointer to array of CRTC pointers
  289. * @num_connector: size of the @connectors and @connector_states arrays
  290. * @connectors: pointer to array of structures with per-connector data
  291. * @num_private_objs: size of the @private_objs array
  292. * @private_objs: pointer to array of private object pointers
  293. * @acquire_ctx: acquire context for this atomic modeset state update
  294. *
  295. * States are added to an atomic update by calling drm_atomic_get_crtc_state(),
  296. * drm_atomic_get_plane_state(), drm_atomic_get_connector_state(), or for
  297. * private state structures, drm_atomic_get_private_obj_state().
  298. */
  299. struct drm_atomic_state {
  300. struct kref ref;
  301. struct drm_device *dev;
  302. /**
  303. * @allow_modeset:
  304. *
  305. * Allow full modeset. This is used by the ATOMIC IOCTL handler to
  306. * implement the DRM_MODE_ATOMIC_ALLOW_MODESET flag. Drivers should
  307. * never consult this flag, instead looking at the output of
  308. * drm_atomic_crtc_needs_modeset().
  309. */
  310. bool allow_modeset : 1;
  311. bool legacy_cursor_update : 1;
  312. bool async_update : 1;
  313. /**
  314. * @duplicated:
  315. *
  316. * Indicates whether or not this atomic state was duplicated using
  317. * drm_atomic_helper_duplicate_state(). Drivers and atomic helpers
  318. * should use this to fixup normal inconsistencies in duplicated
  319. * states.
  320. */
  321. bool duplicated : 1;
  322. struct __drm_planes_state *planes;
  323. struct __drm_crtcs_state *crtcs;
  324. int num_connector;
  325. struct __drm_connnectors_state *connectors;
  326. int num_private_objs;
  327. struct __drm_private_objs_state *private_objs;
  328. struct drm_modeset_acquire_ctx *acquire_ctx;
  329. /**
  330. * @fake_commit:
  331. *
  332. * Used for signaling unbound planes/connectors.
  333. * When a connector or plane is not bound to any CRTC, it's still important
  334. * to preserve linearity to prevent the atomic states from being freed to early.
  335. *
  336. * This commit (if set) is not bound to any CRTC, but will be completed when
  337. * drm_atomic_helper_commit_hw_done() is called.
  338. */
  339. struct drm_crtc_commit *fake_commit;
  340. /**
  341. * @commit_work:
  342. *
  343. * Work item which can be used by the driver or helpers to execute the
  344. * commit without blocking.
  345. */
  346. struct work_struct commit_work;
  347. };
  348. void __drm_crtc_commit_free(struct kref *kref);
  349. /**
  350. * drm_crtc_commit_get - acquire a reference to the CRTC commit
  351. * @commit: CRTC commit
  352. *
  353. * Increases the reference of @commit.
  354. *
  355. * Returns:
  356. * The pointer to @commit, with reference increased.
  357. */
  358. static inline struct drm_crtc_commit *drm_crtc_commit_get(struct drm_crtc_commit *commit)
  359. {
  360. kref_get(&commit->ref);
  361. return commit;
  362. }
  363. /**
  364. * drm_crtc_commit_put - release a reference to the CRTC commmit
  365. * @commit: CRTC commit
  366. *
  367. * This releases a reference to @commit which is freed after removing the
  368. * final reference. No locking required and callable from any context.
  369. */
  370. static inline void drm_crtc_commit_put(struct drm_crtc_commit *commit)
  371. {
  372. kref_put(&commit->ref, __drm_crtc_commit_free);
  373. }
  374. struct drm_atomic_state * __must_check
  375. drm_atomic_state_alloc(struct drm_device *dev);
  376. void drm_atomic_state_clear(struct drm_atomic_state *state);
  377. /**
  378. * drm_atomic_state_get - acquire a reference to the atomic state
  379. * @state: The atomic state
  380. *
  381. * Returns a new reference to the @state
  382. */
  383. static inline struct drm_atomic_state *
  384. drm_atomic_state_get(struct drm_atomic_state *state)
  385. {
  386. kref_get(&state->ref);
  387. return state;
  388. }
  389. void __drm_atomic_state_free(struct kref *ref);
  390. /**
  391. * drm_atomic_state_put - release a reference to the atomic state
  392. * @state: The atomic state
  393. *
  394. * This releases a reference to @state which is freed after removing the
  395. * final reference. No locking required and callable from any context.
  396. */
  397. static inline void drm_atomic_state_put(struct drm_atomic_state *state)
  398. {
  399. kref_put(&state->ref, __drm_atomic_state_free);
  400. }
  401. int __must_check
  402. drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state);
  403. void drm_atomic_state_default_clear(struct drm_atomic_state *state);
  404. void drm_atomic_state_default_release(struct drm_atomic_state *state);
  405. struct drm_crtc_state * __must_check
  406. drm_atomic_get_crtc_state(struct drm_atomic_state *state,
  407. struct drm_crtc *crtc);
  408. struct drm_plane_state * __must_check
  409. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  410. struct drm_plane *plane);
  411. struct drm_connector_state * __must_check
  412. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  413. struct drm_connector *connector);
  414. void drm_atomic_private_obj_init(struct drm_device *dev,
  415. struct drm_private_obj *obj,
  416. struct drm_private_state *state,
  417. const struct drm_private_state_funcs *funcs);
  418. void drm_atomic_private_obj_fini(struct drm_private_obj *obj);
  419. struct drm_private_state * __must_check
  420. drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
  421. struct drm_private_obj *obj);
  422. struct drm_private_state *
  423. drm_atomic_get_old_private_obj_state(struct drm_atomic_state *state,
  424. struct drm_private_obj *obj);
  425. struct drm_private_state *
  426. drm_atomic_get_new_private_obj_state(struct drm_atomic_state *state,
  427. struct drm_private_obj *obj);
  428. struct drm_connector *
  429. drm_atomic_get_old_connector_for_encoder(struct drm_atomic_state *state,
  430. struct drm_encoder *encoder);
  431. struct drm_connector *
  432. drm_atomic_get_new_connector_for_encoder(struct drm_atomic_state *state,
  433. struct drm_encoder *encoder);
  434. /**
  435. * drm_atomic_get_existing_crtc_state - get CRTC state, if it exists
  436. * @state: global atomic state object
  437. * @crtc: CRTC to grab
  438. *
  439. * This function returns the CRTC state for the given CRTC, or NULL
  440. * if the CRTC is not part of the global atomic state.
  441. *
  442. * This function is deprecated, @drm_atomic_get_old_crtc_state or
  443. * @drm_atomic_get_new_crtc_state should be used instead.
  444. */
  445. static inline struct drm_crtc_state *
  446. drm_atomic_get_existing_crtc_state(struct drm_atomic_state *state,
  447. struct drm_crtc *crtc)
  448. {
  449. return state->crtcs[drm_crtc_index(crtc)].state;
  450. }
  451. /**
  452. * drm_atomic_get_old_crtc_state - get old CRTC state, if it exists
  453. * @state: global atomic state object
  454. * @crtc: CRTC to grab
  455. *
  456. * This function returns the old CRTC state for the given CRTC, or
  457. * NULL if the CRTC is not part of the global atomic state.
  458. */
  459. static inline struct drm_crtc_state *
  460. drm_atomic_get_old_crtc_state(struct drm_atomic_state *state,
  461. struct drm_crtc *crtc)
  462. {
  463. return state->crtcs[drm_crtc_index(crtc)].old_state;
  464. }
  465. /**
  466. * drm_atomic_get_new_crtc_state - get new CRTC state, if it exists
  467. * @state: global atomic state object
  468. * @crtc: CRTC to grab
  469. *
  470. * This function returns the new CRTC state for the given CRTC, or
  471. * NULL if the CRTC is not part of the global atomic state.
  472. */
  473. static inline struct drm_crtc_state *
  474. drm_atomic_get_new_crtc_state(struct drm_atomic_state *state,
  475. struct drm_crtc *crtc)
  476. {
  477. return state->crtcs[drm_crtc_index(crtc)].new_state;
  478. }
  479. /**
  480. * drm_atomic_get_existing_plane_state - get plane state, if it exists
  481. * @state: global atomic state object
  482. * @plane: plane to grab
  483. *
  484. * This function returns the plane state for the given plane, or NULL
  485. * if the plane is not part of the global atomic state.
  486. *
  487. * This function is deprecated, @drm_atomic_get_old_plane_state or
  488. * @drm_atomic_get_new_plane_state should be used instead.
  489. */
  490. static inline struct drm_plane_state *
  491. drm_atomic_get_existing_plane_state(struct drm_atomic_state *state,
  492. struct drm_plane *plane)
  493. {
  494. return state->planes[drm_plane_index(plane)].state;
  495. }
  496. /**
  497. * drm_atomic_get_old_plane_state - get plane state, if it exists
  498. * @state: global atomic state object
  499. * @plane: plane to grab
  500. *
  501. * This function returns the old plane state for the given plane, or
  502. * NULL if the plane is not part of the global atomic state.
  503. */
  504. static inline struct drm_plane_state *
  505. drm_atomic_get_old_plane_state(struct drm_atomic_state *state,
  506. struct drm_plane *plane)
  507. {
  508. return state->planes[drm_plane_index(plane)].old_state;
  509. }
  510. /**
  511. * drm_atomic_get_new_plane_state - get plane state, if it exists
  512. * @state: global atomic state object
  513. * @plane: plane to grab
  514. *
  515. * This function returns the new plane state for the given plane, or
  516. * NULL if the plane is not part of the global atomic state.
  517. */
  518. static inline struct drm_plane_state *
  519. drm_atomic_get_new_plane_state(struct drm_atomic_state *state,
  520. struct drm_plane *plane)
  521. {
  522. return state->planes[drm_plane_index(plane)].new_state;
  523. }
  524. /**
  525. * drm_atomic_get_existing_connector_state - get connector state, if it exists
  526. * @state: global atomic state object
  527. * @connector: connector to grab
  528. *
  529. * This function returns the connector state for the given connector,
  530. * or NULL if the connector is not part of the global atomic state.
  531. *
  532. * This function is deprecated, @drm_atomic_get_old_connector_state or
  533. * @drm_atomic_get_new_connector_state should be used instead.
  534. */
  535. static inline struct drm_connector_state *
  536. drm_atomic_get_existing_connector_state(struct drm_atomic_state *state,
  537. struct drm_connector *connector)
  538. {
  539. int index = drm_connector_index(connector);
  540. if (index >= state->num_connector)
  541. return NULL;
  542. return state->connectors[index].state;
  543. }
  544. /**
  545. * drm_atomic_get_old_connector_state - get connector state, if it exists
  546. * @state: global atomic state object
  547. * @connector: connector to grab
  548. *
  549. * This function returns the old connector state for the given connector,
  550. * or NULL if the connector is not part of the global atomic state.
  551. */
  552. static inline struct drm_connector_state *
  553. drm_atomic_get_old_connector_state(struct drm_atomic_state *state,
  554. struct drm_connector *connector)
  555. {
  556. int index = drm_connector_index(connector);
  557. if (index >= state->num_connector)
  558. return NULL;
  559. return state->connectors[index].old_state;
  560. }
  561. /**
  562. * drm_atomic_get_new_connector_state - get connector state, if it exists
  563. * @state: global atomic state object
  564. * @connector: connector to grab
  565. *
  566. * This function returns the new connector state for the given connector,
  567. * or NULL if the connector is not part of the global atomic state.
  568. */
  569. static inline struct drm_connector_state *
  570. drm_atomic_get_new_connector_state(struct drm_atomic_state *state,
  571. struct drm_connector *connector)
  572. {
  573. int index = drm_connector_index(connector);
  574. if (index >= state->num_connector)
  575. return NULL;
  576. return state->connectors[index].new_state;
  577. }
  578. /**
  579. * __drm_atomic_get_current_plane_state - get current plane state
  580. * @state: global atomic state object
  581. * @plane: plane to grab
  582. *
  583. * This function returns the plane state for the given plane, either from
  584. * @state, or if the plane isn't part of the atomic state update, from @plane.
  585. * This is useful in atomic check callbacks, when drivers need to peek at, but
  586. * not change, state of other planes, since it avoids threading an error code
  587. * back up the call chain.
  588. *
  589. * WARNING:
  590. *
  591. * Note that this function is in general unsafe since it doesn't check for the
  592. * required locking for access state structures. Drivers must ensure that it is
  593. * safe to access the returned state structure through other means. One common
  594. * example is when planes are fixed to a single CRTC, and the driver knows that
  595. * the CRTC lock is held already. In that case holding the CRTC lock gives a
  596. * read-lock on all planes connected to that CRTC. But if planes can be
  597. * reassigned things get more tricky. In that case it's better to use
  598. * drm_atomic_get_plane_state and wire up full error handling.
  599. *
  600. * Returns:
  601. *
  602. * Read-only pointer to the current plane state.
  603. */
  604. static inline const struct drm_plane_state *
  605. __drm_atomic_get_current_plane_state(struct drm_atomic_state *state,
  606. struct drm_plane *plane)
  607. {
  608. if (state->planes[drm_plane_index(plane)].state)
  609. return state->planes[drm_plane_index(plane)].state;
  610. return plane->state;
  611. }
  612. int __must_check
  613. drm_atomic_add_encoder_bridges(struct drm_atomic_state *state,
  614. struct drm_encoder *encoder);
  615. int __must_check
  616. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  617. struct drm_crtc *crtc);
  618. int __must_check
  619. drm_atomic_add_affected_planes(struct drm_atomic_state *state,
  620. struct drm_crtc *crtc);
  621. int __must_check drm_atomic_check_only(struct drm_atomic_state *state);
  622. int __must_check drm_atomic_commit(struct drm_atomic_state *state);
  623. int __must_check drm_atomic_nonblocking_commit(struct drm_atomic_state *state);
  624. void drm_state_dump(struct drm_device *dev, struct drm_printer *p);
  625. /**
  626. * for_each_oldnew_connector_in_state - iterate over all connectors in an atomic update
  627. * @__state: &struct drm_atomic_state pointer
  628. * @connector: &struct drm_connector iteration cursor
  629. * @old_connector_state: &struct drm_connector_state iteration cursor for the
  630. * old state
  631. * @new_connector_state: &struct drm_connector_state iteration cursor for the
  632. * new state
  633. * @__i: int iteration cursor, for macro-internal use
  634. *
  635. * This iterates over all connectors in an atomic update, tracking both old and
  636. * new state. This is useful in places where the state delta needs to be
  637. * considered, for example in atomic check functions.
  638. */
  639. #define for_each_oldnew_connector_in_state(__state, connector, old_connector_state, new_connector_state, __i) \
  640. for ((__i) = 0; \
  641. (__i) < (__state)->num_connector; \
  642. (__i)++) \
  643. for_each_if ((__state)->connectors[__i].ptr && \
  644. ((connector) = (__state)->connectors[__i].ptr, \
  645. (void)(connector) /* Only to avoid unused-but-set-variable warning */, \
  646. (old_connector_state) = (__state)->connectors[__i].old_state, \
  647. (new_connector_state) = (__state)->connectors[__i].new_state, 1))
  648. /**
  649. * for_each_old_connector_in_state - iterate over all connectors in an atomic update
  650. * @__state: &struct drm_atomic_state pointer
  651. * @connector: &struct drm_connector iteration cursor
  652. * @old_connector_state: &struct drm_connector_state iteration cursor for the
  653. * old state
  654. * @__i: int iteration cursor, for macro-internal use
  655. *
  656. * This iterates over all connectors in an atomic update, tracking only the old
  657. * state. This is useful in disable functions, where we need the old state the
  658. * hardware is still in.
  659. */
  660. #define for_each_old_connector_in_state(__state, connector, old_connector_state, __i) \
  661. for ((__i) = 0; \
  662. (__i) < (__state)->num_connector; \
  663. (__i)++) \
  664. for_each_if ((__state)->connectors[__i].ptr && \
  665. ((connector) = (__state)->connectors[__i].ptr, \
  666. (void)(connector) /* Only to avoid unused-but-set-variable warning */, \
  667. (old_connector_state) = (__state)->connectors[__i].old_state, 1))
  668. /**
  669. * for_each_new_connector_in_state - iterate over all connectors in an atomic update
  670. * @__state: &struct drm_atomic_state pointer
  671. * @connector: &struct drm_connector iteration cursor
  672. * @new_connector_state: &struct drm_connector_state iteration cursor for the
  673. * new state
  674. * @__i: int iteration cursor, for macro-internal use
  675. *
  676. * This iterates over all connectors in an atomic update, tracking only the new
  677. * state. This is useful in enable functions, where we need the new state the
  678. * hardware should be in when the atomic commit operation has completed.
  679. */
  680. #define for_each_new_connector_in_state(__state, connector, new_connector_state, __i) \
  681. for ((__i) = 0; \
  682. (__i) < (__state)->num_connector; \
  683. (__i)++) \
  684. for_each_if ((__state)->connectors[__i].ptr && \
  685. ((connector) = (__state)->connectors[__i].ptr, \
  686. (void)(connector) /* Only to avoid unused-but-set-variable warning */, \
  687. (new_connector_state) = (__state)->connectors[__i].new_state, \
  688. (void)(new_connector_state) /* Only to avoid unused-but-set-variable warning */, 1))
  689. /**
  690. * for_each_oldnew_crtc_in_state - iterate over all CRTCs in an atomic update
  691. * @__state: &struct drm_atomic_state pointer
  692. * @crtc: &struct drm_crtc iteration cursor
  693. * @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state
  694. * @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state
  695. * @__i: int iteration cursor, for macro-internal use
  696. *
  697. * This iterates over all CRTCs in an atomic update, tracking both old and
  698. * new state. This is useful in places where the state delta needs to be
  699. * considered, for example in atomic check functions.
  700. */
  701. #define for_each_oldnew_crtc_in_state(__state, crtc, old_crtc_state, new_crtc_state, __i) \
  702. for ((__i) = 0; \
  703. (__i) < (__state)->dev->mode_config.num_crtc; \
  704. (__i)++) \
  705. for_each_if ((__state)->crtcs[__i].ptr && \
  706. ((crtc) = (__state)->crtcs[__i].ptr, \
  707. (void)(crtc) /* Only to avoid unused-but-set-variable warning */, \
  708. (old_crtc_state) = (__state)->crtcs[__i].old_state, \
  709. (void)(old_crtc_state) /* Only to avoid unused-but-set-variable warning */, \
  710. (new_crtc_state) = (__state)->crtcs[__i].new_state, 1))
  711. /**
  712. * for_each_old_crtc_in_state - iterate over all CRTCs in an atomic update
  713. * @__state: &struct drm_atomic_state pointer
  714. * @crtc: &struct drm_crtc iteration cursor
  715. * @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state
  716. * @__i: int iteration cursor, for macro-internal use
  717. *
  718. * This iterates over all CRTCs in an atomic update, tracking only the old
  719. * state. This is useful in disable functions, where we need the old state the
  720. * hardware is still in.
  721. */
  722. #define for_each_old_crtc_in_state(__state, crtc, old_crtc_state, __i) \
  723. for ((__i) = 0; \
  724. (__i) < (__state)->dev->mode_config.num_crtc; \
  725. (__i)++) \
  726. for_each_if ((__state)->crtcs[__i].ptr && \
  727. ((crtc) = (__state)->crtcs[__i].ptr, \
  728. (old_crtc_state) = (__state)->crtcs[__i].old_state, 1))
  729. /**
  730. * for_each_new_crtc_in_state - iterate over all CRTCs in an atomic update
  731. * @__state: &struct drm_atomic_state pointer
  732. * @crtc: &struct drm_crtc iteration cursor
  733. * @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state
  734. * @__i: int iteration cursor, for macro-internal use
  735. *
  736. * This iterates over all CRTCs in an atomic update, tracking only the new
  737. * state. This is useful in enable functions, where we need the new state the
  738. * hardware should be in when the atomic commit operation has completed.
  739. */
  740. #define for_each_new_crtc_in_state(__state, crtc, new_crtc_state, __i) \
  741. for ((__i) = 0; \
  742. (__i) < (__state)->dev->mode_config.num_crtc; \
  743. (__i)++) \
  744. for_each_if ((__state)->crtcs[__i].ptr && \
  745. ((crtc) = (__state)->crtcs[__i].ptr, \
  746. (void)(crtc) /* Only to avoid unused-but-set-variable warning */, \
  747. (new_crtc_state) = (__state)->crtcs[__i].new_state, \
  748. (void)(new_crtc_state) /* Only to avoid unused-but-set-variable warning */, 1))
  749. /**
  750. * for_each_oldnew_plane_in_state - iterate over all planes in an atomic update
  751. * @__state: &struct drm_atomic_state pointer
  752. * @plane: &struct drm_plane iteration cursor
  753. * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
  754. * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
  755. * @__i: int iteration cursor, for macro-internal use
  756. *
  757. * This iterates over all planes in an atomic update, tracking both old and
  758. * new state. This is useful in places where the state delta needs to be
  759. * considered, for example in atomic check functions.
  760. */
  761. #define for_each_oldnew_plane_in_state(__state, plane, old_plane_state, new_plane_state, __i) \
  762. for ((__i) = 0; \
  763. (__i) < (__state)->dev->mode_config.num_total_plane; \
  764. (__i)++) \
  765. for_each_if ((__state)->planes[__i].ptr && \
  766. ((plane) = (__state)->planes[__i].ptr, \
  767. (void)(plane) /* Only to avoid unused-but-set-variable warning */, \
  768. (old_plane_state) = (__state)->planes[__i].old_state,\
  769. (new_plane_state) = (__state)->planes[__i].new_state, 1))
  770. /**
  771. * for_each_oldnew_plane_in_state_reverse - iterate over all planes in an atomic
  772. * update in reverse order
  773. * @__state: &struct drm_atomic_state pointer
  774. * @plane: &struct drm_plane iteration cursor
  775. * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
  776. * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
  777. * @__i: int iteration cursor, for macro-internal use
  778. *
  779. * This iterates over all planes in an atomic update in reverse order,
  780. * tracking both old and new state. This is useful in places where the
  781. * state delta needs to be considered, for example in atomic check functions.
  782. */
  783. #define for_each_oldnew_plane_in_state_reverse(__state, plane, old_plane_state, new_plane_state, __i) \
  784. for ((__i) = ((__state)->dev->mode_config.num_total_plane - 1); \
  785. (__i) >= 0; \
  786. (__i)--) \
  787. for_each_if ((__state)->planes[__i].ptr && \
  788. ((plane) = (__state)->planes[__i].ptr, \
  789. (old_plane_state) = (__state)->planes[__i].old_state,\
  790. (new_plane_state) = (__state)->planes[__i].new_state, 1))
  791. /**
  792. * for_each_old_plane_in_state - iterate over all planes in an atomic update
  793. * @__state: &struct drm_atomic_state pointer
  794. * @plane: &struct drm_plane iteration cursor
  795. * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
  796. * @__i: int iteration cursor, for macro-internal use
  797. *
  798. * This iterates over all planes in an atomic update, tracking only the old
  799. * state. This is useful in disable functions, where we need the old state the
  800. * hardware is still in.
  801. */
  802. #define for_each_old_plane_in_state(__state, plane, old_plane_state, __i) \
  803. for ((__i) = 0; \
  804. (__i) < (__state)->dev->mode_config.num_total_plane; \
  805. (__i)++) \
  806. for_each_if ((__state)->planes[__i].ptr && \
  807. ((plane) = (__state)->planes[__i].ptr, \
  808. (old_plane_state) = (__state)->planes[__i].old_state, 1))
  809. /**
  810. * for_each_new_plane_in_state - iterate over all planes in an atomic update
  811. * @__state: &struct drm_atomic_state pointer
  812. * @plane: &struct drm_plane iteration cursor
  813. * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
  814. * @__i: int iteration cursor, for macro-internal use
  815. *
  816. * This iterates over all planes in an atomic update, tracking only the new
  817. * state. This is useful in enable functions, where we need the new state the
  818. * hardware should be in when the atomic commit operation has completed.
  819. */
  820. #define for_each_new_plane_in_state(__state, plane, new_plane_state, __i) \
  821. for ((__i) = 0; \
  822. (__i) < (__state)->dev->mode_config.num_total_plane; \
  823. (__i)++) \
  824. for_each_if ((__state)->planes[__i].ptr && \
  825. ((plane) = (__state)->planes[__i].ptr, \
  826. (void)(plane) /* Only to avoid unused-but-set-variable warning */, \
  827. (new_plane_state) = (__state)->planes[__i].new_state, \
  828. (void)(new_plane_state) /* Only to avoid unused-but-set-variable warning */, 1))
  829. /**
  830. * for_each_oldnew_private_obj_in_state - iterate over all private objects in an atomic update
  831. * @__state: &struct drm_atomic_state pointer
  832. * @obj: &struct drm_private_obj iteration cursor
  833. * @old_obj_state: &struct drm_private_state iteration cursor for the old state
  834. * @new_obj_state: &struct drm_private_state iteration cursor for the new state
  835. * @__i: int iteration cursor, for macro-internal use
  836. *
  837. * This iterates over all private objects in an atomic update, tracking both
  838. * old and new state. This is useful in places where the state delta needs
  839. * to be considered, for example in atomic check functions.
  840. */
  841. #define for_each_oldnew_private_obj_in_state(__state, obj, old_obj_state, new_obj_state, __i) \
  842. for ((__i) = 0; \
  843. (__i) < (__state)->num_private_objs && \
  844. ((obj) = (__state)->private_objs[__i].ptr, \
  845. (old_obj_state) = (__state)->private_objs[__i].old_state, \
  846. (new_obj_state) = (__state)->private_objs[__i].new_state, 1); \
  847. (__i)++)
  848. /**
  849. * for_each_old_private_obj_in_state - iterate over all private objects in an atomic update
  850. * @__state: &struct drm_atomic_state pointer
  851. * @obj: &struct drm_private_obj iteration cursor
  852. * @old_obj_state: &struct drm_private_state iteration cursor for the old state
  853. * @__i: int iteration cursor, for macro-internal use
  854. *
  855. * This iterates over all private objects in an atomic update, tracking only
  856. * the old state. This is useful in disable functions, where we need the old
  857. * state the hardware is still in.
  858. */
  859. #define for_each_old_private_obj_in_state(__state, obj, old_obj_state, __i) \
  860. for ((__i) = 0; \
  861. (__i) < (__state)->num_private_objs && \
  862. ((obj) = (__state)->private_objs[__i].ptr, \
  863. (old_obj_state) = (__state)->private_objs[__i].old_state, 1); \
  864. (__i)++)
  865. /**
  866. * for_each_new_private_obj_in_state - iterate over all private objects in an atomic update
  867. * @__state: &struct drm_atomic_state pointer
  868. * @obj: &struct drm_private_obj iteration cursor
  869. * @new_obj_state: &struct drm_private_state iteration cursor for the new state
  870. * @__i: int iteration cursor, for macro-internal use
  871. *
  872. * This iterates over all private objects in an atomic update, tracking only
  873. * the new state. This is useful in enable functions, where we need the new state the
  874. * hardware should be in when the atomic commit operation has completed.
  875. */
  876. #define for_each_new_private_obj_in_state(__state, obj, new_obj_state, __i) \
  877. for ((__i) = 0; \
  878. (__i) < (__state)->num_private_objs && \
  879. ((obj) = (__state)->private_objs[__i].ptr, \
  880. (new_obj_state) = (__state)->private_objs[__i].new_state, 1); \
  881. (__i)++)
  882. /**
  883. * drm_atomic_crtc_needs_modeset - compute combined modeset need
  884. * @state: &drm_crtc_state for the CRTC
  885. *
  886. * To give drivers flexibility &struct drm_crtc_state has 3 booleans to track
  887. * whether the state CRTC changed enough to need a full modeset cycle:
  888. * mode_changed, active_changed and connectors_changed. This helper simply
  889. * combines these three to compute the overall need for a modeset for @state.
  890. *
  891. * The atomic helper code sets these booleans, but drivers can and should
  892. * change them appropriately to accurately represent whether a modeset is
  893. * really needed. In general, drivers should avoid full modesets whenever
  894. * possible.
  895. *
  896. * For example if the CRTC mode has changed, and the hardware is able to enact
  897. * the requested mode change without going through a full modeset, the driver
  898. * should clear mode_changed in its &drm_mode_config_funcs.atomic_check
  899. * implementation.
  900. */
  901. static inline bool
  902. drm_atomic_crtc_needs_modeset(const struct drm_crtc_state *state)
  903. {
  904. return state->mode_changed || state->active_changed ||
  905. state->connectors_changed;
  906. }
  907. /**
  908. * drm_atomic_crtc_effectively_active - compute whether CRTC is actually active
  909. * @state: &drm_crtc_state for the CRTC
  910. *
  911. * When in self refresh mode, the crtc_state->active value will be false, since
  912. * the CRTC is off. However in some cases we're interested in whether the CRTC
  913. * is active, or effectively active (ie: it's connected to an active display).
  914. * In these cases, use this function instead of just checking active.
  915. */
  916. static inline bool
  917. drm_atomic_crtc_effectively_active(const struct drm_crtc_state *state)
  918. {
  919. return state->active || state->self_refresh_active;
  920. }
  921. /**
  922. * struct drm_bus_cfg - bus configuration
  923. *
  924. * This structure stores the configuration of a physical bus between two
  925. * components in an output pipeline, usually between two bridges, an encoder
  926. * and a bridge, or a bridge and a connector.
  927. *
  928. * The bus configuration is stored in &drm_bridge_state separately for the
  929. * input and output buses, as seen from the point of view of each bridge. The
  930. * bus configuration of a bridge output is usually identical to the
  931. * configuration of the next bridge's input, but may differ if the signals are
  932. * modified between the two bridges, for instance by an inverter on the board.
  933. * The input and output configurations of a bridge may differ if the bridge
  934. * modifies the signals internally, for instance by performing format
  935. * conversion, or modifying signals polarities.
  936. */
  937. struct drm_bus_cfg {
  938. /**
  939. * @format: format used on this bus (one of the MEDIA_BUS_FMT_* format)
  940. *
  941. * This field should not be directly modified by drivers
  942. * (drm_atomic_bridge_chain_select_bus_fmts() takes care of the bus
  943. * format negotiation).
  944. */
  945. u32 format;
  946. /**
  947. * @flags: DRM_BUS_* flags used on this bus
  948. */
  949. u32 flags;
  950. };
  951. /**
  952. * struct drm_bridge_state - Atomic bridge state object
  953. */
  954. struct drm_bridge_state {
  955. /**
  956. * @base: inherit from &drm_private_state
  957. */
  958. struct drm_private_state base;
  959. /**
  960. * @bridge: the bridge this state refers to
  961. */
  962. struct drm_bridge *bridge;
  963. /**
  964. * @input_bus_cfg: input bus configuration
  965. */
  966. struct drm_bus_cfg input_bus_cfg;
  967. /**
  968. * @output_bus_cfg: input bus configuration
  969. */
  970. struct drm_bus_cfg output_bus_cfg;
  971. };
  972. static inline struct drm_bridge_state *
  973. drm_priv_to_bridge_state(struct drm_private_state *priv)
  974. {
  975. return container_of(priv, struct drm_bridge_state, base);
  976. }
  977. struct drm_bridge_state *
  978. drm_atomic_get_bridge_state(struct drm_atomic_state *state,
  979. struct drm_bridge *bridge);
  980. struct drm_bridge_state *
  981. drm_atomic_get_old_bridge_state(struct drm_atomic_state *state,
  982. struct drm_bridge *bridge);
  983. struct drm_bridge_state *
  984. drm_atomic_get_new_bridge_state(struct drm_atomic_state *state,
  985. struct drm_bridge *bridge);
  986. #endif /* DRM_ATOMIC_H_ */