drm/atomic: Only destroy connector states with connection mutex held
[cascardo/linux.git] / drivers / gpu / drm / drm_atomic.c
1 /*
2  * Copyright (C) 2014 Red Hat
3  * Copyright (C) 2014 Intel Corp.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  * Rob Clark <robdclark@gmail.com>
25  * Daniel Vetter <daniel.vetter@ffwll.ch>
26  */
27
28
29 #include <drm/drmP.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_plane_helper.h>
32
33 static void kfree_state(struct drm_atomic_state *state)
34 {
35         kfree(state->connectors);
36         kfree(state->connector_states);
37         kfree(state->crtcs);
38         kfree(state->crtc_states);
39         kfree(state->planes);
40         kfree(state->plane_states);
41         kfree(state);
42 }
43
44 /**
45  * drm_atomic_state_alloc - allocate atomic state
46  * @dev: DRM device
47  *
48  * This allocates an empty atomic state to track updates.
49  */
50 struct drm_atomic_state *
51 drm_atomic_state_alloc(struct drm_device *dev)
52 {
53         struct drm_atomic_state *state;
54
55         state = kzalloc(sizeof(*state), GFP_KERNEL);
56         if (!state)
57                 return NULL;
58
59         state->crtcs = kcalloc(dev->mode_config.num_crtc,
60                                sizeof(*state->crtcs), GFP_KERNEL);
61         if (!state->crtcs)
62                 goto fail;
63         state->crtc_states = kcalloc(dev->mode_config.num_crtc,
64                                      sizeof(*state->crtc_states), GFP_KERNEL);
65         if (!state->crtc_states)
66                 goto fail;
67         state->planes = kcalloc(dev->mode_config.num_total_plane,
68                                 sizeof(*state->planes), GFP_KERNEL);
69         if (!state->planes)
70                 goto fail;
71         state->plane_states = kcalloc(dev->mode_config.num_total_plane,
72                                       sizeof(*state->plane_states), GFP_KERNEL);
73         if (!state->plane_states)
74                 goto fail;
75         state->connectors = kcalloc(dev->mode_config.num_connector,
76                                     sizeof(*state->connectors),
77                                     GFP_KERNEL);
78         if (!state->connectors)
79                 goto fail;
80         state->connector_states = kcalloc(dev->mode_config.num_connector,
81                                           sizeof(*state->connector_states),
82                                           GFP_KERNEL);
83         if (!state->connector_states)
84                 goto fail;
85
86         state->dev = dev;
87
88         DRM_DEBUG_KMS("Allocate atomic state %p\n", state);
89
90         return state;
91 fail:
92         kfree_state(state);
93
94         return NULL;
95 }
96 EXPORT_SYMBOL(drm_atomic_state_alloc);
97
98 /**
99  * drm_atomic_state_clear - clear state object
100  * @state: atomic state
101  *
102  * When the w/w mutex algorithm detects a deadlock we need to back off and drop
103  * all locks. So someone else could sneak in and change the current modeset
104  * configuration. Which means that all the state assembled in @state is no
105  * longer an atomic update to the current state, but to some arbitrary earlier
106  * state. Which could break assumptions the driver's ->atomic_check likely
107  * relies on.
108  *
109  * Hence we must clear all cached state and completely start over, using this
110  * function.
111  */
112 void drm_atomic_state_clear(struct drm_atomic_state *state)
113 {
114         struct drm_device *dev = state->dev;
115         struct drm_mode_config *config = &dev->mode_config;
116         int i;
117
118         DRM_DEBUG_KMS("Clearing atomic state %p\n", state);
119
120         for (i = 0; i < config->num_connector; i++) {
121                 struct drm_connector *connector = state->connectors[i];
122
123                 if (!connector)
124                         continue;
125
126                 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
127
128                 connector->funcs->atomic_destroy_state(connector,
129                                                        state->connector_states[i]);
130         }
131
132         for (i = 0; i < config->num_crtc; i++) {
133                 struct drm_crtc *crtc = state->crtcs[i];
134
135                 if (!crtc)
136                         continue;
137
138                 crtc->funcs->atomic_destroy_state(crtc,
139                                                   state->crtc_states[i]);
140         }
141
142         for (i = 0; i < config->num_total_plane; i++) {
143                 struct drm_plane *plane = state->planes[i];
144
145                 if (!plane)
146                         continue;
147
148                 plane->funcs->atomic_destroy_state(plane,
149                                                    state->plane_states[i]);
150         }
151 }
152 EXPORT_SYMBOL(drm_atomic_state_clear);
153
154 /**
155  * drm_atomic_state_free - free all memory for an atomic state
156  * @state: atomic state to deallocate
157  *
158  * This frees all memory associated with an atomic state, including all the
159  * per-object state for planes, crtcs and connectors.
160  */
161 void drm_atomic_state_free(struct drm_atomic_state *state)
162 {
163         drm_atomic_state_clear(state);
164
165         DRM_DEBUG_KMS("Freeing atomic state %p\n", state);
166
167         kfree_state(state);
168 }
169 EXPORT_SYMBOL(drm_atomic_state_free);
170
171 /**
172  * drm_atomic_get_crtc_state - get crtc state
173  * @state: global atomic state object
174  * @crtc: crtc to get state object for
175  *
176  * This function returns the crtc state for the given crtc, allocating it if
177  * needed. It will also grab the relevant crtc lock to make sure that the state
178  * is consistent.
179  *
180  * Returns:
181  *
182  * Either the allocated state or the error code encoded into the pointer. When
183  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
184  * entire atomic sequence must be restarted. All other errors are fatal.
185  */
186 struct drm_crtc_state *
187 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
188                           struct drm_crtc *crtc)
189 {
190         int ret, index;
191         struct drm_crtc_state *crtc_state;
192
193         index = drm_crtc_index(crtc);
194
195         if (state->crtc_states[index])
196                 return state->crtc_states[index];
197
198         ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
199         if (ret)
200                 return ERR_PTR(ret);
201
202         crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
203         if (!crtc_state)
204                 return ERR_PTR(-ENOMEM);
205
206         state->crtc_states[index] = crtc_state;
207         state->crtcs[index] = crtc;
208         crtc_state->state = state;
209
210         DRM_DEBUG_KMS("Added [CRTC:%d] %p state to %p\n",
211                       crtc->base.id, crtc_state, state);
212
213         return crtc_state;
214 }
215 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
216
217 /**
218  * drm_atomic_get_plane_state - get plane state
219  * @state: global atomic state object
220  * @plane: plane to get state object for
221  *
222  * This function returns the plane state for the given plane, allocating it if
223  * needed. It will also grab the relevant plane lock to make sure that the state
224  * is consistent.
225  *
226  * Returns:
227  *
228  * Either the allocated state or the error code encoded into the pointer. When
229  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
230  * entire atomic sequence must be restarted. All other errors are fatal.
231  */
232 struct drm_plane_state *
233 drm_atomic_get_plane_state(struct drm_atomic_state *state,
234                           struct drm_plane *plane)
235 {
236         int ret, index;
237         struct drm_plane_state *plane_state;
238
239         index = drm_plane_index(plane);
240
241         if (state->plane_states[index])
242                 return state->plane_states[index];
243
244         /*
245          * TODO: We currently don't have per-plane mutexes. So instead of trying
246          * crazy tricks with deferring plane->crtc and hoping for the best just
247          * grab all crtc locks. Once we have per-plane locks we must update this
248          * to only take the plane mutex.
249          */
250         ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
251         if (ret)
252                 return ERR_PTR(ret);
253
254         plane_state = plane->funcs->atomic_duplicate_state(plane);
255         if (!plane_state)
256                 return ERR_PTR(-ENOMEM);
257
258         state->plane_states[index] = plane_state;
259         state->planes[index] = plane;
260         plane_state->state = state;
261
262         DRM_DEBUG_KMS("Added [PLANE:%d] %p state to %p\n",
263                       plane->base.id, plane_state, state);
264
265         if (plane_state->crtc) {
266                 struct drm_crtc_state *crtc_state;
267
268                 crtc_state = drm_atomic_get_crtc_state(state,
269                                                        plane_state->crtc);
270                 if (IS_ERR(crtc_state))
271                         return ERR_CAST(crtc_state);
272         }
273
274         return plane_state;
275 }
276 EXPORT_SYMBOL(drm_atomic_get_plane_state);
277
278 /**
279  * drm_atomic_get_connector_state - get connector state
280  * @state: global atomic state object
281  * @connector: connector to get state object for
282  *
283  * This function returns the connector state for the given connector,
284  * allocating it if needed. It will also grab the relevant connector lock to
285  * make sure that the state is consistent.
286  *
287  * Returns:
288  *
289  * Either the allocated state or the error code encoded into the pointer. When
290  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
291  * entire atomic sequence must be restarted. All other errors are fatal.
292  */
293 struct drm_connector_state *
294 drm_atomic_get_connector_state(struct drm_atomic_state *state,
295                           struct drm_connector *connector)
296 {
297         int ret, index;
298         struct drm_mode_config *config = &connector->dev->mode_config;
299         struct drm_connector_state *connector_state;
300
301         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
302         if (ret)
303                 return ERR_PTR(ret);
304
305         index = drm_connector_index(connector);
306
307         if (state->connector_states[index])
308                 return state->connector_states[index];
309
310         connector_state = connector->funcs->atomic_duplicate_state(connector);
311         if (!connector_state)
312                 return ERR_PTR(-ENOMEM);
313
314         state->connector_states[index] = connector_state;
315         state->connectors[index] = connector;
316         connector_state->state = state;
317
318         DRM_DEBUG_KMS("Added [CONNECTOR:%d] %p state to %p\n",
319                       connector->base.id, connector_state, state);
320
321         if (connector_state->crtc) {
322                 struct drm_crtc_state *crtc_state;
323
324                 crtc_state = drm_atomic_get_crtc_state(state,
325                                                        connector_state->crtc);
326                 if (IS_ERR(crtc_state))
327                         return ERR_CAST(crtc_state);
328         }
329
330         return connector_state;
331 }
332 EXPORT_SYMBOL(drm_atomic_get_connector_state);
333
334 /**
335  * drm_atomic_set_crtc_for_plane - set crtc for plane
336  * @plane_state: atomic state object for the plane
337  * @crtc: crtc to use for the plane
338  *
339  * Changing the assigned crtc for a plane requires us to grab the lock and state
340  * for the new crtc, as needed. This function takes care of all these details
341  * besides updating the pointer in the state object itself.
342  *
343  * Returns:
344  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
345  * then the w/w mutex code has detected a deadlock and the entire atomic
346  * sequence must be restarted. All other errors are fatal.
347  */
348 int
349 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
350                               struct drm_crtc *crtc)
351 {
352         struct drm_crtc_state *crtc_state;
353
354         if (crtc) {
355                 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
356                                                        crtc);
357                 if (IS_ERR(crtc_state))
358                         return PTR_ERR(crtc_state);
359         }
360
361         plane_state->crtc = crtc;
362
363         if (crtc)
364                 DRM_DEBUG_KMS("Link plane state %p to [CRTC:%d]\n",
365                               plane_state, crtc->base.id);
366         else
367                 DRM_DEBUG_KMS("Link plane state %p to [NOCRTC]\n", plane_state);
368
369         return 0;
370 }
371 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
372
373 /**
374  * drm_atomic_set_fb_for_plane - set crtc for plane
375  * @plane_state: atomic state object for the plane
376  * @fb: fb to use for the plane
377  *
378  * Changing the assigned framebuffer for a plane requires us to grab a reference
379  * to the new fb and drop the reference to the old fb, if there is one. This
380  * function takes care of all these details besides updating the pointer in the
381  * state object itself.
382  */
383 void
384 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
385                             struct drm_framebuffer *fb)
386 {
387         if (plane_state->fb)
388                 drm_framebuffer_unreference(plane_state->fb);
389         if (fb)
390                 drm_framebuffer_reference(fb);
391         plane_state->fb = fb;
392
393         if (fb)
394                 DRM_DEBUG_KMS("Set [FB:%d] for plane state %p\n",
395                               fb->base.id, plane_state);
396         else
397                 DRM_DEBUG_KMS("Set [NOFB] for plane state %p\n", plane_state);
398 }
399 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
400
401 /**
402  * drm_atomic_set_crtc_for_connector - set crtc for connector
403  * @conn_state: atomic state object for the connector
404  * @crtc: crtc to use for the connector
405  *
406  * Changing the assigned crtc for a connector requires us to grab the lock and
407  * state for the new crtc, as needed. This function takes care of all these
408  * details besides updating the pointer in the state object itself.
409  *
410  * Returns:
411  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
412  * then the w/w mutex code has detected a deadlock and the entire atomic
413  * sequence must be restarted. All other errors are fatal.
414  */
415 int
416 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
417                                   struct drm_crtc *crtc)
418 {
419         struct drm_crtc_state *crtc_state;
420
421         if (crtc) {
422                 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
423                 if (IS_ERR(crtc_state))
424                         return PTR_ERR(crtc_state);
425         }
426
427         conn_state->crtc = crtc;
428
429         if (crtc)
430                 DRM_DEBUG_KMS("Link connector state %p to [CRTC:%d]\n",
431                               conn_state, crtc->base.id);
432         else
433                 DRM_DEBUG_KMS("Link connector state %p to [NOCRTC]\n",
434                               conn_state);
435
436         return 0;
437 }
438 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
439
440 /**
441  * drm_atomic_add_affected_connectors - add connectors for crtc
442  * @state: atomic state
443  * @crtc: DRM crtc
444  *
445  * This function walks the current configuration and adds all connectors
446  * currently using @crtc to the atomic configuration @state. Note that this
447  * function must acquire the connection mutex. This can potentially cause
448  * unneeded seralization if the update is just for the planes on one crtc. Hence
449  * drivers and helpers should only call this when really needed (e.g. when a
450  * full modeset needs to happen due to some change).
451  *
452  * Returns:
453  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
454  * then the w/w mutex code has detected a deadlock and the entire atomic
455  * sequence must be restarted. All other errors are fatal.
456  */
457 int
458 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
459                                    struct drm_crtc *crtc)
460 {
461         struct drm_mode_config *config = &state->dev->mode_config;
462         struct drm_connector *connector;
463         struct drm_connector_state *conn_state;
464         int ret;
465
466         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
467         if (ret)
468                 return ret;
469
470         DRM_DEBUG_KMS("Adding all current connectors for [CRTC:%d] to %p\n",
471                       crtc->base.id, state);
472
473         /*
474          * Changed connectors are already in @state, so only need to look at the
475          * current configuration.
476          */
477         list_for_each_entry(connector, &config->connector_list, head) {
478                 if (connector->state->crtc != crtc)
479                         continue;
480
481                 conn_state = drm_atomic_get_connector_state(state, connector);
482                 if (IS_ERR(conn_state))
483                         return PTR_ERR(conn_state);
484         }
485
486         return 0;
487 }
488 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
489
490 /**
491  * drm_atomic_connectors_for_crtc - count number of connected outputs
492  * @state: atomic state
493  * @crtc: DRM crtc
494  *
495  * This function counts all connectors which will be connected to @crtc
496  * according to @state. Useful to recompute the enable state for @crtc.
497  */
498 int
499 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
500                                struct drm_crtc *crtc)
501 {
502         int nconnectors = state->dev->mode_config.num_connector;
503         int i, num_connected_connectors = 0;
504
505         for (i = 0; i < nconnectors; i++) {
506                 struct drm_connector_state *conn_state;
507
508                 conn_state = state->connector_states[i];
509
510                 if (conn_state && conn_state->crtc == crtc)
511                         num_connected_connectors++;
512         }
513
514         DRM_DEBUG_KMS("State %p has %i connectors for [CRTC:%d]\n",
515                       state, num_connected_connectors, crtc->base.id);
516
517         return num_connected_connectors;
518 }
519 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
520
521 /**
522  * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
523  * @state: atomic state
524  *
525  * This function should be used by legacy entry points which don't understand
526  * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
527  *  the slowpath completed.
528  */
529 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
530 {
531         int ret;
532
533 retry:
534         drm_modeset_backoff(state->acquire_ctx);
535
536         ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
537                                state->acquire_ctx);
538         if (ret)
539                 goto retry;
540         ret = drm_modeset_lock_all_crtcs(state->dev,
541                                          state->acquire_ctx);
542         if (ret)
543                 goto retry;
544 }
545 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
546
547 /**
548  * drm_atomic_check_only - check whether a given config would work
549  * @state: atomic configuration to check
550  *
551  * Note that this function can return -EDEADLK if the driver needed to acquire
552  * more locks but encountered a deadlock. The caller must then do the usual w/w
553  * backoff dance and restart. All other errors are fatal.
554  *
555  * Returns:
556  * 0 on success, negative error code on failure.
557  */
558 int drm_atomic_check_only(struct drm_atomic_state *state)
559 {
560         struct drm_mode_config *config = &state->dev->mode_config;
561
562         DRM_DEBUG_KMS("checking %p\n", state);
563
564         if (config->funcs->atomic_check)
565                 return config->funcs->atomic_check(state->dev, state);
566         else
567                 return 0;
568 }
569 EXPORT_SYMBOL(drm_atomic_check_only);
570
571 /**
572  * drm_atomic_commit - commit configuration atomically
573  * @state: atomic configuration to check
574  *
575  * Note that this function can return -EDEADLK if the driver needed to acquire
576  * more locks but encountered a deadlock. The caller must then do the usual w/w
577  * backoff dance and restart. All other errors are fatal.
578  *
579  * Also note that on successful execution ownership of @state is transferred
580  * from the caller of this function to the function itself. The caller must not
581  * free or in any other way access @state. If the function fails then the caller
582  * must clean up @state itself.
583  *
584  * Returns:
585  * 0 on success, negative error code on failure.
586  */
587 int drm_atomic_commit(struct drm_atomic_state *state)
588 {
589         struct drm_mode_config *config = &state->dev->mode_config;
590         int ret;
591
592         ret = drm_atomic_check_only(state);
593         if (ret)
594                 return ret;
595
596         DRM_DEBUG_KMS("commiting %p\n", state);
597
598         return config->funcs->atomic_commit(state->dev, state, false);
599 }
600 EXPORT_SYMBOL(drm_atomic_commit);
601
602 /**
603  * drm_atomic_async_commit - atomic&async configuration commit
604  * @state: atomic configuration to check
605  *
606  * Note that this function can return -EDEADLK if the driver needed to acquire
607  * more locks but encountered a deadlock. The caller must then do the usual w/w
608  * backoff dance and restart. All other errors are fatal.
609  *
610  * Also note that on successful execution ownership of @state is transferred
611  * from the caller of this function to the function itself. The caller must not
612  * free or in any other way access @state. If the function fails then the caller
613  * must clean up @state itself.
614  *
615  * Returns:
616  * 0 on success, negative error code on failure.
617  */
618 int drm_atomic_async_commit(struct drm_atomic_state *state)
619 {
620         struct drm_mode_config *config = &state->dev->mode_config;
621         int ret;
622
623         ret = drm_atomic_check_only(state);
624         if (ret)
625                 return ret;
626
627         DRM_DEBUG_KMS("commiting %p asynchronously\n", state);
628
629         return config->funcs->atomic_commit(state->dev, state, true);
630 }
631 EXPORT_SYMBOL(drm_atomic_async_commit);