drm_atomic_uapi.c revision 1.2 1 /* $NetBSD: drm_atomic_uapi.c,v 1.2 2021/12/18 23:44:57 riastradh Exp $ */
2
3 /*
4 * Copyright (C) 2014 Red Hat
5 * Copyright (C) 2014 Intel Corp.
6 * Copyright (C) 2018 Intel Corp.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Authors:
27 * Rob Clark <robdclark (at) gmail.com>
28 * Daniel Vetter <daniel.vetter (at) ffwll.ch>
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: drm_atomic_uapi.c,v 1.2 2021/12/18 23:44:57 riastradh Exp $");
33
34 #include <drm/drm_atomic_uapi.h>
35 #include <drm/drm_atomic.h>
36 #include <drm/drm_print.h>
37 #include <drm/drm_drv.h>
38 #include <drm/drm_writeback.h>
39 #include <drm/drm_vblank.h>
40
41 #include <linux/dma-fence.h>
42 #include <linux/uaccess.h>
43 #include <linux/sync_file.h>
44 #include <linux/file.h>
45
46 #include "drm_crtc_internal.h"
47
48 /**
49 * DOC: overview
50 *
51 * This file contains the marshalling and demarshalling glue for the atomic UAPI
52 * in all its forms: The monster ATOMIC IOCTL itself, code for GET_PROPERTY and
53 * SET_PROPERTY IOCTLs. Plus interface functions for compatibility helpers and
54 * drivers which have special needs to construct their own atomic updates, e.g.
55 * for load detect or similiar.
56 */
57
58 /**
59 * drm_atomic_set_mode_for_crtc - set mode for CRTC
60 * @state: the CRTC whose incoming state to update
61 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
62 *
63 * Set a mode (originating from the kernel) on the desired CRTC state and update
64 * the enable property.
65 *
66 * RETURNS:
67 * Zero on success, error code on failure. Cannot return -EDEADLK.
68 */
69 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
70 const struct drm_display_mode *mode)
71 {
72 struct drm_crtc *crtc = state->crtc;
73 struct drm_mode_modeinfo umode;
74
75 /* Early return for no change. */
76 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
77 return 0;
78
79 drm_property_blob_put(state->mode_blob);
80 state->mode_blob = NULL;
81
82 if (mode) {
83 drm_mode_convert_to_umode(&umode, mode);
84 state->mode_blob =
85 drm_property_create_blob(state->crtc->dev,
86 sizeof(umode),
87 &umode);
88 if (IS_ERR(state->mode_blob))
89 return PTR_ERR(state->mode_blob);
90
91 drm_mode_copy(&state->mode, mode);
92 state->enable = true;
93 DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
94 mode->name, crtc->base.id, crtc->name, state);
95 } else {
96 memset(&state->mode, 0, sizeof(state->mode));
97 state->enable = false;
98 DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
99 crtc->base.id, crtc->name, state);
100 }
101
102 return 0;
103 }
104 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
105
106 /**
107 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
108 * @state: the CRTC whose incoming state to update
109 * @blob: pointer to blob property to use for mode
110 *
111 * Set a mode (originating from a blob property) on the desired CRTC state.
112 * This function will take a reference on the blob property for the CRTC state,
113 * and release the reference held on the state's existing mode property, if any
114 * was set.
115 *
116 * RETURNS:
117 * Zero on success, error code on failure. Cannot return -EDEADLK.
118 */
119 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
120 struct drm_property_blob *blob)
121 {
122 struct drm_crtc *crtc = state->crtc;
123
124 if (blob == state->mode_blob)
125 return 0;
126
127 drm_property_blob_put(state->mode_blob);
128 state->mode_blob = NULL;
129
130 memset(&state->mode, 0, sizeof(state->mode));
131
132 if (blob) {
133 int ret;
134
135 if (blob->length != sizeof(struct drm_mode_modeinfo)) {
136 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] bad mode blob length: %zu\n",
137 crtc->base.id, crtc->name,
138 blob->length);
139 return -EINVAL;
140 }
141
142 ret = drm_mode_convert_umode(crtc->dev,
143 &state->mode, blob->data);
144 if (ret) {
145 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n",
146 crtc->base.id, crtc->name,
147 ret, drm_get_mode_status_name(state->mode.status));
148 drm_mode_debug_printmodeline(&state->mode);
149 return -EINVAL;
150 }
151
152 state->mode_blob = drm_property_blob_get(blob);
153 state->enable = true;
154 DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
155 state->mode.name, crtc->base.id, crtc->name,
156 state);
157 } else {
158 state->enable = false;
159 DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
160 crtc->base.id, crtc->name, state);
161 }
162
163 return 0;
164 }
165 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
166
167 /**
168 * drm_atomic_set_crtc_for_plane - set CRTC for plane
169 * @plane_state: the plane whose incoming state to update
170 * @crtc: CRTC to use for the plane
171 *
172 * Changing the assigned CRTC for a plane requires us to grab the lock and state
173 * for the new CRTC, as needed. This function takes care of all these details
174 * besides updating the pointer in the state object itself.
175 *
176 * Returns:
177 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
178 * then the w/w mutex code has detected a deadlock and the entire atomic
179 * sequence must be restarted. All other errors are fatal.
180 */
181 int
182 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
183 struct drm_crtc *crtc)
184 {
185 struct drm_plane *plane = plane_state->plane;
186 struct drm_crtc_state *crtc_state;
187 /* Nothing to do for same crtc*/
188 if (plane_state->crtc == crtc)
189 return 0;
190 if (plane_state->crtc) {
191 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
192 plane_state->crtc);
193 if (WARN_ON(IS_ERR(crtc_state)))
194 return PTR_ERR(crtc_state);
195
196 crtc_state->plane_mask &= ~drm_plane_mask(plane);
197 }
198
199 plane_state->crtc = crtc;
200
201 if (crtc) {
202 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
203 crtc);
204 if (IS_ERR(crtc_state))
205 return PTR_ERR(crtc_state);
206 crtc_state->plane_mask |= drm_plane_mask(plane);
207 }
208
209 if (crtc)
210 DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
211 plane->base.id, plane->name, plane_state,
212 crtc->base.id, crtc->name);
213 else
214 DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
215 plane->base.id, plane->name, plane_state);
216
217 return 0;
218 }
219 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
220
221 /**
222 * drm_atomic_set_fb_for_plane - set framebuffer for plane
223 * @plane_state: atomic state object for the plane
224 * @fb: fb to use for the plane
225 *
226 * Changing the assigned framebuffer for a plane requires us to grab a reference
227 * to the new fb and drop the reference to the old fb, if there is one. This
228 * function takes care of all these details besides updating the pointer in the
229 * state object itself.
230 */
231 void
232 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
233 struct drm_framebuffer *fb)
234 {
235 struct drm_plane *plane = plane_state->plane;
236
237 if (fb)
238 DRM_DEBUG_ATOMIC("Set [FB:%d] for [PLANE:%d:%s] state %p\n",
239 fb->base.id, plane->base.id, plane->name,
240 plane_state);
241 else
242 DRM_DEBUG_ATOMIC("Set [NOFB] for [PLANE:%d:%s] state %p\n",
243 plane->base.id, plane->name, plane_state);
244
245 drm_framebuffer_assign(&plane_state->fb, fb);
246 }
247 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
248
249 /**
250 * drm_atomic_set_fence_for_plane - set fence for plane
251 * @plane_state: atomic state object for the plane
252 * @fence: dma_fence to use for the plane
253 *
254 * Helper to setup the plane_state fence in case it is not set yet.
255 * By using this drivers doesn't need to worry if the user choose
256 * implicit or explicit fencing.
257 *
258 * This function will not set the fence to the state if it was set
259 * via explicit fencing interfaces on the atomic ioctl. In that case it will
260 * drop the reference to the fence as we are not storing it anywhere.
261 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
262 * with the received implicit fence. In both cases this function consumes a
263 * reference for @fence.
264 *
265 * This way explicit fencing can be used to overrule implicit fencing, which is
266 * important to make explicit fencing use-cases work: One example is using one
267 * buffer for 2 screens with different refresh rates. Implicit fencing will
268 * clamp rendering to the refresh rate of the slower screen, whereas explicit
269 * fence allows 2 independent render and display loops on a single buffer. If a
270 * driver allows obeys both implicit and explicit fences for plane updates, then
271 * it will break all the benefits of explicit fencing.
272 */
273 void
274 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
275 struct dma_fence *fence)
276 {
277 if (plane_state->fence) {
278 dma_fence_put(fence);
279 return;
280 }
281
282 plane_state->fence = fence;
283 }
284 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
285
286 /**
287 * drm_atomic_set_crtc_for_connector - set CRTC for connector
288 * @conn_state: atomic state object for the connector
289 * @crtc: CRTC to use for the connector
290 *
291 * Changing the assigned CRTC for a connector requires us to grab the lock and
292 * state for the new CRTC, as needed. This function takes care of all these
293 * details besides updating the pointer in the state object itself.
294 *
295 * Returns:
296 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
297 * then the w/w mutex code has detected a deadlock and the entire atomic
298 * sequence must be restarted. All other errors are fatal.
299 */
300 int
301 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
302 struct drm_crtc *crtc)
303 {
304 struct drm_connector *connector = conn_state->connector;
305 struct drm_crtc_state *crtc_state;
306
307 if (conn_state->crtc == crtc)
308 return 0;
309
310 if (conn_state->crtc) {
311 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
312 conn_state->crtc);
313
314 crtc_state->connector_mask &=
315 ~drm_connector_mask(conn_state->connector);
316
317 drm_connector_put(conn_state->connector);
318 conn_state->crtc = NULL;
319 }
320
321 if (crtc) {
322 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
323 if (IS_ERR(crtc_state))
324 return PTR_ERR(crtc_state);
325
326 crtc_state->connector_mask |=
327 drm_connector_mask(conn_state->connector);
328
329 drm_connector_get(conn_state->connector);
330 conn_state->crtc = crtc;
331
332 DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
333 connector->base.id, connector->name,
334 conn_state, crtc->base.id, crtc->name);
335 } else {
336 DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
337 connector->base.id, connector->name,
338 conn_state);
339 }
340
341 return 0;
342 }
343 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
344
345 static void set_out_fence_for_crtc(struct drm_atomic_state *state,
346 struct drm_crtc *crtc, s32 __user *fence_ptr)
347 {
348 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
349 }
350
351 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
352 struct drm_crtc *crtc)
353 {
354 s32 __user *fence_ptr;
355
356 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
357 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
358
359 return fence_ptr;
360 }
361
362 static int set_out_fence_for_connector(struct drm_atomic_state *state,
363 struct drm_connector *connector,
364 s32 __user *fence_ptr)
365 {
366 unsigned int index = drm_connector_index(connector);
367
368 if (!fence_ptr)
369 return 0;
370
371 if (put_user(-1, fence_ptr))
372 return -EFAULT;
373
374 state->connectors[index].out_fence_ptr = fence_ptr;
375
376 return 0;
377 }
378
379 static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
380 struct drm_connector *connector)
381 {
382 unsigned int index = drm_connector_index(connector);
383 s32 __user *fence_ptr;
384
385 fence_ptr = state->connectors[index].out_fence_ptr;
386 state->connectors[index].out_fence_ptr = NULL;
387
388 return fence_ptr;
389 }
390
391 static int
392 drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
393 struct drm_property_blob **blob,
394 uint64_t blob_id,
395 ssize_t expected_size,
396 ssize_t expected_elem_size,
397 bool *replaced)
398 {
399 struct drm_property_blob *new_blob = NULL;
400
401 if (blob_id != 0) {
402 new_blob = drm_property_lookup_blob(dev, blob_id);
403 if (new_blob == NULL)
404 return -EINVAL;
405
406 if (expected_size > 0 &&
407 new_blob->length != expected_size) {
408 drm_property_blob_put(new_blob);
409 return -EINVAL;
410 }
411 if (expected_elem_size > 0 &&
412 new_blob->length % expected_elem_size != 0) {
413 drm_property_blob_put(new_blob);
414 return -EINVAL;
415 }
416 }
417
418 *replaced |= drm_property_replace_blob(blob, new_blob);
419 drm_property_blob_put(new_blob);
420
421 return 0;
422 }
423
424 static int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
425 struct drm_crtc_state *state, struct drm_property *property,
426 uint64_t val)
427 {
428 struct drm_device *dev = crtc->dev;
429 struct drm_mode_config *config = &dev->mode_config;
430 bool replaced = false;
431 int ret;
432
433 if (property == config->prop_active)
434 state->active = val;
435 else if (property == config->prop_mode_id) {
436 struct drm_property_blob *mode =
437 drm_property_lookup_blob(dev, val);
438 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
439 drm_property_blob_put(mode);
440 return ret;
441 } else if (property == config->prop_vrr_enabled) {
442 state->vrr_enabled = val;
443 } else if (property == config->degamma_lut_property) {
444 ret = drm_atomic_replace_property_blob_from_id(dev,
445 &state->degamma_lut,
446 val,
447 -1, sizeof(struct drm_color_lut),
448 &replaced);
449 state->color_mgmt_changed |= replaced;
450 return ret;
451 } else if (property == config->ctm_property) {
452 ret = drm_atomic_replace_property_blob_from_id(dev,
453 &state->ctm,
454 val,
455 sizeof(struct drm_color_ctm), -1,
456 &replaced);
457 state->color_mgmt_changed |= replaced;
458 return ret;
459 } else if (property == config->gamma_lut_property) {
460 ret = drm_atomic_replace_property_blob_from_id(dev,
461 &state->gamma_lut,
462 val,
463 -1, sizeof(struct drm_color_lut),
464 &replaced);
465 state->color_mgmt_changed |= replaced;
466 return ret;
467 } else if (property == config->prop_out_fence_ptr) {
468 s32 __user *fence_ptr = u64_to_user_ptr(val);
469
470 if (!fence_ptr)
471 return 0;
472
473 if (put_user(-1, fence_ptr))
474 return -EFAULT;
475
476 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
477 } else if (crtc->funcs->atomic_set_property) {
478 return crtc->funcs->atomic_set_property(crtc, state, property, val);
479 } else {
480 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
481 crtc->base.id, crtc->name,
482 property->base.id, property->name);
483 return -EINVAL;
484 }
485
486 return 0;
487 }
488
489 static int
490 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
491 const struct drm_crtc_state *state,
492 struct drm_property *property, uint64_t *val)
493 {
494 struct drm_device *dev = crtc->dev;
495 struct drm_mode_config *config = &dev->mode_config;
496
497 if (property == config->prop_active)
498 *val = drm_atomic_crtc_effectively_active(state);
499 else if (property == config->prop_mode_id)
500 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
501 else if (property == config->prop_vrr_enabled)
502 *val = state->vrr_enabled;
503 else if (property == config->degamma_lut_property)
504 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
505 else if (property == config->ctm_property)
506 *val = (state->ctm) ? state->ctm->base.id : 0;
507 else if (property == config->gamma_lut_property)
508 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
509 else if (property == config->prop_out_fence_ptr)
510 *val = 0;
511 else if (crtc->funcs->atomic_get_property)
512 return crtc->funcs->atomic_get_property(crtc, state, property, val);
513 else
514 return -EINVAL;
515
516 return 0;
517 }
518
519 static int drm_atomic_plane_set_property(struct drm_plane *plane,
520 struct drm_plane_state *state, struct drm_file *file_priv,
521 struct drm_property *property, uint64_t val)
522 {
523 struct drm_device *dev = plane->dev;
524 struct drm_mode_config *config = &dev->mode_config;
525 bool replaced = false;
526 int ret;
527
528 if (property == config->prop_fb_id) {
529 struct drm_framebuffer *fb;
530 fb = drm_framebuffer_lookup(dev, file_priv, val);
531 drm_atomic_set_fb_for_plane(state, fb);
532 if (fb)
533 drm_framebuffer_put(fb);
534 } else if (property == config->prop_in_fence_fd) {
535 if (state->fence)
536 return -EINVAL;
537
538 if (U642I64(val) == -1)
539 return 0;
540
541 state->fence = sync_file_get_fence(val);
542 if (!state->fence)
543 return -EINVAL;
544
545 } else if (property == config->prop_crtc_id) {
546 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
547 if (val && !crtc)
548 return -EACCES;
549 return drm_atomic_set_crtc_for_plane(state, crtc);
550 } else if (property == config->prop_crtc_x) {
551 state->crtc_x = U642I64(val);
552 } else if (property == config->prop_crtc_y) {
553 state->crtc_y = U642I64(val);
554 } else if (property == config->prop_crtc_w) {
555 state->crtc_w = val;
556 } else if (property == config->prop_crtc_h) {
557 state->crtc_h = val;
558 } else if (property == config->prop_src_x) {
559 state->src_x = val;
560 } else if (property == config->prop_src_y) {
561 state->src_y = val;
562 } else if (property == config->prop_src_w) {
563 state->src_w = val;
564 } else if (property == config->prop_src_h) {
565 state->src_h = val;
566 } else if (property == plane->alpha_property) {
567 state->alpha = val;
568 } else if (property == plane->blend_mode_property) {
569 state->pixel_blend_mode = val;
570 } else if (property == plane->rotation_property) {
571 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
572 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
573 plane->base.id, plane->name, val);
574 return -EINVAL;
575 }
576 state->rotation = val;
577 } else if (property == plane->zpos_property) {
578 state->zpos = val;
579 } else if (property == plane->color_encoding_property) {
580 state->color_encoding = val;
581 } else if (property == plane->color_range_property) {
582 state->color_range = val;
583 } else if (property == config->prop_fb_damage_clips) {
584 ret = drm_atomic_replace_property_blob_from_id(dev,
585 &state->fb_damage_clips,
586 val,
587 -1,
588 sizeof(struct drm_rect),
589 &replaced);
590 return ret;
591 } else if (plane->funcs->atomic_set_property) {
592 return plane->funcs->atomic_set_property(plane, state,
593 property, val);
594 } else {
595 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
596 plane->base.id, plane->name,
597 property->base.id, property->name);
598 return -EINVAL;
599 }
600
601 return 0;
602 }
603
604 static int
605 drm_atomic_plane_get_property(struct drm_plane *plane,
606 const struct drm_plane_state *state,
607 struct drm_property *property, uint64_t *val)
608 {
609 struct drm_device *dev = plane->dev;
610 struct drm_mode_config *config = &dev->mode_config;
611
612 if (property == config->prop_fb_id) {
613 *val = (state->fb) ? state->fb->base.id : 0;
614 } else if (property == config->prop_in_fence_fd) {
615 *val = -1;
616 } else if (property == config->prop_crtc_id) {
617 *val = (state->crtc) ? state->crtc->base.id : 0;
618 } else if (property == config->prop_crtc_x) {
619 *val = I642U64(state->crtc_x);
620 } else if (property == config->prop_crtc_y) {
621 *val = I642U64(state->crtc_y);
622 } else if (property == config->prop_crtc_w) {
623 *val = state->crtc_w;
624 } else if (property == config->prop_crtc_h) {
625 *val = state->crtc_h;
626 } else if (property == config->prop_src_x) {
627 *val = state->src_x;
628 } else if (property == config->prop_src_y) {
629 *val = state->src_y;
630 } else if (property == config->prop_src_w) {
631 *val = state->src_w;
632 } else if (property == config->prop_src_h) {
633 *val = state->src_h;
634 } else if (property == plane->alpha_property) {
635 *val = state->alpha;
636 } else if (property == plane->blend_mode_property) {
637 *val = state->pixel_blend_mode;
638 } else if (property == plane->rotation_property) {
639 *val = state->rotation;
640 } else if (property == plane->zpos_property) {
641 *val = state->zpos;
642 } else if (property == plane->color_encoding_property) {
643 *val = state->color_encoding;
644 } else if (property == plane->color_range_property) {
645 *val = state->color_range;
646 } else if (property == config->prop_fb_damage_clips) {
647 *val = (state->fb_damage_clips) ?
648 state->fb_damage_clips->base.id : 0;
649 } else if (plane->funcs->atomic_get_property) {
650 return plane->funcs->atomic_get_property(plane, state, property, val);
651 } else {
652 return -EINVAL;
653 }
654
655 return 0;
656 }
657
658 static int drm_atomic_set_writeback_fb_for_connector(
659 struct drm_connector_state *conn_state,
660 struct drm_framebuffer *fb)
661 {
662 int ret;
663
664 ret = drm_writeback_set_fb(conn_state, fb);
665 if (ret < 0)
666 return ret;
667
668 if (fb)
669 DRM_DEBUG_ATOMIC("Set [FB:%d] for connector state %p\n",
670 fb->base.id, conn_state);
671 else
672 DRM_DEBUG_ATOMIC("Set [NOFB] for connector state %p\n",
673 conn_state);
674
675 return 0;
676 }
677
678 static int drm_atomic_connector_set_property(struct drm_connector *connector,
679 struct drm_connector_state *state, struct drm_file *file_priv,
680 struct drm_property *property, uint64_t val)
681 {
682 struct drm_device *dev = connector->dev;
683 struct drm_mode_config *config = &dev->mode_config;
684 bool replaced = false;
685 int ret;
686
687 if (property == config->prop_crtc_id) {
688 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
689 if (val && !crtc)
690 return -EACCES;
691 return drm_atomic_set_crtc_for_connector(state, crtc);
692 } else if (property == config->dpms_property) {
693 /* setting DPMS property requires special handling, which
694 * is done in legacy setprop path for us. Disallow (for
695 * now?) atomic writes to DPMS property:
696 */
697 return -EINVAL;
698 } else if (property == config->tv_select_subconnector_property) {
699 state->tv.subconnector = val;
700 } else if (property == config->tv_left_margin_property) {
701 state->tv.margins.left = val;
702 } else if (property == config->tv_right_margin_property) {
703 state->tv.margins.right = val;
704 } else if (property == config->tv_top_margin_property) {
705 state->tv.margins.top = val;
706 } else if (property == config->tv_bottom_margin_property) {
707 state->tv.margins.bottom = val;
708 } else if (property == config->tv_mode_property) {
709 state->tv.mode = val;
710 } else if (property == config->tv_brightness_property) {
711 state->tv.brightness = val;
712 } else if (property == config->tv_contrast_property) {
713 state->tv.contrast = val;
714 } else if (property == config->tv_flicker_reduction_property) {
715 state->tv.flicker_reduction = val;
716 } else if (property == config->tv_overscan_property) {
717 state->tv.overscan = val;
718 } else if (property == config->tv_saturation_property) {
719 state->tv.saturation = val;
720 } else if (property == config->tv_hue_property) {
721 state->tv.hue = val;
722 } else if (property == config->link_status_property) {
723 /* Never downgrade from GOOD to BAD on userspace's request here,
724 * only hw issues can do that.
725 *
726 * For an atomic property the userspace doesn't need to be able
727 * to understand all the properties, but needs to be able to
728 * restore the state it wants on VT switch. So if the userspace
729 * tries to change the link_status from GOOD to BAD, driver
730 * silently rejects it and returns a 0. This prevents userspace
731 * from accidently breaking the display when it restores the
732 * state.
733 */
734 if (state->link_status != DRM_LINK_STATUS_GOOD)
735 state->link_status = val;
736 } else if (property == config->hdr_output_metadata_property) {
737 ret = drm_atomic_replace_property_blob_from_id(dev,
738 &state->hdr_output_metadata,
739 val,
740 sizeof(struct hdr_output_metadata), -1,
741 &replaced);
742 return ret;
743 } else if (property == config->aspect_ratio_property) {
744 state->picture_aspect_ratio = val;
745 } else if (property == config->content_type_property) {
746 state->content_type = val;
747 } else if (property == connector->scaling_mode_property) {
748 state->scaling_mode = val;
749 } else if (property == config->content_protection_property) {
750 if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
751 DRM_DEBUG_KMS("only drivers can set CP Enabled\n");
752 return -EINVAL;
753 }
754 state->content_protection = val;
755 } else if (property == config->hdcp_content_type_property) {
756 state->hdcp_content_type = val;
757 } else if (property == connector->colorspace_property) {
758 state->colorspace = val;
759 } else if (property == config->writeback_fb_id_property) {
760 struct drm_framebuffer *fb;
761 int ret;
762 fb = drm_framebuffer_lookup(dev, file_priv, val);
763 ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
764 if (fb)
765 drm_framebuffer_put(fb);
766 return ret;
767 } else if (property == config->writeback_out_fence_ptr_property) {
768 s32 __user *fence_ptr = u64_to_user_ptr(val);
769
770 return set_out_fence_for_connector(state->state, connector,
771 fence_ptr);
772 } else if (property == connector->max_bpc_property) {
773 state->max_requested_bpc = val;
774 } else if (connector->funcs->atomic_set_property) {
775 return connector->funcs->atomic_set_property(connector,
776 state, property, val);
777 } else {
778 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n",
779 connector->base.id, connector->name,
780 property->base.id, property->name);
781 return -EINVAL;
782 }
783
784 return 0;
785 }
786
787 static int
788 drm_atomic_connector_get_property(struct drm_connector *connector,
789 const struct drm_connector_state *state,
790 struct drm_property *property, uint64_t *val)
791 {
792 struct drm_device *dev = connector->dev;
793 struct drm_mode_config *config = &dev->mode_config;
794
795 if (property == config->prop_crtc_id) {
796 *val = (state->crtc) ? state->crtc->base.id : 0;
797 } else if (property == config->dpms_property) {
798 if (state->crtc && state->crtc->state->self_refresh_active)
799 *val = DRM_MODE_DPMS_ON;
800 else
801 *val = connector->dpms;
802 } else if (property == config->tv_select_subconnector_property) {
803 *val = state->tv.subconnector;
804 } else if (property == config->tv_left_margin_property) {
805 *val = state->tv.margins.left;
806 } else if (property == config->tv_right_margin_property) {
807 *val = state->tv.margins.right;
808 } else if (property == config->tv_top_margin_property) {
809 *val = state->tv.margins.top;
810 } else if (property == config->tv_bottom_margin_property) {
811 *val = state->tv.margins.bottom;
812 } else if (property == config->tv_mode_property) {
813 *val = state->tv.mode;
814 } else if (property == config->tv_brightness_property) {
815 *val = state->tv.brightness;
816 } else if (property == config->tv_contrast_property) {
817 *val = state->tv.contrast;
818 } else if (property == config->tv_flicker_reduction_property) {
819 *val = state->tv.flicker_reduction;
820 } else if (property == config->tv_overscan_property) {
821 *val = state->tv.overscan;
822 } else if (property == config->tv_saturation_property) {
823 *val = state->tv.saturation;
824 } else if (property == config->tv_hue_property) {
825 *val = state->tv.hue;
826 } else if (property == config->link_status_property) {
827 *val = state->link_status;
828 } else if (property == config->aspect_ratio_property) {
829 *val = state->picture_aspect_ratio;
830 } else if (property == config->content_type_property) {
831 *val = state->content_type;
832 } else if (property == connector->colorspace_property) {
833 *val = state->colorspace;
834 } else if (property == connector->scaling_mode_property) {
835 *val = state->scaling_mode;
836 } else if (property == config->hdr_output_metadata_property) {
837 *val = state->hdr_output_metadata ?
838 state->hdr_output_metadata->base.id : 0;
839 } else if (property == config->content_protection_property) {
840 *val = state->content_protection;
841 } else if (property == config->hdcp_content_type_property) {
842 *val = state->hdcp_content_type;
843 } else if (property == config->writeback_fb_id_property) {
844 /* Writeback framebuffer is one-shot, write and forget */
845 *val = 0;
846 } else if (property == config->writeback_out_fence_ptr_property) {
847 *val = 0;
848 } else if (property == connector->max_bpc_property) {
849 *val = state->max_requested_bpc;
850 } else if (connector->funcs->atomic_get_property) {
851 return connector->funcs->atomic_get_property(connector,
852 state, property, val);
853 } else {
854 return -EINVAL;
855 }
856
857 return 0;
858 }
859
860 int drm_atomic_get_property(struct drm_mode_object *obj,
861 struct drm_property *property, uint64_t *val)
862 {
863 struct drm_device *dev = property->dev;
864 int ret;
865
866 switch (obj->type) {
867 case DRM_MODE_OBJECT_CONNECTOR: {
868 struct drm_connector *connector = obj_to_connector(obj);
869 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
870 ret = drm_atomic_connector_get_property(connector,
871 connector->state, property, val);
872 break;
873 }
874 case DRM_MODE_OBJECT_CRTC: {
875 struct drm_crtc *crtc = obj_to_crtc(obj);
876 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
877 ret = drm_atomic_crtc_get_property(crtc,
878 crtc->state, property, val);
879 break;
880 }
881 case DRM_MODE_OBJECT_PLANE: {
882 struct drm_plane *plane = obj_to_plane(obj);
883 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
884 ret = drm_atomic_plane_get_property(plane,
885 plane->state, property, val);
886 break;
887 }
888 default:
889 ret = -EINVAL;
890 break;
891 }
892
893 return ret;
894 }
895
896 /*
897 * The big monster ioctl
898 */
899
900 static struct drm_pending_vblank_event *create_vblank_event(
901 struct drm_crtc *crtc, uint64_t user_data)
902 {
903 struct drm_pending_vblank_event *e = NULL;
904
905 e = kzalloc(sizeof *e, GFP_KERNEL);
906 if (!e)
907 return NULL;
908
909 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
910 e->event.base.length = sizeof(e->event);
911 e->event.vbl.crtc_id = crtc->base.id;
912 e->event.vbl.user_data = user_data;
913
914 return e;
915 }
916
917 int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
918 struct drm_connector *connector,
919 int mode)
920 {
921 struct drm_connector *tmp_connector;
922 struct drm_connector_state *new_conn_state;
923 struct drm_crtc *crtc;
924 struct drm_crtc_state *crtc_state;
925 int i, ret, old_mode = connector->dpms;
926 bool active = false;
927
928 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
929 state->acquire_ctx);
930 if (ret)
931 return ret;
932
933 if (mode != DRM_MODE_DPMS_ON)
934 mode = DRM_MODE_DPMS_OFF;
935 connector->dpms = mode;
936
937 crtc = connector->state->crtc;
938 if (!crtc)
939 goto out;
940 ret = drm_atomic_add_affected_connectors(state, crtc);
941 if (ret)
942 goto out;
943
944 crtc_state = drm_atomic_get_crtc_state(state, crtc);
945 if (IS_ERR(crtc_state)) {
946 ret = PTR_ERR(crtc_state);
947 goto out;
948 }
949
950 for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
951 if (new_conn_state->crtc != crtc)
952 continue;
953 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
954 active = true;
955 break;
956 }
957 }
958
959 crtc_state->active = active;
960 ret = drm_atomic_commit(state);
961 out:
962 if (ret != 0)
963 connector->dpms = old_mode;
964 return ret;
965 }
966
967 int drm_atomic_set_property(struct drm_atomic_state *state,
968 struct drm_file *file_priv,
969 struct drm_mode_object *obj,
970 struct drm_property *prop,
971 uint64_t prop_value)
972 {
973 struct drm_mode_object *ref;
974 int ret;
975
976 if (!drm_property_change_valid_get(prop, prop_value, &ref))
977 return -EINVAL;
978
979 switch (obj->type) {
980 case DRM_MODE_OBJECT_CONNECTOR: {
981 struct drm_connector *connector = obj_to_connector(obj);
982 struct drm_connector_state *connector_state;
983
984 connector_state = drm_atomic_get_connector_state(state, connector);
985 if (IS_ERR(connector_state)) {
986 ret = PTR_ERR(connector_state);
987 break;
988 }
989
990 ret = drm_atomic_connector_set_property(connector,
991 connector_state, file_priv,
992 prop, prop_value);
993 break;
994 }
995 case DRM_MODE_OBJECT_CRTC: {
996 struct drm_crtc *crtc = obj_to_crtc(obj);
997 struct drm_crtc_state *crtc_state;
998
999 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1000 if (IS_ERR(crtc_state)) {
1001 ret = PTR_ERR(crtc_state);
1002 break;
1003 }
1004
1005 ret = drm_atomic_crtc_set_property(crtc,
1006 crtc_state, prop, prop_value);
1007 break;
1008 }
1009 case DRM_MODE_OBJECT_PLANE: {
1010 struct drm_plane *plane = obj_to_plane(obj);
1011 struct drm_plane_state *plane_state;
1012
1013 plane_state = drm_atomic_get_plane_state(state, plane);
1014 if (IS_ERR(plane_state)) {
1015 ret = PTR_ERR(plane_state);
1016 break;
1017 }
1018
1019 ret = drm_atomic_plane_set_property(plane,
1020 plane_state, file_priv,
1021 prop, prop_value);
1022 break;
1023 }
1024 default:
1025 ret = -EINVAL;
1026 break;
1027 }
1028
1029 drm_property_change_valid_put(prop, ref);
1030 return ret;
1031 }
1032
1033 /**
1034 * DOC: explicit fencing properties
1035 *
1036 * Explicit fencing allows userspace to control the buffer synchronization
1037 * between devices. A Fence or a group of fences are transfered to/from
1038 * userspace using Sync File fds and there are two DRM properties for that.
1039 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1040 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1041 *
1042 * As a contrast, with implicit fencing the kernel keeps track of any
1043 * ongoing rendering, and automatically ensures that the atomic update waits
1044 * for any pending rendering to complete. For shared buffers represented with
1045 * a &struct dma_buf this is tracked in &struct dma_resv.
1046 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1047 * whereas explicit fencing is what Android wants.
1048 *
1049 * "IN_FENCE_FD:
1050 * Use this property to pass a fence that DRM should wait on before
1051 * proceeding with the Atomic Commit request and show the framebuffer for
1052 * the plane on the screen. The fence can be either a normal fence or a
1053 * merged one, the sync_file framework will handle both cases and use a
1054 * fence_array if a merged fence is received. Passing -1 here means no
1055 * fences to wait on.
1056 *
1057 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1058 * it will only check if the Sync File is a valid one.
1059 *
1060 * On the driver side the fence is stored on the @fence parameter of
1061 * &struct drm_plane_state. Drivers which also support implicit fencing
1062 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
1063 * to make sure there's consistent behaviour between drivers in precedence
1064 * of implicit vs. explicit fencing.
1065 *
1066 * "OUT_FENCE_PTR:
1067 * Use this property to pass a file descriptor pointer to DRM. Once the
1068 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1069 * the file descriptor number of a Sync File. This Sync File contains the
1070 * CRTC fence that will be signaled when all framebuffers present on the
1071 * Atomic Commit * request for that given CRTC are scanned out on the
1072 * screen.
1073 *
1074 * The Atomic Commit request fails if a invalid pointer is passed. If the
1075 * Atomic Commit request fails for any other reason the out fence fd
1076 * returned will be -1. On a Atomic Commit with the
1077 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1078 *
1079 * Note that out-fences don't have a special interface to drivers and are
1080 * internally represented by a &struct drm_pending_vblank_event in struct
1081 * &drm_crtc_state, which is also used by the nonblocking atomic commit
1082 * helpers and for the DRM event handling for existing userspace.
1083 */
1084
1085 struct drm_out_fence_state {
1086 s32 __user *out_fence_ptr;
1087 struct sync_file *sync_file;
1088 int fd;
1089 };
1090
1091 static int setup_out_fence(struct drm_out_fence_state *fence_state,
1092 struct dma_fence *fence)
1093 {
1094 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1095 if (fence_state->fd < 0)
1096 return fence_state->fd;
1097
1098 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1099 return -EFAULT;
1100
1101 fence_state->sync_file = sync_file_create(fence);
1102 if (!fence_state->sync_file)
1103 return -ENOMEM;
1104
1105 return 0;
1106 }
1107
1108 static int prepare_signaling(struct drm_device *dev,
1109 struct drm_atomic_state *state,
1110 struct drm_mode_atomic *arg,
1111 struct drm_file *file_priv,
1112 struct drm_out_fence_state **fence_state,
1113 unsigned int *num_fences)
1114 {
1115 struct drm_crtc *crtc;
1116 struct drm_crtc_state *crtc_state;
1117 struct drm_connector *conn;
1118 struct drm_connector_state *conn_state;
1119 int i, c = 0, ret;
1120
1121 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1122 return 0;
1123
1124 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1125 s32 __user *fence_ptr;
1126
1127 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1128
1129 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1130 struct drm_pending_vblank_event *e;
1131
1132 e = create_vblank_event(crtc, arg->user_data);
1133 if (!e)
1134 return -ENOMEM;
1135
1136 crtc_state->event = e;
1137 }
1138
1139 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1140 struct drm_pending_vblank_event *e = crtc_state->event;
1141
1142 if (!file_priv)
1143 continue;
1144
1145 ret = drm_event_reserve_init(dev, file_priv, &e->base,
1146 &e->event.base);
1147 if (ret) {
1148 kfree(e);
1149 crtc_state->event = NULL;
1150 return ret;
1151 }
1152 }
1153
1154 if (fence_ptr) {
1155 struct dma_fence *fence;
1156 struct drm_out_fence_state *f;
1157
1158 f = krealloc(*fence_state, sizeof(**fence_state) *
1159 (*num_fences + 1), GFP_KERNEL);
1160 if (!f)
1161 return -ENOMEM;
1162
1163 memset(&f[*num_fences], 0, sizeof(*f));
1164
1165 f[*num_fences].out_fence_ptr = fence_ptr;
1166 *fence_state = f;
1167
1168 fence = drm_crtc_create_fence(crtc);
1169 if (!fence)
1170 return -ENOMEM;
1171
1172 ret = setup_out_fence(&f[(*num_fences)++], fence);
1173 if (ret) {
1174 dma_fence_put(fence);
1175 return ret;
1176 }
1177
1178 crtc_state->event->base.fence = fence;
1179 }
1180
1181 c++;
1182 }
1183
1184 for_each_new_connector_in_state(state, conn, conn_state, i) {
1185 struct drm_writeback_connector *wb_conn;
1186 struct drm_out_fence_state *f;
1187 struct dma_fence *fence;
1188 s32 __user *fence_ptr;
1189
1190 if (!conn_state->writeback_job)
1191 continue;
1192
1193 fence_ptr = get_out_fence_for_connector(state, conn);
1194 if (!fence_ptr)
1195 continue;
1196
1197 f = krealloc(*fence_state, sizeof(**fence_state) *
1198 (*num_fences + 1), GFP_KERNEL);
1199 if (!f)
1200 return -ENOMEM;
1201
1202 memset(&f[*num_fences], 0, sizeof(*f));
1203
1204 f[*num_fences].out_fence_ptr = fence_ptr;
1205 *fence_state = f;
1206
1207 wb_conn = drm_connector_to_writeback(conn);
1208 fence = drm_writeback_get_out_fence(wb_conn);
1209 if (!fence)
1210 return -ENOMEM;
1211
1212 ret = setup_out_fence(&f[(*num_fences)++], fence);
1213 if (ret) {
1214 dma_fence_put(fence);
1215 return ret;
1216 }
1217
1218 conn_state->writeback_job->out_fence = fence;
1219 }
1220
1221 /*
1222 * Having this flag means user mode pends on event which will never
1223 * reach due to lack of at least one CRTC for signaling
1224 */
1225 if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1226 return -EINVAL;
1227
1228 return 0;
1229 }
1230
1231 static void complete_signaling(struct drm_device *dev,
1232 struct drm_atomic_state *state,
1233 struct drm_out_fence_state *fence_state,
1234 unsigned int num_fences,
1235 bool install_fds)
1236 {
1237 struct drm_crtc *crtc;
1238 struct drm_crtc_state *crtc_state;
1239 int i;
1240
1241 if (install_fds) {
1242 for (i = 0; i < num_fences; i++)
1243 fd_install(fence_state[i].fd,
1244 fence_state[i].sync_file->file);
1245
1246 kfree(fence_state);
1247 return;
1248 }
1249
1250 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1251 struct drm_pending_vblank_event *event = crtc_state->event;
1252 /*
1253 * Free the allocated event. drm_atomic_helper_setup_commit
1254 * can allocate an event too, so only free it if it's ours
1255 * to prevent a double free in drm_atomic_state_clear.
1256 */
1257 if (event && (event->base.fence || event->base.file_priv)) {
1258 drm_event_cancel_free(dev, &event->base);
1259 crtc_state->event = NULL;
1260 }
1261 }
1262
1263 if (!fence_state)
1264 return;
1265
1266 for (i = 0; i < num_fences; i++) {
1267 if (fence_state[i].sync_file)
1268 fput(fence_state[i].sync_file->file);
1269 if (fence_state[i].fd >= 0)
1270 put_unused_fd(fence_state[i].fd);
1271
1272 /* If this fails log error to the user */
1273 if (fence_state[i].out_fence_ptr &&
1274 put_user(-1, fence_state[i].out_fence_ptr))
1275 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
1276 }
1277
1278 kfree(fence_state);
1279 }
1280
1281 int drm_mode_atomic_ioctl(struct drm_device *dev,
1282 void *data, struct drm_file *file_priv)
1283 {
1284 struct drm_mode_atomic *arg = data;
1285 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1286 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1287 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1288 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1289 unsigned int copied_objs, copied_props;
1290 struct drm_atomic_state *state;
1291 struct drm_modeset_acquire_ctx ctx;
1292 struct drm_out_fence_state *fence_state;
1293 int ret = 0;
1294 unsigned int i, j, num_fences;
1295
1296 /* disallow for drivers not supporting atomic: */
1297 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1298 return -EOPNOTSUPP;
1299
1300 /* disallow for userspace that has not enabled atomic cap (even
1301 * though this may be a bit overkill, since legacy userspace
1302 * wouldn't know how to call this ioctl)
1303 */
1304 if (!file_priv->atomic)
1305 return -EINVAL;
1306
1307 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1308 return -EINVAL;
1309
1310 if (arg->reserved)
1311 return -EINVAL;
1312
1313 if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC)
1314 return -EINVAL;
1315
1316 /* can't test and expect an event at the same time. */
1317 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1318 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1319 return -EINVAL;
1320
1321 state = drm_atomic_state_alloc(dev);
1322 if (!state)
1323 return -ENOMEM;
1324
1325 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1326 state->acquire_ctx = &ctx;
1327 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1328
1329 retry:
1330 copied_objs = 0;
1331 copied_props = 0;
1332 fence_state = NULL;
1333 num_fences = 0;
1334
1335 for (i = 0; i < arg->count_objs; i++) {
1336 uint32_t obj_id, count_props;
1337 struct drm_mode_object *obj;
1338
1339 if (get_user(obj_id, objs_ptr + copied_objs)) {
1340 ret = -EFAULT;
1341 goto out;
1342 }
1343
1344 obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
1345 if (!obj) {
1346 ret = -ENOENT;
1347 goto out;
1348 }
1349
1350 if (!obj->properties) {
1351 drm_mode_object_put(obj);
1352 ret = -ENOENT;
1353 goto out;
1354 }
1355
1356 if (get_user(count_props, count_props_ptr + copied_objs)) {
1357 drm_mode_object_put(obj);
1358 ret = -EFAULT;
1359 goto out;
1360 }
1361
1362 copied_objs++;
1363
1364 for (j = 0; j < count_props; j++) {
1365 uint32_t prop_id;
1366 uint64_t prop_value;
1367 struct drm_property *prop;
1368
1369 if (get_user(prop_id, props_ptr + copied_props)) {
1370 drm_mode_object_put(obj);
1371 ret = -EFAULT;
1372 goto out;
1373 }
1374
1375 prop = drm_mode_obj_find_prop_id(obj, prop_id);
1376 if (!prop) {
1377 drm_mode_object_put(obj);
1378 ret = -ENOENT;
1379 goto out;
1380 }
1381
1382 if (copy_from_user(&prop_value,
1383 prop_values_ptr + copied_props,
1384 sizeof(prop_value))) {
1385 drm_mode_object_put(obj);
1386 ret = -EFAULT;
1387 goto out;
1388 }
1389
1390 ret = drm_atomic_set_property(state, file_priv,
1391 obj, prop, prop_value);
1392 if (ret) {
1393 drm_mode_object_put(obj);
1394 goto out;
1395 }
1396
1397 copied_props++;
1398 }
1399
1400 drm_mode_object_put(obj);
1401 }
1402
1403 ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
1404 &num_fences);
1405 if (ret)
1406 goto out;
1407
1408 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1409 ret = drm_atomic_check_only(state);
1410 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1411 ret = drm_atomic_nonblocking_commit(state);
1412 } else {
1413 if (drm_debug_enabled(DRM_UT_STATE))
1414 drm_atomic_print_state(state);
1415
1416 ret = drm_atomic_commit(state);
1417 }
1418
1419 out:
1420 complete_signaling(dev, state, fence_state, num_fences, !ret);
1421
1422 if (ret == -EDEADLK) {
1423 drm_atomic_state_clear(state);
1424 ret = drm_modeset_backoff(&ctx);
1425 if (!ret)
1426 goto retry;
1427 }
1428
1429 drm_atomic_state_put(state);
1430
1431 drm_modeset_drop_locks(&ctx);
1432 drm_modeset_acquire_fini(&ctx);
1433
1434 return ret;
1435 }
1436