]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/drm_atomic.c
drm/atomic: Introduce state->obj backpointers
[karo-tx-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->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
60
61         state->crtcs = kcalloc(dev->mode_config.num_crtc,
62                                sizeof(*state->crtcs), GFP_KERNEL);
63         if (!state->crtcs)
64                 goto fail;
65         state->crtc_states = kcalloc(dev->mode_config.num_crtc,
66                                      sizeof(*state->crtc_states), GFP_KERNEL);
67         if (!state->crtc_states)
68                 goto fail;
69         state->planes = kcalloc(dev->mode_config.num_total_plane,
70                                 sizeof(*state->planes), GFP_KERNEL);
71         if (!state->planes)
72                 goto fail;
73         state->plane_states = kcalloc(dev->mode_config.num_total_plane,
74                                       sizeof(*state->plane_states), GFP_KERNEL);
75         if (!state->plane_states)
76                 goto fail;
77         state->connectors = kcalloc(state->num_connector,
78                                     sizeof(*state->connectors),
79                                     GFP_KERNEL);
80         if (!state->connectors)
81                 goto fail;
82         state->connector_states = kcalloc(state->num_connector,
83                                           sizeof(*state->connector_states),
84                                           GFP_KERNEL);
85         if (!state->connector_states)
86                 goto fail;
87
88         state->dev = dev;
89
90         DRM_DEBUG_KMS("Allocate atomic state %p\n", state);
91
92         return state;
93 fail:
94         kfree_state(state);
95
96         return NULL;
97 }
98 EXPORT_SYMBOL(drm_atomic_state_alloc);
99
100 /**
101  * drm_atomic_state_clear - clear state object
102  * @state: atomic state
103  *
104  * When the w/w mutex algorithm detects a deadlock we need to back off and drop
105  * all locks. So someone else could sneak in and change the current modeset
106  * configuration. Which means that all the state assembled in @state is no
107  * longer an atomic update to the current state, but to some arbitrary earlier
108  * state. Which could break assumptions the driver's ->atomic_check likely
109  * relies on.
110  *
111  * Hence we must clear all cached state and completely start over, using this
112  * function.
113  */
114 void drm_atomic_state_clear(struct drm_atomic_state *state)
115 {
116         struct drm_device *dev = state->dev;
117         struct drm_mode_config *config = &dev->mode_config;
118         int i;
119
120         DRM_DEBUG_KMS("Clearing atomic state %p\n", state);
121
122         for (i = 0; i < state->num_connector; i++) {
123                 struct drm_connector *connector = state->connectors[i];
124
125                 if (!connector)
126                         continue;
127
128                 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
129
130                 connector->funcs->atomic_destroy_state(connector,
131                                                        state->connector_states[i]);
132         }
133
134         for (i = 0; i < config->num_crtc; i++) {
135                 struct drm_crtc *crtc = state->crtcs[i];
136
137                 if (!crtc)
138                         continue;
139
140                 crtc->funcs->atomic_destroy_state(crtc,
141                                                   state->crtc_states[i]);
142         }
143
144         for (i = 0; i < config->num_total_plane; i++) {
145                 struct drm_plane *plane = state->planes[i];
146
147                 if (!plane)
148                         continue;
149
150                 plane->funcs->atomic_destroy_state(plane,
151                                                    state->plane_states[i]);
152         }
153 }
154 EXPORT_SYMBOL(drm_atomic_state_clear);
155
156 /**
157  * drm_atomic_state_free - free all memory for an atomic state
158  * @state: atomic state to deallocate
159  *
160  * This frees all memory associated with an atomic state, including all the
161  * per-object state for planes, crtcs and connectors.
162  */
163 void drm_atomic_state_free(struct drm_atomic_state *state)
164 {
165         drm_atomic_state_clear(state);
166
167         DRM_DEBUG_KMS("Freeing atomic state %p\n", state);
168
169         kfree_state(state);
170 }
171 EXPORT_SYMBOL(drm_atomic_state_free);
172
173 /**
174  * drm_atomic_get_crtc_state - get crtc state
175  * @state: global atomic state object
176  * @crtc: crtc to get state object for
177  *
178  * This function returns the crtc state for the given crtc, allocating it if
179  * needed. It will also grab the relevant crtc lock to make sure that the state
180  * is consistent.
181  *
182  * Returns:
183  *
184  * Either the allocated state or the error code encoded into the pointer. When
185  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
186  * entire atomic sequence must be restarted. All other errors are fatal.
187  */
188 struct drm_crtc_state *
189 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
190                           struct drm_crtc *crtc)
191 {
192         int ret, index;
193         struct drm_crtc_state *crtc_state;
194
195         index = drm_crtc_index(crtc);
196
197         if (state->crtc_states[index])
198                 return state->crtc_states[index];
199
200         ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
201         if (ret)
202                 return ERR_PTR(ret);
203
204         crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
205         if (!crtc_state)
206                 return ERR_PTR(-ENOMEM);
207
208         state->crtc_states[index] = crtc_state;
209         state->crtcs[index] = crtc;
210         crtc_state->state = state;
211
212         DRM_DEBUG_KMS("Added [CRTC:%d] %p state to %p\n",
213                       crtc->base.id, crtc_state, state);
214
215         return crtc_state;
216 }
217 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
218
219 /**
220  * drm_atomic_get_plane_state - get plane state
221  * @state: global atomic state object
222  * @plane: plane to get state object for
223  *
224  * This function returns the plane state for the given plane, allocating it if
225  * needed. It will also grab the relevant plane lock to make sure that the state
226  * is consistent.
227  *
228  * Returns:
229  *
230  * Either the allocated state or the error code encoded into the pointer. When
231  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
232  * entire atomic sequence must be restarted. All other errors are fatal.
233  */
234 struct drm_plane_state *
235 drm_atomic_get_plane_state(struct drm_atomic_state *state,
236                           struct drm_plane *plane)
237 {
238         int ret, index;
239         struct drm_plane_state *plane_state;
240
241         index = drm_plane_index(plane);
242
243         if (state->plane_states[index])
244                 return state->plane_states[index];
245
246         ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
247         if (ret)
248                 return ERR_PTR(ret);
249
250         plane_state = plane->funcs->atomic_duplicate_state(plane);
251         if (!plane_state)
252                 return ERR_PTR(-ENOMEM);
253
254         state->plane_states[index] = plane_state;
255         state->planes[index] = plane;
256         plane_state->state = state;
257
258         DRM_DEBUG_KMS("Added [PLANE:%d] %p state to %p\n",
259                       plane->base.id, plane_state, state);
260
261         if (plane_state->crtc) {
262                 struct drm_crtc_state *crtc_state;
263
264                 crtc_state = drm_atomic_get_crtc_state(state,
265                                                        plane_state->crtc);
266                 if (IS_ERR(crtc_state))
267                         return ERR_CAST(crtc_state);
268         }
269
270         return plane_state;
271 }
272 EXPORT_SYMBOL(drm_atomic_get_plane_state);
273
274 /**
275  * drm_atomic_get_connector_state - get connector state
276  * @state: global atomic state object
277  * @connector: connector to get state object for
278  *
279  * This function returns the connector state for the given connector,
280  * allocating it if needed. It will also grab the relevant connector lock to
281  * make sure that the state is consistent.
282  *
283  * Returns:
284  *
285  * Either the allocated state or the error code encoded into the pointer. When
286  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
287  * entire atomic sequence must be restarted. All other errors are fatal.
288  */
289 struct drm_connector_state *
290 drm_atomic_get_connector_state(struct drm_atomic_state *state,
291                           struct drm_connector *connector)
292 {
293         int ret, index;
294         struct drm_mode_config *config = &connector->dev->mode_config;
295         struct drm_connector_state *connector_state;
296
297         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
298         if (ret)
299                 return ERR_PTR(ret);
300
301         index = drm_connector_index(connector);
302
303         /*
304          * Construction of atomic state updates can race with a connector
305          * hot-add which might overflow. In this case flip the table and just
306          * restart the entire ioctl - no one is fast enough to livelock a cpu
307          * with physical hotplug events anyway.
308          *
309          * Note that we only grab the indexes once we have the right lock to
310          * prevent hotplug/unplugging of connectors. So removal is no problem,
311          * at most the array is a bit too large.
312          */
313         if (index >= state->num_connector) {
314                 DRM_DEBUG_KMS("Hot-added connector would overflow state array, restarting\n");
315                 return ERR_PTR(-EAGAIN);
316         }
317
318         if (state->connector_states[index])
319                 return state->connector_states[index];
320
321         connector_state = connector->funcs->atomic_duplicate_state(connector);
322         if (!connector_state)
323                 return ERR_PTR(-ENOMEM);
324
325         state->connector_states[index] = connector_state;
326         state->connectors[index] = connector;
327         connector_state->state = state;
328
329         DRM_DEBUG_KMS("Added [CONNECTOR:%d] %p state to %p\n",
330                       connector->base.id, connector_state, state);
331
332         if (connector_state->crtc) {
333                 struct drm_crtc_state *crtc_state;
334
335                 crtc_state = drm_atomic_get_crtc_state(state,
336                                                        connector_state->crtc);
337                 if (IS_ERR(crtc_state))
338                         return ERR_CAST(crtc_state);
339         }
340
341         return connector_state;
342 }
343 EXPORT_SYMBOL(drm_atomic_get_connector_state);
344
345 /**
346  * drm_atomic_set_crtc_for_plane - set crtc for plane
347  * @plane_state: the plane whose incoming state to update
348  * @crtc: crtc to use for the plane
349  *
350  * Changing the assigned crtc for a plane requires us to grab the lock and state
351  * for the new crtc, as needed. This function takes care of all these details
352  * besides updating the pointer in the state object itself.
353  *
354  * Returns:
355  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
356  * then the w/w mutex code has detected a deadlock and the entire atomic
357  * sequence must be restarted. All other errors are fatal.
358  */
359 int
360 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
361                               struct drm_crtc *crtc)
362 {
363         struct drm_plane *plane = plane_state->plane;
364         struct drm_crtc_state *crtc_state;
365
366         if (plane_state->crtc) {
367                 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
368                                                        plane_state->crtc);
369                 if (WARN_ON(IS_ERR(crtc_state)))
370                         return PTR_ERR(crtc_state);
371
372                 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
373         }
374
375         plane_state->crtc = crtc;
376
377         if (crtc) {
378                 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
379                                                        crtc);
380                 if (IS_ERR(crtc_state))
381                         return PTR_ERR(crtc_state);
382                 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
383         }
384
385         if (crtc)
386                 DRM_DEBUG_KMS("Link plane state %p to [CRTC:%d]\n",
387                               plane_state, crtc->base.id);
388         else
389                 DRM_DEBUG_KMS("Link plane state %p to [NOCRTC]\n", plane_state);
390
391         return 0;
392 }
393 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
394
395 /**
396  * drm_atomic_set_fb_for_plane - set crtc for plane
397  * @plane_state: atomic state object for the plane
398  * @fb: fb to use for the plane
399  *
400  * Changing the assigned framebuffer for a plane requires us to grab a reference
401  * to the new fb and drop the reference to the old fb, if there is one. This
402  * function takes care of all these details besides updating the pointer in the
403  * state object itself.
404  */
405 void
406 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
407                             struct drm_framebuffer *fb)
408 {
409         if (plane_state->fb)
410                 drm_framebuffer_unreference(plane_state->fb);
411         if (fb)
412                 drm_framebuffer_reference(fb);
413         plane_state->fb = fb;
414
415         if (fb)
416                 DRM_DEBUG_KMS("Set [FB:%d] for plane state %p\n",
417                               fb->base.id, plane_state);
418         else
419                 DRM_DEBUG_KMS("Set [NOFB] for plane state %p\n", plane_state);
420 }
421 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
422
423 /**
424  * drm_atomic_set_crtc_for_connector - set crtc for connector
425  * @conn_state: atomic state object for the connector
426  * @crtc: crtc to use for the connector
427  *
428  * Changing the assigned crtc for a connector requires us to grab the lock and
429  * state for the new crtc, as needed. This function takes care of all these
430  * details besides updating the pointer in the state object itself.
431  *
432  * Returns:
433  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
434  * then the w/w mutex code has detected a deadlock and the entire atomic
435  * sequence must be restarted. All other errors are fatal.
436  */
437 int
438 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
439                                   struct drm_crtc *crtc)
440 {
441         struct drm_crtc_state *crtc_state;
442
443         if (crtc) {
444                 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
445                 if (IS_ERR(crtc_state))
446                         return PTR_ERR(crtc_state);
447         }
448
449         conn_state->crtc = crtc;
450
451         if (crtc)
452                 DRM_DEBUG_KMS("Link connector state %p to [CRTC:%d]\n",
453                               conn_state, crtc->base.id);
454         else
455                 DRM_DEBUG_KMS("Link connector state %p to [NOCRTC]\n",
456                               conn_state);
457
458         return 0;
459 }
460 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
461
462 /**
463  * drm_atomic_add_affected_connectors - add connectors for crtc
464  * @state: atomic state
465  * @crtc: DRM crtc
466  *
467  * This function walks the current configuration and adds all connectors
468  * currently using @crtc to the atomic configuration @state. Note that this
469  * function must acquire the connection mutex. This can potentially cause
470  * unneeded seralization if the update is just for the planes on one crtc. Hence
471  * drivers and helpers should only call this when really needed (e.g. when a
472  * full modeset needs to happen due to some change).
473  *
474  * Returns:
475  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
476  * then the w/w mutex code has detected a deadlock and the entire atomic
477  * sequence must be restarted. All other errors are fatal.
478  */
479 int
480 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
481                                    struct drm_crtc *crtc)
482 {
483         struct drm_mode_config *config = &state->dev->mode_config;
484         struct drm_connector *connector;
485         struct drm_connector_state *conn_state;
486         int ret;
487
488         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
489         if (ret)
490                 return ret;
491
492         DRM_DEBUG_KMS("Adding all current connectors for [CRTC:%d] to %p\n",
493                       crtc->base.id, state);
494
495         /*
496          * Changed connectors are already in @state, so only need to look at the
497          * current configuration.
498          */
499         list_for_each_entry(connector, &config->connector_list, head) {
500                 if (connector->state->crtc != crtc)
501                         continue;
502
503                 conn_state = drm_atomic_get_connector_state(state, connector);
504                 if (IS_ERR(conn_state))
505                         return PTR_ERR(conn_state);
506         }
507
508         return 0;
509 }
510 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
511
512 /**
513  * drm_atomic_connectors_for_crtc - count number of connected outputs
514  * @state: atomic state
515  * @crtc: DRM crtc
516  *
517  * This function counts all connectors which will be connected to @crtc
518  * according to @state. Useful to recompute the enable state for @crtc.
519  */
520 int
521 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
522                                struct drm_crtc *crtc)
523 {
524         int i, num_connected_connectors = 0;
525
526         for (i = 0; i < state->num_connector; i++) {
527                 struct drm_connector_state *conn_state;
528
529                 conn_state = state->connector_states[i];
530
531                 if (conn_state && conn_state->crtc == crtc)
532                         num_connected_connectors++;
533         }
534
535         DRM_DEBUG_KMS("State %p has %i connectors for [CRTC:%d]\n",
536                       state, num_connected_connectors, crtc->base.id);
537
538         return num_connected_connectors;
539 }
540 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
541
542 /**
543  * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
544  * @state: atomic state
545  *
546  * This function should be used by legacy entry points which don't understand
547  * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
548  *  the slowpath completed.
549  */
550 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
551 {
552         int ret;
553
554 retry:
555         drm_modeset_backoff(state->acquire_ctx);
556
557         ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
558                                state->acquire_ctx);
559         if (ret)
560                 goto retry;
561         ret = drm_modeset_lock_all_crtcs(state->dev,
562                                          state->acquire_ctx);
563         if (ret)
564                 goto retry;
565 }
566 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
567
568 /**
569  * drm_atomic_check_only - check whether a given config would work
570  * @state: atomic configuration to check
571  *
572  * Note that this function can return -EDEADLK if the driver needed to acquire
573  * more locks but encountered a deadlock. The caller must then do the usual w/w
574  * backoff dance and restart. All other errors are fatal.
575  *
576  * Returns:
577  * 0 on success, negative error code on failure.
578  */
579 int drm_atomic_check_only(struct drm_atomic_state *state)
580 {
581         struct drm_mode_config *config = &state->dev->mode_config;
582
583         DRM_DEBUG_KMS("checking %p\n", state);
584
585         if (config->funcs->atomic_check)
586                 return config->funcs->atomic_check(state->dev, state);
587         else
588                 return 0;
589 }
590 EXPORT_SYMBOL(drm_atomic_check_only);
591
592 /**
593  * drm_atomic_commit - commit configuration atomically
594  * @state: atomic configuration to check
595  *
596  * Note that this function can return -EDEADLK if the driver needed to acquire
597  * more locks but encountered a deadlock. The caller must then do the usual w/w
598  * backoff dance and restart. All other errors are fatal.
599  *
600  * Also note that on successful execution ownership of @state is transferred
601  * from the caller of this function to the function itself. The caller must not
602  * free or in any other way access @state. If the function fails then the caller
603  * must clean up @state itself.
604  *
605  * Returns:
606  * 0 on success, negative error code on failure.
607  */
608 int drm_atomic_commit(struct drm_atomic_state *state)
609 {
610         struct drm_mode_config *config = &state->dev->mode_config;
611         int ret;
612
613         ret = drm_atomic_check_only(state);
614         if (ret)
615                 return ret;
616
617         DRM_DEBUG_KMS("commiting %p\n", state);
618
619         return config->funcs->atomic_commit(state->dev, state, false);
620 }
621 EXPORT_SYMBOL(drm_atomic_commit);
622
623 /**
624  * drm_atomic_async_commit - atomic&async configuration commit
625  * @state: atomic configuration to check
626  *
627  * Note that this function can return -EDEADLK if the driver needed to acquire
628  * more locks but encountered a deadlock. The caller must then do the usual w/w
629  * backoff dance and restart. All other errors are fatal.
630  *
631  * Also note that on successful execution ownership of @state is transferred
632  * from the caller of this function to the function itself. The caller must not
633  * free or in any other way access @state. If the function fails then the caller
634  * must clean up @state itself.
635  *
636  * Returns:
637  * 0 on success, negative error code on failure.
638  */
639 int drm_atomic_async_commit(struct drm_atomic_state *state)
640 {
641         struct drm_mode_config *config = &state->dev->mode_config;
642         int ret;
643
644         ret = drm_atomic_check_only(state);
645         if (ret)
646                 return ret;
647
648         DRM_DEBUG_KMS("commiting %p asynchronously\n", state);
649
650         return config->funcs->atomic_commit(state->dev, state, true);
651 }
652 EXPORT_SYMBOL(drm_atomic_async_commit);