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