2 * Copyright (C) 2018 Intel Corp.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
27 #include <drm/drm_atomic.h>
28 #include <drm/drm_atomic_state_helper.h>
29 #include <drm/drm_bridge.h>
30 #include <drm/drm_connector.h>
31 #include <drm/drm_crtc.h>
32 #include <drm/drm_device.h>
33 #include <drm/drm_plane.h>
34 #include <drm/drm_print.h>
35 #include <drm/drm_writeback.h>
37 #include <linux/slab.h>
38 #include <linux/dma-fence.h>
41 * DOC: atomic state reset and initialization
43 * Both the drm core and the atomic helpers assume that there is always the full
44 * and correct atomic software state for all connectors, CRTCs and planes
45 * available. Which is a bit a problem on driver load and also after system
46 * suspend. One way to solve this is to have a hardware state read-out
47 * infrastructure which reconstructs the full software state (e.g. the i915
50 * The simpler solution is to just reset the software state to everything off,
51 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
52 * the atomic helpers provide default reset implementations for all hooks.
54 * On the upside the precise state tracking of atomic simplifies system suspend
55 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
56 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
57 * For other drivers the building blocks are split out, see the documentation
58 * for these functions.
62 * __drm_atomic_helper_crtc_state_reset - reset the CRTC state
63 * @crtc_state: atomic CRTC state, must not be NULL
64 * @crtc: CRTC object, must not be NULL
66 * Initializes the newly allocated @crtc_state with default
67 * values. This is useful for drivers that subclass the CRTC state.
70 __drm_atomic_helper_crtc_state_reset(struct drm_crtc_state *crtc_state,
71 struct drm_crtc *crtc)
73 crtc_state->crtc = crtc;
75 EXPORT_SYMBOL(__drm_atomic_helper_crtc_state_reset);
78 * __drm_atomic_helper_crtc_reset - reset state on CRTC
80 * @crtc_state: CRTC state to assign
82 * Initializes the newly allocated @crtc_state and assigns it to
83 * the &drm_crtc->state pointer of @crtc, usually required when
84 * initializing the drivers or when called from the &drm_crtc_funcs.reset
87 * This is useful for drivers that subclass the CRTC state.
90 __drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
91 struct drm_crtc_state *crtc_state)
94 __drm_atomic_helper_crtc_state_reset(crtc_state, crtc);
96 crtc->state = crtc_state;
98 EXPORT_SYMBOL(__drm_atomic_helper_crtc_reset);
101 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
104 * Resets the atomic state for @crtc by freeing the state pointer (which might
105 * be NULL, e.g. at driver load time) and allocating a new empty state object.
107 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
109 struct drm_crtc_state *crtc_state =
110 kzalloc(sizeof(*crtc->state), GFP_KERNEL);
113 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
115 __drm_atomic_helper_crtc_reset(crtc, crtc_state);
117 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
120 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
122 * @state: atomic CRTC state
124 * Copies atomic state from a CRTC's current state and resets inferred values.
125 * This is useful for drivers that subclass the CRTC state.
127 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
128 struct drm_crtc_state *state)
130 memcpy(state, crtc->state, sizeof(*state));
132 if (state->mode_blob)
133 drm_property_blob_get(state->mode_blob);
134 if (state->degamma_lut)
135 drm_property_blob_get(state->degamma_lut);
137 drm_property_blob_get(state->ctm);
138 if (state->gamma_lut)
139 drm_property_blob_get(state->gamma_lut);
140 state->mode_changed = false;
141 state->active_changed = false;
142 state->planes_changed = false;
143 state->connectors_changed = false;
144 state->color_mgmt_changed = false;
145 state->zpos_changed = false;
146 state->commit = NULL;
148 state->async_flip = false;
150 /* Self refresh should be canceled when a new update is available */
151 state->active = drm_atomic_crtc_effectively_active(state);
152 state->self_refresh_active = false;
154 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
157 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
160 * Default CRTC state duplicate hook for drivers which don't have their own
161 * subclassed CRTC state structure.
163 struct drm_crtc_state *
164 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
166 struct drm_crtc_state *state;
168 if (WARN_ON(!crtc->state))
171 state = kmalloc(sizeof(*state), GFP_KERNEL);
173 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
177 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
180 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
181 * @state: CRTC state object to release
183 * Releases all resources stored in the CRTC state without actually freeing
184 * the memory of the CRTC state. This is useful for drivers that subclass the
187 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
191 * In the event that a non-blocking commit returns
192 * -ERESTARTSYS before the commit_tail work is queued, we will
193 * have an extra reference to the commit object. Release it, if
194 * the event has not been consumed by the worker.
196 * state->event may be freed, so we can't directly look at
197 * state->event->base.completion.
199 if (state->event && state->commit->abort_completion)
200 drm_crtc_commit_put(state->commit);
202 kfree(state->commit->event);
203 state->commit->event = NULL;
205 drm_crtc_commit_put(state->commit);
208 drm_property_blob_put(state->mode_blob);
209 drm_property_blob_put(state->degamma_lut);
210 drm_property_blob_put(state->ctm);
211 drm_property_blob_put(state->gamma_lut);
213 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
216 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
218 * @state: CRTC state object to release
220 * Default CRTC state destroy hook for drivers which don't have their own
221 * subclassed CRTC state structure.
223 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
224 struct drm_crtc_state *state)
226 __drm_atomic_helper_crtc_destroy_state(state);
229 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
232 * __drm_atomic_helper_plane_state_reset - resets plane state to default values
233 * @plane_state: atomic plane state, must not be NULL
234 * @plane: plane object, must not be NULL
236 * Initializes the newly allocated @plane_state with default
237 * values. This is useful for drivers that subclass the CRTC state.
239 void __drm_atomic_helper_plane_state_reset(struct drm_plane_state *plane_state,
240 struct drm_plane *plane)
242 plane_state->plane = plane;
243 plane_state->rotation = DRM_MODE_ROTATE_0;
245 plane_state->alpha = DRM_BLEND_ALPHA_OPAQUE;
246 plane_state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
248 EXPORT_SYMBOL(__drm_atomic_helper_plane_state_reset);
251 * __drm_atomic_helper_plane_reset - reset state on plane
253 * @plane_state: plane state to assign
255 * Initializes the newly allocated @plane_state and assigns it to
256 * the &drm_crtc->state pointer of @plane, usually required when
257 * initializing the drivers or when called from the &drm_plane_funcs.reset
260 * This is useful for drivers that subclass the plane state.
262 void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
263 struct drm_plane_state *plane_state)
266 __drm_atomic_helper_plane_state_reset(plane_state, plane);
268 plane->state = plane_state;
270 EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
273 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
276 * Resets the atomic state for @plane by freeing the state pointer (which might
277 * be NULL, e.g. at driver load time) and allocating a new empty state object.
279 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
282 __drm_atomic_helper_plane_destroy_state(plane->state);
285 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
287 __drm_atomic_helper_plane_reset(plane, plane->state);
289 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
292 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
293 * @plane: plane object
294 * @state: atomic plane state
296 * Copies atomic state from a plane's current state. This is useful for
297 * drivers that subclass the plane state.
299 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
300 struct drm_plane_state *state)
302 memcpy(state, plane->state, sizeof(*state));
305 drm_framebuffer_get(state->fb);
308 state->commit = NULL;
309 state->fb_damage_clips = NULL;
311 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
314 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
317 * Default plane state duplicate hook for drivers which don't have their own
318 * subclassed plane state structure.
320 struct drm_plane_state *
321 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
323 struct drm_plane_state *state;
325 if (WARN_ON(!plane->state))
328 state = kmalloc(sizeof(*state), GFP_KERNEL);
330 __drm_atomic_helper_plane_duplicate_state(plane, state);
334 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
337 * __drm_atomic_helper_plane_destroy_state - release plane state
338 * @state: plane state object to release
340 * Releases all resources stored in the plane state without actually freeing
341 * the memory of the plane state. This is useful for drivers that subclass the
344 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
347 drm_framebuffer_put(state->fb);
350 dma_fence_put(state->fence);
353 drm_crtc_commit_put(state->commit);
355 drm_property_blob_put(state->fb_damage_clips);
357 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
360 * drm_atomic_helper_plane_destroy_state - default state destroy hook
362 * @state: plane state object to release
364 * Default plane state destroy hook for drivers which don't have their own
365 * subclassed plane state structure.
367 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
368 struct drm_plane_state *state)
370 __drm_atomic_helper_plane_destroy_state(state);
373 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
376 * __drm_atomic_helper_connector_state_reset - reset the connector state
377 * @conn_state: atomic connector state, must not be NULL
378 * @connector: connectotr object, must not be NULL
380 * Initializes the newly allocated @conn_state with default
381 * values. This is useful for drivers that subclass the connector state.
384 __drm_atomic_helper_connector_state_reset(struct drm_connector_state *conn_state,
385 struct drm_connector *connector)
387 conn_state->connector = connector;
389 EXPORT_SYMBOL(__drm_atomic_helper_connector_state_reset);
392 * __drm_atomic_helper_connector_reset - reset state on connector
393 * @connector: drm connector
394 * @conn_state: connector state to assign
396 * Initializes the newly allocated @conn_state and assigns it to
397 * the &drm_connector->state pointer of @connector, usually required when
398 * initializing the drivers or when called from the &drm_connector_funcs.reset
401 * This is useful for drivers that subclass the connector state.
404 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
405 struct drm_connector_state *conn_state)
408 __drm_atomic_helper_connector_state_reset(conn_state, connector);
410 connector->state = conn_state;
412 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
415 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
416 * @connector: drm connector
418 * Resets the atomic state for @connector by freeing the state pointer (which
419 * might be NULL, e.g. at driver load time) and allocating a new empty state
422 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
424 struct drm_connector_state *conn_state =
425 kzalloc(sizeof(*conn_state), GFP_KERNEL);
427 if (connector->state)
428 __drm_atomic_helper_connector_destroy_state(connector->state);
430 kfree(connector->state);
431 __drm_atomic_helper_connector_reset(connector, conn_state);
433 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
436 * drm_atomic_helper_connector_tv_reset - Resets TV connector properties
437 * @connector: DRM connector
439 * Resets the TV-related properties attached to a connector.
441 void drm_atomic_helper_connector_tv_reset(struct drm_connector *connector)
443 struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
444 struct drm_connector_state *state = connector->state;
446 state->tv.margins.left = cmdline->tv_margins.left;
447 state->tv.margins.right = cmdline->tv_margins.right;
448 state->tv.margins.top = cmdline->tv_margins.top;
449 state->tv.margins.bottom = cmdline->tv_margins.bottom;
451 EXPORT_SYMBOL(drm_atomic_helper_connector_tv_reset);
454 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
455 * @connector: connector object
456 * @state: atomic connector state
458 * Copies atomic state from a connector's current state. This is useful for
459 * drivers that subclass the connector state.
462 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
463 struct drm_connector_state *state)
465 memcpy(state, connector->state, sizeof(*state));
467 drm_connector_get(connector);
468 state->commit = NULL;
470 if (state->hdr_output_metadata)
471 drm_property_blob_get(state->hdr_output_metadata);
473 /* Don't copy over a writeback job, they are used only once */
474 state->writeback_job = NULL;
476 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
479 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
480 * @connector: drm connector
482 * Default connector state duplicate hook for drivers which don't have their own
483 * subclassed connector state structure.
485 struct drm_connector_state *
486 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
488 struct drm_connector_state *state;
490 if (WARN_ON(!connector->state))
493 state = kmalloc(sizeof(*state), GFP_KERNEL);
495 __drm_atomic_helper_connector_duplicate_state(connector, state);
499 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
502 * __drm_atomic_helper_connector_destroy_state - release connector state
503 * @state: connector state object to release
505 * Releases all resources stored in the connector state without actually
506 * freeing the memory of the connector state. This is useful for drivers that
507 * subclass the connector state.
510 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
513 drm_connector_put(state->connector);
516 drm_crtc_commit_put(state->commit);
518 if (state->writeback_job)
519 drm_writeback_cleanup_job(state->writeback_job);
521 drm_property_blob_put(state->hdr_output_metadata);
523 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
526 * drm_atomic_helper_connector_destroy_state - default state destroy hook
527 * @connector: drm connector
528 * @state: connector state object to release
530 * Default connector state destroy hook for drivers which don't have their own
531 * subclassed connector state structure.
533 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
534 struct drm_connector_state *state)
536 __drm_atomic_helper_connector_destroy_state(state);
539 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
542 * __drm_atomic_helper_private_duplicate_state - copy atomic private state
544 * @state: new private object state
546 * Copies atomic state from a private objects's current state and resets inferred values.
547 * This is useful for drivers that subclass the private state.
549 void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
550 struct drm_private_state *state)
552 memcpy(state, obj->state, sizeof(*state));
554 EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);
557 * __drm_atomic_helper_bridge_duplicate_state() - Copy atomic bridge state
558 * @bridge: bridge object
559 * @state: atomic bridge state
561 * Copies atomic state from a bridge's current state and resets inferred values.
562 * This is useful for drivers that subclass the bridge state.
564 void __drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge,
565 struct drm_bridge_state *state)
567 __drm_atomic_helper_private_obj_duplicate_state(&bridge->base,
569 state->bridge = bridge;
571 EXPORT_SYMBOL(__drm_atomic_helper_bridge_duplicate_state);
574 * drm_atomic_helper_bridge_duplicate_state() - Duplicate a bridge state object
575 * @bridge: bridge object
577 * Allocates a new bridge state and initializes it with the current bridge
578 * state values. This helper is meant to be used as a bridge
579 * &drm_bridge_funcs.atomic_duplicate_state hook for bridges that don't
580 * subclass the bridge state.
582 struct drm_bridge_state *
583 drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge)
585 struct drm_bridge_state *new;
587 if (WARN_ON(!bridge->base.state))
590 new = kzalloc(sizeof(*new), GFP_KERNEL);
592 __drm_atomic_helper_bridge_duplicate_state(bridge, new);
596 EXPORT_SYMBOL(drm_atomic_helper_bridge_duplicate_state);
599 * drm_atomic_helper_bridge_destroy_state() - Destroy a bridge state object
600 * @bridge: the bridge this state refers to
601 * @state: bridge state to destroy
603 * Destroys a bridge state previously created by
604 * &drm_atomic_helper_bridge_reset() or
605 * &drm_atomic_helper_bridge_duplicate_state(). This helper is meant to be
606 * used as a bridge &drm_bridge_funcs.atomic_destroy_state hook for bridges
607 * that don't subclass the bridge state.
609 void drm_atomic_helper_bridge_destroy_state(struct drm_bridge *bridge,
610 struct drm_bridge_state *state)
614 EXPORT_SYMBOL(drm_atomic_helper_bridge_destroy_state);
617 * __drm_atomic_helper_bridge_reset() - Initialize a bridge state to its
619 * @bridge: the bridge this state refers to
620 * @state: bridge state to initialize
622 * Initializes the bridge state to default values. This is meant to be called
623 * by the bridge &drm_bridge_funcs.atomic_reset hook for bridges that subclass
626 void __drm_atomic_helper_bridge_reset(struct drm_bridge *bridge,
627 struct drm_bridge_state *state)
629 memset(state, 0, sizeof(*state));
630 state->bridge = bridge;
632 EXPORT_SYMBOL(__drm_atomic_helper_bridge_reset);
635 * drm_atomic_helper_bridge_reset() - Allocate and initialize a bridge state
637 * @bridge: the bridge this state refers to
639 * Allocates the bridge state and initializes it to default values. This helper
640 * is meant to be used as a bridge &drm_bridge_funcs.atomic_reset hook for
641 * bridges that don't subclass the bridge state.
643 struct drm_bridge_state *
644 drm_atomic_helper_bridge_reset(struct drm_bridge *bridge)
646 struct drm_bridge_state *bridge_state;
648 bridge_state = kzalloc(sizeof(*bridge_state), GFP_KERNEL);
650 return ERR_PTR(-ENOMEM);
652 __drm_atomic_helper_bridge_reset(bridge, bridge_state);
655 EXPORT_SYMBOL(drm_atomic_helper_bridge_reset);