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.
23 * Rob Clark <robdclark@gmail.com>
24 * Daniel Vetter <daniel.vetter@ffwll.ch>
27 #include <drm/drm_atomic.h>
28 #include <drm/drm_atomic_state_helper.h>
29 #include <drm/drm_connector.h>
30 #include <drm/drm_crtc.h>
31 #include <drm/drm_device.h>
32 #include <drm/drm_plane.h>
33 #include <drm/drm_print.h>
34 #include <drm/drm_writeback.h>
36 #include <linux/slab.h>
37 #include <linux/dma-fence.h>
40 * DOC: atomic state reset and initialization
42 * Both the drm core and the atomic helpers assume that there is always the full
43 * and correct atomic software state for all connectors, CRTCs and planes
44 * available. Which is a bit a problem on driver load and also after system
45 * suspend. One way to solve this is to have a hardware state read-out
46 * infrastructure which reconstructs the full software state (e.g. the i915
49 * The simpler solution is to just reset the software state to everything off,
50 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
51 * the atomic helpers provide default reset implementations for all hooks.
53 * On the upside the precise state tracking of atomic simplifies system suspend
54 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
55 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
56 * For other drivers the building blocks are split out, see the documentation
57 * for these functions.
61 * __drm_atomic_helper_crtc_reset - reset state on CRTC
63 * @crtc_state: CRTC state to assign
65 * Initializes the newly allocated @crtc_state and assigns it to
66 * the &drm_crtc->state pointer of @crtc, usually required when
67 * initializing the drivers or when called from the &drm_crtc_funcs.reset
70 * This is useful for drivers that subclass the CRTC state.
73 __drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
74 struct drm_crtc_state *crtc_state)
77 crtc_state->crtc = crtc;
79 crtc->state = crtc_state;
81 EXPORT_SYMBOL(__drm_atomic_helper_crtc_reset);
84 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
87 * Resets the atomic state for @crtc by freeing the state pointer (which might
88 * be NULL, e.g. at driver load time) and allocating a new empty state object.
90 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
92 struct drm_crtc_state *crtc_state =
93 kzalloc(sizeof(*crtc->state), GFP_KERNEL);
96 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
98 __drm_atomic_helper_crtc_reset(crtc, crtc_state);
100 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
103 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
105 * @state: atomic CRTC state
107 * Copies atomic state from a CRTC's current state and resets inferred values.
108 * This is useful for drivers that subclass the CRTC state.
110 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
111 struct drm_crtc_state *state)
113 memcpy(state, crtc->state, sizeof(*state));
115 if (state->mode_blob)
116 drm_property_blob_get(state->mode_blob);
117 if (state->degamma_lut)
118 drm_property_blob_get(state->degamma_lut);
120 drm_property_blob_get(state->ctm);
121 if (state->gamma_lut)
122 drm_property_blob_get(state->gamma_lut);
123 state->mode_changed = false;
124 state->active_changed = false;
125 state->planes_changed = false;
126 state->connectors_changed = false;
127 state->color_mgmt_changed = false;
128 state->zpos_changed = false;
129 state->commit = NULL;
131 state->pageflip_flags = 0;
133 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
136 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
139 * Default CRTC state duplicate hook for drivers which don't have their own
140 * subclassed CRTC state structure.
142 struct drm_crtc_state *
143 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
145 struct drm_crtc_state *state;
147 if (WARN_ON(!crtc->state))
150 state = kmalloc(sizeof(*state), GFP_KERNEL);
152 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
156 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
159 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
160 * @state: CRTC state object to release
162 * Releases all resources stored in the CRTC state without actually freeing
163 * the memory of the CRTC state. This is useful for drivers that subclass the
166 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
170 * In the event that a non-blocking commit returns
171 * -ERESTARTSYS before the commit_tail work is queued, we will
172 * have an extra reference to the commit object. Release it, if
173 * the event has not been consumed by the worker.
175 * state->event may be freed, so we can't directly look at
176 * state->event->base.completion.
178 if (state->event && state->commit->abort_completion)
179 drm_crtc_commit_put(state->commit);
181 kfree(state->commit->event);
182 state->commit->event = NULL;
184 drm_crtc_commit_put(state->commit);
187 drm_property_blob_put(state->mode_blob);
188 drm_property_blob_put(state->degamma_lut);
189 drm_property_blob_put(state->ctm);
190 drm_property_blob_put(state->gamma_lut);
192 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
195 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
197 * @state: CRTC state object to release
199 * Default CRTC state destroy hook for drivers which don't have their own
200 * subclassed CRTC state structure.
202 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
203 struct drm_crtc_state *state)
205 __drm_atomic_helper_crtc_destroy_state(state);
208 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
211 * __drm_atomic_helper_plane_reset - resets planes state to default values
212 * @plane: plane object, must not be NULL
213 * @state: atomic plane state, must not be NULL
215 * Initializes plane state to default. This is useful for drivers that subclass
218 void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
219 struct drm_plane_state *state)
221 state->plane = plane;
222 state->rotation = DRM_MODE_ROTATE_0;
224 state->alpha = DRM_BLEND_ALPHA_OPAQUE;
225 state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
227 plane->state = state;
229 EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
232 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
235 * Resets the atomic state for @plane by freeing the state pointer (which might
236 * be NULL, e.g. at driver load time) and allocating a new empty state object.
238 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
241 __drm_atomic_helper_plane_destroy_state(plane->state);
244 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
246 __drm_atomic_helper_plane_reset(plane, plane->state);
248 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
251 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
252 * @plane: plane object
253 * @state: atomic plane state
255 * Copies atomic state from a plane's current state. This is useful for
256 * drivers that subclass the plane state.
258 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
259 struct drm_plane_state *state)
261 memcpy(state, plane->state, sizeof(*state));
264 drm_framebuffer_get(state->fb);
267 state->commit = NULL;
268 state->fb_damage_clips = NULL;
270 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
273 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
276 * Default plane state duplicate hook for drivers which don't have their own
277 * subclassed plane state structure.
279 struct drm_plane_state *
280 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
282 struct drm_plane_state *state;
284 if (WARN_ON(!plane->state))
287 state = kmalloc(sizeof(*state), GFP_KERNEL);
289 __drm_atomic_helper_plane_duplicate_state(plane, state);
293 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
296 * __drm_atomic_helper_plane_destroy_state - release plane state
297 * @state: plane state object to release
299 * Releases all resources stored in the plane state without actually freeing
300 * the memory of the plane state. This is useful for drivers that subclass the
303 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
306 drm_framebuffer_put(state->fb);
309 dma_fence_put(state->fence);
312 drm_crtc_commit_put(state->commit);
314 drm_property_blob_put(state->fb_damage_clips);
316 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
319 * drm_atomic_helper_plane_destroy_state - default state destroy hook
321 * @state: plane state object to release
323 * Default plane state destroy hook for drivers which don't have their own
324 * subclassed plane state structure.
326 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
327 struct drm_plane_state *state)
329 __drm_atomic_helper_plane_destroy_state(state);
332 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
335 * __drm_atomic_helper_connector_reset - reset state on connector
336 * @connector: drm connector
337 * @conn_state: connector state to assign
339 * Initializes the newly allocated @conn_state and assigns it to
340 * the &drm_connector->state pointer of @connector, usually required when
341 * initializing the drivers or when called from the &drm_connector_funcs.reset
344 * This is useful for drivers that subclass the connector state.
347 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
348 struct drm_connector_state *conn_state)
351 conn_state->connector = connector;
353 connector->state = conn_state;
355 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
358 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
359 * @connector: drm connector
361 * Resets the atomic state for @connector by freeing the state pointer (which
362 * might be NULL, e.g. at driver load time) and allocating a new empty state
365 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
367 struct drm_connector_state *conn_state =
368 kzalloc(sizeof(*conn_state), GFP_KERNEL);
370 if (connector->state)
371 __drm_atomic_helper_connector_destroy_state(connector->state);
373 kfree(connector->state);
374 __drm_atomic_helper_connector_reset(connector, conn_state);
376 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
379 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
380 * @connector: connector object
381 * @state: atomic connector state
383 * Copies atomic state from a connector's current state. This is useful for
384 * drivers that subclass the connector state.
387 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
388 struct drm_connector_state *state)
390 memcpy(state, connector->state, sizeof(*state));
392 drm_connector_get(connector);
393 state->commit = NULL;
395 if (state->hdr_output_metadata)
396 drm_property_blob_get(state->hdr_output_metadata);
398 /* Don't copy over a writeback job, they are used only once */
399 state->writeback_job = NULL;
401 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
404 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
405 * @connector: drm connector
407 * Default connector state duplicate hook for drivers which don't have their own
408 * subclassed connector state structure.
410 struct drm_connector_state *
411 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
413 struct drm_connector_state *state;
415 if (WARN_ON(!connector->state))
418 state = kmalloc(sizeof(*state), GFP_KERNEL);
420 __drm_atomic_helper_connector_duplicate_state(connector, state);
424 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
427 * __drm_atomic_helper_connector_destroy_state - release connector state
428 * @state: connector state object to release
430 * Releases all resources stored in the connector state without actually
431 * freeing the memory of the connector state. This is useful for drivers that
432 * subclass the connector state.
435 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
438 drm_connector_put(state->connector);
441 drm_crtc_commit_put(state->commit);
443 if (state->writeback_job)
444 drm_writeback_cleanup_job(state->writeback_job);
446 drm_property_blob_put(state->hdr_output_metadata);
448 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
451 * drm_atomic_helper_connector_destroy_state - default state destroy hook
452 * @connector: drm connector
453 * @state: connector state object to release
455 * Default connector state destroy hook for drivers which don't have their own
456 * subclassed connector state structure.
458 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
459 struct drm_connector_state *state)
461 __drm_atomic_helper_connector_destroy_state(state);
464 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
467 * __drm_atomic_helper_private_duplicate_state - copy atomic private state
469 * @state: new private object state
471 * Copies atomic state from a private objects's current state and resets inferred values.
472 * This is useful for drivers that subclass the private state.
474 void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
475 struct drm_private_state *state)
477 memcpy(state, obj->state, sizeof(*state));
479 EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);