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