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