1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2018 Intel Corp.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be included in
12*4882a593Smuzhiyun * all copies or substantial portions of the Software.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17*4882a593Smuzhiyun * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*4882a593Smuzhiyun * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*4882a593Smuzhiyun * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*4882a593Smuzhiyun * OTHER DEALINGS IN THE SOFTWARE.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * Authors:
23*4882a593Smuzhiyun * Rob Clark <robdclark@gmail.com>
24*4882a593Smuzhiyun * Daniel Vetter <daniel.vetter@ffwll.ch>
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include <drm/drm_atomic.h>
28*4882a593Smuzhiyun #include <drm/drm_atomic_state_helper.h>
29*4882a593Smuzhiyun #include <drm/drm_bridge.h>
30*4882a593Smuzhiyun #include <drm/drm_connector.h>
31*4882a593Smuzhiyun #include <drm/drm_crtc.h>
32*4882a593Smuzhiyun #include <drm/drm_device.h>
33*4882a593Smuzhiyun #include <drm/drm_plane.h>
34*4882a593Smuzhiyun #include <drm/drm_print.h>
35*4882a593Smuzhiyun #include <drm/drm_vblank.h>
36*4882a593Smuzhiyun #include <drm/drm_writeback.h>
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #include <linux/slab.h>
39*4882a593Smuzhiyun #include <linux/dma-fence.h>
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /**
42*4882a593Smuzhiyun * DOC: atomic state reset and initialization
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * Both the drm core and the atomic helpers assume that there is always the full
45*4882a593Smuzhiyun * and correct atomic software state for all connectors, CRTCs and planes
46*4882a593Smuzhiyun * available. Which is a bit a problem on driver load and also after system
47*4882a593Smuzhiyun * suspend. One way to solve this is to have a hardware state read-out
48*4882a593Smuzhiyun * infrastructure which reconstructs the full software state (e.g. the i915
49*4882a593Smuzhiyun * driver).
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * The simpler solution is to just reset the software state to everything off,
52*4882a593Smuzhiyun * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
53*4882a593Smuzhiyun * the atomic helpers provide default reset implementations for all hooks.
54*4882a593Smuzhiyun *
55*4882a593Smuzhiyun * On the upside the precise state tracking of atomic simplifies system suspend
56*4882a593Smuzhiyun * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
57*4882a593Smuzhiyun * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
58*4882a593Smuzhiyun * For other drivers the building blocks are split out, see the documentation
59*4882a593Smuzhiyun * for these functions.
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /**
63*4882a593Smuzhiyun * __drm_atomic_helper_crtc_state_reset - reset the CRTC state
64*4882a593Smuzhiyun * @crtc_state: atomic CRTC state, must not be NULL
65*4882a593Smuzhiyun * @crtc: CRTC object, must not be NULL
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * Initializes the newly allocated @crtc_state with default
68*4882a593Smuzhiyun * values. This is useful for drivers that subclass the CRTC state.
69*4882a593Smuzhiyun */
70*4882a593Smuzhiyun void
__drm_atomic_helper_crtc_state_reset(struct drm_crtc_state * crtc_state,struct drm_crtc * crtc)71*4882a593Smuzhiyun __drm_atomic_helper_crtc_state_reset(struct drm_crtc_state *crtc_state,
72*4882a593Smuzhiyun struct drm_crtc *crtc)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun crtc_state->crtc = crtc;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_crtc_state_reset);
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /**
79*4882a593Smuzhiyun * __drm_atomic_helper_crtc_reset - reset state on CRTC
80*4882a593Smuzhiyun * @crtc: drm CRTC
81*4882a593Smuzhiyun * @crtc_state: CRTC state to assign
82*4882a593Smuzhiyun *
83*4882a593Smuzhiyun * Initializes the newly allocated @crtc_state and assigns it to
84*4882a593Smuzhiyun * the &drm_crtc->state pointer of @crtc, usually required when
85*4882a593Smuzhiyun * initializing the drivers or when called from the &drm_crtc_funcs.reset
86*4882a593Smuzhiyun * hook.
87*4882a593Smuzhiyun *
88*4882a593Smuzhiyun * This is useful for drivers that subclass the CRTC state.
89*4882a593Smuzhiyun */
90*4882a593Smuzhiyun void
__drm_atomic_helper_crtc_reset(struct drm_crtc * crtc,struct drm_crtc_state * crtc_state)91*4882a593Smuzhiyun __drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
92*4882a593Smuzhiyun struct drm_crtc_state *crtc_state)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun if (crtc_state)
95*4882a593Smuzhiyun __drm_atomic_helper_crtc_state_reset(crtc_state, crtc);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun if (drm_dev_has_vblank(crtc->dev))
98*4882a593Smuzhiyun drm_crtc_vblank_reset(crtc);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun crtc->state = crtc_state;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_crtc_reset);
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /**
105*4882a593Smuzhiyun * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
106*4882a593Smuzhiyun * @crtc: drm CRTC
107*4882a593Smuzhiyun *
108*4882a593Smuzhiyun * Resets the atomic state for @crtc by freeing the state pointer (which might
109*4882a593Smuzhiyun * be NULL, e.g. at driver load time) and allocating a new empty state object.
110*4882a593Smuzhiyun */
drm_atomic_helper_crtc_reset(struct drm_crtc * crtc)111*4882a593Smuzhiyun void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun struct drm_crtc_state *crtc_state =
114*4882a593Smuzhiyun kzalloc(sizeof(*crtc->state), GFP_KERNEL);
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun if (crtc->state)
117*4882a593Smuzhiyun crtc->funcs->atomic_destroy_state(crtc, crtc->state);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun __drm_atomic_helper_crtc_reset(crtc, crtc_state);
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /**
124*4882a593Smuzhiyun * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
125*4882a593Smuzhiyun * @crtc: CRTC object
126*4882a593Smuzhiyun * @state: atomic CRTC state
127*4882a593Smuzhiyun *
128*4882a593Smuzhiyun * Copies atomic state from a CRTC's current state and resets inferred values.
129*4882a593Smuzhiyun * This is useful for drivers that subclass the CRTC state.
130*4882a593Smuzhiyun */
__drm_atomic_helper_crtc_duplicate_state(struct drm_crtc * crtc,struct drm_crtc_state * state)131*4882a593Smuzhiyun void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
132*4882a593Smuzhiyun struct drm_crtc_state *state)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun memcpy(state, crtc->state, sizeof(*state));
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun if (state->mode_blob)
137*4882a593Smuzhiyun drm_property_blob_get(state->mode_blob);
138*4882a593Smuzhiyun if (state->degamma_lut)
139*4882a593Smuzhiyun drm_property_blob_get(state->degamma_lut);
140*4882a593Smuzhiyun if (state->ctm)
141*4882a593Smuzhiyun drm_property_blob_get(state->ctm);
142*4882a593Smuzhiyun if (state->gamma_lut)
143*4882a593Smuzhiyun drm_property_blob_get(state->gamma_lut);
144*4882a593Smuzhiyun state->mode_changed = false;
145*4882a593Smuzhiyun state->active_changed = false;
146*4882a593Smuzhiyun state->planes_changed = false;
147*4882a593Smuzhiyun state->connectors_changed = false;
148*4882a593Smuzhiyun state->color_mgmt_changed = false;
149*4882a593Smuzhiyun state->zpos_changed = false;
150*4882a593Smuzhiyun state->commit = NULL;
151*4882a593Smuzhiyun state->event = NULL;
152*4882a593Smuzhiyun state->async_flip = false;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /* Self refresh should be canceled when a new update is available */
155*4882a593Smuzhiyun state->active = drm_atomic_crtc_effectively_active(state);
156*4882a593Smuzhiyun state->self_refresh_active = false;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun /**
161*4882a593Smuzhiyun * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
162*4882a593Smuzhiyun * @crtc: drm CRTC
163*4882a593Smuzhiyun *
164*4882a593Smuzhiyun * Default CRTC state duplicate hook for drivers which don't have their own
165*4882a593Smuzhiyun * subclassed CRTC state structure.
166*4882a593Smuzhiyun */
167*4882a593Smuzhiyun struct drm_crtc_state *
drm_atomic_helper_crtc_duplicate_state(struct drm_crtc * crtc)168*4882a593Smuzhiyun drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun struct drm_crtc_state *state;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun if (WARN_ON(!crtc->state))
173*4882a593Smuzhiyun return NULL;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun state = kmalloc(sizeof(*state), GFP_KERNEL);
176*4882a593Smuzhiyun if (state)
177*4882a593Smuzhiyun __drm_atomic_helper_crtc_duplicate_state(crtc, state);
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun return state;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun /**
184*4882a593Smuzhiyun * __drm_atomic_helper_crtc_destroy_state - release CRTC state
185*4882a593Smuzhiyun * @state: CRTC state object to release
186*4882a593Smuzhiyun *
187*4882a593Smuzhiyun * Releases all resources stored in the CRTC state without actually freeing
188*4882a593Smuzhiyun * the memory of the CRTC state. This is useful for drivers that subclass the
189*4882a593Smuzhiyun * CRTC state.
190*4882a593Smuzhiyun */
__drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state * state)191*4882a593Smuzhiyun void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun if (state->commit) {
194*4882a593Smuzhiyun /*
195*4882a593Smuzhiyun * In the event that a non-blocking commit returns
196*4882a593Smuzhiyun * -ERESTARTSYS before the commit_tail work is queued, we will
197*4882a593Smuzhiyun * have an extra reference to the commit object. Release it, if
198*4882a593Smuzhiyun * the event has not been consumed by the worker.
199*4882a593Smuzhiyun *
200*4882a593Smuzhiyun * state->event may be freed, so we can't directly look at
201*4882a593Smuzhiyun * state->event->base.completion.
202*4882a593Smuzhiyun */
203*4882a593Smuzhiyun if (state->event && state->commit->abort_completion)
204*4882a593Smuzhiyun drm_crtc_commit_put(state->commit);
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun kfree(state->commit->event);
207*4882a593Smuzhiyun state->commit->event = NULL;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun drm_crtc_commit_put(state->commit);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun drm_property_blob_put(state->mode_blob);
213*4882a593Smuzhiyun drm_property_blob_put(state->degamma_lut);
214*4882a593Smuzhiyun drm_property_blob_put(state->ctm);
215*4882a593Smuzhiyun drm_property_blob_put(state->gamma_lut);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /**
220*4882a593Smuzhiyun * drm_atomic_helper_crtc_destroy_state - default state destroy hook
221*4882a593Smuzhiyun * @crtc: drm CRTC
222*4882a593Smuzhiyun * @state: CRTC state object to release
223*4882a593Smuzhiyun *
224*4882a593Smuzhiyun * Default CRTC state destroy hook for drivers which don't have their own
225*4882a593Smuzhiyun * subclassed CRTC state structure.
226*4882a593Smuzhiyun */
drm_atomic_helper_crtc_destroy_state(struct drm_crtc * crtc,struct drm_crtc_state * state)227*4882a593Smuzhiyun void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
228*4882a593Smuzhiyun struct drm_crtc_state *state)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun __drm_atomic_helper_crtc_destroy_state(state);
231*4882a593Smuzhiyun kfree(state);
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun /**
236*4882a593Smuzhiyun * __drm_atomic_helper_plane_state_reset - resets plane state to default values
237*4882a593Smuzhiyun * @plane_state: atomic plane state, must not be NULL
238*4882a593Smuzhiyun * @plane: plane object, must not be NULL
239*4882a593Smuzhiyun *
240*4882a593Smuzhiyun * Initializes the newly allocated @plane_state with default
241*4882a593Smuzhiyun * values. This is useful for drivers that subclass the CRTC state.
242*4882a593Smuzhiyun */
__drm_atomic_helper_plane_state_reset(struct drm_plane_state * plane_state,struct drm_plane * plane)243*4882a593Smuzhiyun void __drm_atomic_helper_plane_state_reset(struct drm_plane_state *plane_state,
244*4882a593Smuzhiyun struct drm_plane *plane)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun plane_state->plane = plane;
247*4882a593Smuzhiyun plane_state->rotation = DRM_MODE_ROTATE_0;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun plane_state->alpha = DRM_BLEND_ALPHA_OPAQUE;
250*4882a593Smuzhiyun plane_state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_plane_state_reset);
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /**
255*4882a593Smuzhiyun * __drm_atomic_helper_plane_reset - reset state on plane
256*4882a593Smuzhiyun * @plane: drm plane
257*4882a593Smuzhiyun * @plane_state: plane state to assign
258*4882a593Smuzhiyun *
259*4882a593Smuzhiyun * Initializes the newly allocated @plane_state and assigns it to
260*4882a593Smuzhiyun * the &drm_crtc->state pointer of @plane, usually required when
261*4882a593Smuzhiyun * initializing the drivers or when called from the &drm_plane_funcs.reset
262*4882a593Smuzhiyun * hook.
263*4882a593Smuzhiyun *
264*4882a593Smuzhiyun * This is useful for drivers that subclass the plane state.
265*4882a593Smuzhiyun */
__drm_atomic_helper_plane_reset(struct drm_plane * plane,struct drm_plane_state * plane_state)266*4882a593Smuzhiyun void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
267*4882a593Smuzhiyun struct drm_plane_state *plane_state)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun if (plane_state)
270*4882a593Smuzhiyun __drm_atomic_helper_plane_state_reset(plane_state, plane);
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun plane->state = plane_state;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun /**
277*4882a593Smuzhiyun * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
278*4882a593Smuzhiyun * @plane: drm plane
279*4882a593Smuzhiyun *
280*4882a593Smuzhiyun * Resets the atomic state for @plane by freeing the state pointer (which might
281*4882a593Smuzhiyun * be NULL, e.g. at driver load time) and allocating a new empty state object.
282*4882a593Smuzhiyun */
drm_atomic_helper_plane_reset(struct drm_plane * plane)283*4882a593Smuzhiyun void drm_atomic_helper_plane_reset(struct drm_plane *plane)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun if (plane->state)
286*4882a593Smuzhiyun __drm_atomic_helper_plane_destroy_state(plane->state);
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun kfree(plane->state);
289*4882a593Smuzhiyun plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
290*4882a593Smuzhiyun if (plane->state)
291*4882a593Smuzhiyun __drm_atomic_helper_plane_reset(plane, plane->state);
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /**
296*4882a593Smuzhiyun * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
297*4882a593Smuzhiyun * @plane: plane object
298*4882a593Smuzhiyun * @state: atomic plane state
299*4882a593Smuzhiyun *
300*4882a593Smuzhiyun * Copies atomic state from a plane's current state. This is useful for
301*4882a593Smuzhiyun * drivers that subclass the plane state.
302*4882a593Smuzhiyun */
__drm_atomic_helper_plane_duplicate_state(struct drm_plane * plane,struct drm_plane_state * state)303*4882a593Smuzhiyun void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
304*4882a593Smuzhiyun struct drm_plane_state *state)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun memcpy(state, plane->state, sizeof(*state));
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun if (state->fb)
309*4882a593Smuzhiyun drm_framebuffer_get(state->fb);
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun state->fence = NULL;
312*4882a593Smuzhiyun state->commit = NULL;
313*4882a593Smuzhiyun state->fb_damage_clips = NULL;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun /**
318*4882a593Smuzhiyun * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
319*4882a593Smuzhiyun * @plane: drm plane
320*4882a593Smuzhiyun *
321*4882a593Smuzhiyun * Default plane state duplicate hook for drivers which don't have their own
322*4882a593Smuzhiyun * subclassed plane state structure.
323*4882a593Smuzhiyun */
324*4882a593Smuzhiyun struct drm_plane_state *
drm_atomic_helper_plane_duplicate_state(struct drm_plane * plane)325*4882a593Smuzhiyun drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun struct drm_plane_state *state;
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun if (WARN_ON(!plane->state))
330*4882a593Smuzhiyun return NULL;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun state = kmalloc(sizeof(*state), GFP_KERNEL);
333*4882a593Smuzhiyun if (state)
334*4882a593Smuzhiyun __drm_atomic_helper_plane_duplicate_state(plane, state);
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun return state;
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun /**
341*4882a593Smuzhiyun * __drm_atomic_helper_plane_destroy_state - release plane state
342*4882a593Smuzhiyun * @state: plane state object to release
343*4882a593Smuzhiyun *
344*4882a593Smuzhiyun * Releases all resources stored in the plane state without actually freeing
345*4882a593Smuzhiyun * the memory of the plane state. This is useful for drivers that subclass the
346*4882a593Smuzhiyun * plane state.
347*4882a593Smuzhiyun */
__drm_atomic_helper_plane_destroy_state(struct drm_plane_state * state)348*4882a593Smuzhiyun void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun if (state->fb)
351*4882a593Smuzhiyun drm_framebuffer_put(state->fb);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun if (state->fence)
354*4882a593Smuzhiyun dma_fence_put(state->fence);
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun if (state->commit)
357*4882a593Smuzhiyun drm_crtc_commit_put(state->commit);
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun drm_property_blob_put(state->fb_damage_clips);
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun /**
364*4882a593Smuzhiyun * drm_atomic_helper_plane_destroy_state - default state destroy hook
365*4882a593Smuzhiyun * @plane: drm plane
366*4882a593Smuzhiyun * @state: plane state object to release
367*4882a593Smuzhiyun *
368*4882a593Smuzhiyun * Default plane state destroy hook for drivers which don't have their own
369*4882a593Smuzhiyun * subclassed plane state structure.
370*4882a593Smuzhiyun */
drm_atomic_helper_plane_destroy_state(struct drm_plane * plane,struct drm_plane_state * state)371*4882a593Smuzhiyun void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
372*4882a593Smuzhiyun struct drm_plane_state *state)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun __drm_atomic_helper_plane_destroy_state(state);
375*4882a593Smuzhiyun kfree(state);
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun /**
380*4882a593Smuzhiyun * __drm_atomic_helper_connector_state_reset - reset the connector state
381*4882a593Smuzhiyun * @conn_state: atomic connector state, must not be NULL
382*4882a593Smuzhiyun * @connector: connectotr object, must not be NULL
383*4882a593Smuzhiyun *
384*4882a593Smuzhiyun * Initializes the newly allocated @conn_state with default
385*4882a593Smuzhiyun * values. This is useful for drivers that subclass the connector state.
386*4882a593Smuzhiyun */
387*4882a593Smuzhiyun void
__drm_atomic_helper_connector_state_reset(struct drm_connector_state * conn_state,struct drm_connector * connector)388*4882a593Smuzhiyun __drm_atomic_helper_connector_state_reset(struct drm_connector_state *conn_state,
389*4882a593Smuzhiyun struct drm_connector *connector)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun conn_state->connector = connector;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_connector_state_reset);
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun /**
396*4882a593Smuzhiyun * __drm_atomic_helper_connector_reset - reset state on connector
397*4882a593Smuzhiyun * @connector: drm connector
398*4882a593Smuzhiyun * @conn_state: connector state to assign
399*4882a593Smuzhiyun *
400*4882a593Smuzhiyun * Initializes the newly allocated @conn_state and assigns it to
401*4882a593Smuzhiyun * the &drm_connector->state pointer of @connector, usually required when
402*4882a593Smuzhiyun * initializing the drivers or when called from the &drm_connector_funcs.reset
403*4882a593Smuzhiyun * hook.
404*4882a593Smuzhiyun *
405*4882a593Smuzhiyun * This is useful for drivers that subclass the connector state.
406*4882a593Smuzhiyun */
407*4882a593Smuzhiyun void
__drm_atomic_helper_connector_reset(struct drm_connector * connector,struct drm_connector_state * conn_state)408*4882a593Smuzhiyun __drm_atomic_helper_connector_reset(struct drm_connector *connector,
409*4882a593Smuzhiyun struct drm_connector_state *conn_state)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun if (conn_state)
412*4882a593Smuzhiyun __drm_atomic_helper_connector_state_reset(conn_state, connector);
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun connector->state = conn_state;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun /**
419*4882a593Smuzhiyun * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
420*4882a593Smuzhiyun * @connector: drm connector
421*4882a593Smuzhiyun *
422*4882a593Smuzhiyun * Resets the atomic state for @connector by freeing the state pointer (which
423*4882a593Smuzhiyun * might be NULL, e.g. at driver load time) and allocating a new empty state
424*4882a593Smuzhiyun * object.
425*4882a593Smuzhiyun */
drm_atomic_helper_connector_reset(struct drm_connector * connector)426*4882a593Smuzhiyun void drm_atomic_helper_connector_reset(struct drm_connector *connector)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun struct drm_connector_state *conn_state =
429*4882a593Smuzhiyun kzalloc(sizeof(*conn_state), GFP_KERNEL);
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun if (connector->state)
432*4882a593Smuzhiyun __drm_atomic_helper_connector_destroy_state(connector->state);
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun kfree(connector->state);
435*4882a593Smuzhiyun __drm_atomic_helper_connector_reset(connector, conn_state);
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun /**
440*4882a593Smuzhiyun * drm_atomic_helper_connector_tv_reset - Resets TV connector properties
441*4882a593Smuzhiyun * @connector: DRM connector
442*4882a593Smuzhiyun *
443*4882a593Smuzhiyun * Resets the TV-related properties attached to a connector.
444*4882a593Smuzhiyun */
drm_atomic_helper_connector_tv_reset(struct drm_connector * connector)445*4882a593Smuzhiyun void drm_atomic_helper_connector_tv_reset(struct drm_connector *connector)
446*4882a593Smuzhiyun {
447*4882a593Smuzhiyun struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
448*4882a593Smuzhiyun struct drm_connector_state *state = connector->state;
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun state->tv.margins.left = cmdline->tv_margins.left;
451*4882a593Smuzhiyun state->tv.margins.right = cmdline->tv_margins.right;
452*4882a593Smuzhiyun state->tv.margins.top = cmdline->tv_margins.top;
453*4882a593Smuzhiyun state->tv.margins.bottom = cmdline->tv_margins.bottom;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_helper_connector_tv_reset);
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun /**
458*4882a593Smuzhiyun * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
459*4882a593Smuzhiyun * @connector: connector object
460*4882a593Smuzhiyun * @state: atomic connector state
461*4882a593Smuzhiyun *
462*4882a593Smuzhiyun * Copies atomic state from a connector's current state. This is useful for
463*4882a593Smuzhiyun * drivers that subclass the connector state.
464*4882a593Smuzhiyun */
465*4882a593Smuzhiyun void
__drm_atomic_helper_connector_duplicate_state(struct drm_connector * connector,struct drm_connector_state * state)466*4882a593Smuzhiyun __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
467*4882a593Smuzhiyun struct drm_connector_state *state)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun memcpy(state, connector->state, sizeof(*state));
470*4882a593Smuzhiyun if (state->crtc)
471*4882a593Smuzhiyun drm_connector_get(connector);
472*4882a593Smuzhiyun state->commit = NULL;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun if (state->hdr_output_metadata)
475*4882a593Smuzhiyun drm_property_blob_get(state->hdr_output_metadata);
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun /* Don't copy over a writeback job, they are used only once */
478*4882a593Smuzhiyun state->writeback_job = NULL;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun /**
483*4882a593Smuzhiyun * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
484*4882a593Smuzhiyun * @connector: drm connector
485*4882a593Smuzhiyun *
486*4882a593Smuzhiyun * Default connector state duplicate hook for drivers which don't have their own
487*4882a593Smuzhiyun * subclassed connector state structure.
488*4882a593Smuzhiyun */
489*4882a593Smuzhiyun struct drm_connector_state *
drm_atomic_helper_connector_duplicate_state(struct drm_connector * connector)490*4882a593Smuzhiyun drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
491*4882a593Smuzhiyun {
492*4882a593Smuzhiyun struct drm_connector_state *state;
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun if (WARN_ON(!connector->state))
495*4882a593Smuzhiyun return NULL;
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun state = kmalloc(sizeof(*state), GFP_KERNEL);
498*4882a593Smuzhiyun if (state)
499*4882a593Smuzhiyun __drm_atomic_helper_connector_duplicate_state(connector, state);
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun return state;
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun /**
506*4882a593Smuzhiyun * __drm_atomic_helper_connector_destroy_state - release connector state
507*4882a593Smuzhiyun * @state: connector state object to release
508*4882a593Smuzhiyun *
509*4882a593Smuzhiyun * Releases all resources stored in the connector state without actually
510*4882a593Smuzhiyun * freeing the memory of the connector state. This is useful for drivers that
511*4882a593Smuzhiyun * subclass the connector state.
512*4882a593Smuzhiyun */
513*4882a593Smuzhiyun void
__drm_atomic_helper_connector_destroy_state(struct drm_connector_state * state)514*4882a593Smuzhiyun __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun if (state->crtc)
517*4882a593Smuzhiyun drm_connector_put(state->connector);
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun if (state->commit)
520*4882a593Smuzhiyun drm_crtc_commit_put(state->commit);
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun if (state->writeback_job)
523*4882a593Smuzhiyun drm_writeback_cleanup_job(state->writeback_job);
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun drm_property_blob_put(state->hdr_output_metadata);
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun /**
530*4882a593Smuzhiyun * drm_atomic_helper_connector_destroy_state - default state destroy hook
531*4882a593Smuzhiyun * @connector: drm connector
532*4882a593Smuzhiyun * @state: connector state object to release
533*4882a593Smuzhiyun *
534*4882a593Smuzhiyun * Default connector state destroy hook for drivers which don't have their own
535*4882a593Smuzhiyun * subclassed connector state structure.
536*4882a593Smuzhiyun */
drm_atomic_helper_connector_destroy_state(struct drm_connector * connector,struct drm_connector_state * state)537*4882a593Smuzhiyun void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
538*4882a593Smuzhiyun struct drm_connector_state *state)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun __drm_atomic_helper_connector_destroy_state(state);
541*4882a593Smuzhiyun kfree(state);
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun /**
546*4882a593Smuzhiyun * __drm_atomic_helper_private_duplicate_state - copy atomic private state
547*4882a593Smuzhiyun * @obj: CRTC object
548*4882a593Smuzhiyun * @state: new private object state
549*4882a593Smuzhiyun *
550*4882a593Smuzhiyun * Copies atomic state from a private objects's current state and resets inferred values.
551*4882a593Smuzhiyun * This is useful for drivers that subclass the private state.
552*4882a593Smuzhiyun */
__drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj * obj,struct drm_private_state * state)553*4882a593Smuzhiyun void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
554*4882a593Smuzhiyun struct drm_private_state *state)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun memcpy(state, obj->state, sizeof(*state));
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun /**
561*4882a593Smuzhiyun * __drm_atomic_helper_bridge_duplicate_state() - Copy atomic bridge state
562*4882a593Smuzhiyun * @bridge: bridge object
563*4882a593Smuzhiyun * @state: atomic bridge state
564*4882a593Smuzhiyun *
565*4882a593Smuzhiyun * Copies atomic state from a bridge's current state and resets inferred values.
566*4882a593Smuzhiyun * This is useful for drivers that subclass the bridge state.
567*4882a593Smuzhiyun */
__drm_atomic_helper_bridge_duplicate_state(struct drm_bridge * bridge,struct drm_bridge_state * state)568*4882a593Smuzhiyun void __drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge,
569*4882a593Smuzhiyun struct drm_bridge_state *state)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun __drm_atomic_helper_private_obj_duplicate_state(&bridge->base,
572*4882a593Smuzhiyun &state->base);
573*4882a593Smuzhiyun state->bridge = bridge;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_bridge_duplicate_state);
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun /**
578*4882a593Smuzhiyun * drm_atomic_helper_bridge_duplicate_state() - Duplicate a bridge state object
579*4882a593Smuzhiyun * @bridge: bridge object
580*4882a593Smuzhiyun *
581*4882a593Smuzhiyun * Allocates a new bridge state and initializes it with the current bridge
582*4882a593Smuzhiyun * state values. This helper is meant to be used as a bridge
583*4882a593Smuzhiyun * &drm_bridge_funcs.atomic_duplicate_state hook for bridges that don't
584*4882a593Smuzhiyun * subclass the bridge state.
585*4882a593Smuzhiyun */
586*4882a593Smuzhiyun struct drm_bridge_state *
drm_atomic_helper_bridge_duplicate_state(struct drm_bridge * bridge)587*4882a593Smuzhiyun drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge)
588*4882a593Smuzhiyun {
589*4882a593Smuzhiyun struct drm_bridge_state *new;
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun if (WARN_ON(!bridge->base.state))
592*4882a593Smuzhiyun return NULL;
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun new = kzalloc(sizeof(*new), GFP_KERNEL);
595*4882a593Smuzhiyun if (new)
596*4882a593Smuzhiyun __drm_atomic_helper_bridge_duplicate_state(bridge, new);
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun return new;
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_helper_bridge_duplicate_state);
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun /**
603*4882a593Smuzhiyun * drm_atomic_helper_bridge_destroy_state() - Destroy a bridge state object
604*4882a593Smuzhiyun * @bridge: the bridge this state refers to
605*4882a593Smuzhiyun * @state: bridge state to destroy
606*4882a593Smuzhiyun *
607*4882a593Smuzhiyun * Destroys a bridge state previously created by
608*4882a593Smuzhiyun * &drm_atomic_helper_bridge_reset() or
609*4882a593Smuzhiyun * &drm_atomic_helper_bridge_duplicate_state(). This helper is meant to be
610*4882a593Smuzhiyun * used as a bridge &drm_bridge_funcs.atomic_destroy_state hook for bridges
611*4882a593Smuzhiyun * that don't subclass the bridge state.
612*4882a593Smuzhiyun */
drm_atomic_helper_bridge_destroy_state(struct drm_bridge * bridge,struct drm_bridge_state * state)613*4882a593Smuzhiyun void drm_atomic_helper_bridge_destroy_state(struct drm_bridge *bridge,
614*4882a593Smuzhiyun struct drm_bridge_state *state)
615*4882a593Smuzhiyun {
616*4882a593Smuzhiyun kfree(state);
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_helper_bridge_destroy_state);
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun /**
621*4882a593Smuzhiyun * __drm_atomic_helper_bridge_reset() - Initialize a bridge state to its
622*4882a593Smuzhiyun * default
623*4882a593Smuzhiyun * @bridge: the bridge this state refers to
624*4882a593Smuzhiyun * @state: bridge state to initialize
625*4882a593Smuzhiyun *
626*4882a593Smuzhiyun * Initializes the bridge state to default values. This is meant to be called
627*4882a593Smuzhiyun * by the bridge &drm_bridge_funcs.atomic_reset hook for bridges that subclass
628*4882a593Smuzhiyun * the bridge state.
629*4882a593Smuzhiyun */
__drm_atomic_helper_bridge_reset(struct drm_bridge * bridge,struct drm_bridge_state * state)630*4882a593Smuzhiyun void __drm_atomic_helper_bridge_reset(struct drm_bridge *bridge,
631*4882a593Smuzhiyun struct drm_bridge_state *state)
632*4882a593Smuzhiyun {
633*4882a593Smuzhiyun memset(state, 0, sizeof(*state));
634*4882a593Smuzhiyun state->bridge = bridge;
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun EXPORT_SYMBOL(__drm_atomic_helper_bridge_reset);
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun /**
639*4882a593Smuzhiyun * drm_atomic_helper_bridge_reset() - Allocate and initialize a bridge state
640*4882a593Smuzhiyun * to its default
641*4882a593Smuzhiyun * @bridge: the bridge this state refers to
642*4882a593Smuzhiyun *
643*4882a593Smuzhiyun * Allocates the bridge state and initializes it to default values. This helper
644*4882a593Smuzhiyun * is meant to be used as a bridge &drm_bridge_funcs.atomic_reset hook for
645*4882a593Smuzhiyun * bridges that don't subclass the bridge state.
646*4882a593Smuzhiyun */
647*4882a593Smuzhiyun struct drm_bridge_state *
drm_atomic_helper_bridge_reset(struct drm_bridge * bridge)648*4882a593Smuzhiyun drm_atomic_helper_bridge_reset(struct drm_bridge *bridge)
649*4882a593Smuzhiyun {
650*4882a593Smuzhiyun struct drm_bridge_state *bridge_state;
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun bridge_state = kzalloc(sizeof(*bridge_state), GFP_KERNEL);
653*4882a593Smuzhiyun if (!bridge_state)
654*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
655*4882a593Smuzhiyun
656*4882a593Smuzhiyun __drm_atomic_helper_bridge_reset(bridge, bridge_state);
657*4882a593Smuzhiyun return bridge_state;
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_helper_bridge_reset);
660