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_state_helper.h>
28 #include <drm/drm_crtc.h>
29 #include <drm/drm_plane.h>
30 #include <drm/drm_connector.h>
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_device.h>
33 #include <drm/drm_writeback.h>
35 #include <linux/slab.h>
36 #include <linux/dma-fence.h>
39 * DOC: atomic state reset and initialization
41 * Both the drm core and the atomic helpers assume that there is always the full
42 * and correct atomic software state for all connectors, CRTCs and planes
43 * available. Which is a bit a problem on driver load and also after system
44 * suspend. One way to solve this is to have a hardware state read-out
45 * infrastructure which reconstructs the full software state (e.g. the i915
48 * The simpler solution is to just reset the software state to everything off,
49 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
50 * the atomic helpers provide default reset implementations for all hooks.
52 * On the upside the precise state tracking of atomic simplifies system suspend
53 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
54 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
55 * For other drivers the building blocks are split out, see the documentation
56 * for these functions.
60 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
63 * Resets the atomic state for @crtc by freeing the state pointer (which might
64 * be NULL, e.g. at driver load time) and allocating a new empty state object.
66 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
69 __drm_atomic_helper_crtc_destroy_state(crtc->state);
72 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
75 crtc->state->crtc = crtc;
77 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
80 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
82 * @state: atomic CRTC state
84 * Copies atomic state from a CRTC's current state and resets inferred values.
85 * This is useful for drivers that subclass the CRTC state.
87 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
88 struct drm_crtc_state *state)
90 memcpy(state, crtc->state, sizeof(*state));
93 drm_property_blob_get(state->mode_blob);
94 if (state->degamma_lut)
95 drm_property_blob_get(state->degamma_lut);
97 drm_property_blob_get(state->ctm);
99 drm_property_blob_get(state->gamma_lut);
100 state->mode_changed = false;
101 state->active_changed = false;
102 state->planes_changed = false;
103 state->connectors_changed = false;
104 state->color_mgmt_changed = false;
105 state->zpos_changed = false;
106 state->commit = NULL;
108 state->pageflip_flags = 0;
110 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
113 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
116 * Default CRTC state duplicate hook for drivers which don't have their own
117 * subclassed CRTC state structure.
119 struct drm_crtc_state *
120 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
122 struct drm_crtc_state *state;
124 if (WARN_ON(!crtc->state))
127 state = kmalloc(sizeof(*state), GFP_KERNEL);
129 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
133 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
136 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
137 * @state: CRTC state object to release
139 * Releases all resources stored in the CRTC state without actually freeing
140 * the memory of the CRTC state. This is useful for drivers that subclass the
143 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
147 * In the event that a non-blocking commit returns
148 * -ERESTARTSYS before the commit_tail work is queued, we will
149 * have an extra reference to the commit object. Release it, if
150 * the event has not been consumed by the worker.
152 * state->event may be freed, so we can't directly look at
153 * state->event->base.completion.
155 if (state->event && state->commit->abort_completion)
156 drm_crtc_commit_put(state->commit);
158 kfree(state->commit->event);
159 state->commit->event = NULL;
161 drm_crtc_commit_put(state->commit);
164 drm_property_blob_put(state->mode_blob);
165 drm_property_blob_put(state->degamma_lut);
166 drm_property_blob_put(state->ctm);
167 drm_property_blob_put(state->gamma_lut);
169 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
172 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
174 * @state: CRTC state object to release
176 * Default CRTC state destroy hook for drivers which don't have their own
177 * subclassed CRTC state structure.
179 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
180 struct drm_crtc_state *state)
182 __drm_atomic_helper_crtc_destroy_state(state);
185 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
188 * __drm_atomic_helper_plane_reset - resets planes state to default values
189 * @plane: plane object, must not be NULL
190 * @state: atomic plane state, must not be NULL
192 * Initializes plane state to default. This is useful for drivers that subclass
195 void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
196 struct drm_plane_state *state)
198 state->plane = plane;
199 state->rotation = DRM_MODE_ROTATE_0;
201 state->alpha = DRM_BLEND_ALPHA_OPAQUE;
202 state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
204 plane->state = state;
206 EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
209 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
212 * Resets the atomic state for @plane by freeing the state pointer (which might
213 * be NULL, e.g. at driver load time) and allocating a new empty state object.
215 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
218 __drm_atomic_helper_plane_destroy_state(plane->state);
221 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
223 __drm_atomic_helper_plane_reset(plane, plane->state);
225 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
228 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
229 * @plane: plane object
230 * @state: atomic plane state
232 * Copies atomic state from a plane's current state. This is useful for
233 * drivers that subclass the plane state.
235 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
236 struct drm_plane_state *state)
238 memcpy(state, plane->state, sizeof(*state));
241 drm_framebuffer_get(state->fb);
244 state->commit = NULL;
245 state->fb_damage_clips = NULL;
247 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
250 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
253 * Default plane state duplicate hook for drivers which don't have their own
254 * subclassed plane state structure.
256 struct drm_plane_state *
257 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
259 struct drm_plane_state *state;
261 if (WARN_ON(!plane->state))
264 state = kmalloc(sizeof(*state), GFP_KERNEL);
266 __drm_atomic_helper_plane_duplicate_state(plane, state);
270 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
273 * __drm_atomic_helper_plane_destroy_state - release plane state
274 * @state: plane state object to release
276 * Releases all resources stored in the plane state without actually freeing
277 * the memory of the plane state. This is useful for drivers that subclass the
280 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
283 drm_framebuffer_put(state->fb);
286 dma_fence_put(state->fence);
289 drm_crtc_commit_put(state->commit);
291 drm_property_blob_put(state->fb_damage_clips);
293 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
296 * drm_atomic_helper_plane_destroy_state - default state destroy hook
298 * @state: plane state object to release
300 * Default plane state destroy hook for drivers which don't have their own
301 * subclassed plane state structure.
303 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
304 struct drm_plane_state *state)
306 __drm_atomic_helper_plane_destroy_state(state);
309 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
312 * __drm_atomic_helper_connector_reset - reset state on connector
313 * @connector: drm connector
314 * @conn_state: connector state to assign
316 * Initializes the newly allocated @conn_state and assigns it to
317 * the &drm_conector->state pointer of @connector, usually required when
318 * initializing the drivers or when called from the &drm_connector_funcs.reset
321 * This is useful for drivers that subclass the connector state.
324 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
325 struct drm_connector_state *conn_state)
328 conn_state->connector = connector;
330 connector->state = conn_state;
332 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
335 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
336 * @connector: drm connector
338 * Resets the atomic state for @connector by freeing the state pointer (which
339 * might be NULL, e.g. at driver load time) and allocating a new empty state
342 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
344 struct drm_connector_state *conn_state =
345 kzalloc(sizeof(*conn_state), GFP_KERNEL);
347 if (connector->state)
348 __drm_atomic_helper_connector_destroy_state(connector->state);
350 kfree(connector->state);
351 __drm_atomic_helper_connector_reset(connector, conn_state);
353 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
356 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
357 * @connector: connector object
358 * @state: atomic connector state
360 * Copies atomic state from a connector's current state. This is useful for
361 * drivers that subclass the connector state.
364 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
365 struct drm_connector_state *state)
367 memcpy(state, connector->state, sizeof(*state));
369 drm_connector_get(connector);
370 state->commit = NULL;
372 /* Don't copy over a writeback job, they are used only once */
373 state->writeback_job = NULL;
375 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
378 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
379 * @connector: drm connector
381 * Default connector state duplicate hook for drivers which don't have their own
382 * subclassed connector state structure.
384 struct drm_connector_state *
385 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
387 struct drm_connector_state *state;
389 if (WARN_ON(!connector->state))
392 state = kmalloc(sizeof(*state), GFP_KERNEL);
394 __drm_atomic_helper_connector_duplicate_state(connector, state);
398 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
401 * __drm_atomic_helper_connector_destroy_state - release connector state
402 * @state: connector state object to release
404 * Releases all resources stored in the connector state without actually
405 * freeing the memory of the connector state. This is useful for drivers that
406 * subclass the connector state.
409 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
412 drm_connector_put(state->connector);
415 drm_crtc_commit_put(state->commit);
417 if (state->writeback_job)
418 drm_writeback_cleanup_job(state->writeback_job);
420 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
423 * drm_atomic_helper_connector_destroy_state - default state destroy hook
424 * @connector: drm connector
425 * @state: connector state object to release
427 * Default connector state destroy hook for drivers which don't have their own
428 * subclassed connector state structure.
430 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
431 struct drm_connector_state *state)
433 __drm_atomic_helper_connector_destroy_state(state);
436 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
439 * __drm_atomic_helper_private_duplicate_state - copy atomic private state
441 * @state: new private object state
443 * Copies atomic state from a private objects's current state and resets inferred values.
444 * This is useful for drivers that subclass the private state.
446 void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
447 struct drm_private_state *state)
449 memcpy(state, obj->state, sizeof(*state));
451 EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);