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