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