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