Merge branch 'akpm' (patches from Andrew)
[linux/fpc-iii.git] / drivers / gpu / drm / drm_atomic_state_helper.c
blob7cf3cf93654760e1d4de92d5dce923a61ffd1769
1 /*
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.
22 * Authors:
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>
39 /**
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
47 * driver).
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.
60 /**
61 * __drm_atomic_helper_crtc_state_reset - reset the CRTC state
62 * @crtc_state: atomic CRTC state, must not be NULL
63 * @crtc: CRTC object, must not be NULL
65 * Initializes the newly allocated @crtc_state with default
66 * values. This is useful for drivers that subclass the CRTC state.
68 void
69 __drm_atomic_helper_crtc_state_reset(struct drm_crtc_state *crtc_state,
70 struct drm_crtc *crtc)
72 crtc_state->crtc = crtc;
74 EXPORT_SYMBOL(__drm_atomic_helper_crtc_state_reset);
76 /**
77 * __drm_atomic_helper_crtc_reset - reset state on CRTC
78 * @crtc: drm CRTC
79 * @crtc_state: CRTC state to assign
81 * Initializes the newly allocated @crtc_state and assigns it to
82 * the &drm_crtc->state pointer of @crtc, usually required when
83 * initializing the drivers or when called from the &drm_crtc_funcs.reset
84 * hook.
86 * This is useful for drivers that subclass the CRTC state.
88 void
89 __drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
90 struct drm_crtc_state *crtc_state)
92 if (crtc_state)
93 __drm_atomic_helper_crtc_state_reset(crtc_state, crtc);
95 crtc->state = crtc_state;
97 EXPORT_SYMBOL(__drm_atomic_helper_crtc_reset);
99 /**
100 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
101 * @crtc: drm CRTC
103 * Resets the atomic state for @crtc by freeing the state pointer (which might
104 * be NULL, e.g. at driver load time) and allocating a new empty state object.
106 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
108 struct drm_crtc_state *crtc_state =
109 kzalloc(sizeof(*crtc->state), GFP_KERNEL);
111 if (crtc->state)
112 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
114 __drm_atomic_helper_crtc_reset(crtc, crtc_state);
116 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
119 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
120 * @crtc: CRTC object
121 * @state: atomic CRTC state
123 * Copies atomic state from a CRTC's current state and resets inferred values.
124 * This is useful for drivers that subclass the CRTC state.
126 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
127 struct drm_crtc_state *state)
129 memcpy(state, crtc->state, sizeof(*state));
131 if (state->mode_blob)
132 drm_property_blob_get(state->mode_blob);
133 if (state->degamma_lut)
134 drm_property_blob_get(state->degamma_lut);
135 if (state->ctm)
136 drm_property_blob_get(state->ctm);
137 if (state->gamma_lut)
138 drm_property_blob_get(state->gamma_lut);
139 state->mode_changed = false;
140 state->active_changed = false;
141 state->planes_changed = false;
142 state->connectors_changed = false;
143 state->color_mgmt_changed = false;
144 state->zpos_changed = false;
145 state->commit = NULL;
146 state->event = NULL;
147 state->async_flip = false;
149 /* Self refresh should be canceled when a new update is available */
150 state->active = drm_atomic_crtc_effectively_active(state);
151 state->self_refresh_active = false;
153 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
156 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
157 * @crtc: drm CRTC
159 * Default CRTC state duplicate hook for drivers which don't have their own
160 * subclassed CRTC state structure.
162 struct drm_crtc_state *
163 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
165 struct drm_crtc_state *state;
167 if (WARN_ON(!crtc->state))
168 return NULL;
170 state = kmalloc(sizeof(*state), GFP_KERNEL);
171 if (state)
172 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
174 return state;
176 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
179 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
180 * @state: CRTC state object to release
182 * Releases all resources stored in the CRTC state without actually freeing
183 * the memory of the CRTC state. This is useful for drivers that subclass the
184 * CRTC state.
186 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
188 if (state->commit) {
190 * In the event that a non-blocking commit returns
191 * -ERESTARTSYS before the commit_tail work is queued, we will
192 * have an extra reference to the commit object. Release it, if
193 * the event has not been consumed by the worker.
195 * state->event may be freed, so we can't directly look at
196 * state->event->base.completion.
198 if (state->event && state->commit->abort_completion)
199 drm_crtc_commit_put(state->commit);
201 kfree(state->commit->event);
202 state->commit->event = NULL;
204 drm_crtc_commit_put(state->commit);
207 drm_property_blob_put(state->mode_blob);
208 drm_property_blob_put(state->degamma_lut);
209 drm_property_blob_put(state->ctm);
210 drm_property_blob_put(state->gamma_lut);
212 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
215 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
216 * @crtc: drm CRTC
217 * @state: CRTC state object to release
219 * Default CRTC state destroy hook for drivers which don't have their own
220 * subclassed CRTC state structure.
222 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
223 struct drm_crtc_state *state)
225 __drm_atomic_helper_crtc_destroy_state(state);
226 kfree(state);
228 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
231 * __drm_atomic_helper_plane_state_reset - resets plane state to default values
232 * @plane_state: atomic plane state, must not be NULL
233 * @plane: plane object, must not be NULL
235 * Initializes the newly allocated @plane_state with default
236 * values. This is useful for drivers that subclass the CRTC state.
238 void __drm_atomic_helper_plane_state_reset(struct drm_plane_state *plane_state,
239 struct drm_plane *plane)
241 plane_state->plane = plane;
242 plane_state->rotation = DRM_MODE_ROTATE_0;
244 plane_state->alpha = DRM_BLEND_ALPHA_OPAQUE;
245 plane_state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
247 EXPORT_SYMBOL(__drm_atomic_helper_plane_state_reset);
250 * __drm_atomic_helper_plane_reset - reset state on plane
251 * @plane: drm plane
252 * @plane_state: plane state to assign
254 * Initializes the newly allocated @plane_state and assigns it to
255 * the &drm_crtc->state pointer of @plane, usually required when
256 * initializing the drivers or when called from the &drm_plane_funcs.reset
257 * hook.
259 * This is useful for drivers that subclass the plane state.
261 void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
262 struct drm_plane_state *plane_state)
264 if (plane_state)
265 __drm_atomic_helper_plane_state_reset(plane_state, plane);
267 plane->state = plane_state;
269 EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
272 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
273 * @plane: drm plane
275 * Resets the atomic state for @plane by freeing the state pointer (which might
276 * be NULL, e.g. at driver load time) and allocating a new empty state object.
278 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
280 if (plane->state)
281 __drm_atomic_helper_plane_destroy_state(plane->state);
283 kfree(plane->state);
284 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
285 if (plane->state)
286 __drm_atomic_helper_plane_reset(plane, plane->state);
288 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
291 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
292 * @plane: plane object
293 * @state: atomic plane state
295 * Copies atomic state from a plane's current state. This is useful for
296 * drivers that subclass the plane state.
298 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
299 struct drm_plane_state *state)
301 memcpy(state, plane->state, sizeof(*state));
303 if (state->fb)
304 drm_framebuffer_get(state->fb);
306 state->fence = NULL;
307 state->commit = NULL;
308 state->fb_damage_clips = NULL;
310 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
313 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
314 * @plane: drm plane
316 * Default plane state duplicate hook for drivers which don't have their own
317 * subclassed plane state structure.
319 struct drm_plane_state *
320 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
322 struct drm_plane_state *state;
324 if (WARN_ON(!plane->state))
325 return NULL;
327 state = kmalloc(sizeof(*state), GFP_KERNEL);
328 if (state)
329 __drm_atomic_helper_plane_duplicate_state(plane, state);
331 return state;
333 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
336 * __drm_atomic_helper_plane_destroy_state - release plane state
337 * @state: plane state object to release
339 * Releases all resources stored in the plane state without actually freeing
340 * the memory of the plane state. This is useful for drivers that subclass the
341 * plane state.
343 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
345 if (state->fb)
346 drm_framebuffer_put(state->fb);
348 if (state->fence)
349 dma_fence_put(state->fence);
351 if (state->commit)
352 drm_crtc_commit_put(state->commit);
354 drm_property_blob_put(state->fb_damage_clips);
356 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
359 * drm_atomic_helper_plane_destroy_state - default state destroy hook
360 * @plane: drm plane
361 * @state: plane state object to release
363 * Default plane state destroy hook for drivers which don't have their own
364 * subclassed plane state structure.
366 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
367 struct drm_plane_state *state)
369 __drm_atomic_helper_plane_destroy_state(state);
370 kfree(state);
372 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
375 * __drm_atomic_helper_connector_state_reset - reset the connector state
376 * @conn_state: atomic connector state, must not be NULL
377 * @connector: connectotr object, must not be NULL
379 * Initializes the newly allocated @conn_state with default
380 * values. This is useful for drivers that subclass the connector state.
382 void
383 __drm_atomic_helper_connector_state_reset(struct drm_connector_state *conn_state,
384 struct drm_connector *connector)
386 conn_state->connector = connector;
388 EXPORT_SYMBOL(__drm_atomic_helper_connector_state_reset);
391 * __drm_atomic_helper_connector_reset - reset state on connector
392 * @connector: drm connector
393 * @conn_state: connector state to assign
395 * Initializes the newly allocated @conn_state and assigns it to
396 * the &drm_connector->state pointer of @connector, usually required when
397 * initializing the drivers or when called from the &drm_connector_funcs.reset
398 * hook.
400 * This is useful for drivers that subclass the connector state.
402 void
403 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
404 struct drm_connector_state *conn_state)
406 if (conn_state)
407 __drm_atomic_helper_connector_state_reset(conn_state, connector);
409 connector->state = conn_state;
411 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
414 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
415 * @connector: drm connector
417 * Resets the atomic state for @connector by freeing the state pointer (which
418 * might be NULL, e.g. at driver load time) and allocating a new empty state
419 * object.
421 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
423 struct drm_connector_state *conn_state =
424 kzalloc(sizeof(*conn_state), GFP_KERNEL);
426 if (connector->state)
427 __drm_atomic_helper_connector_destroy_state(connector->state);
429 kfree(connector->state);
430 __drm_atomic_helper_connector_reset(connector, conn_state);
432 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
435 * drm_atomic_helper_connector_tv_reset - Resets TV connector properties
436 * @connector: DRM connector
438 * Resets the TV-related properties attached to a connector.
440 void drm_atomic_helper_connector_tv_reset(struct drm_connector *connector)
442 struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
443 struct drm_connector_state *state = connector->state;
445 state->tv.margins.left = cmdline->tv_margins.left;
446 state->tv.margins.right = cmdline->tv_margins.right;
447 state->tv.margins.top = cmdline->tv_margins.top;
448 state->tv.margins.bottom = cmdline->tv_margins.bottom;
450 EXPORT_SYMBOL(drm_atomic_helper_connector_tv_reset);
453 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
454 * @connector: connector object
455 * @state: atomic connector state
457 * Copies atomic state from a connector's current state. This is useful for
458 * drivers that subclass the connector state.
460 void
461 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
462 struct drm_connector_state *state)
464 memcpy(state, connector->state, sizeof(*state));
465 if (state->crtc)
466 drm_connector_get(connector);
467 state->commit = NULL;
469 if (state->hdr_output_metadata)
470 drm_property_blob_get(state->hdr_output_metadata);
472 /* Don't copy over a writeback job, they are used only once */
473 state->writeback_job = NULL;
475 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
478 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
479 * @connector: drm connector
481 * Default connector state duplicate hook for drivers which don't have their own
482 * subclassed connector state structure.
484 struct drm_connector_state *
485 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
487 struct drm_connector_state *state;
489 if (WARN_ON(!connector->state))
490 return NULL;
492 state = kmalloc(sizeof(*state), GFP_KERNEL);
493 if (state)
494 __drm_atomic_helper_connector_duplicate_state(connector, state);
496 return state;
498 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
501 * __drm_atomic_helper_connector_destroy_state - release connector state
502 * @state: connector state object to release
504 * Releases all resources stored in the connector state without actually
505 * freeing the memory of the connector state. This is useful for drivers that
506 * subclass the connector state.
508 void
509 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
511 if (state->crtc)
512 drm_connector_put(state->connector);
514 if (state->commit)
515 drm_crtc_commit_put(state->commit);
517 if (state->writeback_job)
518 drm_writeback_cleanup_job(state->writeback_job);
520 drm_property_blob_put(state->hdr_output_metadata);
522 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
525 * drm_atomic_helper_connector_destroy_state - default state destroy hook
526 * @connector: drm connector
527 * @state: connector state object to release
529 * Default connector state destroy hook for drivers which don't have their own
530 * subclassed connector state structure.
532 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
533 struct drm_connector_state *state)
535 __drm_atomic_helper_connector_destroy_state(state);
536 kfree(state);
538 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
541 * __drm_atomic_helper_private_duplicate_state - copy atomic private state
542 * @obj: CRTC object
543 * @state: new private object state
545 * Copies atomic state from a private objects's current state and resets inferred values.
546 * This is useful for drivers that subclass the private state.
548 void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
549 struct drm_private_state *state)
551 memcpy(state, obj->state, sizeof(*state));
553 EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);