drm_atomic.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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 &drm_crtc.commit_list. Protected by
  119. * $drm_crtc.commit_lock.
  120. */
  121. struct list_head commit_entry;
  122. /**
  123. * @event:
  124. *
  125. * &drm_pending_vblank_event pointer to clean up private events.
  126. */
  127. struct drm_pending_vblank_event *event;
  128. /**
  129. * @abort_completion:
  130. *
  131. * A flag that's set after drm_atomic_helper_setup_commit takes a second
  132. * reference for the completion of $drm_crtc_state.event. It's used by
  133. * the free code to remove the second reference if commit fails.
  134. */
  135. bool abort_completion;
  136. };
  137. struct __drm_planes_state {
  138. struct drm_plane *ptr;
  139. struct drm_plane_state *state, *old_state, *new_state;
  140. };
  141. struct __drm_crtcs_state {
  142. struct drm_crtc *ptr;
  143. struct drm_crtc_state *state, *old_state, *new_state;
  144. /**
  145. * @commit:
  146. *
  147. * A reference to the CRTC commit object that is kept for use by
  148. * drm_atomic_helper_wait_for_flip_done() after
  149. * drm_atomic_helper_commit_hw_done() is called. This ensures that a
  150. * concurrent commit won't free a commit object that is still in use.
  151. */
  152. struct drm_crtc_commit *commit;
  153. s32 __user *out_fence_ptr;
  154. u64 last_vblank_count;
  155. };
  156. struct __drm_connnectors_state {
  157. struct drm_connector *ptr;
  158. struct drm_connector_state *state, *old_state, *new_state;
  159. /**
  160. * @out_fence_ptr:
  161. *
  162. * User-provided pointer which the kernel uses to return a sync_file
  163. * file descriptor. Used by writeback connectors to signal completion of
  164. * the writeback.
  165. */
  166. s32 __user *out_fence_ptr;
  167. };
  168. struct drm_private_obj;
  169. struct drm_private_state;
  170. /**
  171. * struct drm_private_state_funcs - atomic state functions for private objects
  172. *
  173. * These hooks are used by atomic helpers to create, swap and destroy states of
  174. * private objects. The structure itself is used as a vtable to identify the
  175. * associated private object type. Each private object type that needs to be
  176. * added to the atomic states is expected to have an implementation of these
  177. * hooks and pass a pointer to it's drm_private_state_funcs struct to
  178. * drm_atomic_get_private_obj_state().
  179. */
  180. struct drm_private_state_funcs {
  181. /**
  182. * @atomic_duplicate_state:
  183. *
  184. * Duplicate the current state of the private object and return it. It
  185. * is an error to call this before obj->state has been initialized.
  186. *
  187. * RETURNS:
  188. *
  189. * Duplicated atomic state or NULL when obj->state is not
  190. * initialized or allocation failed.
  191. */
  192. struct drm_private_state *(*atomic_duplicate_state)(struct drm_private_obj *obj);
  193. /**
  194. * @atomic_destroy_state:
  195. *
  196. * Frees the private object state created with @atomic_duplicate_state.
  197. */
  198. void (*atomic_destroy_state)(struct drm_private_obj *obj,
  199. struct drm_private_state *state);
  200. };
  201. /**
  202. * struct drm_private_obj - base struct for driver private atomic object
  203. *
  204. * A driver private object is initialized by calling
  205. * drm_atomic_private_obj_init() and cleaned up by calling
  206. * drm_atomic_private_obj_fini().
  207. *
  208. * Currently only tracks the state update functions and the opaque driver
  209. * private state itself, but in the future might also track which
  210. * &drm_modeset_lock is required to duplicate and update this object's state.
  211. *
  212. * All private objects must be initialized before the DRM device they are
  213. * attached to is registered to the DRM subsystem (call to drm_dev_register())
  214. * and should stay around until this DRM device is unregistered (call to
  215. * drm_dev_unregister()). In other words, private objects lifetime is tied
  216. * to the DRM device lifetime. This implies that:
  217. *
  218. * 1/ all calls to drm_atomic_private_obj_init() must be done before calling
  219. * drm_dev_register()
  220. * 2/ all calls to drm_atomic_private_obj_fini() must be done after calling
  221. * drm_dev_unregister()
  222. */
  223. struct drm_private_obj {
  224. /**
  225. * @head: List entry used to attach a private object to a &drm_device
  226. * (queued to &drm_mode_config.privobj_list).
  227. */
  228. struct list_head head;
  229. /**
  230. * @lock: Modeset lock to protect the state object.
  231. */
  232. struct drm_modeset_lock lock;
  233. /**
  234. * @state: Current atomic state for this driver private object.
  235. */
  236. struct drm_private_state *state;
  237. /**
  238. * @funcs:
  239. *
  240. * Functions to manipulate the state of this driver private object, see
  241. * &drm_private_state_funcs.
  242. */
  243. const struct drm_private_state_funcs *funcs;
  244. };
  245. /**
  246. * drm_for_each_privobj() - private object iterator
  247. *
  248. * @privobj: pointer to the current private object. Updated after each
  249. * iteration
  250. * @dev: the DRM device we want get private objects from
  251. *
  252. * Allows one to iterate over all private objects attached to @dev
  253. */
  254. #define drm_for_each_privobj(privobj, dev) \
  255. list_for_each_entry(privobj, &(dev)->mode_config.privobj_list, head)
  256. /**
  257. * struct drm_private_state - base struct for driver private object state
  258. * @state: backpointer to global drm_atomic_state
  259. *
  260. * Currently only contains a backpointer to the overall atomic update, but in
  261. * the future also might hold synchronization information similar to e.g.
  262. * &drm_crtc.commit.
  263. */
  264. struct drm_private_state {
  265. struct drm_atomic_state *state;
  266. };
  267. struct __drm_private_objs_state {
  268. struct drm_private_obj *ptr;
  269. struct drm_private_state *state, *old_state, *new_state;
  270. };
  271. /**
  272. * struct drm_atomic_state - the global state object for atomic updates
  273. * @ref: count of all references to this state (will not be freed until zero)
  274. * @dev: parent DRM device
  275. * @allow_modeset: allow full modeset
  276. * @legacy_cursor_update: hint to enforce legacy cursor IOCTL semantics
  277. * @async_update: hint for asynchronous plane update
  278. * @planes: pointer to array of structures with per-plane data
  279. * @crtcs: pointer to array of CRTC pointers
  280. * @num_connector: size of the @connectors and @connector_states arrays
  281. * @connectors: pointer to array of structures with per-connector data
  282. * @num_private_objs: size of the @private_objs array
  283. * @private_objs: pointer to array of private object pointers
  284. * @acquire_ctx: acquire context for this atomic modeset state update
  285. *
  286. * States are added to an atomic update by calling drm_atomic_get_crtc_state(),
  287. * drm_atomic_get_plane_state(), drm_atomic_get_connector_state(), or for
  288. * private state structures, drm_atomic_get_private_obj_state().
  289. */
  290. struct drm_atomic_state {
  291. struct kref ref;
  292. struct drm_device *dev;
  293. bool allow_modeset : 1;
  294. bool legacy_cursor_update : 1;
  295. bool async_update : 1;
  296. struct __drm_planes_state *planes;
  297. struct __drm_crtcs_state *crtcs;
  298. int num_connector;
  299. struct __drm_connnectors_state *connectors;
  300. int num_private_objs;
  301. struct __drm_private_objs_state *private_objs;
  302. struct drm_modeset_acquire_ctx *acquire_ctx;
  303. /**
  304. * @fake_commit:
  305. *
  306. * Used for signaling unbound planes/connectors.
  307. * When a connector or plane is not bound to any CRTC, it's still important
  308. * to preserve linearity to prevent the atomic states from being freed to early.
  309. *
  310. * This commit (if set) is not bound to any crtc, but will be completed when
  311. * drm_atomic_helper_commit_hw_done() is called.
  312. */
  313. struct drm_crtc_commit *fake_commit;
  314. /**
  315. * @commit_work:
  316. *
  317. * Work item which can be used by the driver or helpers to execute the
  318. * commit without blocking.
  319. */
  320. struct work_struct commit_work;
  321. };
  322. void __drm_crtc_commit_free(struct kref *kref);
  323. /**
  324. * drm_crtc_commit_get - acquire a reference to the CRTC commit
  325. * @commit: CRTC commit
  326. *
  327. * Increases the reference of @commit.
  328. *
  329. * Returns:
  330. * The pointer to @commit, with reference increased.
  331. */
  332. static inline struct drm_crtc_commit *drm_crtc_commit_get(struct drm_crtc_commit *commit)
  333. {
  334. kref_get(&commit->ref);
  335. return commit;
  336. }
  337. /**
  338. * drm_crtc_commit_put - release a reference to the CRTC commmit
  339. * @commit: CRTC commit
  340. *
  341. * This releases a reference to @commit which is freed after removing the
  342. * final reference. No locking required and callable from any context.
  343. */
  344. static inline void drm_crtc_commit_put(struct drm_crtc_commit *commit)
  345. {
  346. kref_put(&commit->ref, __drm_crtc_commit_free);
  347. }
  348. struct drm_atomic_state * __must_check
  349. drm_atomic_state_alloc(struct drm_device *dev);
  350. void drm_atomic_state_clear(struct drm_atomic_state *state);
  351. /**
  352. * drm_atomic_state_get - acquire a reference to the atomic state
  353. * @state: The atomic state
  354. *
  355. * Returns a new reference to the @state
  356. */
  357. static inline struct drm_atomic_state *
  358. drm_atomic_state_get(struct drm_atomic_state *state)
  359. {
  360. kref_get(&state->ref);
  361. return state;
  362. }
  363. void __drm_atomic_state_free(struct kref *ref);
  364. /**
  365. * drm_atomic_state_put - release a reference to the atomic state
  366. * @state: The atomic state
  367. *
  368. * This releases a reference to @state which is freed after removing the
  369. * final reference. No locking required and callable from any context.
  370. */
  371. static inline void drm_atomic_state_put(struct drm_atomic_state *state)
  372. {
  373. kref_put(&state->ref, __drm_atomic_state_free);
  374. }
  375. int __must_check
  376. drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state);
  377. void drm_atomic_state_default_clear(struct drm_atomic_state *state);
  378. void drm_atomic_state_default_release(struct drm_atomic_state *state);
  379. struct drm_crtc_state * __must_check
  380. drm_atomic_get_crtc_state(struct drm_atomic_state *state,
  381. struct drm_crtc *crtc);
  382. int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
  383. struct drm_crtc_state *state, struct drm_property *property,
  384. uint64_t val);
  385. struct drm_plane_state * __must_check
  386. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  387. struct drm_plane *plane);
  388. struct drm_connector_state * __must_check
  389. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  390. struct drm_connector *connector);
  391. void drm_atomic_private_obj_init(struct drm_device *dev,
  392. struct drm_private_obj *obj,
  393. struct drm_private_state *state,
  394. const struct drm_private_state_funcs *funcs);
  395. void drm_atomic_private_obj_fini(struct drm_private_obj *obj);
  396. struct drm_private_state * __must_check
  397. drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
  398. struct drm_private_obj *obj);
  399. /**
  400. * drm_atomic_get_existing_crtc_state - get crtc state, if it exists
  401. * @state: global atomic state object
  402. * @crtc: crtc to grab
  403. *
  404. * This function returns the crtc state for the given crtc, or NULL
  405. * if the crtc is not part of the global atomic state.
  406. *
  407. * This function is deprecated, @drm_atomic_get_old_crtc_state or
  408. * @drm_atomic_get_new_crtc_state should be used instead.
  409. */
  410. static inline struct drm_crtc_state *
  411. drm_atomic_get_existing_crtc_state(struct drm_atomic_state *state,
  412. struct drm_crtc *crtc)
  413. {
  414. return state->crtcs[drm_crtc_index(crtc)].state;
  415. }
  416. /**
  417. * drm_atomic_get_old_crtc_state - get old crtc state, if it exists
  418. * @state: global atomic state object
  419. * @crtc: crtc to grab
  420. *
  421. * This function returns the old crtc state for the given crtc, or
  422. * NULL if the crtc is not part of the global atomic state.
  423. */
  424. static inline struct drm_crtc_state *
  425. drm_atomic_get_old_crtc_state(struct drm_atomic_state *state,
  426. struct drm_crtc *crtc)
  427. {
  428. return state->crtcs[drm_crtc_index(crtc)].old_state;
  429. }
  430. /**
  431. * drm_atomic_get_new_crtc_state - get new crtc state, if it exists
  432. * @state: global atomic state object
  433. * @crtc: crtc to grab
  434. *
  435. * This function returns the new crtc state for the given crtc, or
  436. * NULL if the crtc is not part of the global atomic state.
  437. */
  438. static inline struct drm_crtc_state *
  439. drm_atomic_get_new_crtc_state(struct drm_atomic_state *state,
  440. struct drm_crtc *crtc)
  441. {
  442. return state->crtcs[drm_crtc_index(crtc)].new_state;
  443. }
  444. /**
  445. * drm_atomic_get_existing_plane_state - get plane state, if it exists
  446. * @state: global atomic state object
  447. * @plane: plane to grab
  448. *
  449. * This function returns the plane state for the given plane, or NULL
  450. * if the plane is not part of the global atomic state.
  451. *
  452. * This function is deprecated, @drm_atomic_get_old_plane_state or
  453. * @drm_atomic_get_new_plane_state should be used instead.
  454. */
  455. static inline struct drm_plane_state *
  456. drm_atomic_get_existing_plane_state(struct drm_atomic_state *state,
  457. struct drm_plane *plane)
  458. {
  459. return state->planes[drm_plane_index(plane)].state;
  460. }
  461. /**
  462. * drm_atomic_get_old_plane_state - get plane state, if it exists
  463. * @state: global atomic state object
  464. * @plane: plane to grab
  465. *
  466. * This function returns the old plane state for the given plane, or
  467. * NULL if the plane is not part of the global atomic state.
  468. */
  469. static inline struct drm_plane_state *
  470. drm_atomic_get_old_plane_state(struct drm_atomic_state *state,
  471. struct drm_plane *plane)
  472. {
  473. return state->planes[drm_plane_index(plane)].old_state;
  474. }
  475. /**
  476. * drm_atomic_get_new_plane_state - get plane state, if it exists
  477. * @state: global atomic state object
  478. * @plane: plane to grab
  479. *
  480. * This function returns the new plane state for the given plane, or
  481. * NULL if the plane is not part of the global atomic state.
  482. */
  483. static inline struct drm_plane_state *
  484. drm_atomic_get_new_plane_state(struct drm_atomic_state *state,
  485. struct drm_plane *plane)
  486. {
  487. return state->planes[drm_plane_index(plane)].new_state;
  488. }
  489. /**
  490. * drm_atomic_get_existing_connector_state - get connector state, if it exists
  491. * @state: global atomic state object
  492. * @connector: connector to grab
  493. *
  494. * This function returns the connector state for the given connector,
  495. * or NULL if the connector is not part of the global atomic state.
  496. *
  497. * This function is deprecated, @drm_atomic_get_old_connector_state or
  498. * @drm_atomic_get_new_connector_state should be used instead.
  499. */
  500. static inline struct drm_connector_state *
  501. drm_atomic_get_existing_connector_state(struct drm_atomic_state *state,
  502. struct drm_connector *connector)
  503. {
  504. int index = drm_connector_index(connector);
  505. if (index >= state->num_connector)
  506. return NULL;
  507. return state->connectors[index].state;
  508. }
  509. /**
  510. * drm_atomic_get_old_connector_state - get connector state, if it exists
  511. * @state: global atomic state object
  512. * @connector: connector to grab
  513. *
  514. * This function returns the old connector state for the given connector,
  515. * or NULL if the connector is not part of the global atomic state.
  516. */
  517. static inline struct drm_connector_state *
  518. drm_atomic_get_old_connector_state(struct drm_atomic_state *state,
  519. struct drm_connector *connector)
  520. {
  521. int index = drm_connector_index(connector);
  522. if (index >= state->num_connector)
  523. return NULL;
  524. return state->connectors[index].old_state;
  525. }
  526. /**
  527. * drm_atomic_get_new_connector_state - get connector state, if it exists
  528. * @state: global atomic state object
  529. * @connector: connector to grab
  530. *
  531. * This function returns the new connector state for the given connector,
  532. * or NULL if the connector is not part of the global atomic state.
  533. */
  534. static inline struct drm_connector_state *
  535. drm_atomic_get_new_connector_state(struct drm_atomic_state *state,
  536. struct drm_connector *connector)
  537. {
  538. int index = drm_connector_index(connector);
  539. if (index >= state->num_connector)
  540. return NULL;
  541. return state->connectors[index].new_state;
  542. }
  543. /**
  544. * __drm_atomic_get_current_plane_state - get current plane state
  545. * @state: global atomic state object
  546. * @plane: plane to grab
  547. *
  548. * This function returns the plane state for the given plane, either from
  549. * @state, or if the plane isn't part of the atomic state update, from @plane.
  550. * This is useful in atomic check callbacks, when drivers need to peek at, but
  551. * not change, state of other planes, since it avoids threading an error code
  552. * back up the call chain.
  553. *
  554. * WARNING:
  555. *
  556. * Note that this function is in general unsafe since it doesn't check for the
  557. * required locking for access state structures. Drivers must ensure that it is
  558. * safe to access the returned state structure through other means. One common
  559. * example is when planes are fixed to a single CRTC, and the driver knows that
  560. * the CRTC lock is held already. In that case holding the CRTC lock gives a
  561. * read-lock on all planes connected to that CRTC. But if planes can be
  562. * reassigned things get more tricky. In that case it's better to use
  563. * drm_atomic_get_plane_state and wire up full error handling.
  564. *
  565. * Returns:
  566. *
  567. * Read-only pointer to the current plane state.
  568. */
  569. static inline const struct drm_plane_state *
  570. __drm_atomic_get_current_plane_state(struct drm_atomic_state *state,
  571. struct drm_plane *plane)
  572. {
  573. if (state->planes[drm_plane_index(plane)].state)
  574. return state->planes[drm_plane_index(plane)].state;
  575. return plane->state;
  576. }
  577. int __must_check
  578. drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
  579. const struct drm_display_mode *mode);
  580. int __must_check
  581. drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
  582. struct drm_property_blob *blob);
  583. int __must_check
  584. drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
  585. struct drm_crtc *crtc);
  586. void drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
  587. struct drm_framebuffer *fb);
  588. void drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
  589. struct dma_fence *fence);
  590. int __must_check
  591. drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
  592. struct drm_crtc *crtc);
  593. int drm_atomic_set_writeback_fb_for_connector(
  594. struct drm_connector_state *conn_state,
  595. struct drm_framebuffer *fb);
  596. int __must_check
  597. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  598. struct drm_crtc *crtc);
  599. int __must_check
  600. drm_atomic_add_affected_planes(struct drm_atomic_state *state,
  601. struct drm_crtc *crtc);
  602. int __must_check drm_atomic_check_only(struct drm_atomic_state *state);
  603. int __must_check drm_atomic_commit(struct drm_atomic_state *state);
  604. int __must_check drm_atomic_nonblocking_commit(struct drm_atomic_state *state);
  605. void drm_state_dump(struct drm_device *dev, struct drm_printer *p);
  606. /**
  607. * for_each_oldnew_connector_in_state - iterate over all connectors in an atomic update
  608. * @__state: &struct drm_atomic_state pointer
  609. * @connector: &struct drm_connector iteration cursor
  610. * @old_connector_state: &struct drm_connector_state iteration cursor for the
  611. * old state
  612. * @new_connector_state: &struct drm_connector_state iteration cursor for the
  613. * new state
  614. * @__i: int iteration cursor, for macro-internal use
  615. *
  616. * This iterates over all connectors in an atomic update, tracking both old and
  617. * new state. This is useful in places where the state delta needs to be
  618. * considered, for example in atomic check functions.
  619. */
  620. #define for_each_oldnew_connector_in_state(__state, connector, old_connector_state, new_connector_state, __i) \
  621. for ((__i) = 0; \
  622. (__i) < (__state)->num_connector; \
  623. (__i)++) \
  624. for_each_if ((__state)->connectors[__i].ptr && \
  625. ((connector) = (__state)->connectors[__i].ptr, \
  626. (old_connector_state) = (__state)->connectors[__i].old_state, \
  627. (new_connector_state) = (__state)->connectors[__i].new_state, 1))
  628. /**
  629. * for_each_old_connector_in_state - iterate over all connectors in an atomic update
  630. * @__state: &struct drm_atomic_state pointer
  631. * @connector: &struct drm_connector iteration cursor
  632. * @old_connector_state: &struct drm_connector_state iteration cursor for the
  633. * old state
  634. * @__i: int iteration cursor, for macro-internal use
  635. *
  636. * This iterates over all connectors in an atomic update, tracking only the old
  637. * state. This is useful in disable functions, where we need the old state the
  638. * hardware is still in.
  639. */
  640. #define for_each_old_connector_in_state(__state, connector, old_connector_state, __i) \
  641. for ((__i) = 0; \
  642. (__i) < (__state)->num_connector; \
  643. (__i)++) \
  644. for_each_if ((__state)->connectors[__i].ptr && \
  645. ((connector) = (__state)->connectors[__i].ptr, \
  646. (old_connector_state) = (__state)->connectors[__i].old_state, 1))
  647. /**
  648. * for_each_new_connector_in_state - iterate over all connectors in an atomic update
  649. * @__state: &struct drm_atomic_state pointer
  650. * @connector: &struct drm_connector iteration cursor
  651. * @new_connector_state: &struct drm_connector_state iteration cursor for the
  652. * new state
  653. * @__i: int iteration cursor, for macro-internal use
  654. *
  655. * This iterates over all connectors in an atomic update, tracking only the new
  656. * state. This is useful in enable functions, where we need the new state the
  657. * hardware should be in when the atomic commit operation has completed.
  658. */
  659. #define for_each_new_connector_in_state(__state, connector, new_connector_state, __i) \
  660. for ((__i) = 0; \
  661. (__i) < (__state)->num_connector; \
  662. (__i)++) \
  663. for_each_if ((__state)->connectors[__i].ptr && \
  664. ((connector) = (__state)->connectors[__i].ptr, \
  665. (new_connector_state) = (__state)->connectors[__i].new_state, 1))
  666. /**
  667. * for_each_oldnew_crtc_in_state - iterate over all CRTCs in an atomic update
  668. * @__state: &struct drm_atomic_state pointer
  669. * @crtc: &struct drm_crtc iteration cursor
  670. * @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state
  671. * @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state
  672. * @__i: int iteration cursor, for macro-internal use
  673. *
  674. * This iterates over all CRTCs in an atomic update, tracking both old and
  675. * new state. This is useful in places where the state delta needs to be
  676. * considered, for example in atomic check functions.
  677. */
  678. #define for_each_oldnew_crtc_in_state(__state, crtc, old_crtc_state, new_crtc_state, __i) \
  679. for ((__i) = 0; \
  680. (__i) < (__state)->dev->mode_config.num_crtc; \
  681. (__i)++) \
  682. for_each_if ((__state)->crtcs[__i].ptr && \
  683. ((crtc) = (__state)->crtcs[__i].ptr, \
  684. (old_crtc_state) = (__state)->crtcs[__i].old_state, \
  685. (new_crtc_state) = (__state)->crtcs[__i].new_state, 1))
  686. /**
  687. * for_each_old_crtc_in_state - iterate over all CRTCs in an atomic update
  688. * @__state: &struct drm_atomic_state pointer
  689. * @crtc: &struct drm_crtc iteration cursor
  690. * @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state
  691. * @__i: int iteration cursor, for macro-internal use
  692. *
  693. * This iterates over all CRTCs in an atomic update, tracking only the old
  694. * state. This is useful in disable functions, where we need the old state the
  695. * hardware is still in.
  696. */
  697. #define for_each_old_crtc_in_state(__state, crtc, old_crtc_state, __i) \
  698. for ((__i) = 0; \
  699. (__i) < (__state)->dev->mode_config.num_crtc; \
  700. (__i)++) \
  701. for_each_if ((__state)->crtcs[__i].ptr && \
  702. ((crtc) = (__state)->crtcs[__i].ptr, \
  703. (old_crtc_state) = (__state)->crtcs[__i].old_state, 1))
  704. /**
  705. * for_each_new_crtc_in_state - iterate over all CRTCs in an atomic update
  706. * @__state: &struct drm_atomic_state pointer
  707. * @crtc: &struct drm_crtc iteration cursor
  708. * @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state
  709. * @__i: int iteration cursor, for macro-internal use
  710. *
  711. * This iterates over all CRTCs in an atomic update, tracking only the new
  712. * state. This is useful in enable functions, where we need the new state the
  713. * hardware should be in when the atomic commit operation has completed.
  714. */
  715. #define for_each_new_crtc_in_state(__state, crtc, new_crtc_state, __i) \
  716. for ((__i) = 0; \
  717. (__i) < (__state)->dev->mode_config.num_crtc; \
  718. (__i)++) \
  719. for_each_if ((__state)->crtcs[__i].ptr && \
  720. ((crtc) = (__state)->crtcs[__i].ptr, \
  721. (new_crtc_state) = (__state)->crtcs[__i].new_state, 1))
  722. /**
  723. * for_each_oldnew_plane_in_state - iterate over all planes in an atomic update
  724. * @__state: &struct drm_atomic_state pointer
  725. * @plane: &struct drm_plane iteration cursor
  726. * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
  727. * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
  728. * @__i: int iteration cursor, for macro-internal use
  729. *
  730. * This iterates over all planes in an atomic update, tracking both old and
  731. * new state. This is useful in places where the state delta needs to be
  732. * considered, for example in atomic check functions.
  733. */
  734. #define for_each_oldnew_plane_in_state(__state, plane, old_plane_state, new_plane_state, __i) \
  735. for ((__i) = 0; \
  736. (__i) < (__state)->dev->mode_config.num_total_plane; \
  737. (__i)++) \
  738. for_each_if ((__state)->planes[__i].ptr && \
  739. ((plane) = (__state)->planes[__i].ptr, \
  740. (old_plane_state) = (__state)->planes[__i].old_state,\
  741. (new_plane_state) = (__state)->planes[__i].new_state, 1))
  742. /**
  743. * for_each_oldnew_plane_in_state_reverse - iterate over all planes in an atomic
  744. * update in reverse order
  745. * @__state: &struct drm_atomic_state pointer
  746. * @plane: &struct drm_plane iteration cursor
  747. * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
  748. * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
  749. * @__i: int iteration cursor, for macro-internal use
  750. *
  751. * This iterates over all planes in an atomic update in reverse order,
  752. * tracking both old and new state. This is useful in places where the
  753. * state delta needs to be considered, for example in atomic check functions.
  754. */
  755. #define for_each_oldnew_plane_in_state_reverse(__state, plane, old_plane_state, new_plane_state, __i) \
  756. for ((__i) = ((__state)->dev->mode_config.num_total_plane - 1); \
  757. (__i) >= 0; \
  758. (__i)--) \
  759. for_each_if ((__state)->planes[__i].ptr && \
  760. ((plane) = (__state)->planes[__i].ptr, \
  761. (old_plane_state) = (__state)->planes[__i].old_state,\
  762. (new_plane_state) = (__state)->planes[__i].new_state, 1))
  763. /**
  764. * for_each_old_plane_in_state - iterate over all planes in an atomic update
  765. * @__state: &struct drm_atomic_state pointer
  766. * @plane: &struct drm_plane iteration cursor
  767. * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
  768. * @__i: int iteration cursor, for macro-internal use
  769. *
  770. * This iterates over all planes in an atomic update, tracking only the old
  771. * state. This is useful in disable functions, where we need the old state the
  772. * hardware is still in.
  773. */
  774. #define for_each_old_plane_in_state(__state, plane, old_plane_state, __i) \
  775. for ((__i) = 0; \
  776. (__i) < (__state)->dev->mode_config.num_total_plane; \
  777. (__i)++) \
  778. for_each_if ((__state)->planes[__i].ptr && \
  779. ((plane) = (__state)->planes[__i].ptr, \
  780. (old_plane_state) = (__state)->planes[__i].old_state, 1))
  781. /**
  782. * for_each_new_plane_in_state - iterate over all planes in an atomic update
  783. * @__state: &struct drm_atomic_state pointer
  784. * @plane: &struct drm_plane iteration cursor
  785. * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
  786. * @__i: int iteration cursor, for macro-internal use
  787. *
  788. * This iterates over all planes in an atomic update, tracking only the new
  789. * state. This is useful in enable functions, where we need the new state the
  790. * hardware should be in when the atomic commit operation has completed.
  791. */
  792. #define for_each_new_plane_in_state(__state, plane, new_plane_state, __i) \
  793. for ((__i) = 0; \
  794. (__i) < (__state)->dev->mode_config.num_total_plane; \
  795. (__i)++) \
  796. for_each_if ((__state)->planes[__i].ptr && \
  797. ((plane) = (__state)->planes[__i].ptr, \
  798. (new_plane_state) = (__state)->planes[__i].new_state, 1))
  799. /**
  800. * for_each_oldnew_private_obj_in_state - iterate over all private objects in an atomic update
  801. * @__state: &struct drm_atomic_state pointer
  802. * @obj: &struct drm_private_obj iteration cursor
  803. * @old_obj_state: &struct drm_private_state iteration cursor for the old state
  804. * @new_obj_state: &struct drm_private_state iteration cursor for the new state
  805. * @__i: int iteration cursor, for macro-internal use
  806. *
  807. * This iterates over all private objects in an atomic update, tracking both
  808. * old and new state. This is useful in places where the state delta needs
  809. * to be considered, for example in atomic check functions.
  810. */
  811. #define for_each_oldnew_private_obj_in_state(__state, obj, old_obj_state, new_obj_state, __i) \
  812. for ((__i) = 0; \
  813. (__i) < (__state)->num_private_objs && \
  814. ((obj) = (__state)->private_objs[__i].ptr, \
  815. (old_obj_state) = (__state)->private_objs[__i].old_state, \
  816. (new_obj_state) = (__state)->private_objs[__i].new_state, 1); \
  817. (__i)++)
  818. /**
  819. * for_each_old_private_obj_in_state - iterate over all private objects in an atomic update
  820. * @__state: &struct drm_atomic_state pointer
  821. * @obj: &struct drm_private_obj iteration cursor
  822. * @old_obj_state: &struct drm_private_state iteration cursor for the old state
  823. * @__i: int iteration cursor, for macro-internal use
  824. *
  825. * This iterates over all private objects in an atomic update, tracking only
  826. * the old state. This is useful in disable functions, where we need the old
  827. * state the hardware is still in.
  828. */
  829. #define for_each_old_private_obj_in_state(__state, obj, old_obj_state, __i) \
  830. for ((__i) = 0; \
  831. (__i) < (__state)->num_private_objs && \
  832. ((obj) = (__state)->private_objs[__i].ptr, \
  833. (old_obj_state) = (__state)->private_objs[__i].old_state, 1); \
  834. (__i)++)
  835. /**
  836. * for_each_new_private_obj_in_state - iterate over all private objects in an atomic update
  837. * @__state: &struct drm_atomic_state pointer
  838. * @obj: &struct drm_private_obj iteration cursor
  839. * @new_obj_state: &struct drm_private_state iteration cursor for the new state
  840. * @__i: int iteration cursor, for macro-internal use
  841. *
  842. * This iterates over all private objects in an atomic update, tracking only
  843. * the new state. This is useful in enable functions, where we need the new state the
  844. * hardware should be in when the atomic commit operation has completed.
  845. */
  846. #define for_each_new_private_obj_in_state(__state, obj, new_obj_state, __i) \
  847. for ((__i) = 0; \
  848. (__i) < (__state)->num_private_objs && \
  849. ((obj) = (__state)->private_objs[__i].ptr, \
  850. (new_obj_state) = (__state)->private_objs[__i].new_state, 1); \
  851. (__i)++)
  852. /**
  853. * drm_atomic_crtc_needs_modeset - compute combined modeset need
  854. * @state: &drm_crtc_state for the CRTC
  855. *
  856. * To give drivers flexibility &struct drm_crtc_state has 3 booleans to track
  857. * whether the state CRTC changed enough to need a full modeset cycle:
  858. * mode_changed, active_changed and connectors_changed. This helper simply
  859. * combines these three to compute the overall need for a modeset for @state.
  860. *
  861. * The atomic helper code sets these booleans, but drivers can and should
  862. * change them appropriately to accurately represent whether a modeset is
  863. * really needed. In general, drivers should avoid full modesets whenever
  864. * possible.
  865. *
  866. * For example if the CRTC mode has changed, and the hardware is able to enact
  867. * the requested mode change without going through a full modeset, the driver
  868. * should clear mode_changed in its &drm_mode_config_funcs.atomic_check
  869. * implementation.
  870. */
  871. static inline bool
  872. drm_atomic_crtc_needs_modeset(const struct drm_crtc_state *state)
  873. {
  874. return state->mode_changed || state->active_changed ||
  875. state->connectors_changed;
  876. }
  877. #endif /* DRM_ATOMIC_H_ */