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.h>
31 #include <drm/drm_atomic_helper.h>
32 #include <drm/drm_atomic_uapi.h>
33 #include <drm/drm_framebuffer.h>
34 #include <drm/drm_print.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_writeback.h>
37 #include <drm/drm_vblank.h>
38 #include <drm/drm_colorop.h>
39
40 #include <linux/export.h>
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 similar.
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 */
drm_atomic_set_mode_for_crtc(struct drm_crtc_state * state,const struct drm_display_mode * mode)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 struct drm_property_blob *blob;
84
85 drm_mode_convert_to_umode(&umode, mode);
86 blob = drm_property_create_blob(crtc->dev,
87 sizeof(umode), &umode);
88 if (IS_ERR(blob))
89 return PTR_ERR(blob);
90
91 drm_mode_copy(&state->mode, mode);
92
93 state->mode_blob = blob;
94 state->enable = true;
95 drm_dbg_atomic(crtc->dev,
96 "Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
97 mode->name, crtc->base.id, crtc->name, state);
98 } else {
99 memset(&state->mode, 0, sizeof(state->mode));
100 state->enable = false;
101 drm_dbg_atomic(crtc->dev,
102 "Set [NOMODE] for [CRTC:%d:%s] state %p\n",
103 crtc->base.id, crtc->name, state);
104 }
105
106 return 0;
107 }
108 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
109
110 /**
111 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
112 * @state: the CRTC whose incoming state to update
113 * @blob: pointer to blob property to use for mode
114 *
115 * Set a mode (originating from a blob property) on the desired CRTC state.
116 * This function will take a reference on the blob property for the CRTC state,
117 * and release the reference held on the state's existing mode property, if any
118 * was set.
119 *
120 * RETURNS:
121 * Zero on success, error code on failure. Cannot return -EDEADLK.
122 */
drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state * state,struct drm_property_blob * blob)123 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
124 struct drm_property_blob *blob)
125 {
126 struct drm_crtc *crtc = state->crtc;
127
128 if (blob == state->mode_blob)
129 return 0;
130
131 drm_property_blob_put(state->mode_blob);
132 state->mode_blob = NULL;
133
134 memset(&state->mode, 0, sizeof(state->mode));
135
136 if (blob) {
137 int ret;
138
139 if (blob->length != sizeof(struct drm_mode_modeinfo)) {
140 drm_dbg_atomic(crtc->dev,
141 "[CRTC:%d:%s] bad mode blob length: %zu\n",
142 crtc->base.id, crtc->name,
143 blob->length);
144 return -EINVAL;
145 }
146
147 ret = drm_mode_convert_umode(crtc->dev,
148 &state->mode, blob->data);
149 if (ret) {
150 drm_dbg_atomic(crtc->dev,
151 "[CRTC:%d:%s] invalid mode (%s, %pe): " DRM_MODE_FMT "\n",
152 crtc->base.id, crtc->name,
153 drm_get_mode_status_name(state->mode.status),
154 ERR_PTR(ret), DRM_MODE_ARG(&state->mode));
155 return -EINVAL;
156 }
157
158 state->mode_blob = drm_property_blob_get(blob);
159 state->enable = true;
160 drm_dbg_atomic(crtc->dev,
161 "Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
162 state->mode.name, crtc->base.id, crtc->name,
163 state);
164 } else {
165 state->enable = false;
166 drm_dbg_atomic(crtc->dev,
167 "Set [NOMODE] for [CRTC:%d:%s] state %p\n",
168 crtc->base.id, crtc->name, state);
169 }
170
171 return 0;
172 }
173 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
174
175 /**
176 * drm_atomic_set_crtc_for_plane - set CRTC for plane
177 * @plane_state: the plane whose incoming state to update
178 * @crtc: CRTC to use for the plane
179 *
180 * Changing the assigned CRTC for a plane requires us to grab the lock and state
181 * for the new CRTC, as needed. This function takes care of all these details
182 * besides updating the pointer in the state object itself.
183 *
184 * Returns:
185 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
186 * then the w/w mutex code has detected a deadlock and the entire atomic
187 * sequence must be restarted. All other errors are fatal.
188 */
189 int
drm_atomic_set_crtc_for_plane(struct drm_plane_state * plane_state,struct drm_crtc * crtc)190 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
191 struct drm_crtc *crtc)
192 {
193 struct drm_plane *plane = plane_state->plane;
194 struct drm_crtc_state *crtc_state;
195 /* Nothing to do for same crtc*/
196 if (plane_state->crtc == crtc)
197 return 0;
198 if (plane_state->crtc) {
199 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
200 plane_state->crtc);
201 if (WARN_ON(IS_ERR(crtc_state)))
202 return PTR_ERR(crtc_state);
203
204 crtc_state->plane_mask &= ~drm_plane_mask(plane);
205 }
206
207 plane_state->crtc = crtc;
208
209 if (crtc) {
210 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
211 crtc);
212 if (IS_ERR(crtc_state))
213 return PTR_ERR(crtc_state);
214 crtc_state->plane_mask |= drm_plane_mask(plane);
215 }
216
217 if (crtc)
218 drm_dbg_atomic(plane->dev,
219 "Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
220 plane->base.id, plane->name, plane_state,
221 crtc->base.id, crtc->name);
222 else
223 drm_dbg_atomic(plane->dev,
224 "Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
225 plane->base.id, plane->name, plane_state);
226
227 return 0;
228 }
229 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
230
231 /**
232 * drm_atomic_set_fb_for_plane - set framebuffer for plane
233 * @plane_state: atomic state object for the plane
234 * @fb: fb to use for the plane
235 *
236 * Changing the assigned framebuffer for a plane requires us to grab a reference
237 * to the new fb and drop the reference to the old fb, if there is one. This
238 * function takes care of all these details besides updating the pointer in the
239 * state object itself.
240 */
241 void
drm_atomic_set_fb_for_plane(struct drm_plane_state * plane_state,struct drm_framebuffer * fb)242 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
243 struct drm_framebuffer *fb)
244 {
245 struct drm_plane *plane = plane_state->plane;
246
247 if (fb)
248 drm_dbg_atomic(plane->dev,
249 "Set [FB:%d] for [PLANE:%d:%s] state %p\n",
250 fb->base.id, plane->base.id, plane->name,
251 plane_state);
252 else
253 drm_dbg_atomic(plane->dev,
254 "Set [NOFB] for [PLANE:%d:%s] state %p\n",
255 plane->base.id, plane->name, plane_state);
256
257 drm_framebuffer_assign(&plane_state->fb, fb);
258 }
259 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
260
261 /**
262 * drm_atomic_set_colorop_for_plane - set colorop for plane
263 * @plane_state: atomic state object for the plane
264 * @colorop: colorop to use for the plane
265 *
266 * Helper function to select the color pipeline on a plane by setting
267 * it to the first drm_colorop element of the pipeline.
268 *
269 * Return: true if plane color pipeline value changed, false otherwise.
270 */
271 bool
drm_atomic_set_colorop_for_plane(struct drm_plane_state * plane_state,struct drm_colorop * colorop)272 drm_atomic_set_colorop_for_plane(struct drm_plane_state *plane_state,
273 struct drm_colorop *colorop)
274 {
275 struct drm_plane *plane = plane_state->plane;
276
277 /* Color pipeline didn't change */
278 if (plane_state->color_pipeline == colorop)
279 return false;
280
281 if (colorop)
282 drm_dbg_atomic(plane->dev,
283 "Set [COLOROP:%d] for [PLANE:%d:%s] state %p\n",
284 colorop->base.id, plane->base.id, plane->name,
285 plane_state);
286 else
287 drm_dbg_atomic(plane->dev,
288 "Set [NOCOLOROP] for [PLANE:%d:%s] state %p\n",
289 plane->base.id, plane->name, plane_state);
290
291 plane_state->color_pipeline = colorop;
292
293 return true;
294 }
295 EXPORT_SYMBOL(drm_atomic_set_colorop_for_plane);
296
297 /**
298 * drm_atomic_set_crtc_for_connector - set CRTC for connector
299 * @conn_state: atomic state object for the connector
300 * @crtc: CRTC to use for the connector
301 *
302 * Changing the assigned CRTC for a connector requires us to grab the lock and
303 * state for the new CRTC, as needed. This function takes care of all these
304 * details besides updating the pointer in the state object itself.
305 *
306 * Returns:
307 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
308 * then the w/w mutex code has detected a deadlock and the entire atomic
309 * sequence must be restarted. All other errors are fatal.
310 */
311 int
drm_atomic_set_crtc_for_connector(struct drm_connector_state * conn_state,struct drm_crtc * crtc)312 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
313 struct drm_crtc *crtc)
314 {
315 struct drm_connector *connector = conn_state->connector;
316 struct drm_crtc_state *crtc_state;
317
318 if (conn_state->crtc == crtc)
319 return 0;
320
321 if (conn_state->crtc) {
322 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
323 conn_state->crtc);
324
325 crtc_state->connector_mask &=
326 ~drm_connector_mask(conn_state->connector);
327
328 drm_connector_put(conn_state->connector);
329 conn_state->crtc = NULL;
330 }
331
332 if (crtc) {
333 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
334 if (IS_ERR(crtc_state))
335 return PTR_ERR(crtc_state);
336
337 crtc_state->connector_mask |=
338 drm_connector_mask(conn_state->connector);
339
340 drm_connector_get(conn_state->connector);
341 conn_state->crtc = crtc;
342
343 drm_dbg_atomic(connector->dev,
344 "Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
345 connector->base.id, connector->name,
346 conn_state, crtc->base.id, crtc->name);
347 } else {
348 drm_dbg_atomic(connector->dev,
349 "Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
350 connector->base.id, connector->name,
351 conn_state);
352 }
353
354 return 0;
355 }
356 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
357
set_out_fence_for_crtc(struct drm_atomic_commit * state,struct drm_crtc * crtc,s32 __user * fence_ptr)358 static void set_out_fence_for_crtc(struct drm_atomic_commit *state,
359 struct drm_crtc *crtc, s32 __user *fence_ptr)
360 {
361 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
362 }
363
get_out_fence_for_crtc(struct drm_atomic_commit * state,struct drm_crtc * crtc)364 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_commit *state,
365 struct drm_crtc *crtc)
366 {
367 s32 __user *fence_ptr;
368
369 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
370 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
371
372 return fence_ptr;
373 }
374
set_out_fence_for_connector(struct drm_atomic_commit * state,struct drm_connector * connector,s32 __user * fence_ptr)375 static int set_out_fence_for_connector(struct drm_atomic_commit *state,
376 struct drm_connector *connector,
377 s32 __user *fence_ptr)
378 {
379 unsigned int index = drm_connector_index(connector);
380
381 if (!fence_ptr)
382 return 0;
383
384 if (put_user(-1, fence_ptr))
385 return -EFAULT;
386
387 state->connectors[index].out_fence_ptr = fence_ptr;
388
389 return 0;
390 }
391
get_out_fence_for_connector(struct drm_atomic_commit * state,struct drm_connector * connector)392 static s32 __user *get_out_fence_for_connector(struct drm_atomic_commit *state,
393 struct drm_connector *connector)
394 {
395 unsigned int index = drm_connector_index(connector);
396 s32 __user *fence_ptr;
397
398 fence_ptr = state->connectors[index].out_fence_ptr;
399 state->connectors[index].out_fence_ptr = NULL;
400
401 return fence_ptr;
402 }
403
drm_atomic_crtc_set_property(struct drm_crtc * crtc,struct drm_crtc_state * state,struct drm_property * property,uint64_t val)404 static int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
405 struct drm_crtc_state *state, struct drm_property *property,
406 uint64_t val)
407 {
408 struct drm_device *dev = crtc->dev;
409 struct drm_mode_config *config = &dev->mode_config;
410 bool replaced = false;
411 int ret;
412
413 if (property == config->prop_active)
414 state->active = val;
415 else if (property == config->prop_mode_id) {
416 struct drm_property_blob *mode =
417 drm_property_lookup_blob(dev, val);
418 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
419 drm_property_blob_put(mode);
420 return ret;
421 } else if (property == config->prop_vrr_enabled) {
422 state->vrr_enabled = val;
423 } else if (property == config->degamma_lut_property) {
424 const size_t elem_size = sizeof(struct drm_color_lut);
425 u64 lut_size;
426
427 ret = drm_object_immutable_property_get_value(&crtc->base,
428 config->degamma_lut_size_property,
429 &lut_size);
430 if (ret)
431 return ret;
432
433 ret = drm_property_replace_blob_from_id(dev,
434 &state->degamma_lut,
435 val,
436 elem_size * lut_size, -1, elem_size,
437 &replaced);
438 state->color_mgmt_changed |= replaced;
439 return ret;
440 } else if (property == config->ctm_property) {
441 ret = drm_property_replace_blob_from_id(dev,
442 &state->ctm,
443 val,
444 -1, sizeof(struct drm_color_ctm), -1,
445 &replaced);
446 state->color_mgmt_changed |= replaced;
447 return ret;
448 } else if (property == config->gamma_lut_property) {
449 const size_t elem_size = sizeof(struct drm_color_lut);
450 u64 lut_size;
451
452 ret = drm_object_immutable_property_get_value(&crtc->base,
453 config->gamma_lut_size_property,
454 &lut_size);
455 if (ret)
456 return ret;
457
458 ret = drm_property_replace_blob_from_id(dev,
459 &state->gamma_lut,
460 val,
461 elem_size * lut_size, -1, elem_size,
462 &replaced);
463 state->color_mgmt_changed |= replaced;
464 return ret;
465 } else if (property == config->background_color_property) {
466 state->background_color = val;
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 (property == crtc->scaling_filter_property) {
478 state->scaling_filter = val;
479 } else if (property == crtc->sharpness_strength_property) {
480 state->sharpness_strength = val;
481 } else if (crtc->funcs->atomic_set_property) {
482 return crtc->funcs->atomic_set_property(crtc, state, property, val);
483 } else {
484 drm_dbg_atomic(crtc->dev,
485 "[CRTC:%d:%s] unknown property [PROP:%d:%s]\n",
486 crtc->base.id, crtc->name,
487 property->base.id, property->name);
488 return -EINVAL;
489 }
490
491 return 0;
492 }
493
494 static int
drm_atomic_crtc_get_property(struct drm_crtc * crtc,const struct drm_crtc_state * state,struct drm_property * property,uint64_t * val)495 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
496 const struct drm_crtc_state *state,
497 struct drm_property *property, uint64_t *val)
498 {
499 struct drm_device *dev = crtc->dev;
500 struct drm_mode_config *config = &dev->mode_config;
501
502 if (property == config->prop_active)
503 *val = drm_atomic_crtc_effectively_active(state);
504 else if (property == config->prop_mode_id)
505 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
506 else if (property == config->prop_vrr_enabled)
507 *val = state->vrr_enabled;
508 else if (property == config->degamma_lut_property)
509 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
510 else if (property == config->ctm_property)
511 *val = (state->ctm) ? state->ctm->base.id : 0;
512 else if (property == config->gamma_lut_property)
513 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
514 else if (property == config->background_color_property)
515 *val = state->background_color;
516 else if (property == config->prop_out_fence_ptr)
517 *val = 0;
518 else if (property == crtc->scaling_filter_property)
519 *val = state->scaling_filter;
520 else if (property == crtc->sharpness_strength_property)
521 *val = state->sharpness_strength;
522 else if (crtc->funcs->atomic_get_property)
523 return crtc->funcs->atomic_get_property(crtc, state, property, val);
524 else {
525 drm_dbg_atomic(dev,
526 "[CRTC:%d:%s] unknown property [PROP:%d:%s]\n",
527 crtc->base.id, crtc->name,
528 property->base.id, property->name);
529 return -EINVAL;
530 }
531
532 return 0;
533 }
534
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)535 static int drm_atomic_plane_set_property(struct drm_plane *plane,
536 struct drm_plane_state *state, struct drm_file *file_priv,
537 struct drm_property *property, uint64_t val)
538 {
539 struct drm_device *dev = plane->dev;
540 struct drm_mode_config *config = &dev->mode_config;
541 bool replaced = false;
542 int ret;
543
544 if (property == config->prop_fb_id) {
545 struct drm_framebuffer *fb;
546
547 fb = drm_framebuffer_lookup(dev, file_priv, val);
548 drm_atomic_set_fb_for_plane(state, fb);
549 if (fb)
550 drm_framebuffer_put(fb);
551 } else if (property == config->prop_in_fence_fd) {
552 if (state->fence)
553 return -EINVAL;
554
555 if (U642I64(val) == -1)
556 return 0;
557
558 state->fence = sync_file_get_fence(val);
559 if (!state->fence)
560 return -EINVAL;
561
562 } else if (property == config->prop_crtc_id) {
563 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
564
565 if (val && !crtc) {
566 drm_dbg_atomic(dev,
567 "[PROP:%d:%s] cannot find CRTC with ID %llu\n",
568 property->base.id, property->name, val);
569 return -EACCES;
570 }
571 return drm_atomic_set_crtc_for_plane(state, crtc);
572 } else if (property == config->prop_crtc_x) {
573 state->crtc_x = U642I64(val);
574 } else if (property == config->prop_crtc_y) {
575 state->crtc_y = U642I64(val);
576 } else if (property == config->prop_crtc_w) {
577 state->crtc_w = val;
578 } else if (property == config->prop_crtc_h) {
579 state->crtc_h = val;
580 } else if (property == config->prop_src_x) {
581 state->src_x = val;
582 } else if (property == config->prop_src_y) {
583 state->src_y = val;
584 } else if (property == config->prop_src_w) {
585 state->src_w = val;
586 } else if (property == config->prop_src_h) {
587 state->src_h = val;
588 } else if (property == plane->alpha_property) {
589 state->alpha = val;
590 } else if (property == plane->blend_mode_property) {
591 state->pixel_blend_mode = val;
592 } else if (property == plane->rotation_property) {
593 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
594 drm_dbg_atomic(plane->dev,
595 "[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
596 plane->base.id, plane->name, val);
597 return -EINVAL;
598 }
599 state->rotation = val;
600 } else if (property == plane->zpos_property) {
601 state->zpos = val;
602 } else if (property == plane->color_encoding_property) {
603 state->color_encoding = val;
604 } else if (property == plane->color_range_property) {
605 state->color_range = val;
606 } else if (property == plane->color_pipeline_property) {
607 /* find DRM colorop object */
608 struct drm_colorop *colorop = NULL;
609
610 colorop = drm_colorop_find(dev, file_priv, val);
611
612 if (val && !colorop)
613 return -EACCES;
614
615 state->color_mgmt_changed |= drm_atomic_set_colorop_for_plane(state, colorop);
616 } else if (property == config->prop_fb_damage_clips) {
617 ret = drm_property_replace_blob_from_id(dev,
618 &state->fb_damage_clips,
619 val,
620 -1, -1, sizeof(struct drm_mode_rect),
621 &replaced);
622 return ret;
623 } else if (property == plane->scaling_filter_property) {
624 state->scaling_filter = val;
625 } else if (plane->funcs->atomic_set_property) {
626 return plane->funcs->atomic_set_property(plane, state,
627 property, val);
628 } else if (property == plane->hotspot_x_property) {
629 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
630 drm_dbg_atomic(plane->dev,
631 "[PLANE:%d:%s] is not a cursor plane: 0x%llx\n",
632 plane->base.id, plane->name, val);
633 return -EINVAL;
634 }
635 state->hotspot_x = val;
636 } else if (property == plane->hotspot_y_property) {
637 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
638 drm_dbg_atomic(plane->dev,
639 "[PLANE:%d:%s] is not a cursor plane: 0x%llx\n",
640 plane->base.id, plane->name, val);
641 return -EINVAL;
642 }
643 state->hotspot_y = val;
644 } else {
645 drm_dbg_atomic(plane->dev,
646 "[PLANE:%d:%s] unknown property [PROP:%d:%s]\n",
647 plane->base.id, plane->name,
648 property->base.id, property->name);
649 return -EINVAL;
650 }
651
652 return 0;
653 }
654
655 static int
drm_atomic_plane_get_property(struct drm_plane * plane,const struct drm_plane_state * state,struct drm_property * property,uint64_t * val)656 drm_atomic_plane_get_property(struct drm_plane *plane,
657 const struct drm_plane_state *state,
658 struct drm_property *property, uint64_t *val)
659 {
660 struct drm_device *dev = plane->dev;
661 struct drm_mode_config *config = &dev->mode_config;
662
663 if (property == config->prop_fb_id) {
664 *val = (state->fb) ? state->fb->base.id : 0;
665 } else if (property == config->prop_in_fence_fd) {
666 *val = -1;
667 } else if (property == config->prop_crtc_id) {
668 *val = (state->crtc) ? state->crtc->base.id : 0;
669 } else if (property == config->prop_crtc_x) {
670 *val = I642U64(state->crtc_x);
671 } else if (property == config->prop_crtc_y) {
672 *val = I642U64(state->crtc_y);
673 } else if (property == config->prop_crtc_w) {
674 *val = state->crtc_w;
675 } else if (property == config->prop_crtc_h) {
676 *val = state->crtc_h;
677 } else if (property == config->prop_src_x) {
678 *val = state->src_x;
679 } else if (property == config->prop_src_y) {
680 *val = state->src_y;
681 } else if (property == config->prop_src_w) {
682 *val = state->src_w;
683 } else if (property == config->prop_src_h) {
684 *val = state->src_h;
685 } else if (property == plane->alpha_property) {
686 *val = state->alpha;
687 } else if (property == plane->blend_mode_property) {
688 *val = state->pixel_blend_mode;
689 } else if (property == plane->rotation_property) {
690 *val = state->rotation;
691 } else if (property == plane->zpos_property) {
692 *val = state->zpos;
693 } else if (property == plane->color_encoding_property) {
694 *val = state->color_encoding;
695 } else if (property == plane->color_range_property) {
696 *val = state->color_range;
697 } else if (property == plane->color_pipeline_property) {
698 *val = (state->color_pipeline) ? state->color_pipeline->base.id : 0;
699 } else if (property == config->prop_fb_damage_clips) {
700 *val = (state->fb_damage_clips) ?
701 state->fb_damage_clips->base.id : 0;
702 } else if (property == plane->scaling_filter_property) {
703 *val = state->scaling_filter;
704 } else if (plane->funcs->atomic_get_property) {
705 return plane->funcs->atomic_get_property(plane, state, property, val);
706 } else if (property == plane->hotspot_x_property) {
707 *val = state->hotspot_x;
708 } else if (property == plane->hotspot_y_property) {
709 *val = state->hotspot_y;
710 } else {
711 drm_dbg_atomic(dev,
712 "[PLANE:%d:%s] unknown property [PROP:%d:%s]\n",
713 plane->base.id, plane->name,
714 property->base.id, property->name);
715 return -EINVAL;
716 }
717
718 return 0;
719 }
720
drm_atomic_color_set_data_property(struct drm_colorop * colorop,struct drm_colorop_state * state,struct drm_property * property,uint64_t val,bool * replaced)721 static int drm_atomic_color_set_data_property(struct drm_colorop *colorop,
722 struct drm_colorop_state *state,
723 struct drm_property *property,
724 uint64_t val,
725 bool *replaced)
726 {
727 ssize_t elem_size = -1;
728 ssize_t size = -1;
729
730 switch (colorop->type) {
731 case DRM_COLOROP_1D_LUT:
732 size = colorop->size * sizeof(struct drm_color_lut32);
733 break;
734 case DRM_COLOROP_CTM_3X4:
735 size = sizeof(struct drm_color_ctm_3x4);
736 break;
737 case DRM_COLOROP_3D_LUT:
738 size = colorop->size * colorop->size * colorop->size *
739 sizeof(struct drm_color_lut32);
740 break;
741 default:
742 /* should never get here */
743 return -EINVAL;
744 }
745
746 return drm_property_replace_blob_from_id(colorop->dev,
747 &state->data,
748 val,
749 -1, size, elem_size,
750 replaced);
751 }
752
drm_atomic_colorop_set_property(struct drm_colorop * colorop,struct drm_colorop_state * state,struct drm_file * file_priv,struct drm_property * property,uint64_t val,bool * replaced)753 static int drm_atomic_colorop_set_property(struct drm_colorop *colorop,
754 struct drm_colorop_state *state,
755 struct drm_file *file_priv,
756 struct drm_property *property,
757 uint64_t val,
758 bool *replaced)
759 {
760 if (property == colorop->bypass_property) {
761 if (state->bypass != val) {
762 state->bypass = val;
763 *replaced = true;
764 }
765 } else if (property == colorop->lut1d_interpolation_property) {
766 if (state->lut1d_interpolation != val) {
767 state->lut1d_interpolation = val;
768 *replaced = true;
769 }
770 } else if (property == colorop->curve_1d_type_property) {
771 if (state->curve_1d_type != val) {
772 state->curve_1d_type = val;
773 *replaced = true;
774 }
775 } else if (property == colorop->multiplier_property) {
776 if (state->multiplier != val) {
777 state->multiplier = val;
778 *replaced = true;
779 }
780 } else if (property == colorop->lut3d_interpolation_property) {
781 if (state->lut3d_interpolation != val) {
782 state->lut3d_interpolation = val;
783 *replaced = true;
784 }
785 } else if (property == colorop->data_property) {
786 return drm_atomic_color_set_data_property(colorop, state,
787 property, val,
788 replaced);
789 } else {
790 drm_dbg_atomic(colorop->dev,
791 "[COLOROP:%d:%d] unknown property [PROP:%d:%s]\n",
792 colorop->base.id, colorop->type,
793 property->base.id, property->name);
794 return -EINVAL;
795 }
796
797 return 0;
798 }
799
800 static int
drm_atomic_colorop_get_property(struct drm_colorop * colorop,const struct drm_colorop_state * state,struct drm_property * property,uint64_t * val)801 drm_atomic_colorop_get_property(struct drm_colorop *colorop,
802 const struct drm_colorop_state *state,
803 struct drm_property *property, uint64_t *val)
804 {
805 if (property == colorop->type_property)
806 *val = colorop->type;
807 else if (property == colorop->bypass_property)
808 *val = state->bypass;
809 else if (property == colorop->lut1d_interpolation_property)
810 *val = state->lut1d_interpolation;
811 else if (property == colorop->curve_1d_type_property)
812 *val = state->curve_1d_type;
813 else if (property == colorop->multiplier_property)
814 *val = state->multiplier;
815 else if (property == colorop->size_property)
816 *val = colorop->size;
817 else if (property == colorop->lut3d_interpolation_property)
818 *val = state->lut3d_interpolation;
819 else if (property == colorop->data_property)
820 *val = (state->data) ? state->data->base.id : 0;
821 else
822 return -EINVAL;
823
824 return 0;
825 }
826
drm_atomic_set_writeback_fb_for_connector(struct drm_connector_state * conn_state,struct drm_framebuffer * fb)827 static int drm_atomic_set_writeback_fb_for_connector(
828 struct drm_connector_state *conn_state,
829 struct drm_framebuffer *fb)
830 {
831 int ret;
832 struct drm_connector *conn = conn_state->connector;
833
834 ret = drm_writeback_set_fb(conn_state, fb);
835 if (ret < 0)
836 return ret;
837
838 if (fb)
839 drm_dbg_atomic(conn->dev,
840 "Set [FB:%d] for connector state %p\n",
841 fb->base.id, conn_state);
842 else
843 drm_dbg_atomic(conn->dev,
844 "Set [NOFB] for connector state %p\n",
845 conn_state);
846
847 return 0;
848 }
849
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)850 static int drm_atomic_connector_set_property(struct drm_connector *connector,
851 struct drm_connector_state *state, struct drm_file *file_priv,
852 struct drm_property *property, uint64_t val)
853 {
854 struct drm_device *dev = connector->dev;
855 struct drm_mode_config *config = &dev->mode_config;
856 bool replaced = false;
857 int ret;
858
859 if (property == config->prop_crtc_id) {
860 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
861
862 if (val && !crtc) {
863 drm_dbg_atomic(dev,
864 "[PROP:%d:%s] cannot find CRTC with ID %llu\n",
865 property->base.id, property->name, val);
866 return -EACCES;
867 }
868 return drm_atomic_set_crtc_for_connector(state, crtc);
869 } else if (property == config->dpms_property) {
870 /* setting DPMS property requires special handling, which
871 * is done in legacy setprop path for us. Disallow (for
872 * now?) atomic writes to DPMS property:
873 */
874 drm_dbg_atomic(dev,
875 "legacy [PROP:%d:%s] can only be set via legacy uAPI\n",
876 property->base.id, property->name);
877 return -EINVAL;
878 } else if (property == config->tv_select_subconnector_property) {
879 state->tv.select_subconnector = val;
880 } else if (property == config->tv_subconnector_property) {
881 state->tv.subconnector = val;
882 } else if (property == config->tv_left_margin_property) {
883 state->tv.margins.left = val;
884 } else if (property == config->tv_right_margin_property) {
885 state->tv.margins.right = val;
886 } else if (property == config->tv_top_margin_property) {
887 state->tv.margins.top = val;
888 } else if (property == config->tv_bottom_margin_property) {
889 state->tv.margins.bottom = val;
890 } else if (property == config->legacy_tv_mode_property) {
891 state->tv.legacy_mode = val;
892 } else if (property == config->tv_mode_property) {
893 state->tv.mode = val;
894 } else if (property == config->tv_brightness_property) {
895 state->tv.brightness = val;
896 } else if (property == config->tv_contrast_property) {
897 state->tv.contrast = val;
898 } else if (property == config->tv_flicker_reduction_property) {
899 state->tv.flicker_reduction = val;
900 } else if (property == config->tv_overscan_property) {
901 state->tv.overscan = val;
902 } else if (property == config->tv_saturation_property) {
903 state->tv.saturation = val;
904 } else if (property == config->tv_hue_property) {
905 state->tv.hue = val;
906 } else if (property == config->link_status_property) {
907 /* Never downgrade from GOOD to BAD on userspace's request here,
908 * only hw issues can do that.
909 *
910 * For an atomic property the userspace doesn't need to be able
911 * to understand all the properties, but needs to be able to
912 * restore the state it wants on VT switch. So if the userspace
913 * tries to change the link_status from GOOD to BAD, driver
914 * silently rejects it and returns a 0. This prevents userspace
915 * from accidentally breaking the display when it restores the
916 * state.
917 */
918 if (state->link_status != DRM_LINK_STATUS_GOOD)
919 state->link_status = val;
920 } else if (property == config->hdr_output_metadata_property) {
921 ret = drm_property_replace_blob_from_id(dev,
922 &state->hdr_output_metadata,
923 val,
924 -1, sizeof(struct hdr_output_metadata), -1,
925 &replaced);
926 return ret;
927 } else if (property == config->aspect_ratio_property) {
928 state->picture_aspect_ratio = val;
929 } else if (property == config->content_type_property) {
930 state->content_type = val;
931 } else if (property == connector->scaling_mode_property) {
932 state->scaling_mode = val;
933 } else if (property == config->content_protection_property) {
934 if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
935 drm_dbg_kms(dev, "only drivers can set CP Enabled\n");
936 return -EINVAL;
937 }
938 state->content_protection = val;
939 } else if (property == config->hdcp_content_type_property) {
940 state->hdcp_content_type = val;
941 } else if (property == connector->colorspace_property) {
942 state->colorspace = val;
943 } else if (property == config->writeback_fb_id_property) {
944 struct drm_framebuffer *fb;
945 int ret;
946
947 fb = drm_framebuffer_lookup(dev, file_priv, val);
948 ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
949 if (fb)
950 drm_framebuffer_put(fb);
951 return ret;
952 } else if (property == config->writeback_out_fence_ptr_property) {
953 s32 __user *fence_ptr = u64_to_user_ptr(val);
954
955 return set_out_fence_for_connector(state->state, connector,
956 fence_ptr);
957 } else if (property == connector->max_bpc_property) {
958 state->max_requested_bpc = val;
959 } else if (property == connector->privacy_screen_sw_state_property) {
960 state->privacy_screen_sw_state = val;
961 } else if (property == connector->broadcast_rgb_property) {
962 state->hdmi.broadcast_rgb = val;
963 } else if (property == connector->color_format_property) {
964 state->color_format = val;
965 } else if (connector->funcs->atomic_set_property) {
966 return connector->funcs->atomic_set_property(connector,
967 state, property, val);
968 } else {
969 drm_dbg_atomic(connector->dev,
970 "[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]\n",
971 connector->base.id, connector->name,
972 property->base.id, property->name);
973 return -EINVAL;
974 }
975
976 return 0;
977 }
978
979 static int
drm_atomic_connector_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)980 drm_atomic_connector_get_property(struct drm_connector *connector,
981 const struct drm_connector_state *state,
982 struct drm_property *property, uint64_t *val)
983 {
984 struct drm_device *dev = connector->dev;
985 struct drm_mode_config *config = &dev->mode_config;
986
987 if (property == config->prop_crtc_id) {
988 *val = (state->crtc) ? state->crtc->base.id : 0;
989 } else if (property == config->dpms_property) {
990 if (state->crtc && state->crtc->state->self_refresh_active)
991 *val = DRM_MODE_DPMS_ON;
992 else
993 *val = connector->dpms;
994 } else if (property == config->tv_select_subconnector_property) {
995 *val = state->tv.select_subconnector;
996 } else if (property == config->tv_subconnector_property) {
997 *val = state->tv.subconnector;
998 } else if (property == config->tv_left_margin_property) {
999 *val = state->tv.margins.left;
1000 } else if (property == config->tv_right_margin_property) {
1001 *val = state->tv.margins.right;
1002 } else if (property == config->tv_top_margin_property) {
1003 *val = state->tv.margins.top;
1004 } else if (property == config->tv_bottom_margin_property) {
1005 *val = state->tv.margins.bottom;
1006 } else if (property == config->legacy_tv_mode_property) {
1007 *val = state->tv.legacy_mode;
1008 } else if (property == config->tv_mode_property) {
1009 *val = state->tv.mode;
1010 } else if (property == config->tv_brightness_property) {
1011 *val = state->tv.brightness;
1012 } else if (property == config->tv_contrast_property) {
1013 *val = state->tv.contrast;
1014 } else if (property == config->tv_flicker_reduction_property) {
1015 *val = state->tv.flicker_reduction;
1016 } else if (property == config->tv_overscan_property) {
1017 *val = state->tv.overscan;
1018 } else if (property == config->tv_saturation_property) {
1019 *val = state->tv.saturation;
1020 } else if (property == config->tv_hue_property) {
1021 *val = state->tv.hue;
1022 } else if (property == config->link_status_property) {
1023 *val = state->link_status;
1024 } else if (property == config->aspect_ratio_property) {
1025 *val = state->picture_aspect_ratio;
1026 } else if (property == config->content_type_property) {
1027 *val = state->content_type;
1028 } else if (property == connector->colorspace_property) {
1029 *val = state->colorspace;
1030 } else if (property == connector->scaling_mode_property) {
1031 *val = state->scaling_mode;
1032 } else if (property == config->hdr_output_metadata_property) {
1033 *val = state->hdr_output_metadata ?
1034 state->hdr_output_metadata->base.id : 0;
1035 } else if (property == config->content_protection_property) {
1036 *val = state->content_protection;
1037 } else if (property == config->hdcp_content_type_property) {
1038 *val = state->hdcp_content_type;
1039 } else if (property == config->writeback_fb_id_property) {
1040 /* Writeback framebuffer is one-shot, write and forget */
1041 *val = 0;
1042 } else if (property == config->writeback_out_fence_ptr_property) {
1043 *val = 0;
1044 } else if (property == connector->max_bpc_property) {
1045 *val = state->max_requested_bpc;
1046 } else if (property == connector->privacy_screen_sw_state_property) {
1047 *val = state->privacy_screen_sw_state;
1048 } else if (property == connector->broadcast_rgb_property) {
1049 *val = state->hdmi.broadcast_rgb;
1050 } else if (property == connector->color_format_property) {
1051 *val = state->color_format;
1052 } else if (connector->funcs->atomic_get_property) {
1053 return connector->funcs->atomic_get_property(connector,
1054 state, property, val);
1055 } else {
1056 drm_dbg_atomic(dev,
1057 "[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]\n",
1058 connector->base.id, connector->name,
1059 property->base.id, property->name);
1060 return -EINVAL;
1061 }
1062
1063 return 0;
1064 }
1065
drm_atomic_get_property(struct drm_mode_object * obj,struct drm_property * property,uint64_t * val)1066 int drm_atomic_get_property(struct drm_mode_object *obj,
1067 struct drm_property *property, uint64_t *val)
1068 {
1069 struct drm_device *dev = property->dev;
1070 int ret;
1071
1072 switch (obj->type) {
1073 case DRM_MODE_OBJECT_CONNECTOR: {
1074 struct drm_connector *connector = obj_to_connector(obj);
1075
1076 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1077 ret = drm_atomic_connector_get_property(connector,
1078 connector->state, property, val);
1079 break;
1080 }
1081 case DRM_MODE_OBJECT_CRTC: {
1082 struct drm_crtc *crtc = obj_to_crtc(obj);
1083
1084 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1085 ret = drm_atomic_crtc_get_property(crtc,
1086 crtc->state, property, val);
1087 break;
1088 }
1089 case DRM_MODE_OBJECT_PLANE: {
1090 struct drm_plane *plane = obj_to_plane(obj);
1091
1092 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1093 ret = drm_atomic_plane_get_property(plane,
1094 plane->state, property, val);
1095 break;
1096 }
1097 case DRM_MODE_OBJECT_COLOROP: {
1098 struct drm_colorop *colorop = obj_to_colorop(obj);
1099
1100 if (colorop->plane)
1101 WARN_ON(!drm_modeset_is_locked(&colorop->plane->mutex));
1102
1103 ret = drm_atomic_colorop_get_property(colorop, colorop->state, property, val);
1104 break;
1105 }
1106 default:
1107 drm_dbg_atomic(dev, "[OBJECT:%d] has no properties\n", obj->id);
1108 ret = -EINVAL;
1109 break;
1110 }
1111
1112 return ret;
1113 }
1114
1115 /*
1116 * The big monster ioctl
1117 */
1118
create_vblank_event(struct drm_crtc * crtc,uint64_t user_data)1119 static struct drm_pending_vblank_event *create_vblank_event(
1120 struct drm_crtc *crtc, uint64_t user_data)
1121 {
1122 struct drm_pending_vblank_event *e = NULL;
1123
1124 e = kzalloc_obj(*e);
1125 if (!e)
1126 return NULL;
1127
1128 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1129 e->event.base.length = sizeof(e->event);
1130 e->event.vbl.crtc_id = crtc->base.id;
1131 e->event.vbl.user_data = user_data;
1132
1133 return e;
1134 }
1135
drm_atomic_connector_commit_dpms(struct drm_atomic_commit * state,struct drm_connector * connector,int mode)1136 int drm_atomic_connector_commit_dpms(struct drm_atomic_commit *state,
1137 struct drm_connector *connector,
1138 int mode)
1139 {
1140 struct drm_connector *tmp_connector;
1141 struct drm_connector_state *new_conn_state;
1142 struct drm_crtc *crtc;
1143 struct drm_crtc_state *crtc_state;
1144 int i, ret, old_mode = connector->dpms;
1145 bool active = false;
1146
1147 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1148 state->acquire_ctx);
1149 if (ret)
1150 return ret;
1151
1152 if (mode != DRM_MODE_DPMS_ON)
1153 mode = DRM_MODE_DPMS_OFF;
1154
1155 if (connector->dpms == mode)
1156 goto out;
1157
1158 connector->dpms = mode;
1159
1160 crtc = connector->state->crtc;
1161 if (!crtc)
1162 goto out;
1163 ret = drm_atomic_add_affected_connectors(state, crtc);
1164 if (ret)
1165 goto out;
1166
1167 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1168 if (IS_ERR(crtc_state)) {
1169 ret = PTR_ERR(crtc_state);
1170 goto out;
1171 }
1172
1173 for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
1174 if (new_conn_state->crtc != crtc)
1175 continue;
1176 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
1177 active = true;
1178 break;
1179 }
1180 }
1181
1182 crtc_state->active = active;
1183 ret = drm_atomic_commit(state);
1184 out:
1185 if (ret != 0)
1186 connector->dpms = old_mode;
1187 return ret;
1188 }
1189
drm_atomic_check_prop_changes(int ret,uint64_t old_val,uint64_t prop_value,struct drm_property * prop)1190 static int drm_atomic_check_prop_changes(int ret, uint64_t old_val, uint64_t prop_value,
1191 struct drm_property *prop)
1192 {
1193 if (ret != 0 || old_val != prop_value) {
1194 drm_dbg_atomic(prop->dev,
1195 "[PROP:%d:%s] No prop can be changed during async flip\n",
1196 prop->base.id, prop->name);
1197 return -EINVAL;
1198 }
1199
1200 return 0;
1201 }
1202
drm_atomic_set_property(struct drm_atomic_commit * state,struct drm_file * file_priv,struct drm_mode_object * obj,struct drm_property * prop,u64 prop_value,bool async_flip)1203 int drm_atomic_set_property(struct drm_atomic_commit *state,
1204 struct drm_file *file_priv,
1205 struct drm_mode_object *obj,
1206 struct drm_property *prop,
1207 u64 prop_value,
1208 bool async_flip)
1209 {
1210 struct drm_mode_object *ref;
1211 u64 old_val;
1212 int ret;
1213
1214 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1215 return -EINVAL;
1216
1217 switch (obj->type) {
1218 case DRM_MODE_OBJECT_CONNECTOR: {
1219 struct drm_connector *connector = obj_to_connector(obj);
1220 struct drm_connector_state *connector_state;
1221
1222 connector_state = drm_atomic_get_connector_state(state, connector);
1223 if (IS_ERR(connector_state)) {
1224 ret = PTR_ERR(connector_state);
1225 break;
1226 }
1227
1228 if (async_flip) {
1229 ret = drm_atomic_connector_get_property(connector, connector_state,
1230 prop, &old_val);
1231 ret = drm_atomic_check_prop_changes(ret, old_val, prop_value, prop);
1232 break;
1233 }
1234
1235 ret = drm_atomic_connector_set_property(connector,
1236 connector_state, file_priv,
1237 prop, prop_value);
1238 break;
1239 }
1240 case DRM_MODE_OBJECT_CRTC: {
1241 struct drm_crtc *crtc = obj_to_crtc(obj);
1242 struct drm_crtc_state *crtc_state;
1243
1244 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1245 if (IS_ERR(crtc_state)) {
1246 ret = PTR_ERR(crtc_state);
1247 break;
1248 }
1249
1250 if (async_flip) {
1251 ret = drm_atomic_crtc_get_property(crtc, crtc_state,
1252 prop, &old_val);
1253 ret = drm_atomic_check_prop_changes(ret, old_val, prop_value, prop);
1254 break;
1255 }
1256
1257 ret = drm_atomic_crtc_set_property(crtc,
1258 crtc_state, prop, prop_value);
1259 break;
1260 }
1261 case DRM_MODE_OBJECT_PLANE: {
1262 struct drm_plane *plane = obj_to_plane(obj);
1263 struct drm_plane_state *plane_state;
1264 struct drm_mode_config *config = &plane->dev->mode_config;
1265 const struct drm_plane_helper_funcs *plane_funcs = plane->helper_private;
1266
1267 plane_state = drm_atomic_get_plane_state(state, plane);
1268 if (IS_ERR(plane_state)) {
1269 ret = PTR_ERR(plane_state);
1270 break;
1271 }
1272
1273 if (async_flip) {
1274 /* no-op changes are always allowed */
1275 ret = drm_atomic_plane_get_property(plane, plane_state,
1276 prop, &old_val);
1277 ret = drm_atomic_check_prop_changes(ret, old_val, prop_value, prop);
1278
1279 /* fail everything that isn't no-op or a pure flip */
1280 if (ret && prop != config->prop_fb_id &&
1281 prop != config->prop_in_fence_fd &&
1282 prop != config->prop_fb_damage_clips) {
1283 break;
1284 }
1285
1286 if (ret && plane->type != DRM_PLANE_TYPE_PRIMARY) {
1287 /* ask the driver if this non-primary plane is supported */
1288 if (plane_funcs && plane_funcs->atomic_async_check)
1289 ret = plane_funcs->atomic_async_check(plane, state, true);
1290
1291 if (ret) {
1292 drm_dbg_atomic(prop->dev,
1293 "[PLANE:%d:%s] does not support async flips\n",
1294 obj->id, plane->name);
1295 break;
1296 }
1297 }
1298 }
1299
1300 ret = drm_atomic_plane_set_property(plane,
1301 plane_state, file_priv,
1302 prop, prop_value);
1303
1304 break;
1305 }
1306 case DRM_MODE_OBJECT_COLOROP: {
1307 struct drm_plane_state *plane_state;
1308 struct drm_colorop *colorop = obj_to_colorop(obj);
1309 struct drm_colorop_state *colorop_state;
1310 bool replaced = false;
1311
1312 colorop_state = drm_atomic_get_colorop_state(state, colorop);
1313 if (IS_ERR(colorop_state)) {
1314 ret = PTR_ERR(colorop_state);
1315 break;
1316 }
1317
1318 ret = drm_atomic_colorop_set_property(colorop, colorop_state,
1319 file_priv, prop, prop_value,
1320 &replaced);
1321 if (ret || !replaced)
1322 break;
1323
1324 plane_state = drm_atomic_get_plane_state(state, colorop->plane);
1325 if (IS_ERR(plane_state)) {
1326 ret = PTR_ERR(plane_state);
1327 break;
1328 }
1329 plane_state->color_mgmt_changed |= replaced;
1330
1331 break;
1332 }
1333 default:
1334 drm_dbg_atomic(prop->dev, "[OBJECT:%d] has no properties\n", obj->id);
1335 ret = -EINVAL;
1336 break;
1337 }
1338
1339 drm_property_change_valid_put(prop, ref);
1340 return ret;
1341 }
1342
1343 /**
1344 * DOC: explicit fencing properties
1345 *
1346 * Explicit fencing allows userspace to control the buffer synchronization
1347 * between devices. A Fence or a group of fences are transferred to/from
1348 * userspace using Sync File fds and there are two DRM properties for that.
1349 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1350 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1351 *
1352 * As a contrast, with implicit fencing the kernel keeps track of any
1353 * ongoing rendering, and automatically ensures that the atomic update waits
1354 * for any pending rendering to complete. This is usually tracked in &struct
1355 * dma_resv which can also contain mandatory kernel fences. Implicit syncing
1356 * is how Linux traditionally worked (e.g. DRI2/3 on X.org), whereas explicit
1357 * fencing is what Android wants.
1358 *
1359 * "IN_FENCE_FD”:
1360 * Use this property to pass a fence that DRM should wait on before
1361 * proceeding with the Atomic Commit request and show the framebuffer for
1362 * the plane on the screen. The fence can be either a normal fence or a
1363 * merged one, the sync_file framework will handle both cases and use a
1364 * fence_array if a merged fence is received. Passing -1 here means no
1365 * fences to wait on.
1366 *
1367 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1368 * it will only check if the Sync File is a valid one.
1369 *
1370 * On the driver side the fence is stored on the @fence parameter of
1371 * &struct drm_plane_state. Drivers which also support implicit fencing
1372 * should extract the implicit fence using drm_gem_plane_helper_prepare_fb(),
1373 * to make sure there's consistent behaviour between drivers in precedence
1374 * of implicit vs. explicit fencing.
1375 *
1376 * "OUT_FENCE_PTR”:
1377 * Use this property to pass a file descriptor pointer to DRM. Once the
1378 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1379 * the file descriptor number of a Sync File. This Sync File contains the
1380 * CRTC fence that will be signaled when all framebuffers present on the
1381 * Atomic Commit * request for that given CRTC are scanned out on the
1382 * screen.
1383 *
1384 * The Atomic Commit request fails if a invalid pointer is passed. If the
1385 * Atomic Commit request fails for any other reason the out fence fd
1386 * returned will be -1. On a Atomic Commit with the
1387 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1388 *
1389 * Note that out-fences don't have a special interface to drivers and are
1390 * internally represented by a &struct drm_pending_vblank_event in struct
1391 * &drm_crtc_state, which is also used by the nonblocking atomic commit
1392 * helpers and for the DRM event handling for existing userspace.
1393 */
1394
1395 struct drm_out_fence_state {
1396 s32 __user *out_fence_ptr;
1397 struct sync_file *sync_file;
1398 int fd;
1399 };
1400
setup_out_fence(struct drm_out_fence_state * fence_state,struct dma_fence * fence)1401 static int setup_out_fence(struct drm_out_fence_state *fence_state,
1402 struct dma_fence *fence)
1403 {
1404 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1405 if (fence_state->fd < 0)
1406 return fence_state->fd;
1407
1408 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1409 return -EFAULT;
1410
1411 fence_state->sync_file = sync_file_create(fence);
1412 if (!fence_state->sync_file)
1413 return -ENOMEM;
1414
1415 return 0;
1416 }
1417
prepare_signaling(struct drm_device * dev,struct drm_atomic_commit * state,struct drm_mode_atomic * arg,struct drm_file * file_priv,struct drm_out_fence_state ** fence_state,unsigned int * num_fences)1418 static int prepare_signaling(struct drm_device *dev,
1419 struct drm_atomic_commit *state,
1420 struct drm_mode_atomic *arg,
1421 struct drm_file *file_priv,
1422 struct drm_out_fence_state **fence_state,
1423 unsigned int *num_fences)
1424 {
1425 struct drm_crtc *crtc;
1426 struct drm_crtc_state *crtc_state;
1427 struct drm_connector *conn;
1428 struct drm_connector_state *conn_state;
1429 int i, c = 0, ret;
1430
1431 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1432 return 0;
1433
1434 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1435 s32 __user *fence_ptr;
1436
1437 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1438
1439 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1440 struct drm_pending_vblank_event *e;
1441
1442 e = create_vblank_event(crtc, arg->user_data);
1443 if (!e)
1444 return -ENOMEM;
1445
1446 crtc_state->event = e;
1447 }
1448
1449 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1450 struct drm_pending_vblank_event *e = crtc_state->event;
1451
1452 ret = drm_event_reserve_init(dev, file_priv, &e->base,
1453 &e->event.base);
1454 if (ret) {
1455 kfree(e);
1456 crtc_state->event = NULL;
1457 return ret;
1458 }
1459 }
1460
1461 if (fence_ptr) {
1462 struct dma_fence *fence;
1463 struct drm_out_fence_state *f;
1464
1465 ret = -ENOMEM;
1466
1467 f = krealloc(*fence_state, sizeof(**fence_state) *
1468 (*num_fences + 1), GFP_KERNEL);
1469 if (!f)
1470 goto err_free_event;
1471
1472 memset(&f[*num_fences], 0, sizeof(*f));
1473
1474 f[*num_fences].out_fence_ptr = fence_ptr;
1475 *fence_state = f;
1476
1477 fence = drm_crtc_create_fence(crtc);
1478 if (!fence)
1479 goto err_free_event;
1480
1481 ret = setup_out_fence(&f[(*num_fences)++], fence);
1482 if (ret) {
1483 dma_fence_put(fence);
1484 goto err_free_event;
1485 }
1486
1487 crtc_state->event->base.fence = fence;
1488 }
1489
1490 c++;
1491 }
1492
1493 for_each_new_connector_in_state(state, conn, conn_state, i) {
1494 struct drm_writeback_connector *wb_conn;
1495 struct drm_out_fence_state *f;
1496 struct dma_fence *fence;
1497 s32 __user *fence_ptr;
1498
1499 if (!conn_state->writeback_job)
1500 continue;
1501
1502 fence_ptr = get_out_fence_for_connector(state, conn);
1503 if (!fence_ptr)
1504 continue;
1505
1506 f = krealloc(*fence_state, sizeof(**fence_state) *
1507 (*num_fences + 1), GFP_KERNEL);
1508 if (!f)
1509 return -ENOMEM;
1510
1511 memset(&f[*num_fences], 0, sizeof(*f));
1512
1513 f[*num_fences].out_fence_ptr = fence_ptr;
1514 *fence_state = f;
1515
1516 wb_conn = drm_connector_to_writeback(conn);
1517 fence = drm_writeback_get_out_fence(wb_conn);
1518 if (!fence)
1519 return -ENOMEM;
1520
1521 ret = setup_out_fence(&f[(*num_fences)++], fence);
1522 if (ret) {
1523 dma_fence_put(fence);
1524 return ret;
1525 }
1526
1527 conn_state->writeback_job->out_fence = fence;
1528 }
1529
1530 /*
1531 * Having this flag means user mode pends on event which will never
1532 * reach due to lack of at least one CRTC for signaling
1533 */
1534 if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) {
1535 drm_dbg_atomic(dev, "need at least one CRTC for DRM_MODE_PAGE_FLIP_EVENT");
1536 return -EINVAL;
1537 }
1538
1539 return 0;
1540
1541 err_free_event:
1542 drm_event_cancel_free(dev, &crtc_state->event->base);
1543 crtc_state->event = NULL;
1544 return ret;
1545 }
1546
complete_signaling(struct drm_device * dev,struct drm_atomic_commit * state,struct drm_out_fence_state * fence_state,unsigned int num_fences,bool install_fds)1547 static void complete_signaling(struct drm_device *dev,
1548 struct drm_atomic_commit *state,
1549 struct drm_out_fence_state *fence_state,
1550 unsigned int num_fences,
1551 bool install_fds)
1552 {
1553 struct drm_crtc *crtc;
1554 struct drm_crtc_state *crtc_state;
1555 int i;
1556
1557 if (install_fds) {
1558 for (i = 0; i < num_fences; i++)
1559 fd_install(fence_state[i].fd,
1560 fence_state[i].sync_file->file);
1561
1562 kfree(fence_state);
1563 return;
1564 }
1565
1566 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1567 struct drm_pending_vblank_event *event = crtc_state->event;
1568 /*
1569 * Free the allocated event. drm_atomic_helper_setup_commit
1570 * can allocate an event too, so only free it if it's ours
1571 * to prevent a double free in drm_atomic_commit_clear.
1572 */
1573 if (event && (event->base.fence || event->base.file_priv)) {
1574 if (crtc_state->commit && crtc_state->commit->abort_completion)
1575 drm_crtc_commit_put(crtc_state->commit);
1576 drm_event_cancel_free(dev, &event->base);
1577 crtc_state->event = NULL;
1578 }
1579 }
1580
1581 if (!fence_state)
1582 return;
1583
1584 for (i = 0; i < num_fences; i++) {
1585 if (fence_state[i].sync_file)
1586 fput(fence_state[i].sync_file->file);
1587 if (fence_state[i].fd >= 0)
1588 put_unused_fd(fence_state[i].fd);
1589
1590 /* If this fails log error to the user */
1591 if (fence_state[i].out_fence_ptr &&
1592 put_user(-1, fence_state[i].out_fence_ptr))
1593 drm_dbg_atomic(dev, "Couldn't clear out_fence_ptr\n");
1594 }
1595
1596 kfree(fence_state);
1597 }
1598
1599 static void
set_async_flip(struct drm_atomic_commit * state)1600 set_async_flip(struct drm_atomic_commit *state)
1601 {
1602 struct drm_crtc *crtc;
1603 struct drm_crtc_state *crtc_state;
1604 int i;
1605
1606 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1607 crtc_state->async_flip = true;
1608 }
1609 }
1610
drm_mode_atomic_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1611 int drm_mode_atomic_ioctl(struct drm_device *dev,
1612 void *data, struct drm_file *file_priv)
1613 {
1614 struct drm_mode_atomic *arg = data;
1615 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1616 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1617 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1618 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1619 unsigned int copied_objs, copied_props;
1620 struct drm_atomic_commit *state;
1621 struct drm_modeset_acquire_ctx ctx;
1622 struct drm_out_fence_state *fence_state;
1623 int ret = 0;
1624 unsigned int i, j, num_fences;
1625 bool async_flip = false;
1626
1627 /* disallow for drivers not supporting atomic: */
1628 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1629 return -EOPNOTSUPP;
1630
1631 /* disallow for userspace that has not enabled atomic cap (even
1632 * though this may be a bit overkill, since legacy userspace
1633 * wouldn't know how to call this ioctl)
1634 */
1635 if (!file_priv->atomic) {
1636 drm_dbg_atomic(dev,
1637 "commit failed: atomic cap not enabled\n");
1638 return -EINVAL;
1639 }
1640
1641 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS) {
1642 drm_dbg_atomic(dev, "commit failed: invalid flag\n");
1643 return -EINVAL;
1644 }
1645
1646 if (arg->reserved) {
1647 drm_dbg_atomic(dev, "commit failed: reserved field set\n");
1648 return -EINVAL;
1649 }
1650
1651 if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) {
1652 if (!dev->mode_config.async_page_flip) {
1653 drm_dbg_atomic(dev,
1654 "commit failed: DRM_MODE_PAGE_FLIP_ASYNC not supported\n");
1655 return -EINVAL;
1656 }
1657
1658 async_flip = true;
1659 }
1660
1661 /* can't test and expect an event at the same time. */
1662 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1663 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) {
1664 drm_dbg_atomic(dev,
1665 "commit failed: page-flip event requested with test-only commit\n");
1666 return -EINVAL;
1667 }
1668
1669 state = drm_atomic_commit_alloc(dev);
1670 if (!state)
1671 return -ENOMEM;
1672
1673 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1674 state->acquire_ctx = &ctx;
1675 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1676 state->plane_color_pipeline = file_priv->plane_color_pipeline;
1677
1678 retry:
1679 copied_objs = 0;
1680 copied_props = 0;
1681 fence_state = NULL;
1682 num_fences = 0;
1683
1684 for (i = 0; i < arg->count_objs; i++) {
1685 uint32_t obj_id, count_props;
1686 struct drm_mode_object *obj;
1687
1688 if (get_user(obj_id, objs_ptr + copied_objs)) {
1689 ret = -EFAULT;
1690 goto out;
1691 }
1692
1693 obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
1694 if (!obj) {
1695 drm_dbg_atomic(dev, "cannot find object ID %d", obj_id);
1696 ret = -ENOENT;
1697 goto out;
1698 }
1699
1700 if (!obj->properties) {
1701 drm_dbg_atomic(dev, "[OBJECT:%d] has no properties", obj_id);
1702 drm_mode_object_put(obj);
1703 ret = -ENOENT;
1704 goto out;
1705 }
1706
1707 if (get_user(count_props, count_props_ptr + copied_objs)) {
1708 drm_mode_object_put(obj);
1709 ret = -EFAULT;
1710 goto out;
1711 }
1712
1713 copied_objs++;
1714
1715 for (j = 0; j < count_props; j++) {
1716 uint32_t prop_id;
1717 uint64_t prop_value;
1718 struct drm_property *prop;
1719
1720 if (get_user(prop_id, props_ptr + copied_props)) {
1721 drm_mode_object_put(obj);
1722 ret = -EFAULT;
1723 goto out;
1724 }
1725
1726 prop = drm_mode_obj_find_prop_id(obj, prop_id);
1727 if (!prop) {
1728 drm_dbg_atomic(dev,
1729 "[OBJECT:%d] cannot find property ID %d",
1730 obj_id, prop_id);
1731 drm_mode_object_put(obj);
1732 ret = -ENOENT;
1733 goto out;
1734 }
1735
1736 if (copy_from_user(&prop_value,
1737 prop_values_ptr + copied_props,
1738 sizeof(prop_value))) {
1739 drm_mode_object_put(obj);
1740 ret = -EFAULT;
1741 goto out;
1742 }
1743
1744 ret = drm_atomic_set_property(state, file_priv, obj,
1745 prop, prop_value, async_flip);
1746 if (ret) {
1747 drm_mode_object_put(obj);
1748 goto out;
1749 }
1750
1751 copied_props++;
1752 }
1753
1754 drm_mode_object_put(obj);
1755 }
1756
1757 ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
1758 &num_fences);
1759 if (ret)
1760 goto out;
1761
1762 if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC)
1763 set_async_flip(state);
1764
1765 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1766 ret = drm_atomic_check_only(state);
1767 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1768 ret = drm_atomic_nonblocking_commit(state);
1769 } else {
1770 ret = drm_atomic_commit(state);
1771 }
1772
1773 out:
1774 complete_signaling(dev, state, fence_state, num_fences, !ret);
1775
1776 if (ret == -EDEADLK) {
1777 drm_atomic_commit_clear(state);
1778 ret = drm_modeset_backoff(&ctx);
1779 if (!ret)
1780 goto retry;
1781 }
1782
1783 drm_atomic_commit_put(state);
1784
1785 drm_modeset_drop_locks(&ctx);
1786 drm_modeset_acquire_fini(&ctx);
1787
1788 return ret;
1789 }
1790