]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/drm_atomic.c
Merge tag 'v4.3-rc2' into topic/drm-misc
[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 /**
34  * drm_atomic_state_default_release -
35  * release memory initialized by drm_atomic_state_init
36  * @state: atomic state
37  *
38  * Free all the memory allocated by drm_atomic_state_init.
39  * This is useful for drivers that subclass the atomic state.
40  */
41 void drm_atomic_state_default_release(struct drm_atomic_state *state)
42 {
43         kfree(state->connectors);
44         kfree(state->connector_states);
45         kfree(state->crtcs);
46         kfree(state->crtc_states);
47         kfree(state->planes);
48         kfree(state->plane_states);
49 }
50 EXPORT_SYMBOL(drm_atomic_state_default_release);
51
52 /**
53  * drm_atomic_state_init - init new atomic state
54  * @dev: DRM device
55  * @state: atomic state
56  *
57  * Default implementation for filling in a new atomic state.
58  * This is useful for drivers that subclass the atomic state.
59  */
60 int
61 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
62 {
63         /* TODO legacy paths should maybe do a better job about
64          * setting this appropriately?
65          */
66         state->allow_modeset = true;
67
68         state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
69
70         state->crtcs = kcalloc(dev->mode_config.num_crtc,
71                                sizeof(*state->crtcs), GFP_KERNEL);
72         if (!state->crtcs)
73                 goto fail;
74         state->crtc_states = kcalloc(dev->mode_config.num_crtc,
75                                      sizeof(*state->crtc_states), GFP_KERNEL);
76         if (!state->crtc_states)
77                 goto fail;
78         state->planes = kcalloc(dev->mode_config.num_total_plane,
79                                 sizeof(*state->planes), GFP_KERNEL);
80         if (!state->planes)
81                 goto fail;
82         state->plane_states = kcalloc(dev->mode_config.num_total_plane,
83                                       sizeof(*state->plane_states), GFP_KERNEL);
84         if (!state->plane_states)
85                 goto fail;
86         state->connectors = kcalloc(state->num_connector,
87                                     sizeof(*state->connectors),
88                                     GFP_KERNEL);
89         if (!state->connectors)
90                 goto fail;
91         state->connector_states = kcalloc(state->num_connector,
92                                           sizeof(*state->connector_states),
93                                           GFP_KERNEL);
94         if (!state->connector_states)
95                 goto fail;
96
97         state->dev = dev;
98
99         DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
100
101         return 0;
102 fail:
103         drm_atomic_state_default_release(state);
104         return -ENOMEM;
105 }
106 EXPORT_SYMBOL(drm_atomic_state_init);
107
108 /**
109  * drm_atomic_state_alloc - allocate atomic state
110  * @dev: DRM device
111  *
112  * This allocates an empty atomic state to track updates.
113  */
114 struct drm_atomic_state *
115 drm_atomic_state_alloc(struct drm_device *dev)
116 {
117         struct drm_mode_config *config = &dev->mode_config;
118         struct drm_atomic_state *state;
119
120         if (!config->funcs->atomic_state_alloc) {
121                 state = kzalloc(sizeof(*state), GFP_KERNEL);
122                 if (!state)
123                         return NULL;
124                 if (drm_atomic_state_init(dev, state) < 0) {
125                         kfree(state);
126                         return NULL;
127                 }
128                 return state;
129         }
130
131         return config->funcs->atomic_state_alloc(dev);
132 }
133 EXPORT_SYMBOL(drm_atomic_state_alloc);
134
135 /**
136  * drm_atomic_state_default_clear - clear base atomic state
137  * @state: atomic state
138  *
139  * Default implementation for clearing atomic state.
140  * This is useful for drivers that subclass the atomic state.
141  */
142 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
143 {
144         struct drm_device *dev = state->dev;
145         struct drm_mode_config *config = &dev->mode_config;
146         int i;
147
148         DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
149
150         for (i = 0; i < state->num_connector; i++) {
151                 struct drm_connector *connector = state->connectors[i];
152
153                 if (!connector)
154                         continue;
155
156                 /*
157                  * FIXME: Async commits can race with connector unplugging and
158                  * there's currently nothing that prevents cleanup up state for
159                  * deleted connectors. As long as the callback doesn't look at
160                  * the connector we'll be fine though, so make sure that's the
161                  * case by setting all connector pointers to NULL.
162                  */
163                 state->connector_states[i]->connector = NULL;
164                 connector->funcs->atomic_destroy_state(NULL,
165                                                        state->connector_states[i]);
166                 state->connectors[i] = NULL;
167                 state->connector_states[i] = NULL;
168         }
169
170         for (i = 0; i < config->num_crtc; i++) {
171                 struct drm_crtc *crtc = state->crtcs[i];
172
173                 if (!crtc)
174                         continue;
175
176                 crtc->funcs->atomic_destroy_state(crtc,
177                                                   state->crtc_states[i]);
178                 state->crtcs[i] = NULL;
179                 state->crtc_states[i] = NULL;
180         }
181
182         for (i = 0; i < config->num_total_plane; i++) {
183                 struct drm_plane *plane = state->planes[i];
184
185                 if (!plane)
186                         continue;
187
188                 plane->funcs->atomic_destroy_state(plane,
189                                                    state->plane_states[i]);
190                 state->planes[i] = NULL;
191                 state->plane_states[i] = NULL;
192         }
193 }
194 EXPORT_SYMBOL(drm_atomic_state_default_clear);
195
196 /**
197  * drm_atomic_state_clear - clear state object
198  * @state: atomic state
199  *
200  * When the w/w mutex algorithm detects a deadlock we need to back off and drop
201  * all locks. So someone else could sneak in and change the current modeset
202  * configuration. Which means that all the state assembled in @state is no
203  * longer an atomic update to the current state, but to some arbitrary earlier
204  * state. Which could break assumptions the driver's ->atomic_check likely
205  * relies on.
206  *
207  * Hence we must clear all cached state and completely start over, using this
208  * function.
209  */
210 void drm_atomic_state_clear(struct drm_atomic_state *state)
211 {
212         struct drm_device *dev = state->dev;
213         struct drm_mode_config *config = &dev->mode_config;
214
215         if (config->funcs->atomic_state_clear)
216                 config->funcs->atomic_state_clear(state);
217         else
218                 drm_atomic_state_default_clear(state);
219 }
220 EXPORT_SYMBOL(drm_atomic_state_clear);
221
222 /**
223  * drm_atomic_state_free - free all memory for an atomic state
224  * @state: atomic state to deallocate
225  *
226  * This frees all memory associated with an atomic state, including all the
227  * per-object state for planes, crtcs and connectors.
228  */
229 void drm_atomic_state_free(struct drm_atomic_state *state)
230 {
231         struct drm_device *dev;
232         struct drm_mode_config *config;
233
234         if (!state)
235                 return;
236
237         dev = state->dev;
238         config = &dev->mode_config;
239
240         drm_atomic_state_clear(state);
241
242         DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
243
244         if (config->funcs->atomic_state_free) {
245                 config->funcs->atomic_state_free(state);
246         } else {
247                 drm_atomic_state_default_release(state);
248                 kfree(state);
249         }
250 }
251 EXPORT_SYMBOL(drm_atomic_state_free);
252
253 /**
254  * drm_atomic_get_crtc_state - get crtc state
255  * @state: global atomic state object
256  * @crtc: crtc to get state object for
257  *
258  * This function returns the crtc state for the given crtc, allocating it if
259  * needed. It will also grab the relevant crtc lock to make sure that the state
260  * is consistent.
261  *
262  * Returns:
263  *
264  * Either the allocated state or the error code encoded into the pointer. When
265  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
266  * entire atomic sequence must be restarted. All other errors are fatal.
267  */
268 struct drm_crtc_state *
269 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
270                           struct drm_crtc *crtc)
271 {
272         int ret, index = drm_crtc_index(crtc);
273         struct drm_crtc_state *crtc_state;
274
275         crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
276         if (crtc_state)
277                 return crtc_state;
278
279         ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
280         if (ret)
281                 return ERR_PTR(ret);
282
283         crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
284         if (!crtc_state)
285                 return ERR_PTR(-ENOMEM);
286
287         state->crtc_states[index] = crtc_state;
288         state->crtcs[index] = crtc;
289         crtc_state->state = state;
290
291         DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
292                          crtc->base.id, crtc_state, state);
293
294         return crtc_state;
295 }
296 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
297
298 /**
299  * drm_atomic_set_mode_for_crtc - set mode for CRTC
300  * @state: the CRTC whose incoming state to update
301  * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
302  *
303  * Set a mode (originating from the kernel) on the desired CRTC state. Does
304  * not change any other state properties, including enable, active, or
305  * mode_changed.
306  *
307  * RETURNS:
308  * Zero on success, error code on failure. Cannot return -EDEADLK.
309  */
310 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
311                                  struct drm_display_mode *mode)
312 {
313         struct drm_mode_modeinfo umode;
314
315         /* Early return for no change. */
316         if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
317                 return 0;
318
319         if (state->mode_blob)
320                 drm_property_unreference_blob(state->mode_blob);
321         state->mode_blob = NULL;
322
323         if (mode) {
324                 drm_mode_convert_to_umode(&umode, mode);
325                 state->mode_blob =
326                         drm_property_create_blob(state->crtc->dev,
327                                                  sizeof(umode),
328                                                  &umode);
329                 if (IS_ERR(state->mode_blob))
330                         return PTR_ERR(state->mode_blob);
331
332                 drm_mode_copy(&state->mode, mode);
333                 state->enable = true;
334                 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
335                                  mode->name, state);
336         } else {
337                 memset(&state->mode, 0, sizeof(state->mode));
338                 state->enable = false;
339                 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
340                                  state);
341         }
342
343         return 0;
344 }
345 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
346
347 /**
348  * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
349  * @state: the CRTC whose incoming state to update
350  * @blob: pointer to blob property to use for mode
351  *
352  * Set a mode (originating from a blob property) on the desired CRTC state.
353  * This function will take a reference on the blob property for the CRTC state,
354  * and release the reference held on the state's existing mode property, if any
355  * was set.
356  *
357  * RETURNS:
358  * Zero on success, error code on failure. Cannot return -EDEADLK.
359  */
360 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
361                                       struct drm_property_blob *blob)
362 {
363         if (blob == state->mode_blob)
364                 return 0;
365
366         if (state->mode_blob)
367                 drm_property_unreference_blob(state->mode_blob);
368         state->mode_blob = NULL;
369
370         if (blob) {
371                 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
372                     drm_mode_convert_umode(&state->mode,
373                                            (const struct drm_mode_modeinfo *)
374                                             blob->data))
375                         return -EINVAL;
376
377                 state->mode_blob = drm_property_reference_blob(blob);
378                 state->enable = true;
379                 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
380                                  state->mode.name, state);
381         } else {
382                 memset(&state->mode, 0, sizeof(state->mode));
383                 state->enable = false;
384                 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
385                                  state);
386         }
387
388         return 0;
389 }
390 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
391
392 /**
393  * drm_atomic_crtc_set_property - set property on CRTC
394  * @crtc: the drm CRTC to set a property on
395  * @state: the state object to update with the new property value
396  * @property: the property to set
397  * @val: the new property value
398  *
399  * Use this instead of calling crtc->atomic_set_property directly.
400  * This function handles generic/core properties and calls out to
401  * driver's ->atomic_set_property() for driver properties.  To ensure
402  * consistent behavior you must call this function rather than the
403  * driver hook directly.
404  *
405  * RETURNS:
406  * Zero on success, error code on failure
407  */
408 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
409                 struct drm_crtc_state *state, struct drm_property *property,
410                 uint64_t val)
411 {
412         struct drm_device *dev = crtc->dev;
413         struct drm_mode_config *config = &dev->mode_config;
414         int ret;
415
416         if (property == config->prop_active)
417                 state->active = val;
418         else if (property == config->prop_mode_id) {
419                 struct drm_property_blob *mode =
420                         drm_property_lookup_blob(dev, val);
421                 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
422                 if (mode)
423                         drm_property_unreference_blob(mode);
424                 return ret;
425         }
426         else if (crtc->funcs->atomic_set_property)
427                 return crtc->funcs->atomic_set_property(crtc, state, property, val);
428         else
429                 return -EINVAL;
430
431         return 0;
432 }
433 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
434
435 /*
436  * This function handles generic/core properties and calls out to
437  * driver's ->atomic_get_property() for driver properties.  To ensure
438  * consistent behavior you must call this function rather than the
439  * driver hook directly.
440  */
441 int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
442                 const struct drm_crtc_state *state,
443                 struct drm_property *property, uint64_t *val)
444 {
445         struct drm_device *dev = crtc->dev;
446         struct drm_mode_config *config = &dev->mode_config;
447
448         if (property == config->prop_active)
449                 *val = state->active;
450         else if (property == config->prop_mode_id)
451                 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
452         else if (crtc->funcs->atomic_get_property)
453                 return crtc->funcs->atomic_get_property(crtc, state, property, val);
454         else
455                 return -EINVAL;
456
457         return 0;
458 }
459
460 /**
461  * drm_atomic_crtc_check - check crtc state
462  * @crtc: crtc to check
463  * @state: crtc state to check
464  *
465  * Provides core sanity checks for crtc state.
466  *
467  * RETURNS:
468  * Zero on success, error code on failure
469  */
470 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
471                 struct drm_crtc_state *state)
472 {
473         /* NOTE: we explicitly don't enforce constraints such as primary
474          * layer covering entire screen, since that is something we want
475          * to allow (on hw that supports it).  For hw that does not, it
476          * should be checked in driver's crtc->atomic_check() vfunc.
477          *
478          * TODO: Add generic modeset state checks once we support those.
479          */
480
481         if (state->active && !state->enable) {
482                 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
483                                  crtc->base.id);
484                 return -EINVAL;
485         }
486
487         /* The state->enable vs. state->mode_blob checks can be WARN_ON,
488          * as this is a kernel-internal detail that userspace should never
489          * be able to trigger. */
490         if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
491             WARN_ON(state->enable && !state->mode_blob)) {
492                 DRM_DEBUG_ATOMIC("[CRTC:%d] enabled without mode blob\n",
493                                  crtc->base.id);
494                 return -EINVAL;
495         }
496
497         if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
498             WARN_ON(!state->enable && state->mode_blob)) {
499                 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled with mode blob\n",
500                                  crtc->base.id);
501                 return -EINVAL;
502         }
503
504         return 0;
505 }
506
507 /**
508  * drm_atomic_get_plane_state - get plane state
509  * @state: global atomic state object
510  * @plane: plane to get state object for
511  *
512  * This function returns the plane state for the given plane, allocating it if
513  * needed. It will also grab the relevant plane lock to make sure that the state
514  * is consistent.
515  *
516  * Returns:
517  *
518  * Either the allocated state or the error code encoded into the pointer. When
519  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
520  * entire atomic sequence must be restarted. All other errors are fatal.
521  */
522 struct drm_plane_state *
523 drm_atomic_get_plane_state(struct drm_atomic_state *state,
524                           struct drm_plane *plane)
525 {
526         int ret, index = drm_plane_index(plane);
527         struct drm_plane_state *plane_state;
528
529         plane_state = drm_atomic_get_existing_plane_state(state, plane);
530         if (plane_state)
531                 return plane_state;
532
533         ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
534         if (ret)
535                 return ERR_PTR(ret);
536
537         plane_state = plane->funcs->atomic_duplicate_state(plane);
538         if (!plane_state)
539                 return ERR_PTR(-ENOMEM);
540
541         state->plane_states[index] = plane_state;
542         state->planes[index] = plane;
543         plane_state->state = state;
544
545         DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
546                          plane->base.id, plane_state, state);
547
548         if (plane_state->crtc) {
549                 struct drm_crtc_state *crtc_state;
550
551                 crtc_state = drm_atomic_get_crtc_state(state,
552                                                        plane_state->crtc);
553                 if (IS_ERR(crtc_state))
554                         return ERR_CAST(crtc_state);
555         }
556
557         return plane_state;
558 }
559 EXPORT_SYMBOL(drm_atomic_get_plane_state);
560
561 /**
562  * drm_atomic_plane_set_property - set property on plane
563  * @plane: the drm plane to set a property on
564  * @state: the state object to update with the new property value
565  * @property: the property to set
566  * @val: the new property value
567  *
568  * Use this instead of calling plane->atomic_set_property directly.
569  * This function handles generic/core properties and calls out to
570  * driver's ->atomic_set_property() for driver properties.  To ensure
571  * consistent behavior you must call this function rather than the
572  * driver hook directly.
573  *
574  * RETURNS:
575  * Zero on success, error code on failure
576  */
577 int drm_atomic_plane_set_property(struct drm_plane *plane,
578                 struct drm_plane_state *state, struct drm_property *property,
579                 uint64_t val)
580 {
581         struct drm_device *dev = plane->dev;
582         struct drm_mode_config *config = &dev->mode_config;
583
584         if (property == config->prop_fb_id) {
585                 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
586                 drm_atomic_set_fb_for_plane(state, fb);
587                 if (fb)
588                         drm_framebuffer_unreference(fb);
589         } else if (property == config->prop_crtc_id) {
590                 struct drm_crtc *crtc = drm_crtc_find(dev, val);
591                 return drm_atomic_set_crtc_for_plane(state, crtc);
592         } else if (property == config->prop_crtc_x) {
593                 state->crtc_x = U642I64(val);
594         } else if (property == config->prop_crtc_y) {
595                 state->crtc_y = U642I64(val);
596         } else if (property == config->prop_crtc_w) {
597                 state->crtc_w = val;
598         } else if (property == config->prop_crtc_h) {
599                 state->crtc_h = val;
600         } else if (property == config->prop_src_x) {
601                 state->src_x = val;
602         } else if (property == config->prop_src_y) {
603                 state->src_y = val;
604         } else if (property == config->prop_src_w) {
605                 state->src_w = val;
606         } else if (property == config->prop_src_h) {
607                 state->src_h = val;
608         } else if (property == config->rotation_property) {
609                 state->rotation = val;
610         } else if (plane->funcs->atomic_set_property) {
611                 return plane->funcs->atomic_set_property(plane, state,
612                                 property, val);
613         } else {
614                 return -EINVAL;
615         }
616
617         return 0;
618 }
619 EXPORT_SYMBOL(drm_atomic_plane_set_property);
620
621 /*
622  * This function handles generic/core properties and calls out to
623  * driver's ->atomic_get_property() for driver properties.  To ensure
624  * consistent behavior you must call this function rather than the
625  * driver hook directly.
626  */
627 static int
628 drm_atomic_plane_get_property(struct drm_plane *plane,
629                 const struct drm_plane_state *state,
630                 struct drm_property *property, uint64_t *val)
631 {
632         struct drm_device *dev = plane->dev;
633         struct drm_mode_config *config = &dev->mode_config;
634
635         if (property == config->prop_fb_id) {
636                 *val = (state->fb) ? state->fb->base.id : 0;
637         } else if (property == config->prop_crtc_id) {
638                 *val = (state->crtc) ? state->crtc->base.id : 0;
639         } else if (property == config->prop_crtc_x) {
640                 *val = I642U64(state->crtc_x);
641         } else if (property == config->prop_crtc_y) {
642                 *val = I642U64(state->crtc_y);
643         } else if (property == config->prop_crtc_w) {
644                 *val = state->crtc_w;
645         } else if (property == config->prop_crtc_h) {
646                 *val = state->crtc_h;
647         } else if (property == config->prop_src_x) {
648                 *val = state->src_x;
649         } else if (property == config->prop_src_y) {
650                 *val = state->src_y;
651         } else if (property == config->prop_src_w) {
652                 *val = state->src_w;
653         } else if (property == config->prop_src_h) {
654                 *val = state->src_h;
655         } else if (property == config->rotation_property) {
656                 *val = state->rotation;
657         } else if (plane->funcs->atomic_get_property) {
658                 return plane->funcs->atomic_get_property(plane, state, property, val);
659         } else {
660                 return -EINVAL;
661         }
662
663         return 0;
664 }
665
666 static bool
667 plane_switching_crtc(struct drm_atomic_state *state,
668                      struct drm_plane *plane,
669                      struct drm_plane_state *plane_state)
670 {
671         if (!plane->state->crtc || !plane_state->crtc)
672                 return false;
673
674         if (plane->state->crtc == plane_state->crtc)
675                 return false;
676
677         /* This could be refined, but currently there's no helper or driver code
678          * to implement direct switching of active planes nor userspace to take
679          * advantage of more direct plane switching without the intermediate
680          * full OFF state.
681          */
682         return true;
683 }
684
685 /**
686  * drm_atomic_plane_check - check plane state
687  * @plane: plane to check
688  * @state: plane state to check
689  *
690  * Provides core sanity checks for plane state.
691  *
692  * RETURNS:
693  * Zero on success, error code on failure
694  */
695 static int drm_atomic_plane_check(struct drm_plane *plane,
696                 struct drm_plane_state *state)
697 {
698         unsigned int fb_width, fb_height;
699         int ret;
700
701         /* either *both* CRTC and FB must be set, or neither */
702         if (WARN_ON(state->crtc && !state->fb)) {
703                 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
704                 return -EINVAL;
705         } else if (WARN_ON(state->fb && !state->crtc)) {
706                 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
707                 return -EINVAL;
708         }
709
710         /* if disabled, we don't care about the rest of the state: */
711         if (!state->crtc)
712                 return 0;
713
714         /* Check whether this plane is usable on this CRTC */
715         if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
716                 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
717                 return -EINVAL;
718         }
719
720         /* Check whether this plane supports the fb pixel format. */
721         ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
722         if (ret) {
723                 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
724                                  drm_get_format_name(state->fb->pixel_format));
725                 return ret;
726         }
727
728         /* Give drivers some help against integer overflows */
729         if (state->crtc_w > INT_MAX ||
730             state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
731             state->crtc_h > INT_MAX ||
732             state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
733                 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
734                                  state->crtc_w, state->crtc_h,
735                                  state->crtc_x, state->crtc_y);
736                 return -ERANGE;
737         }
738
739         fb_width = state->fb->width << 16;
740         fb_height = state->fb->height << 16;
741
742         /* Make sure source coordinates are inside the fb. */
743         if (state->src_w > fb_width ||
744             state->src_x > fb_width - state->src_w ||
745             state->src_h > fb_height ||
746             state->src_y > fb_height - state->src_h) {
747                 DRM_DEBUG_ATOMIC("Invalid source coordinates "
748                                  "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
749                                  state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
750                                  state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
751                                  state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
752                                  state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
753                 return -ENOSPC;
754         }
755
756         if (plane_switching_crtc(state->state, plane, state)) {
757                 DRM_DEBUG_ATOMIC("[PLANE:%d] switching CRTC directly\n",
758                                  plane->base.id);
759                 return -EINVAL;
760         }
761
762         return 0;
763 }
764
765 /**
766  * drm_atomic_get_connector_state - get connector state
767  * @state: global atomic state object
768  * @connector: connector to get state object for
769  *
770  * This function returns the connector state for the given connector,
771  * allocating it if needed. It will also grab the relevant connector lock to
772  * make sure that the state is consistent.
773  *
774  * Returns:
775  *
776  * Either the allocated state or the error code encoded into the pointer. When
777  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
778  * entire atomic sequence must be restarted. All other errors are fatal.
779  */
780 struct drm_connector_state *
781 drm_atomic_get_connector_state(struct drm_atomic_state *state,
782                           struct drm_connector *connector)
783 {
784         int ret, index;
785         struct drm_mode_config *config = &connector->dev->mode_config;
786         struct drm_connector_state *connector_state;
787
788         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
789         if (ret)
790                 return ERR_PTR(ret);
791
792         index = drm_connector_index(connector);
793
794         /*
795          * Construction of atomic state updates can race with a connector
796          * hot-add which might overflow. In this case flip the table and just
797          * restart the entire ioctl - no one is fast enough to livelock a cpu
798          * with physical hotplug events anyway.
799          *
800          * Note that we only grab the indexes once we have the right lock to
801          * prevent hotplug/unplugging of connectors. So removal is no problem,
802          * at most the array is a bit too large.
803          */
804         if (index >= state->num_connector) {
805                 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
806                 return ERR_PTR(-EAGAIN);
807         }
808
809         if (state->connector_states[index])
810                 return state->connector_states[index];
811
812         connector_state = connector->funcs->atomic_duplicate_state(connector);
813         if (!connector_state)
814                 return ERR_PTR(-ENOMEM);
815
816         state->connector_states[index] = connector_state;
817         state->connectors[index] = connector;
818         connector_state->state = state;
819
820         DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
821                          connector->base.id, connector_state, state);
822
823         if (connector_state->crtc) {
824                 struct drm_crtc_state *crtc_state;
825
826                 crtc_state = drm_atomic_get_crtc_state(state,
827                                                        connector_state->crtc);
828                 if (IS_ERR(crtc_state))
829                         return ERR_CAST(crtc_state);
830         }
831
832         return connector_state;
833 }
834 EXPORT_SYMBOL(drm_atomic_get_connector_state);
835
836 /**
837  * drm_atomic_connector_set_property - set property on connector.
838  * @connector: the drm connector to set a property on
839  * @state: the state object to update with the new property value
840  * @property: the property to set
841  * @val: the new property value
842  *
843  * Use this instead of calling connector->atomic_set_property directly.
844  * This function handles generic/core properties and calls out to
845  * driver's ->atomic_set_property() for driver properties.  To ensure
846  * consistent behavior you must call this function rather than the
847  * driver hook directly.
848  *
849  * RETURNS:
850  * Zero on success, error code on failure
851  */
852 int drm_atomic_connector_set_property(struct drm_connector *connector,
853                 struct drm_connector_state *state, struct drm_property *property,
854                 uint64_t val)
855 {
856         struct drm_device *dev = connector->dev;
857         struct drm_mode_config *config = &dev->mode_config;
858
859         if (property == config->prop_crtc_id) {
860                 struct drm_crtc *crtc = drm_crtc_find(dev, val);
861                 return drm_atomic_set_crtc_for_connector(state, crtc);
862         } else if (property == config->dpms_property) {
863                 /* setting DPMS property requires special handling, which
864                  * is done in legacy setprop path for us.  Disallow (for
865                  * now?) atomic writes to DPMS property:
866                  */
867                 return -EINVAL;
868         } else if (connector->funcs->atomic_set_property) {
869                 return connector->funcs->atomic_set_property(connector,
870                                 state, property, val);
871         } else {
872                 return -EINVAL;
873         }
874 }
875 EXPORT_SYMBOL(drm_atomic_connector_set_property);
876
877 /*
878  * This function handles generic/core properties and calls out to
879  * driver's ->atomic_get_property() for driver properties.  To ensure
880  * consistent behavior you must call this function rather than the
881  * driver hook directly.
882  */
883 static int
884 drm_atomic_connector_get_property(struct drm_connector *connector,
885                 const struct drm_connector_state *state,
886                 struct drm_property *property, uint64_t *val)
887 {
888         struct drm_device *dev = connector->dev;
889         struct drm_mode_config *config = &dev->mode_config;
890
891         if (property == config->prop_crtc_id) {
892                 *val = (state->crtc) ? state->crtc->base.id : 0;
893         } else if (property == config->dpms_property) {
894                 *val = connector->dpms;
895         } else if (connector->funcs->atomic_get_property) {
896                 return connector->funcs->atomic_get_property(connector,
897                                 state, property, val);
898         } else {
899                 return -EINVAL;
900         }
901
902         return 0;
903 }
904
905 int drm_atomic_get_property(struct drm_mode_object *obj,
906                 struct drm_property *property, uint64_t *val)
907 {
908         struct drm_device *dev = property->dev;
909         int ret;
910
911         switch (obj->type) {
912         case DRM_MODE_OBJECT_CONNECTOR: {
913                 struct drm_connector *connector = obj_to_connector(obj);
914                 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
915                 ret = drm_atomic_connector_get_property(connector,
916                                 connector->state, property, val);
917                 break;
918         }
919         case DRM_MODE_OBJECT_CRTC: {
920                 struct drm_crtc *crtc = obj_to_crtc(obj);
921                 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
922                 ret = drm_atomic_crtc_get_property(crtc,
923                                 crtc->state, property, val);
924                 break;
925         }
926         case DRM_MODE_OBJECT_PLANE: {
927                 struct drm_plane *plane = obj_to_plane(obj);
928                 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
929                 ret = drm_atomic_plane_get_property(plane,
930                                 plane->state, property, val);
931                 break;
932         }
933         default:
934                 ret = -EINVAL;
935                 break;
936         }
937
938         return ret;
939 }
940
941 /**
942  * drm_atomic_set_crtc_for_plane - set crtc for plane
943  * @plane_state: the plane whose incoming state to update
944  * @crtc: crtc to use for the plane
945  *
946  * Changing the assigned crtc for a plane requires us to grab the lock and state
947  * for the new crtc, as needed. This function takes care of all these details
948  * besides updating the pointer in the state object itself.
949  *
950  * Returns:
951  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
952  * then the w/w mutex code has detected a deadlock and the entire atomic
953  * sequence must be restarted. All other errors are fatal.
954  */
955 int
956 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
957                               struct drm_crtc *crtc)
958 {
959         struct drm_plane *plane = plane_state->plane;
960         struct drm_crtc_state *crtc_state;
961
962         if (plane_state->crtc) {
963                 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
964                                                        plane_state->crtc);
965                 if (WARN_ON(IS_ERR(crtc_state)))
966                         return PTR_ERR(crtc_state);
967
968                 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
969         }
970
971         plane_state->crtc = crtc;
972
973         if (crtc) {
974                 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
975                                                        crtc);
976                 if (IS_ERR(crtc_state))
977                         return PTR_ERR(crtc_state);
978                 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
979         }
980
981         if (crtc)
982                 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
983                                  plane_state, crtc->base.id);
984         else
985                 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
986                                  plane_state);
987
988         return 0;
989 }
990 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
991
992 /**
993  * drm_atomic_set_fb_for_plane - set framebuffer for plane
994  * @plane_state: atomic state object for the plane
995  * @fb: fb to use for the plane
996  *
997  * Changing the assigned framebuffer for a plane requires us to grab a reference
998  * to the new fb and drop the reference to the old fb, if there is one. This
999  * function takes care of all these details besides updating the pointer in the
1000  * state object itself.
1001  */
1002 void
1003 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1004                             struct drm_framebuffer *fb)
1005 {
1006         if (plane_state->fb)
1007                 drm_framebuffer_unreference(plane_state->fb);
1008         if (fb)
1009                 drm_framebuffer_reference(fb);
1010         plane_state->fb = fb;
1011
1012         if (fb)
1013                 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1014                                  fb->base.id, plane_state);
1015         else
1016                 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1017                                  plane_state);
1018 }
1019 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1020
1021 /**
1022  * drm_atomic_set_crtc_for_connector - set crtc for connector
1023  * @conn_state: atomic state object for the connector
1024  * @crtc: crtc to use for the connector
1025  *
1026  * Changing the assigned crtc for a connector requires us to grab the lock and
1027  * state for the new crtc, as needed. This function takes care of all these
1028  * details besides updating the pointer in the state object itself.
1029  *
1030  * Returns:
1031  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1032  * then the w/w mutex code has detected a deadlock and the entire atomic
1033  * sequence must be restarted. All other errors are fatal.
1034  */
1035 int
1036 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1037                                   struct drm_crtc *crtc)
1038 {
1039         struct drm_crtc_state *crtc_state;
1040
1041         if (crtc) {
1042                 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1043                 if (IS_ERR(crtc_state))
1044                         return PTR_ERR(crtc_state);
1045         }
1046
1047         conn_state->crtc = crtc;
1048
1049         if (crtc)
1050                 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
1051                                  conn_state, crtc->base.id);
1052         else
1053                 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1054                                  conn_state);
1055
1056         return 0;
1057 }
1058 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1059
1060 /**
1061  * drm_atomic_add_affected_connectors - add connectors for crtc
1062  * @state: atomic state
1063  * @crtc: DRM crtc
1064  *
1065  * This function walks the current configuration and adds all connectors
1066  * currently using @crtc to the atomic configuration @state. Note that this
1067  * function must acquire the connection mutex. This can potentially cause
1068  * unneeded seralization if the update is just for the planes on one crtc. Hence
1069  * drivers and helpers should only call this when really needed (e.g. when a
1070  * full modeset needs to happen due to some change).
1071  *
1072  * Returns:
1073  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1074  * then the w/w mutex code has detected a deadlock and the entire atomic
1075  * sequence must be restarted. All other errors are fatal.
1076  */
1077 int
1078 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1079                                    struct drm_crtc *crtc)
1080 {
1081         struct drm_mode_config *config = &state->dev->mode_config;
1082         struct drm_connector *connector;
1083         struct drm_connector_state *conn_state;
1084         int ret;
1085
1086         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1087         if (ret)
1088                 return ret;
1089
1090         DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
1091                          crtc->base.id, state);
1092
1093         /*
1094          * Changed connectors are already in @state, so only need to look at the
1095          * current configuration.
1096          */
1097         drm_for_each_connector(connector, state->dev) {
1098                 if (connector->state->crtc != crtc)
1099                         continue;
1100
1101                 conn_state = drm_atomic_get_connector_state(state, connector);
1102                 if (IS_ERR(conn_state))
1103                         return PTR_ERR(conn_state);
1104         }
1105
1106         return 0;
1107 }
1108 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1109
1110 /**
1111  * drm_atomic_add_affected_planes - add planes for crtc
1112  * @state: atomic state
1113  * @crtc: DRM crtc
1114  *
1115  * This function walks the current configuration and adds all planes
1116  * currently used by @crtc to the atomic configuration @state. This is useful
1117  * when an atomic commit also needs to check all currently enabled plane on
1118  * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1119  * to avoid special code to force-enable all planes.
1120  *
1121  * Since acquiring a plane state will always also acquire the w/w mutex of the
1122  * current CRTC for that plane (if there is any) adding all the plane states for
1123  * a CRTC will not reduce parallism of atomic updates.
1124  *
1125  * Returns:
1126  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1127  * then the w/w mutex code has detected a deadlock and the entire atomic
1128  * sequence must be restarted. All other errors are fatal.
1129  */
1130 int
1131 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1132                                struct drm_crtc *crtc)
1133 {
1134         struct drm_plane *plane;
1135
1136         WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1137
1138         drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1139                 struct drm_plane_state *plane_state =
1140                         drm_atomic_get_plane_state(state, plane);
1141
1142                 if (IS_ERR(plane_state))
1143                         return PTR_ERR(plane_state);
1144         }
1145         return 0;
1146 }
1147 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1148
1149 /**
1150  * drm_atomic_connectors_for_crtc - count number of connected outputs
1151  * @state: atomic state
1152  * @crtc: DRM crtc
1153  *
1154  * This function counts all connectors which will be connected to @crtc
1155  * according to @state. Useful to recompute the enable state for @crtc.
1156  */
1157 int
1158 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
1159                                struct drm_crtc *crtc)
1160 {
1161         struct drm_connector *connector;
1162         struct drm_connector_state *conn_state;
1163
1164         int i, num_connected_connectors = 0;
1165
1166         for_each_connector_in_state(state, connector, conn_state, i) {
1167                 if (conn_state->crtc == crtc)
1168                         num_connected_connectors++;
1169         }
1170
1171         DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
1172                          state, num_connected_connectors, crtc->base.id);
1173
1174         return num_connected_connectors;
1175 }
1176 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
1177
1178 /**
1179  * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1180  * @state: atomic state
1181  *
1182  * This function should be used by legacy entry points which don't understand
1183  * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1184  * the slowpath completed.
1185  */
1186 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1187 {
1188         int ret;
1189
1190 retry:
1191         drm_modeset_backoff(state->acquire_ctx);
1192
1193         ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1194                                state->acquire_ctx);
1195         if (ret)
1196                 goto retry;
1197         ret = drm_modeset_lock_all_crtcs(state->dev,
1198                                          state->acquire_ctx);
1199         if (ret)
1200                 goto retry;
1201 }
1202 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1203
1204 /**
1205  * drm_atomic_check_only - check whether a given config would work
1206  * @state: atomic configuration to check
1207  *
1208  * Note that this function can return -EDEADLK if the driver needed to acquire
1209  * more locks but encountered a deadlock. The caller must then do the usual w/w
1210  * backoff dance and restart. All other errors are fatal.
1211  *
1212  * Returns:
1213  * 0 on success, negative error code on failure.
1214  */
1215 int drm_atomic_check_only(struct drm_atomic_state *state)
1216 {
1217         struct drm_device *dev = state->dev;
1218         struct drm_mode_config *config = &dev->mode_config;
1219         struct drm_plane *plane;
1220         struct drm_plane_state *plane_state;
1221         struct drm_crtc *crtc;
1222         struct drm_crtc_state *crtc_state;
1223         int i, ret = 0;
1224
1225         DRM_DEBUG_ATOMIC("checking %p\n", state);
1226
1227         for_each_plane_in_state(state, plane, plane_state, i) {
1228                 ret = drm_atomic_plane_check(plane, plane_state);
1229                 if (ret) {
1230                         DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
1231                                          plane->base.id);
1232                         return ret;
1233                 }
1234         }
1235
1236         for_each_crtc_in_state(state, crtc, crtc_state, i) {
1237                 ret = drm_atomic_crtc_check(crtc, crtc_state);
1238                 if (ret) {
1239                         DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
1240                                          crtc->base.id);
1241                         return ret;
1242                 }
1243         }
1244
1245         if (config->funcs->atomic_check)
1246                 ret = config->funcs->atomic_check(state->dev, state);
1247
1248         if (!state->allow_modeset) {
1249                 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1250                         if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1251                                 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1252                                                  crtc->base.id);
1253                                 return -EINVAL;
1254                         }
1255                 }
1256         }
1257
1258         return ret;
1259 }
1260 EXPORT_SYMBOL(drm_atomic_check_only);
1261
1262 /**
1263  * drm_atomic_commit - commit configuration atomically
1264  * @state: atomic configuration to check
1265  *
1266  * Note that this function can return -EDEADLK if the driver needed to acquire
1267  * more locks but encountered a deadlock. The caller must then do the usual w/w
1268  * backoff dance and restart. All other errors are fatal.
1269  *
1270  * Also note that on successful execution ownership of @state is transferred
1271  * from the caller of this function to the function itself. The caller must not
1272  * free or in any other way access @state. If the function fails then the caller
1273  * must clean up @state itself.
1274  *
1275  * Returns:
1276  * 0 on success, negative error code on failure.
1277  */
1278 int drm_atomic_commit(struct drm_atomic_state *state)
1279 {
1280         struct drm_mode_config *config = &state->dev->mode_config;
1281         int ret;
1282
1283         ret = drm_atomic_check_only(state);
1284         if (ret)
1285                 return ret;
1286
1287         DRM_DEBUG_ATOMIC("commiting %p\n", state);
1288
1289         return config->funcs->atomic_commit(state->dev, state, false);
1290 }
1291 EXPORT_SYMBOL(drm_atomic_commit);
1292
1293 /**
1294  * drm_atomic_async_commit - atomic&async configuration commit
1295  * @state: atomic configuration to check
1296  *
1297  * Note that this function can return -EDEADLK if the driver needed to acquire
1298  * more locks but encountered a deadlock. The caller must then do the usual w/w
1299  * backoff dance and restart. All other errors are fatal.
1300  *
1301  * Also note that on successful execution ownership of @state is transferred
1302  * from the caller of this function to the function itself. The caller must not
1303  * free or in any other way access @state. If the function fails then the caller
1304  * must clean up @state itself.
1305  *
1306  * Returns:
1307  * 0 on success, negative error code on failure.
1308  */
1309 int drm_atomic_async_commit(struct drm_atomic_state *state)
1310 {
1311         struct drm_mode_config *config = &state->dev->mode_config;
1312         int ret;
1313
1314         ret = drm_atomic_check_only(state);
1315         if (ret)
1316                 return ret;
1317
1318         DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1319
1320         return config->funcs->atomic_commit(state->dev, state, true);
1321 }
1322 EXPORT_SYMBOL(drm_atomic_async_commit);
1323
1324 /*
1325  * The big monstor ioctl
1326  */
1327
1328 static struct drm_pending_vblank_event *create_vblank_event(
1329                 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1330 {
1331         struct drm_pending_vblank_event *e = NULL;
1332         unsigned long flags;
1333
1334         spin_lock_irqsave(&dev->event_lock, flags);
1335         if (file_priv->event_space < sizeof e->event) {
1336                 spin_unlock_irqrestore(&dev->event_lock, flags);
1337                 goto out;
1338         }
1339         file_priv->event_space -= sizeof e->event;
1340         spin_unlock_irqrestore(&dev->event_lock, flags);
1341
1342         e = kzalloc(sizeof *e, GFP_KERNEL);
1343         if (e == NULL) {
1344                 spin_lock_irqsave(&dev->event_lock, flags);
1345                 file_priv->event_space += sizeof e->event;
1346                 spin_unlock_irqrestore(&dev->event_lock, flags);
1347                 goto out;
1348         }
1349
1350         e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1351         e->event.base.length = sizeof e->event;
1352         e->event.user_data = user_data;
1353         e->base.event = &e->event.base;
1354         e->base.file_priv = file_priv;
1355         e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1356
1357 out:
1358         return e;
1359 }
1360
1361 static void destroy_vblank_event(struct drm_device *dev,
1362                 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1363 {
1364         unsigned long flags;
1365
1366         spin_lock_irqsave(&dev->event_lock, flags);
1367         file_priv->event_space += sizeof e->event;
1368         spin_unlock_irqrestore(&dev->event_lock, flags);
1369         kfree(e);
1370 }
1371
1372 static int atomic_set_prop(struct drm_atomic_state *state,
1373                 struct drm_mode_object *obj, struct drm_property *prop,
1374                 uint64_t prop_value)
1375 {
1376         struct drm_mode_object *ref;
1377         int ret;
1378
1379         if (!drm_property_change_valid_get(prop, prop_value, &ref))
1380                 return -EINVAL;
1381
1382         switch (obj->type) {
1383         case DRM_MODE_OBJECT_CONNECTOR: {
1384                 struct drm_connector *connector = obj_to_connector(obj);
1385                 struct drm_connector_state *connector_state;
1386
1387                 connector_state = drm_atomic_get_connector_state(state, connector);
1388                 if (IS_ERR(connector_state)) {
1389                         ret = PTR_ERR(connector_state);
1390                         break;
1391                 }
1392
1393                 ret = drm_atomic_connector_set_property(connector,
1394                                 connector_state, prop, prop_value);
1395                 break;
1396         }
1397         case DRM_MODE_OBJECT_CRTC: {
1398                 struct drm_crtc *crtc = obj_to_crtc(obj);
1399                 struct drm_crtc_state *crtc_state;
1400
1401                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1402                 if (IS_ERR(crtc_state)) {
1403                         ret = PTR_ERR(crtc_state);
1404                         break;
1405                 }
1406
1407                 ret = drm_atomic_crtc_set_property(crtc,
1408                                 crtc_state, prop, prop_value);
1409                 break;
1410         }
1411         case DRM_MODE_OBJECT_PLANE: {
1412                 struct drm_plane *plane = obj_to_plane(obj);
1413                 struct drm_plane_state *plane_state;
1414
1415                 plane_state = drm_atomic_get_plane_state(state, plane);
1416                 if (IS_ERR(plane_state)) {
1417                         ret = PTR_ERR(plane_state);
1418                         break;
1419                 }
1420
1421                 ret = drm_atomic_plane_set_property(plane,
1422                                 plane_state, prop, prop_value);
1423                 break;
1424         }
1425         default:
1426                 ret = -EINVAL;
1427                 break;
1428         }
1429
1430         drm_property_change_valid_put(prop, ref);
1431         return ret;
1432 }
1433
1434 int drm_mode_atomic_ioctl(struct drm_device *dev,
1435                           void *data, struct drm_file *file_priv)
1436 {
1437         struct drm_mode_atomic *arg = data;
1438         uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1439         uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1440         uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1441         uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1442         unsigned int copied_objs, copied_props;
1443         struct drm_atomic_state *state;
1444         struct drm_modeset_acquire_ctx ctx;
1445         struct drm_plane *plane;
1446         struct drm_crtc *crtc;
1447         struct drm_crtc_state *crtc_state;
1448         unsigned plane_mask = 0;
1449         int ret = 0;
1450         unsigned int i, j;
1451
1452         /* disallow for drivers not supporting atomic: */
1453         if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1454                 return -EINVAL;
1455
1456         /* disallow for userspace that has not enabled atomic cap (even
1457          * though this may be a bit overkill, since legacy userspace
1458          * wouldn't know how to call this ioctl)
1459          */
1460         if (!file_priv->atomic)
1461                 return -EINVAL;
1462
1463         if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1464                 return -EINVAL;
1465
1466         if (arg->reserved)
1467                 return -EINVAL;
1468
1469         if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1470                         !dev->mode_config.async_page_flip)
1471                 return -EINVAL;
1472
1473         /* can't test and expect an event at the same time. */
1474         if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1475                         (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1476                 return -EINVAL;
1477
1478         drm_modeset_acquire_init(&ctx, 0);
1479
1480         state = drm_atomic_state_alloc(dev);
1481         if (!state)
1482                 return -ENOMEM;
1483
1484         state->acquire_ctx = &ctx;
1485         state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1486
1487 retry:
1488         copied_objs = 0;
1489         copied_props = 0;
1490
1491         for (i = 0; i < arg->count_objs; i++) {
1492                 uint32_t obj_id, count_props;
1493                 struct drm_mode_object *obj;
1494
1495                 if (get_user(obj_id, objs_ptr + copied_objs)) {
1496                         ret = -EFAULT;
1497                         goto out;
1498                 }
1499
1500                 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1501                 if (!obj || !obj->properties) {
1502                         ret = -ENOENT;
1503                         goto out;
1504                 }
1505
1506                 if (get_user(count_props, count_props_ptr + copied_objs)) {
1507                         ret = -EFAULT;
1508                         goto out;
1509                 }
1510
1511                 copied_objs++;
1512
1513                 for (j = 0; j < count_props; j++) {
1514                         uint32_t prop_id;
1515                         uint64_t prop_value;
1516                         struct drm_property *prop;
1517
1518                         if (get_user(prop_id, props_ptr + copied_props)) {
1519                                 ret = -EFAULT;
1520                                 goto out;
1521                         }
1522
1523                         prop = drm_property_find(dev, prop_id);
1524                         if (!prop) {
1525                                 ret = -ENOENT;
1526                                 goto out;
1527                         }
1528
1529                         if (copy_from_user(&prop_value,
1530                                            prop_values_ptr + copied_props,
1531                                            sizeof(prop_value))) {
1532                                 ret = -EFAULT;
1533                                 goto out;
1534                         }
1535
1536                         ret = atomic_set_prop(state, obj, prop, prop_value);
1537                         if (ret)
1538                                 goto out;
1539
1540                         copied_props++;
1541                 }
1542
1543                 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
1544                     !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
1545                         plane = obj_to_plane(obj);
1546                         plane_mask |= (1 << drm_plane_index(plane));
1547                         plane->old_fb = plane->fb;
1548                 }
1549         }
1550
1551         if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1552                 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1553                         struct drm_pending_vblank_event *e;
1554
1555                         e = create_vblank_event(dev, file_priv, arg->user_data);
1556                         if (!e) {
1557                                 ret = -ENOMEM;
1558                                 goto out;
1559                         }
1560
1561                         crtc_state->event = e;
1562                 }
1563         }
1564
1565         if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1566                 /*
1567                  * Unlike commit, check_only does not clean up state.
1568                  * Below we call drm_atomic_state_free for it.
1569                  */
1570                 ret = drm_atomic_check_only(state);
1571         } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1572                 ret = drm_atomic_async_commit(state);
1573         } else {
1574                 ret = drm_atomic_commit(state);
1575         }
1576
1577 out:
1578         /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1579          * locks (ie. while it is still safe to deref plane->state).  We
1580          * need to do this here because the driver entry points cannot
1581          * distinguish between legacy and atomic ioctls.
1582          */
1583         drm_for_each_plane_mask(plane, dev, plane_mask) {
1584                 if (ret == 0) {
1585                         struct drm_framebuffer *new_fb = plane->state->fb;
1586                         if (new_fb)
1587                                 drm_framebuffer_reference(new_fb);
1588                         plane->fb = new_fb;
1589                         plane->crtc = plane->state->crtc;
1590
1591                         if (plane->old_fb)
1592                                 drm_framebuffer_unreference(plane->old_fb);
1593                 }
1594                 plane->old_fb = NULL;
1595         }
1596
1597         if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1598                 /*
1599                  * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
1600                  * if they weren't, this code should be called on success
1601                  * for TEST_ONLY too.
1602                  */
1603
1604                 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1605                         if (!crtc_state->event)
1606                                 continue;
1607
1608                         destroy_vblank_event(dev, file_priv,
1609                                              crtc_state->event);
1610                 }
1611         }
1612
1613         if (ret == -EDEADLK) {
1614                 drm_atomic_state_clear(state);
1615                 drm_modeset_backoff(&ctx);
1616                 goto retry;
1617         }
1618
1619         if (ret || arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1620                 drm_atomic_state_free(state);
1621
1622         drm_modeset_drop_locks(&ctx);
1623         drm_modeset_acquire_fini(&ctx);
1624
1625         return ret;
1626 }