drm_atomic_state_helper.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * Copyright (C) 2018 Intel Corp.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors:
  23. * Rob Clark <robdclark@gmail.com>
  24. * Daniel Vetter <daniel.vetter@ffwll.ch>
  25. */
  26. #include <drm/drm_atomic.h>
  27. #include <drm/drm_atomic_state_helper.h>
  28. #include <drm/drm_blend.h>
  29. #include <drm/drm_bridge.h>
  30. #include <drm/drm_connector.h>
  31. #include <drm/drm_crtc.h>
  32. #include <drm/drm_device.h>
  33. #include <drm/drm_framebuffer.h>
  34. #include <drm/drm_plane.h>
  35. #include <drm/drm_print.h>
  36. #include <drm/drm_vblank.h>
  37. #include <drm/drm_writeback.h>
  38. #include <linux/export.h>
  39. #include <linux/slab.h>
  40. #include <linux/dma-fence.h>
  41. /**
  42. * DOC: atomic state reset and initialization
  43. *
  44. * Both the drm core and the atomic helpers assume that there is always the full
  45. * and correct atomic software state for all connectors, CRTCs and planes
  46. * available. Which is a bit a problem on driver load and also after system
  47. * suspend. One way to solve this is to have a hardware state read-out
  48. * infrastructure which reconstructs the full software state (e.g. the i915
  49. * driver).
  50. *
  51. * The simpler solution is to just reset the software state to everything off,
  52. * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
  53. * the atomic helpers provide default reset implementations for all hooks.
  54. *
  55. * On the upside the precise state tracking of atomic simplifies system suspend
  56. * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
  57. * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
  58. * For other drivers the building blocks are split out, see the documentation
  59. * for these functions.
  60. */
  61. /**
  62. * __drm_atomic_helper_crtc_state_reset - reset the CRTC state
  63. * @crtc_state: atomic CRTC state, must not be NULL
  64. * @crtc: CRTC object, must not be NULL
  65. *
  66. * Initializes the newly allocated @crtc_state with default
  67. * values. This is useful for drivers that subclass the CRTC state.
  68. */
  69. void
  70. __drm_atomic_helper_crtc_state_reset(struct drm_crtc_state *crtc_state,
  71. struct drm_crtc *crtc)
  72. {
  73. crtc_state->crtc = crtc;
  74. }
  75. EXPORT_SYMBOL(__drm_atomic_helper_crtc_state_reset);
  76. /**
  77. * __drm_atomic_helper_crtc_reset - reset state on CRTC
  78. * @crtc: drm CRTC
  79. * @crtc_state: CRTC state to assign
  80. *
  81. * Initializes the newly allocated @crtc_state and assigns it to
  82. * the &drm_crtc->state pointer of @crtc, usually required when
  83. * initializing the drivers or when called from the &drm_crtc_funcs.reset
  84. * hook.
  85. *
  86. * This is useful for drivers that subclass the CRTC state.
  87. */
  88. void
  89. __drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
  90. struct drm_crtc_state *crtc_state)
  91. {
  92. if (crtc_state)
  93. __drm_atomic_helper_crtc_state_reset(crtc_state, crtc);
  94. if (drm_dev_has_vblank(crtc->dev))
  95. drm_crtc_vblank_reset(crtc);
  96. crtc->state = crtc_state;
  97. }
  98. EXPORT_SYMBOL(__drm_atomic_helper_crtc_reset);
  99. /**
  100. * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
  101. * @crtc: drm CRTC
  102. *
  103. * Resets the atomic state for @crtc by freeing the state pointer (which might
  104. * be NULL, e.g. at driver load time) and allocating a new empty state object.
  105. */
  106. void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
  107. {
  108. struct drm_crtc_state *crtc_state =
  109. kzalloc_obj(*crtc->state);
  110. if (crtc->state)
  111. crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  112. __drm_atomic_helper_crtc_reset(crtc, crtc_state);
  113. }
  114. EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
  115. /**
  116. * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
  117. * @crtc: CRTC object
  118. * @state: atomic CRTC state
  119. *
  120. * Copies atomic state from a CRTC's current state and resets inferred values.
  121. * This is useful for drivers that subclass the CRTC state.
  122. */
  123. void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
  124. struct drm_crtc_state *state)
  125. {
  126. memcpy(state, crtc->state, sizeof(*state));
  127. if (state->mode_blob)
  128. drm_property_blob_get(state->mode_blob);
  129. if (state->degamma_lut)
  130. drm_property_blob_get(state->degamma_lut);
  131. if (state->ctm)
  132. drm_property_blob_get(state->ctm);
  133. if (state->gamma_lut)
  134. drm_property_blob_get(state->gamma_lut);
  135. state->mode_changed = false;
  136. state->active_changed = false;
  137. state->planes_changed = false;
  138. state->connectors_changed = false;
  139. state->color_mgmt_changed = false;
  140. state->zpos_changed = false;
  141. state->commit = NULL;
  142. state->event = NULL;
  143. state->async_flip = false;
  144. /* Self refresh should be canceled when a new update is available */
  145. state->active = drm_atomic_crtc_effectively_active(state);
  146. state->self_refresh_active = false;
  147. }
  148. EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
  149. /**
  150. * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
  151. * @crtc: drm CRTC
  152. *
  153. * Default CRTC state duplicate hook for drivers which don't have their own
  154. * subclassed CRTC state structure.
  155. */
  156. struct drm_crtc_state *
  157. drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
  158. {
  159. struct drm_crtc_state *state;
  160. if (WARN_ON(!crtc->state))
  161. return NULL;
  162. state = kmalloc_obj(*state);
  163. if (state)
  164. __drm_atomic_helper_crtc_duplicate_state(crtc, state);
  165. return state;
  166. }
  167. EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
  168. /**
  169. * __drm_atomic_helper_crtc_destroy_state - release CRTC state
  170. * @state: CRTC state object to release
  171. *
  172. * Releases all resources stored in the CRTC state without actually freeing
  173. * the memory of the CRTC state. This is useful for drivers that subclass the
  174. * CRTC state.
  175. */
  176. void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
  177. {
  178. if (state->commit) {
  179. /*
  180. * In the event that a non-blocking commit returns
  181. * -ERESTARTSYS before the commit_tail work is queued, we will
  182. * have an extra reference to the commit object. Release it, if
  183. * the event has not been consumed by the worker.
  184. *
  185. * state->event may be freed, so we can't directly look at
  186. * state->event->base.completion.
  187. */
  188. if (state->event && state->commit->abort_completion)
  189. drm_crtc_commit_put(state->commit);
  190. kfree(state->commit->event);
  191. state->commit->event = NULL;
  192. drm_crtc_commit_put(state->commit);
  193. }
  194. drm_property_blob_put(state->mode_blob);
  195. drm_property_blob_put(state->degamma_lut);
  196. drm_property_blob_put(state->ctm);
  197. drm_property_blob_put(state->gamma_lut);
  198. }
  199. EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
  200. /**
  201. * drm_atomic_helper_crtc_destroy_state - default state destroy hook
  202. * @crtc: drm CRTC
  203. * @state: CRTC state object to release
  204. *
  205. * Default CRTC state destroy hook for drivers which don't have their own
  206. * subclassed CRTC state structure.
  207. */
  208. void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
  209. struct drm_crtc_state *state)
  210. {
  211. __drm_atomic_helper_crtc_destroy_state(state);
  212. kfree(state);
  213. }
  214. EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
  215. /**
  216. * __drm_atomic_helper_plane_state_reset - resets plane state to default values
  217. * @plane_state: atomic plane state, must not be NULL
  218. * @plane: plane object, must not be NULL
  219. *
  220. * Initializes the newly allocated @plane_state with default
  221. * values. This is useful for drivers that subclass the CRTC state.
  222. */
  223. void __drm_atomic_helper_plane_state_reset(struct drm_plane_state *plane_state,
  224. struct drm_plane *plane)
  225. {
  226. u64 val;
  227. plane_state->plane = plane;
  228. plane_state->rotation = DRM_MODE_ROTATE_0;
  229. plane_state->alpha = DRM_BLEND_ALPHA_OPAQUE;
  230. plane_state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
  231. if (plane->color_encoding_property) {
  232. if (!drm_object_property_get_default_value(&plane->base,
  233. plane->color_encoding_property,
  234. &val))
  235. plane_state->color_encoding = val;
  236. }
  237. if (plane->color_range_property) {
  238. if (!drm_object_property_get_default_value(&plane->base,
  239. plane->color_range_property,
  240. &val))
  241. plane_state->color_range = val;
  242. }
  243. if (plane->color_pipeline_property) {
  244. /* default is always NULL, i.e., bypass */
  245. plane_state->color_pipeline = NULL;
  246. }
  247. if (plane->zpos_property) {
  248. if (!drm_object_property_get_default_value(&plane->base,
  249. plane->zpos_property,
  250. &val)) {
  251. plane_state->zpos = val;
  252. plane_state->normalized_zpos = val;
  253. }
  254. }
  255. if (plane->hotspot_x_property) {
  256. if (!drm_object_property_get_default_value(&plane->base,
  257. plane->hotspot_x_property,
  258. &val))
  259. plane_state->hotspot_x = val;
  260. }
  261. if (plane->hotspot_y_property) {
  262. if (!drm_object_property_get_default_value(&plane->base,
  263. plane->hotspot_y_property,
  264. &val))
  265. plane_state->hotspot_y = val;
  266. }
  267. }
  268. EXPORT_SYMBOL(__drm_atomic_helper_plane_state_reset);
  269. /**
  270. * __drm_atomic_helper_plane_reset - reset state on plane
  271. * @plane: drm plane
  272. * @plane_state: plane state to assign
  273. *
  274. * Initializes the newly allocated @plane_state and assigns it to
  275. * the &drm_crtc->state pointer of @plane, usually required when
  276. * initializing the drivers or when called from the &drm_plane_funcs.reset
  277. * hook.
  278. *
  279. * This is useful for drivers that subclass the plane state.
  280. */
  281. void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
  282. struct drm_plane_state *plane_state)
  283. {
  284. if (plane_state)
  285. __drm_atomic_helper_plane_state_reset(plane_state, plane);
  286. plane->state = plane_state;
  287. }
  288. EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
  289. /**
  290. * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
  291. * @plane: drm plane
  292. *
  293. * Resets the atomic state for @plane by freeing the state pointer (which might
  294. * be NULL, e.g. at driver load time) and allocating a new empty state object.
  295. */
  296. void drm_atomic_helper_plane_reset(struct drm_plane *plane)
  297. {
  298. if (plane->state)
  299. __drm_atomic_helper_plane_destroy_state(plane->state);
  300. kfree(plane->state);
  301. plane->state = kzalloc_obj(*plane->state);
  302. if (plane->state)
  303. __drm_atomic_helper_plane_reset(plane, plane->state);
  304. }
  305. EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
  306. /**
  307. * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
  308. * @plane: plane object
  309. * @state: atomic plane state
  310. *
  311. * Copies atomic state from a plane's current state. This is useful for
  312. * drivers that subclass the plane state.
  313. */
  314. void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
  315. struct drm_plane_state *state)
  316. {
  317. memcpy(state, plane->state, sizeof(*state));
  318. if (state->fb)
  319. drm_framebuffer_get(state->fb);
  320. state->fence = NULL;
  321. state->commit = NULL;
  322. state->fb_damage_clips = NULL;
  323. state->color_mgmt_changed = false;
  324. }
  325. EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
  326. /**
  327. * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
  328. * @plane: drm plane
  329. *
  330. * Default plane state duplicate hook for drivers which don't have their own
  331. * subclassed plane state structure.
  332. */
  333. struct drm_plane_state *
  334. drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
  335. {
  336. struct drm_plane_state *state;
  337. if (WARN_ON(!plane->state))
  338. return NULL;
  339. state = kmalloc_obj(*state);
  340. if (state)
  341. __drm_atomic_helper_plane_duplicate_state(plane, state);
  342. return state;
  343. }
  344. EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
  345. /**
  346. * __drm_atomic_helper_plane_destroy_state - release plane state
  347. * @state: plane state object to release
  348. *
  349. * Releases all resources stored in the plane state without actually freeing
  350. * the memory of the plane state. This is useful for drivers that subclass the
  351. * plane state.
  352. */
  353. void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
  354. {
  355. if (state->fb)
  356. drm_framebuffer_put(state->fb);
  357. if (state->fence)
  358. dma_fence_put(state->fence);
  359. if (state->commit)
  360. drm_crtc_commit_put(state->commit);
  361. drm_property_blob_put(state->fb_damage_clips);
  362. }
  363. EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
  364. /**
  365. * drm_atomic_helper_plane_destroy_state - default state destroy hook
  366. * @plane: drm plane
  367. * @state: plane state object to release
  368. *
  369. * Default plane state destroy hook for drivers which don't have their own
  370. * subclassed plane state structure.
  371. */
  372. void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
  373. struct drm_plane_state *state)
  374. {
  375. __drm_atomic_helper_plane_destroy_state(state);
  376. kfree(state);
  377. }
  378. EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
  379. /**
  380. * __drm_atomic_helper_connector_state_reset - reset the connector state
  381. * @conn_state: atomic connector state, must not be NULL
  382. * @connector: connectotr object, must not be NULL
  383. *
  384. * Initializes the newly allocated @conn_state with default
  385. * values. This is useful for drivers that subclass the connector state.
  386. */
  387. void
  388. __drm_atomic_helper_connector_state_reset(struct drm_connector_state *conn_state,
  389. struct drm_connector *connector)
  390. {
  391. conn_state->connector = connector;
  392. }
  393. EXPORT_SYMBOL(__drm_atomic_helper_connector_state_reset);
  394. /**
  395. * __drm_atomic_helper_connector_reset - reset state on connector
  396. * @connector: drm connector
  397. * @conn_state: connector state to assign
  398. *
  399. * Initializes the newly allocated @conn_state and assigns it to
  400. * the &drm_connector->state pointer of @connector, usually required when
  401. * initializing the drivers or when called from the &drm_connector_funcs.reset
  402. * hook.
  403. *
  404. * This is useful for drivers that subclass the connector state.
  405. */
  406. void
  407. __drm_atomic_helper_connector_reset(struct drm_connector *connector,
  408. struct drm_connector_state *conn_state)
  409. {
  410. if (conn_state)
  411. __drm_atomic_helper_connector_state_reset(conn_state, connector);
  412. connector->state = conn_state;
  413. }
  414. EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
  415. /**
  416. * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
  417. * @connector: drm connector
  418. *
  419. * Resets the atomic state for @connector by freeing the state pointer (which
  420. * might be NULL, e.g. at driver load time) and allocating a new empty state
  421. * object.
  422. */
  423. void drm_atomic_helper_connector_reset(struct drm_connector *connector)
  424. {
  425. struct drm_connector_state *conn_state = kzalloc_obj(*conn_state);
  426. if (connector->state)
  427. __drm_atomic_helper_connector_destroy_state(connector->state);
  428. kfree(connector->state);
  429. __drm_atomic_helper_connector_reset(connector, conn_state);
  430. }
  431. EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
  432. /**
  433. * drm_atomic_helper_connector_tv_margins_reset - Resets TV connector properties
  434. * @connector: DRM connector
  435. *
  436. * Resets the TV-related properties attached to a connector.
  437. */
  438. void drm_atomic_helper_connector_tv_margins_reset(struct drm_connector *connector)
  439. {
  440. struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
  441. struct drm_connector_state *state = connector->state;
  442. state->tv.margins.left = cmdline->tv_margins.left;
  443. state->tv.margins.right = cmdline->tv_margins.right;
  444. state->tv.margins.top = cmdline->tv_margins.top;
  445. state->tv.margins.bottom = cmdline->tv_margins.bottom;
  446. }
  447. EXPORT_SYMBOL(drm_atomic_helper_connector_tv_margins_reset);
  448. /**
  449. * drm_atomic_helper_connector_tv_reset - Resets Analog TV connector properties
  450. * @connector: DRM connector
  451. *
  452. * Resets the analog TV properties attached to a connector
  453. */
  454. void drm_atomic_helper_connector_tv_reset(struct drm_connector *connector)
  455. {
  456. struct drm_device *dev = connector->dev;
  457. struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
  458. struct drm_connector_state *state = connector->state;
  459. struct drm_property *prop;
  460. uint64_t val;
  461. prop = dev->mode_config.tv_mode_property;
  462. if (prop)
  463. if (!drm_object_property_get_default_value(&connector->base,
  464. prop, &val))
  465. state->tv.mode = val;
  466. if (cmdline->tv_mode_specified)
  467. state->tv.mode = cmdline->tv_mode;
  468. prop = dev->mode_config.tv_select_subconnector_property;
  469. if (prop)
  470. if (!drm_object_property_get_default_value(&connector->base,
  471. prop, &val))
  472. state->tv.select_subconnector = val;
  473. prop = dev->mode_config.tv_subconnector_property;
  474. if (prop)
  475. if (!drm_object_property_get_default_value(&connector->base,
  476. prop, &val))
  477. state->tv.subconnector = val;
  478. prop = dev->mode_config.tv_brightness_property;
  479. if (prop)
  480. if (!drm_object_property_get_default_value(&connector->base,
  481. prop, &val))
  482. state->tv.brightness = val;
  483. prop = dev->mode_config.tv_contrast_property;
  484. if (prop)
  485. if (!drm_object_property_get_default_value(&connector->base,
  486. prop, &val))
  487. state->tv.contrast = val;
  488. prop = dev->mode_config.tv_flicker_reduction_property;
  489. if (prop)
  490. if (!drm_object_property_get_default_value(&connector->base,
  491. prop, &val))
  492. state->tv.flicker_reduction = val;
  493. prop = dev->mode_config.tv_overscan_property;
  494. if (prop)
  495. if (!drm_object_property_get_default_value(&connector->base,
  496. prop, &val))
  497. state->tv.overscan = val;
  498. prop = dev->mode_config.tv_saturation_property;
  499. if (prop)
  500. if (!drm_object_property_get_default_value(&connector->base,
  501. prop, &val))
  502. state->tv.saturation = val;
  503. prop = dev->mode_config.tv_hue_property;
  504. if (prop)
  505. if (!drm_object_property_get_default_value(&connector->base,
  506. prop, &val))
  507. state->tv.hue = val;
  508. drm_atomic_helper_connector_tv_margins_reset(connector);
  509. }
  510. EXPORT_SYMBOL(drm_atomic_helper_connector_tv_reset);
  511. /**
  512. * drm_atomic_helper_connector_tv_check - Validate an analog TV connector state
  513. * @connector: DRM Connector
  514. * @state: the DRM State object
  515. *
  516. * Checks the state object to see if the requested state is valid for an
  517. * analog TV connector.
  518. *
  519. * Return:
  520. * %0 for success, a negative error code on error.
  521. */
  522. int drm_atomic_helper_connector_tv_check(struct drm_connector *connector,
  523. struct drm_atomic_state *state)
  524. {
  525. struct drm_connector_state *old_conn_state =
  526. drm_atomic_get_old_connector_state(state, connector);
  527. struct drm_connector_state *new_conn_state =
  528. drm_atomic_get_new_connector_state(state, connector);
  529. struct drm_crtc_state *crtc_state;
  530. struct drm_crtc *crtc;
  531. crtc = new_conn_state->crtc;
  532. if (!crtc)
  533. return 0;
  534. crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  535. if (!crtc_state)
  536. return -EINVAL;
  537. if (old_conn_state->tv.mode != new_conn_state->tv.mode)
  538. crtc_state->mode_changed = true;
  539. if (old_conn_state->tv.margins.left != new_conn_state->tv.margins.left ||
  540. old_conn_state->tv.margins.right != new_conn_state->tv.margins.right ||
  541. old_conn_state->tv.margins.top != new_conn_state->tv.margins.top ||
  542. old_conn_state->tv.margins.bottom != new_conn_state->tv.margins.bottom ||
  543. old_conn_state->tv.mode != new_conn_state->tv.mode ||
  544. old_conn_state->tv.brightness != new_conn_state->tv.brightness ||
  545. old_conn_state->tv.contrast != new_conn_state->tv.contrast ||
  546. old_conn_state->tv.flicker_reduction != new_conn_state->tv.flicker_reduction ||
  547. old_conn_state->tv.overscan != new_conn_state->tv.overscan ||
  548. old_conn_state->tv.saturation != new_conn_state->tv.saturation ||
  549. old_conn_state->tv.hue != new_conn_state->tv.hue)
  550. crtc_state->connectors_changed = true;
  551. return 0;
  552. }
  553. EXPORT_SYMBOL(drm_atomic_helper_connector_tv_check);
  554. /**
  555. * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
  556. * @connector: connector object
  557. * @state: atomic connector state
  558. *
  559. * Copies atomic state from a connector's current state. This is useful for
  560. * drivers that subclass the connector state.
  561. */
  562. void
  563. __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
  564. struct drm_connector_state *state)
  565. {
  566. memcpy(state, connector->state, sizeof(*state));
  567. if (state->crtc)
  568. drm_connector_get(connector);
  569. state->commit = NULL;
  570. if (state->hdr_output_metadata)
  571. drm_property_blob_get(state->hdr_output_metadata);
  572. /* Don't copy over a writeback job, they are used only once */
  573. state->writeback_job = NULL;
  574. }
  575. EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
  576. /**
  577. * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
  578. * @connector: drm connector
  579. *
  580. * Default connector state duplicate hook for drivers which don't have their own
  581. * subclassed connector state structure.
  582. */
  583. struct drm_connector_state *
  584. drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
  585. {
  586. struct drm_connector_state *state;
  587. if (WARN_ON(!connector->state))
  588. return NULL;
  589. state = kmalloc_obj(*state);
  590. if (state)
  591. __drm_atomic_helper_connector_duplicate_state(connector, state);
  592. return state;
  593. }
  594. EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
  595. /**
  596. * __drm_atomic_helper_connector_destroy_state - release connector state
  597. * @state: connector state object to release
  598. *
  599. * Releases all resources stored in the connector state without actually
  600. * freeing the memory of the connector state. This is useful for drivers that
  601. * subclass the connector state.
  602. */
  603. void
  604. __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
  605. {
  606. if (state->crtc)
  607. drm_connector_put(state->connector);
  608. if (state->commit)
  609. drm_crtc_commit_put(state->commit);
  610. if (state->writeback_job)
  611. drm_writeback_cleanup_job(state->writeback_job);
  612. drm_property_blob_put(state->hdr_output_metadata);
  613. }
  614. EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
  615. /**
  616. * drm_atomic_helper_connector_destroy_state - default state destroy hook
  617. * @connector: drm connector
  618. * @state: connector state object to release
  619. *
  620. * Default connector state destroy hook for drivers which don't have their own
  621. * subclassed connector state structure.
  622. */
  623. void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
  624. struct drm_connector_state *state)
  625. {
  626. __drm_atomic_helper_connector_destroy_state(state);
  627. kfree(state);
  628. }
  629. EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
  630. /**
  631. * __drm_atomic_helper_private_obj_duplicate_state - copy atomic private state
  632. * @obj: CRTC object
  633. * @state: new private object state
  634. *
  635. * Copies atomic state from a private objects's current state and resets inferred values.
  636. * This is useful for drivers that subclass the private state.
  637. */
  638. void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
  639. struct drm_private_state *state)
  640. {
  641. memcpy(state, obj->state, sizeof(*state));
  642. }
  643. EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);
  644. /**
  645. * __drm_atomic_helper_bridge_duplicate_state() - Copy atomic bridge state
  646. * @bridge: bridge object
  647. * @state: atomic bridge state
  648. *
  649. * Copies atomic state from a bridge's current state and resets inferred values.
  650. * This is useful for drivers that subclass the bridge state.
  651. */
  652. void __drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge,
  653. struct drm_bridge_state *state)
  654. {
  655. __drm_atomic_helper_private_obj_duplicate_state(&bridge->base,
  656. &state->base);
  657. state->bridge = bridge;
  658. }
  659. EXPORT_SYMBOL(__drm_atomic_helper_bridge_duplicate_state);
  660. /**
  661. * drm_atomic_helper_bridge_duplicate_state() - Duplicate a bridge state object
  662. * @bridge: bridge object
  663. *
  664. * Allocates a new bridge state and initializes it with the current bridge
  665. * state values. This helper is meant to be used as a bridge
  666. * &drm_bridge_funcs.atomic_duplicate_state hook for bridges that don't
  667. * subclass the bridge state.
  668. */
  669. struct drm_bridge_state *
  670. drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge)
  671. {
  672. struct drm_bridge_state *new;
  673. if (WARN_ON(!bridge->base.state))
  674. return NULL;
  675. new = kzalloc_obj(*new);
  676. if (new)
  677. __drm_atomic_helper_bridge_duplicate_state(bridge, new);
  678. return new;
  679. }
  680. EXPORT_SYMBOL(drm_atomic_helper_bridge_duplicate_state);
  681. /**
  682. * drm_atomic_helper_bridge_destroy_state() - Destroy a bridge state object
  683. * @bridge: the bridge this state refers to
  684. * @state: bridge state to destroy
  685. *
  686. * Destroys a bridge state previously created by
  687. * &drm_atomic_helper_bridge_reset() or
  688. * &drm_atomic_helper_bridge_duplicate_state(). This helper is meant to be
  689. * used as a bridge &drm_bridge_funcs.atomic_destroy_state hook for bridges
  690. * that don't subclass the bridge state.
  691. */
  692. void drm_atomic_helper_bridge_destroy_state(struct drm_bridge *bridge,
  693. struct drm_bridge_state *state)
  694. {
  695. kfree(state);
  696. }
  697. EXPORT_SYMBOL(drm_atomic_helper_bridge_destroy_state);
  698. /**
  699. * __drm_atomic_helper_bridge_reset() - Initialize a bridge state to its
  700. * default
  701. * @bridge: the bridge this state refers to
  702. * @state: bridge state to initialize
  703. *
  704. * Initializes the bridge state to default values. This is meant to be called
  705. * by the bridge &drm_bridge_funcs.atomic_reset hook for bridges that subclass
  706. * the bridge state.
  707. */
  708. void __drm_atomic_helper_bridge_reset(struct drm_bridge *bridge,
  709. struct drm_bridge_state *state)
  710. {
  711. memset(state, 0, sizeof(*state));
  712. state->bridge = bridge;
  713. }
  714. EXPORT_SYMBOL(__drm_atomic_helper_bridge_reset);
  715. /**
  716. * drm_atomic_helper_bridge_reset() - Allocate and initialize a bridge state
  717. * to its default
  718. * @bridge: the bridge this state refers to
  719. *
  720. * Allocates the bridge state and initializes it to default values. This helper
  721. * is meant to be used as a bridge &drm_bridge_funcs.atomic_reset hook for
  722. * bridges that don't subclass the bridge state.
  723. */
  724. struct drm_bridge_state *
  725. drm_atomic_helper_bridge_reset(struct drm_bridge *bridge)
  726. {
  727. struct drm_bridge_state *bridge_state;
  728. bridge_state = kzalloc_obj(*bridge_state);
  729. if (!bridge_state)
  730. return ERR_PTR(-ENOMEM);
  731. __drm_atomic_helper_bridge_reset(bridge, bridge_state);
  732. return bridge_state;
  733. }
  734. EXPORT_SYMBOL(drm_atomic_helper_bridge_reset);