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