Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[sfrench/cifs-2.6.git] / drivers / gpu / drm / drm_atomic_state_helper.c
1 /*
2  * Copyright (C) 2018 Intel Corp.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  * Authors:
23  * Rob Clark <robdclark@gmail.com>
24  * Daniel Vetter <daniel.vetter@ffwll.ch>
25  */
26
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>
34
35 #include <linux/slab.h>
36 #include <linux/dma-fence.h>
37
38 /**
39  * DOC: atomic state reset and initialization
40  *
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
46  * driver).
47  *
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.
51  *
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.
57  */
58
59 /**
60  * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
61  * @crtc: drm CRTC
62  *
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.
65  */
66 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
67 {
68         if (crtc->state)
69                 __drm_atomic_helper_crtc_destroy_state(crtc->state);
70
71         kfree(crtc->state);
72         crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
73
74         if (crtc->state)
75                 crtc->state->crtc = crtc;
76 }
77 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
78
79 /**
80  * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
81  * @crtc: CRTC object
82  * @state: atomic CRTC state
83  *
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.
86  */
87 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
88                                               struct drm_crtc_state *state)
89 {
90         memcpy(state, crtc->state, sizeof(*state));
91
92         if (state->mode_blob)
93                 drm_property_blob_get(state->mode_blob);
94         if (state->degamma_lut)
95                 drm_property_blob_get(state->degamma_lut);
96         if (state->ctm)
97                 drm_property_blob_get(state->ctm);
98         if (state->gamma_lut)
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;
107         state->event = NULL;
108         state->pageflip_flags = 0;
109 }
110 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
111
112 /**
113  * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
114  * @crtc: drm CRTC
115  *
116  * Default CRTC state duplicate hook for drivers which don't have their own
117  * subclassed CRTC state structure.
118  */
119 struct drm_crtc_state *
120 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
121 {
122         struct drm_crtc_state *state;
123
124         if (WARN_ON(!crtc->state))
125                 return NULL;
126
127         state = kmalloc(sizeof(*state), GFP_KERNEL);
128         if (state)
129                 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
130
131         return state;
132 }
133 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
134
135 /**
136  * __drm_atomic_helper_crtc_destroy_state - release CRTC state
137  * @state: CRTC state object to release
138  *
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
141  * CRTC state.
142  */
143 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
144 {
145         if (state->commit) {
146                 /*
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.
151                  *
152                  * state->event may be freed, so we can't directly look at
153                  * state->event->base.completion.
154                  */
155                 if (state->event && state->commit->abort_completion)
156                         drm_crtc_commit_put(state->commit);
157
158                 kfree(state->commit->event);
159                 state->commit->event = NULL;
160
161                 drm_crtc_commit_put(state->commit);
162         }
163
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);
168 }
169 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
170
171 /**
172  * drm_atomic_helper_crtc_destroy_state - default state destroy hook
173  * @crtc: drm CRTC
174  * @state: CRTC state object to release
175  *
176  * Default CRTC state destroy hook for drivers which don't have their own
177  * subclassed CRTC state structure.
178  */
179 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
180                                           struct drm_crtc_state *state)
181 {
182         __drm_atomic_helper_crtc_destroy_state(state);
183         kfree(state);
184 }
185 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
186
187 /**
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
191  *
192  * Initializes plane state to default. This is useful for drivers that subclass
193  * the plane state.
194  */
195 void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
196                                      struct drm_plane_state *state)
197 {
198         state->plane = plane;
199         state->rotation = DRM_MODE_ROTATE_0;
200
201         state->alpha = DRM_BLEND_ALPHA_OPAQUE;
202         state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
203
204         plane->state = state;
205 }
206 EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
207
208 /**
209  * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
210  * @plane: drm plane
211  *
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.
214  */
215 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
216 {
217         if (plane->state)
218                 __drm_atomic_helper_plane_destroy_state(plane->state);
219
220         kfree(plane->state);
221         plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
222         if (plane->state)
223                 __drm_atomic_helper_plane_reset(plane, plane->state);
224 }
225 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
226
227 /**
228  * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
229  * @plane: plane object
230  * @state: atomic plane state
231  *
232  * Copies atomic state from a plane's current state. This is useful for
233  * drivers that subclass the plane state.
234  */
235 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
236                                                struct drm_plane_state *state)
237 {
238         memcpy(state, plane->state, sizeof(*state));
239
240         if (state->fb)
241                 drm_framebuffer_get(state->fb);
242
243         state->fence = NULL;
244         state->commit = NULL;
245         state->fb_damage_clips = NULL;
246 }
247 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
248
249 /**
250  * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
251  * @plane: drm plane
252  *
253  * Default plane state duplicate hook for drivers which don't have their own
254  * subclassed plane state structure.
255  */
256 struct drm_plane_state *
257 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
258 {
259         struct drm_plane_state *state;
260
261         if (WARN_ON(!plane->state))
262                 return NULL;
263
264         state = kmalloc(sizeof(*state), GFP_KERNEL);
265         if (state)
266                 __drm_atomic_helper_plane_duplicate_state(plane, state);
267
268         return state;
269 }
270 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
271
272 /**
273  * __drm_atomic_helper_plane_destroy_state - release plane state
274  * @state: plane state object to release
275  *
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
278  * plane state.
279  */
280 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
281 {
282         if (state->fb)
283                 drm_framebuffer_put(state->fb);
284
285         if (state->fence)
286                 dma_fence_put(state->fence);
287
288         if (state->commit)
289                 drm_crtc_commit_put(state->commit);
290
291         drm_property_blob_put(state->fb_damage_clips);
292 }
293 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
294
295 /**
296  * drm_atomic_helper_plane_destroy_state - default state destroy hook
297  * @plane: drm plane
298  * @state: plane state object to release
299  *
300  * Default plane state destroy hook for drivers which don't have their own
301  * subclassed plane state structure.
302  */
303 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
304                                            struct drm_plane_state *state)
305 {
306         __drm_atomic_helper_plane_destroy_state(state);
307         kfree(state);
308 }
309 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
310
311 /**
312  * __drm_atomic_helper_connector_reset - reset state on connector
313  * @connector: drm connector
314  * @conn_state: connector state to assign
315  *
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
319  * hook.
320  *
321  * This is useful for drivers that subclass the connector state.
322  */
323 void
324 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
325                                     struct drm_connector_state *conn_state)
326 {
327         if (conn_state)
328                 conn_state->connector = connector;
329
330         connector->state = conn_state;
331 }
332 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
333
334 /**
335  * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
336  * @connector: drm connector
337  *
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
340  * object.
341  */
342 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
343 {
344         struct drm_connector_state *conn_state =
345                 kzalloc(sizeof(*conn_state), GFP_KERNEL);
346
347         if (connector->state)
348                 __drm_atomic_helper_connector_destroy_state(connector->state);
349
350         kfree(connector->state);
351         __drm_atomic_helper_connector_reset(connector, conn_state);
352 }
353 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
354
355 /**
356  * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
357  * @connector: connector object
358  * @state: atomic connector state
359  *
360  * Copies atomic state from a connector's current state. This is useful for
361  * drivers that subclass the connector state.
362  */
363 void
364 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
365                                             struct drm_connector_state *state)
366 {
367         memcpy(state, connector->state, sizeof(*state));
368         if (state->crtc)
369                 drm_connector_get(connector);
370         state->commit = NULL;
371
372         /* Don't copy over a writeback job, they are used only once */
373         state->writeback_job = NULL;
374 }
375 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
376
377 /**
378  * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
379  * @connector: drm connector
380  *
381  * Default connector state duplicate hook for drivers which don't have their own
382  * subclassed connector state structure.
383  */
384 struct drm_connector_state *
385 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
386 {
387         struct drm_connector_state *state;
388
389         if (WARN_ON(!connector->state))
390                 return NULL;
391
392         state = kmalloc(sizeof(*state), GFP_KERNEL);
393         if (state)
394                 __drm_atomic_helper_connector_duplicate_state(connector, state);
395
396         return state;
397 }
398 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
399
400 /**
401  * __drm_atomic_helper_connector_destroy_state - release connector state
402  * @state: connector state object to release
403  *
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.
407  */
408 void
409 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
410 {
411         if (state->crtc)
412                 drm_connector_put(state->connector);
413
414         if (state->commit)
415                 drm_crtc_commit_put(state->commit);
416
417         if (state->writeback_job)
418                 drm_writeback_cleanup_job(state->writeback_job);
419 }
420 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
421
422 /**
423  * drm_atomic_helper_connector_destroy_state - default state destroy hook
424  * @connector: drm connector
425  * @state: connector state object to release
426  *
427  * Default connector state destroy hook for drivers which don't have their own
428  * subclassed connector state structure.
429  */
430 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
431                                           struct drm_connector_state *state)
432 {
433         __drm_atomic_helper_connector_destroy_state(state);
434         kfree(state);
435 }
436 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
437
438 /**
439  * __drm_atomic_helper_private_duplicate_state - copy atomic private state
440  * @obj: CRTC object
441  * @state: new private object state
442  *
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.
445  */
446 void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
447                                                      struct drm_private_state *state)
448 {
449         memcpy(state, obj->state, sizeof(*state));
450 }
451 EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);