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