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