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