1 /*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Rob Clark <robdclark@gmail.com>
26 * Daniel Vetter <daniel.vetter@ffwll.ch>
27 */
28
29
30 #include <linux/sync_file.h>
31
32 #include <drm/drm_atomic.h>
33 #include <drm/drm_atomic_uapi.h>
34 #include <drm/drm_bridge.h>
35 #include <drm/drm_debugfs.h>
36 #include <drm/drm_device.h>
37 #include <drm/drm_drv.h>
38 #include <drm/drm_file.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_mode.h>
41 #include <drm/drm_print.h>
42 #include <drm/drm_writeback.h>
43
44 #include "drm_crtc_internal.h"
45 #include "drm_internal.h"
46
__drm_crtc_commit_free(struct kref * kref)47 void __drm_crtc_commit_free(struct kref *kref)
48 {
49 struct drm_crtc_commit *commit =
50 container_of(kref, struct drm_crtc_commit, ref);
51
52 kfree(commit);
53 }
54 EXPORT_SYMBOL(__drm_crtc_commit_free);
55
56 /**
57 * drm_crtc_commit_wait - Waits for a commit to complete
58 * @commit: &drm_crtc_commit to wait for
59 *
60 * Waits for a given &drm_crtc_commit to be programmed into the
61 * hardware and flipped to.
62 *
63 * Returns:
64 *
65 * 0 on success, a negative error code otherwise.
66 */
drm_crtc_commit_wait(struct drm_crtc_commit * commit)67 int drm_crtc_commit_wait(struct drm_crtc_commit *commit)
68 {
69 unsigned long timeout = 10 * HZ;
70 int ret;
71
72 if (!commit)
73 return 0;
74
75 ret = wait_for_completion_timeout(&commit->hw_done, timeout);
76 if (!ret) {
77 DRM_ERROR("hw_done timed out\n");
78 return -ETIMEDOUT;
79 }
80
81 /*
82 * Currently no support for overwriting flips, hence
83 * stall for previous one to execute completely.
84 */
85 ret = wait_for_completion_timeout(&commit->flip_done, timeout);
86 if (!ret) {
87 DRM_ERROR("flip_done timed out\n");
88 return -ETIMEDOUT;
89 }
90
91 return 0;
92 }
93 EXPORT_SYMBOL(drm_crtc_commit_wait);
94
95 /**
96 * drm_atomic_state_default_release -
97 * release memory initialized by drm_atomic_state_init
98 * @state: atomic state
99 *
100 * Free all the memory allocated by drm_atomic_state_init.
101 * This should only be used by drivers which are still subclassing
102 * &drm_atomic_state and haven't switched to &drm_private_state yet.
103 */
drm_atomic_state_default_release(struct drm_atomic_state * state)104 void drm_atomic_state_default_release(struct drm_atomic_state *state)
105 {
106 kfree(state->connectors);
107 kfree(state->crtcs);
108 kfree(state->planes);
109 kfree(state->private_objs);
110 }
111 EXPORT_SYMBOL(drm_atomic_state_default_release);
112
113 /**
114 * drm_atomic_state_init - init new atomic state
115 * @dev: DRM device
116 * @state: atomic state
117 *
118 * Default implementation for filling in a new atomic state.
119 * This should only be used by drivers which are still subclassing
120 * &drm_atomic_state and haven't switched to &drm_private_state yet.
121 */
122 int
drm_atomic_state_init(struct drm_device * dev,struct drm_atomic_state * state)123 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
124 {
125 kref_init(&state->ref);
126
127 /* TODO legacy paths should maybe do a better job about
128 * setting this appropriately?
129 */
130 state->allow_modeset = true;
131
132 state->crtcs = kcalloc(dev->mode_config.num_crtc,
133 sizeof(*state->crtcs), GFP_KERNEL);
134 if (!state->crtcs)
135 goto fail;
136 state->planes = kcalloc(dev->mode_config.num_total_plane,
137 sizeof(*state->planes), GFP_KERNEL);
138 if (!state->planes)
139 goto fail;
140
141 /*
142 * Because drm_atomic_state can be committed asynchronously we need our
143 * own reference and cannot rely on the on implied by drm_file in the
144 * ioctl call.
145 */
146 drm_dev_get(dev);
147 state->dev = dev;
148
149 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
150
151 return 0;
152 fail:
153 drm_atomic_state_default_release(state);
154 return -ENOMEM;
155 }
156 EXPORT_SYMBOL(drm_atomic_state_init);
157
158 /**
159 * drm_atomic_state_alloc - allocate atomic state
160 * @dev: DRM device
161 *
162 * This allocates an empty atomic state to track updates.
163 */
164 struct drm_atomic_state *
drm_atomic_state_alloc(struct drm_device * dev)165 drm_atomic_state_alloc(struct drm_device *dev)
166 {
167 struct drm_mode_config *config = &dev->mode_config;
168
169 if (!config->funcs->atomic_state_alloc) {
170 struct drm_atomic_state *state;
171
172 state = kzalloc(sizeof(*state), GFP_KERNEL);
173 if (!state)
174 return NULL;
175 if (drm_atomic_state_init(dev, state) < 0) {
176 kfree(state);
177 return NULL;
178 }
179 return state;
180 }
181
182 return config->funcs->atomic_state_alloc(dev);
183 }
184 EXPORT_SYMBOL(drm_atomic_state_alloc);
185
186 /**
187 * drm_atomic_state_default_clear - clear base atomic state
188 * @state: atomic state
189 *
190 * Default implementation for clearing atomic state.
191 * This should only be used by drivers which are still subclassing
192 * &drm_atomic_state and haven't switched to &drm_private_state yet.
193 */
drm_atomic_state_default_clear(struct drm_atomic_state * state)194 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
195 {
196 struct drm_device *dev = state->dev;
197 struct drm_mode_config *config = &dev->mode_config;
198 int i;
199
200 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
201
202 for (i = 0; i < state->num_connector; i++) {
203 struct drm_connector *connector = state->connectors[i].ptr;
204
205 if (!connector)
206 continue;
207
208 connector->funcs->atomic_destroy_state(connector,
209 state->connectors[i].state);
210 state->connectors[i].ptr = NULL;
211 state->connectors[i].state = NULL;
212 state->connectors[i].old_state = NULL;
213 state->connectors[i].new_state = NULL;
214 drm_connector_put(connector);
215 }
216
217 for (i = 0; i < config->num_crtc; i++) {
218 struct drm_crtc *crtc = state->crtcs[i].ptr;
219
220 if (!crtc)
221 continue;
222
223 crtc->funcs->atomic_destroy_state(crtc,
224 state->crtcs[i].state);
225
226 state->crtcs[i].ptr = NULL;
227 state->crtcs[i].state = NULL;
228 state->crtcs[i].old_state = NULL;
229 state->crtcs[i].new_state = NULL;
230
231 if (state->crtcs[i].commit) {
232 drm_crtc_commit_put(state->crtcs[i].commit);
233 state->crtcs[i].commit = NULL;
234 }
235 }
236
237 for (i = 0; i < config->num_total_plane; i++) {
238 struct drm_plane *plane = state->planes[i].ptr;
239
240 if (!plane)
241 continue;
242
243 plane->funcs->atomic_destroy_state(plane,
244 state->planes[i].state);
245 state->planes[i].ptr = NULL;
246 state->planes[i].state = NULL;
247 state->planes[i].old_state = NULL;
248 state->planes[i].new_state = NULL;
249 }
250
251 for (i = 0; i < state->num_private_objs; i++) {
252 struct drm_private_obj *obj = state->private_objs[i].ptr;
253
254 obj->funcs->atomic_destroy_state(obj,
255 state->private_objs[i].state);
256 state->private_objs[i].ptr = NULL;
257 state->private_objs[i].state = NULL;
258 state->private_objs[i].old_state = NULL;
259 state->private_objs[i].new_state = NULL;
260 }
261 state->num_private_objs = 0;
262
263 if (state->fake_commit) {
264 drm_crtc_commit_put(state->fake_commit);
265 state->fake_commit = NULL;
266 }
267 }
268 EXPORT_SYMBOL(drm_atomic_state_default_clear);
269
270 /**
271 * drm_atomic_state_clear - clear state object
272 * @state: atomic state
273 *
274 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
275 * all locks. So someone else could sneak in and change the current modeset
276 * configuration. Which means that all the state assembled in @state is no
277 * longer an atomic update to the current state, but to some arbitrary earlier
278 * state. Which could break assumptions the driver's
279 * &drm_mode_config_funcs.atomic_check likely relies on.
280 *
281 * Hence we must clear all cached state and completely start over, using this
282 * function.
283 */
drm_atomic_state_clear(struct drm_atomic_state * state)284 void drm_atomic_state_clear(struct drm_atomic_state *state)
285 {
286 struct drm_device *dev = state->dev;
287 struct drm_mode_config *config = &dev->mode_config;
288
289 if (config->funcs->atomic_state_clear)
290 config->funcs->atomic_state_clear(state);
291 else
292 drm_atomic_state_default_clear(state);
293 }
294 EXPORT_SYMBOL(drm_atomic_state_clear);
295
296 /**
297 * __drm_atomic_state_free - free all memory for an atomic state
298 * @ref: This atomic state to deallocate
299 *
300 * This frees all memory associated with an atomic state, including all the
301 * per-object state for planes, CRTCs and connectors.
302 */
__drm_atomic_state_free(struct kref * ref)303 void __drm_atomic_state_free(struct kref *ref)
304 {
305 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
306 struct drm_device *dev = state->dev;
307 struct drm_mode_config *config = &dev->mode_config;
308
309 drm_atomic_state_clear(state);
310
311 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
312
313 if (config->funcs->atomic_state_free) {
314 config->funcs->atomic_state_free(state);
315 } else {
316 drm_atomic_state_default_release(state);
317 kfree(state);
318 }
319
320 drm_dev_put(dev);
321 }
322 EXPORT_SYMBOL(__drm_atomic_state_free);
323
324 /**
325 * drm_atomic_get_crtc_state - get CRTC state
326 * @state: global atomic state object
327 * @crtc: CRTC to get state object for
328 *
329 * This function returns the CRTC state for the given CRTC, allocating it if
330 * needed. It will also grab the relevant CRTC lock to make sure that the state
331 * is consistent.
332 *
333 * WARNING: Drivers may only add new CRTC states to a @state if
334 * drm_atomic_state.allow_modeset is set, or if it's a driver-internal commit
335 * not created by userspace through an IOCTL call.
336 *
337 * Returns:
338 *
339 * Either the allocated state or the error code encoded into the pointer. When
340 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
341 * entire atomic sequence must be restarted. All other errors are fatal.
342 */
343 struct drm_crtc_state *
drm_atomic_get_crtc_state(struct drm_atomic_state * state,struct drm_crtc * crtc)344 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
345 struct drm_crtc *crtc)
346 {
347 int ret, index = drm_crtc_index(crtc);
348 struct drm_crtc_state *crtc_state;
349
350 WARN_ON(!state->acquire_ctx);
351
352 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
353 if (crtc_state)
354 return crtc_state;
355
356 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
357 if (ret)
358 return ERR_PTR(ret);
359
360 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
361 if (!crtc_state)
362 return ERR_PTR(-ENOMEM);
363
364 state->crtcs[index].state = crtc_state;
365 state->crtcs[index].old_state = crtc->state;
366 state->crtcs[index].new_state = crtc_state;
367 state->crtcs[index].ptr = crtc;
368 crtc_state->state = state;
369
370 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
371 crtc->base.id, crtc->name, crtc_state, state);
372
373 return crtc_state;
374 }
375 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
376
drm_atomic_crtc_check(const struct drm_crtc_state * old_crtc_state,const struct drm_crtc_state * new_crtc_state)377 static int drm_atomic_crtc_check(const struct drm_crtc_state *old_crtc_state,
378 const struct drm_crtc_state *new_crtc_state)
379 {
380 struct drm_crtc *crtc = new_crtc_state->crtc;
381
382 /* NOTE: we explicitly don't enforce constraints such as primary
383 * layer covering entire screen, since that is something we want
384 * to allow (on hw that supports it). For hw that does not, it
385 * should be checked in driver's crtc->atomic_check() vfunc.
386 *
387 * TODO: Add generic modeset state checks once we support those.
388 */
389
390 if (new_crtc_state->active && !new_crtc_state->enable) {
391 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
392 crtc->base.id, crtc->name);
393 return -EINVAL;
394 }
395
396 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
397 * as this is a kernel-internal detail that userspace should never
398 * be able to trigger.
399 */
400 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
401 WARN_ON(new_crtc_state->enable && !new_crtc_state->mode_blob)) {
402 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
403 crtc->base.id, crtc->name);
404 return -EINVAL;
405 }
406
407 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
408 WARN_ON(!new_crtc_state->enable && new_crtc_state->mode_blob)) {
409 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
410 crtc->base.id, crtc->name);
411 return -EINVAL;
412 }
413
414 /*
415 * Reject event generation for when a CRTC is off and stays off.
416 * It wouldn't be hard to implement this, but userspace has a track
417 * record of happily burning through 100% cpu (or worse, crash) when the
418 * display pipe is suspended. To avoid all that fun just reject updates
419 * that ask for events since likely that indicates a bug in the
420 * compositor's drawing loop. This is consistent with the vblank IOCTL
421 * and legacy page_flip IOCTL which also reject service on a disabled
422 * pipe.
423 */
424 if (new_crtc_state->event &&
425 !new_crtc_state->active && !old_crtc_state->active) {
426 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
427 crtc->base.id, crtc->name);
428 return -EINVAL;
429 }
430
431 return 0;
432 }
433
drm_atomic_crtc_print_state(struct drm_printer * p,const struct drm_crtc_state * state)434 static void drm_atomic_crtc_print_state(struct drm_printer *p,
435 const struct drm_crtc_state *state)
436 {
437 struct drm_crtc *crtc = state->crtc;
438
439 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
440 drm_printf(p, "\tenable=%d\n", state->enable);
441 drm_printf(p, "\tactive=%d\n", state->active);
442 drm_printf(p, "\tself_refresh_active=%d\n", state->self_refresh_active);
443 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
444 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
445 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
446 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
447 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
448 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
449 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
450 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
451 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
452
453 if (crtc->funcs->atomic_print_state)
454 crtc->funcs->atomic_print_state(p, state);
455 }
456
drm_atomic_connector_check(struct drm_connector * connector,struct drm_connector_state * state)457 static int drm_atomic_connector_check(struct drm_connector *connector,
458 struct drm_connector_state *state)
459 {
460 struct drm_crtc_state *crtc_state;
461 struct drm_writeback_job *writeback_job = state->writeback_job;
462 const struct drm_display_info *info = &connector->display_info;
463
464 state->max_bpc = info->bpc ? info->bpc : 8;
465 if (connector->max_bpc_property)
466 state->max_bpc = min(state->max_bpc, state->max_requested_bpc);
467
468 if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job)
469 return 0;
470
471 if (writeback_job->fb && !state->crtc) {
472 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] framebuffer without CRTC\n",
473 connector->base.id, connector->name);
474 return -EINVAL;
475 }
476
477 if (state->crtc)
478 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
479 state->crtc);
480
481 if (writeback_job->fb && !crtc_state->active) {
482 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n",
483 connector->base.id, connector->name,
484 state->crtc->base.id);
485 return -EINVAL;
486 }
487
488 if (!writeback_job->fb) {
489 if (writeback_job->out_fence) {
490 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n",
491 connector->base.id, connector->name);
492 return -EINVAL;
493 }
494
495 drm_writeback_cleanup_job(writeback_job);
496 state->writeback_job = NULL;
497 }
498
499 return 0;
500 }
501
502 /**
503 * drm_atomic_get_plane_state - get plane state
504 * @state: global atomic state object
505 * @plane: plane to get state object for
506 *
507 * This function returns the plane state for the given plane, allocating it if
508 * needed. It will also grab the relevant plane lock to make sure that the state
509 * is consistent.
510 *
511 * Returns:
512 *
513 * Either the allocated state or the error code encoded into the pointer. When
514 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
515 * entire atomic sequence must be restarted. All other errors are fatal.
516 */
517 struct drm_plane_state *
drm_atomic_get_plane_state(struct drm_atomic_state * state,struct drm_plane * plane)518 drm_atomic_get_plane_state(struct drm_atomic_state *state,
519 struct drm_plane *plane)
520 {
521 int ret, index = drm_plane_index(plane);
522 struct drm_plane_state *plane_state;
523
524 WARN_ON(!state->acquire_ctx);
525
526 /* the legacy pointers should never be set */
527 WARN_ON(plane->fb);
528 WARN_ON(plane->old_fb);
529 WARN_ON(plane->crtc);
530
531 plane_state = drm_atomic_get_existing_plane_state(state, plane);
532 if (plane_state)
533 return plane_state;
534
535 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
536 if (ret)
537 return ERR_PTR(ret);
538
539 plane_state = plane->funcs->atomic_duplicate_state(plane);
540 if (!plane_state)
541 return ERR_PTR(-ENOMEM);
542
543 state->planes[index].state = plane_state;
544 state->planes[index].ptr = plane;
545 state->planes[index].old_state = plane->state;
546 state->planes[index].new_state = plane_state;
547 plane_state->state = state;
548
549 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
550 plane->base.id, plane->name, plane_state, state);
551
552 if (plane_state->crtc) {
553 struct drm_crtc_state *crtc_state;
554
555 crtc_state = drm_atomic_get_crtc_state(state,
556 plane_state->crtc);
557 if (IS_ERR(crtc_state))
558 return ERR_CAST(crtc_state);
559 }
560
561 return plane_state;
562 }
563 EXPORT_SYMBOL(drm_atomic_get_plane_state);
564
565 static bool
plane_switching_crtc(const struct drm_plane_state * old_plane_state,const struct drm_plane_state * new_plane_state)566 plane_switching_crtc(const struct drm_plane_state *old_plane_state,
567 const struct drm_plane_state *new_plane_state)
568 {
569 if (!old_plane_state->crtc || !new_plane_state->crtc)
570 return false;
571
572 if (old_plane_state->crtc == new_plane_state->crtc)
573 return false;
574
575 /* This could be refined, but currently there's no helper or driver code
576 * to implement direct switching of active planes nor userspace to take
577 * advantage of more direct plane switching without the intermediate
578 * full OFF state.
579 */
580 return true;
581 }
582
583 /**
584 * drm_atomic_plane_check - check plane state
585 * @old_plane_state: old plane state to check
586 * @new_plane_state: new plane state to check
587 *
588 * Provides core sanity checks for plane state.
589 *
590 * RETURNS:
591 * Zero on success, error code on failure
592 */
drm_atomic_plane_check(const struct drm_plane_state * old_plane_state,const struct drm_plane_state * new_plane_state)593 static int drm_atomic_plane_check(const struct drm_plane_state *old_plane_state,
594 const struct drm_plane_state *new_plane_state)
595 {
596 struct drm_plane *plane = new_plane_state->plane;
597 struct drm_crtc *crtc = new_plane_state->crtc;
598 const struct drm_framebuffer *fb = new_plane_state->fb;
599 unsigned int fb_width, fb_height;
600 struct drm_mode_rect *clips;
601 uint32_t num_clips;
602 int ret;
603
604 /* either *both* CRTC and FB must be set, or neither */
605 if (crtc && !fb) {
606 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] CRTC set but no FB\n",
607 plane->base.id, plane->name);
608 return -EINVAL;
609 } else if (fb && !crtc) {
610 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] FB set but no CRTC\n",
611 plane->base.id, plane->name);
612 return -EINVAL;
613 }
614
615 /* if disabled, we don't care about the rest of the state: */
616 if (!crtc)
617 return 0;
618
619 /* Check whether this plane is usable on this CRTC */
620 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
621 DRM_DEBUG_ATOMIC("Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
622 crtc->base.id, crtc->name,
623 plane->base.id, plane->name);
624 return -EINVAL;
625 }
626
627 /* Check whether this plane supports the fb pixel format. */
628 ret = drm_plane_check_pixel_format(plane, fb->format->format,
629 fb->modifier);
630 if (ret) {
631 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid pixel format %p4cc, modifier 0x%llx\n",
632 plane->base.id, plane->name,
633 &fb->format->format, fb->modifier);
634 return ret;
635 }
636
637 /* Give drivers some help against integer overflows */
638 if (new_plane_state->crtc_w > INT_MAX ||
639 new_plane_state->crtc_x > INT_MAX - (int32_t) new_plane_state->crtc_w ||
640 new_plane_state->crtc_h > INT_MAX ||
641 new_plane_state->crtc_y > INT_MAX - (int32_t) new_plane_state->crtc_h) {
642 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
643 plane->base.id, plane->name,
644 new_plane_state->crtc_w, new_plane_state->crtc_h,
645 new_plane_state->crtc_x, new_plane_state->crtc_y);
646 return -ERANGE;
647 }
648
649 fb_width = fb->width << 16;
650 fb_height = fb->height << 16;
651
652 /* Make sure source coordinates are inside the fb. */
653 if (new_plane_state->src_w > fb_width ||
654 new_plane_state->src_x > fb_width - new_plane_state->src_w ||
655 new_plane_state->src_h > fb_height ||
656 new_plane_state->src_y > fb_height - new_plane_state->src_h) {
657 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid source coordinates "
658 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
659 plane->base.id, plane->name,
660 new_plane_state->src_w >> 16,
661 ((new_plane_state->src_w & 0xffff) * 15625) >> 10,
662 new_plane_state->src_h >> 16,
663 ((new_plane_state->src_h & 0xffff) * 15625) >> 10,
664 new_plane_state->src_x >> 16,
665 ((new_plane_state->src_x & 0xffff) * 15625) >> 10,
666 new_plane_state->src_y >> 16,
667 ((new_plane_state->src_y & 0xffff) * 15625) >> 10,
668 fb->width, fb->height);
669 return -ENOSPC;
670 }
671
672 clips = __drm_plane_get_damage_clips(new_plane_state);
673 num_clips = drm_plane_get_damage_clips_count(new_plane_state);
674
675 /* Make sure damage clips are valid and inside the fb. */
676 while (num_clips > 0) {
677 if (clips->x1 >= clips->x2 ||
678 clips->y1 >= clips->y2 ||
679 clips->x1 < 0 ||
680 clips->y1 < 0 ||
681 clips->x2 > fb_width ||
682 clips->y2 > fb_height) {
683 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid damage clip %d %d %d %d\n",
684 plane->base.id, plane->name, clips->x1,
685 clips->y1, clips->x2, clips->y2);
686 return -EINVAL;
687 }
688 clips++;
689 num_clips--;
690 }
691
692 if (plane_switching_crtc(old_plane_state, new_plane_state)) {
693 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
694 plane->base.id, plane->name);
695 return -EINVAL;
696 }
697
698 return 0;
699 }
700
drm_atomic_plane_print_state(struct drm_printer * p,const struct drm_plane_state * state)701 static void drm_atomic_plane_print_state(struct drm_printer *p,
702 const struct drm_plane_state *state)
703 {
704 struct drm_plane *plane = state->plane;
705 struct drm_rect src = drm_plane_state_src(state);
706 struct drm_rect dest = drm_plane_state_dest(state);
707
708 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
709 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
710 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
711 if (state->fb)
712 drm_framebuffer_print_info(p, 2, state->fb);
713 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
714 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
715 drm_printf(p, "\trotation=%x\n", state->rotation);
716 drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos);
717 drm_printf(p, "\tcolor-encoding=%s\n",
718 drm_get_color_encoding_name(state->color_encoding));
719 drm_printf(p, "\tcolor-range=%s\n",
720 drm_get_color_range_name(state->color_range));
721
722 if (plane->funcs->atomic_print_state)
723 plane->funcs->atomic_print_state(p, state);
724 }
725
726 /**
727 * DOC: handling driver private state
728 *
729 * Very often the DRM objects exposed to userspace in the atomic modeset api
730 * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the
731 * underlying hardware. Especially for any kind of shared resources (e.g. shared
732 * clocks, scaler units, bandwidth and fifo limits shared among a group of
733 * planes or CRTCs, and so on) it makes sense to model these as independent
734 * objects. Drivers then need to do similar state tracking and commit ordering for
735 * such private (since not exposed to userspace) objects as the atomic core and
736 * helpers already provide for connectors, planes and CRTCs.
737 *
738 * To make this easier on drivers the atomic core provides some support to track
739 * driver private state objects using struct &drm_private_obj, with the
740 * associated state struct &drm_private_state.
741 *
742 * Similar to userspace-exposed objects, private state structures can be
743 * acquired by calling drm_atomic_get_private_obj_state(). This also takes care
744 * of locking, hence drivers should not have a need to call drm_modeset_lock()
745 * directly. Sequence of the actual hardware state commit is not handled,
746 * drivers might need to keep track of struct drm_crtc_commit within subclassed
747 * structure of &drm_private_state as necessary, e.g. similar to
748 * &drm_plane_state.commit. See also &drm_atomic_state.fake_commit.
749 *
750 * All private state structures contained in a &drm_atomic_state update can be
751 * iterated using for_each_oldnew_private_obj_in_state(),
752 * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state().
753 * Drivers are recommended to wrap these for each type of driver private state
754 * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at
755 * least if they want to iterate over all objects of a given type.
756 *
757 * An earlier way to handle driver private state was by subclassing struct
758 * &drm_atomic_state. But since that encourages non-standard ways to implement
759 * the check/commit split atomic requires (by using e.g. "check and rollback or
760 * commit instead" of "duplicate state, check, then either commit or release
761 * duplicated state) it is deprecated in favour of using &drm_private_state.
762 */
763
764 /**
765 * drm_atomic_private_obj_init - initialize private object
766 * @dev: DRM device this object will be attached to
767 * @obj: private object
768 * @state: initial private object state
769 * @funcs: pointer to the struct of function pointers that identify the object
770 * type
771 *
772 * Initialize the private object, which can be embedded into any
773 * driver private object that needs its own atomic state.
774 */
775 void
drm_atomic_private_obj_init(struct drm_device * dev,struct drm_private_obj * obj,struct drm_private_state * state,const struct drm_private_state_funcs * funcs)776 drm_atomic_private_obj_init(struct drm_device *dev,
777 struct drm_private_obj *obj,
778 struct drm_private_state *state,
779 const struct drm_private_state_funcs *funcs)
780 {
781 memset(obj, 0, sizeof(*obj));
782
783 drm_modeset_lock_init(&obj->lock);
784
785 obj->state = state;
786 obj->funcs = funcs;
787 list_add_tail(&obj->head, &dev->mode_config.privobj_list);
788 }
789 EXPORT_SYMBOL(drm_atomic_private_obj_init);
790
791 /**
792 * drm_atomic_private_obj_fini - finalize private object
793 * @obj: private object
794 *
795 * Finalize the private object.
796 */
797 void
drm_atomic_private_obj_fini(struct drm_private_obj * obj)798 drm_atomic_private_obj_fini(struct drm_private_obj *obj)
799 {
800 list_del(&obj->head);
801 obj->funcs->atomic_destroy_state(obj, obj->state);
802 drm_modeset_lock_fini(&obj->lock);
803 }
804 EXPORT_SYMBOL(drm_atomic_private_obj_fini);
805
806 /**
807 * drm_atomic_get_private_obj_state - get private object state
808 * @state: global atomic state
809 * @obj: private object to get the state for
810 *
811 * This function returns the private object state for the given private object,
812 * allocating the state if needed. It will also grab the relevant private
813 * object lock to make sure that the state is consistent.
814 *
815 * RETURNS:
816 *
817 * Either the allocated state or the error code encoded into a pointer.
818 */
819 struct drm_private_state *
drm_atomic_get_private_obj_state(struct drm_atomic_state * state,struct drm_private_obj * obj)820 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
821 struct drm_private_obj *obj)
822 {
823 int index, num_objs, i, ret;
824 size_t size;
825 struct __drm_private_objs_state *arr;
826 struct drm_private_state *obj_state;
827
828 for (i = 0; i < state->num_private_objs; i++)
829 if (obj == state->private_objs[i].ptr)
830 return state->private_objs[i].state;
831
832 ret = drm_modeset_lock(&obj->lock, state->acquire_ctx);
833 if (ret)
834 return ERR_PTR(ret);
835
836 num_objs = state->num_private_objs + 1;
837 size = sizeof(*state->private_objs) * num_objs;
838 arr = krealloc(state->private_objs, size, GFP_KERNEL);
839 if (!arr)
840 return ERR_PTR(-ENOMEM);
841
842 state->private_objs = arr;
843 index = state->num_private_objs;
844 memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
845
846 obj_state = obj->funcs->atomic_duplicate_state(obj);
847 if (!obj_state)
848 return ERR_PTR(-ENOMEM);
849
850 state->private_objs[index].state = obj_state;
851 state->private_objs[index].old_state = obj->state;
852 state->private_objs[index].new_state = obj_state;
853 state->private_objs[index].ptr = obj;
854 obj_state->state = state;
855
856 state->num_private_objs = num_objs;
857
858 DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
859 obj, obj_state, state);
860
861 return obj_state;
862 }
863 EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
864
865 /**
866 * drm_atomic_get_old_private_obj_state
867 * @state: global atomic state object
868 * @obj: private_obj to grab
869 *
870 * This function returns the old private object state for the given private_obj,
871 * or NULL if the private_obj is not part of the global atomic state.
872 */
873 struct drm_private_state *
drm_atomic_get_old_private_obj_state(struct drm_atomic_state * state,struct drm_private_obj * obj)874 drm_atomic_get_old_private_obj_state(struct drm_atomic_state *state,
875 struct drm_private_obj *obj)
876 {
877 int i;
878
879 for (i = 0; i < state->num_private_objs; i++)
880 if (obj == state->private_objs[i].ptr)
881 return state->private_objs[i].old_state;
882
883 return NULL;
884 }
885 EXPORT_SYMBOL(drm_atomic_get_old_private_obj_state);
886
887 /**
888 * drm_atomic_get_new_private_obj_state
889 * @state: global atomic state object
890 * @obj: private_obj to grab
891 *
892 * This function returns the new private object state for the given private_obj,
893 * or NULL if the private_obj is not part of the global atomic state.
894 */
895 struct drm_private_state *
drm_atomic_get_new_private_obj_state(struct drm_atomic_state * state,struct drm_private_obj * obj)896 drm_atomic_get_new_private_obj_state(struct drm_atomic_state *state,
897 struct drm_private_obj *obj)
898 {
899 int i;
900
901 for (i = 0; i < state->num_private_objs; i++)
902 if (obj == state->private_objs[i].ptr)
903 return state->private_objs[i].new_state;
904
905 return NULL;
906 }
907 EXPORT_SYMBOL(drm_atomic_get_new_private_obj_state);
908
909 /**
910 * drm_atomic_get_old_connector_for_encoder - Get old connector for an encoder
911 * @state: Atomic state
912 * @encoder: The encoder to fetch the connector state for
913 *
914 * This function finds and returns the connector that was connected to @encoder
915 * as specified by the @state.
916 *
917 * If there is no connector in @state which previously had @encoder connected to
918 * it, this function will return NULL. While this may seem like an invalid use
919 * case, it is sometimes useful to differentiate commits which had no prior
920 * connectors attached to @encoder vs ones that did (and to inspect their
921 * state). This is especially true in enable hooks because the pipeline has
922 * changed.
923 *
924 * Returns: The old connector connected to @encoder, or NULL if the encoder is
925 * not connected.
926 */
927 struct drm_connector *
drm_atomic_get_old_connector_for_encoder(struct drm_atomic_state * state,struct drm_encoder * encoder)928 drm_atomic_get_old_connector_for_encoder(struct drm_atomic_state *state,
929 struct drm_encoder *encoder)
930 {
931 struct drm_connector_state *conn_state;
932 struct drm_connector *connector;
933 unsigned int i;
934
935 for_each_old_connector_in_state(state, connector, conn_state, i) {
936 if (conn_state->best_encoder == encoder)
937 return connector;
938 }
939
940 return NULL;
941 }
942 EXPORT_SYMBOL(drm_atomic_get_old_connector_for_encoder);
943
944 /**
945 * drm_atomic_get_new_connector_for_encoder - Get new connector for an encoder
946 * @state: Atomic state
947 * @encoder: The encoder to fetch the connector state for
948 *
949 * This function finds and returns the connector that will be connected to
950 * @encoder as specified by the @state.
951 *
952 * If there is no connector in @state which will have @encoder connected to it,
953 * this function will return NULL. While this may seem like an invalid use case,
954 * it is sometimes useful to differentiate commits which have no connectors
955 * attached to @encoder vs ones that do (and to inspect their state). This is
956 * especially true in disable hooks because the pipeline will change.
957 *
958 * Returns: The new connector connected to @encoder, or NULL if the encoder is
959 * not connected.
960 */
961 struct drm_connector *
drm_atomic_get_new_connector_for_encoder(struct drm_atomic_state * state,struct drm_encoder * encoder)962 drm_atomic_get_new_connector_for_encoder(struct drm_atomic_state *state,
963 struct drm_encoder *encoder)
964 {
965 struct drm_connector_state *conn_state;
966 struct drm_connector *connector;
967 unsigned int i;
968
969 for_each_new_connector_in_state(state, connector, conn_state, i) {
970 if (conn_state->best_encoder == encoder)
971 return connector;
972 }
973
974 return NULL;
975 }
976 EXPORT_SYMBOL(drm_atomic_get_new_connector_for_encoder);
977
978 /**
979 * drm_atomic_get_connector_state - get connector state
980 * @state: global atomic state object
981 * @connector: connector to get state object for
982 *
983 * This function returns the connector state for the given connector,
984 * allocating it if needed. It will also grab the relevant connector lock to
985 * make sure that the state is consistent.
986 *
987 * Returns:
988 *
989 * Either the allocated state or the error code encoded into the pointer. When
990 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
991 * entire atomic sequence must be restarted. All other errors are fatal.
992 */
993 struct drm_connector_state *
drm_atomic_get_connector_state(struct drm_atomic_state * state,struct drm_connector * connector)994 drm_atomic_get_connector_state(struct drm_atomic_state *state,
995 struct drm_connector *connector)
996 {
997 int ret, index;
998 struct drm_mode_config *config = &connector->dev->mode_config;
999 struct drm_connector_state *connector_state;
1000
1001 WARN_ON(!state->acquire_ctx);
1002
1003 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1004 if (ret)
1005 return ERR_PTR(ret);
1006
1007 index = drm_connector_index(connector);
1008
1009 if (index >= state->num_connector) {
1010 struct __drm_connnectors_state *c;
1011 int alloc = max(index + 1, config->num_connector);
1012
1013 c = krealloc_array(state->connectors, alloc,
1014 sizeof(*state->connectors), GFP_KERNEL);
1015 if (!c)
1016 return ERR_PTR(-ENOMEM);
1017
1018 state->connectors = c;
1019 memset(&state->connectors[state->num_connector], 0,
1020 sizeof(*state->connectors) * (alloc - state->num_connector));
1021
1022 state->num_connector = alloc;
1023 }
1024
1025 if (state->connectors[index].state)
1026 return state->connectors[index].state;
1027
1028 connector_state = connector->funcs->atomic_duplicate_state(connector);
1029 if (!connector_state)
1030 return ERR_PTR(-ENOMEM);
1031
1032 drm_connector_get(connector);
1033 state->connectors[index].state = connector_state;
1034 state->connectors[index].old_state = connector->state;
1035 state->connectors[index].new_state = connector_state;
1036 state->connectors[index].ptr = connector;
1037 connector_state->state = state;
1038
1039 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1040 connector->base.id, connector->name,
1041 connector_state, state);
1042
1043 if (connector_state->crtc) {
1044 struct drm_crtc_state *crtc_state;
1045
1046 crtc_state = drm_atomic_get_crtc_state(state,
1047 connector_state->crtc);
1048 if (IS_ERR(crtc_state))
1049 return ERR_CAST(crtc_state);
1050 }
1051
1052 return connector_state;
1053 }
1054 EXPORT_SYMBOL(drm_atomic_get_connector_state);
1055
drm_atomic_connector_print_state(struct drm_printer * p,const struct drm_connector_state * state)1056 static void drm_atomic_connector_print_state(struct drm_printer *p,
1057 const struct drm_connector_state *state)
1058 {
1059 struct drm_connector *connector = state->connector;
1060
1061 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1062 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1063 drm_printf(p, "\tself_refresh_aware=%d\n", state->self_refresh_aware);
1064 drm_printf(p, "\tmax_requested_bpc=%d\n", state->max_requested_bpc);
1065
1066 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1067 if (state->writeback_job && state->writeback_job->fb)
1068 drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id);
1069
1070 if (connector->funcs->atomic_print_state)
1071 connector->funcs->atomic_print_state(p, state);
1072 }
1073
1074 /**
1075 * drm_atomic_get_bridge_state - get bridge state
1076 * @state: global atomic state object
1077 * @bridge: bridge to get state object for
1078 *
1079 * This function returns the bridge state for the given bridge, allocating it
1080 * if needed. It will also grab the relevant bridge lock to make sure that the
1081 * state is consistent.
1082 *
1083 * Returns:
1084 *
1085 * Either the allocated state or the error code encoded into the pointer. When
1086 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1087 * entire atomic sequence must be restarted.
1088 */
1089 struct drm_bridge_state *
drm_atomic_get_bridge_state(struct drm_atomic_state * state,struct drm_bridge * bridge)1090 drm_atomic_get_bridge_state(struct drm_atomic_state *state,
1091 struct drm_bridge *bridge)
1092 {
1093 struct drm_private_state *obj_state;
1094
1095 obj_state = drm_atomic_get_private_obj_state(state, &bridge->base);
1096 if (IS_ERR(obj_state))
1097 return ERR_CAST(obj_state);
1098
1099 return drm_priv_to_bridge_state(obj_state);
1100 }
1101 EXPORT_SYMBOL(drm_atomic_get_bridge_state);
1102
1103 /**
1104 * drm_atomic_get_old_bridge_state - get old bridge state, if it exists
1105 * @state: global atomic state object
1106 * @bridge: bridge to grab
1107 *
1108 * This function returns the old bridge state for the given bridge, or NULL if
1109 * the bridge is not part of the global atomic state.
1110 */
1111 struct drm_bridge_state *
drm_atomic_get_old_bridge_state(struct drm_atomic_state * state,struct drm_bridge * bridge)1112 drm_atomic_get_old_bridge_state(struct drm_atomic_state *state,
1113 struct drm_bridge *bridge)
1114 {
1115 struct drm_private_state *obj_state;
1116
1117 obj_state = drm_atomic_get_old_private_obj_state(state, &bridge->base);
1118 if (!obj_state)
1119 return NULL;
1120
1121 return drm_priv_to_bridge_state(obj_state);
1122 }
1123 EXPORT_SYMBOL(drm_atomic_get_old_bridge_state);
1124
1125 /**
1126 * drm_atomic_get_new_bridge_state - get new bridge state, if it exists
1127 * @state: global atomic state object
1128 * @bridge: bridge to grab
1129 *
1130 * This function returns the new bridge state for the given bridge, or NULL if
1131 * the bridge is not part of the global atomic state.
1132 */
1133 struct drm_bridge_state *
drm_atomic_get_new_bridge_state(struct drm_atomic_state * state,struct drm_bridge * bridge)1134 drm_atomic_get_new_bridge_state(struct drm_atomic_state *state,
1135 struct drm_bridge *bridge)
1136 {
1137 struct drm_private_state *obj_state;
1138
1139 obj_state = drm_atomic_get_new_private_obj_state(state, &bridge->base);
1140 if (!obj_state)
1141 return NULL;
1142
1143 return drm_priv_to_bridge_state(obj_state);
1144 }
1145 EXPORT_SYMBOL(drm_atomic_get_new_bridge_state);
1146
1147 /**
1148 * drm_atomic_add_encoder_bridges - add bridges attached to an encoder
1149 * @state: atomic state
1150 * @encoder: DRM encoder
1151 *
1152 * This function adds all bridges attached to @encoder. This is needed to add
1153 * bridge states to @state and make them available when
1154 * &drm_bridge_funcs.atomic_check(), &drm_bridge_funcs.atomic_pre_enable(),
1155 * &drm_bridge_funcs.atomic_enable(),
1156 * &drm_bridge_funcs.atomic_disable_post_disable() are called.
1157 *
1158 * Returns:
1159 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1160 * then the w/w mutex code has detected a deadlock and the entire atomic
1161 * sequence must be restarted. All other errors are fatal.
1162 */
1163 int
drm_atomic_add_encoder_bridges(struct drm_atomic_state * state,struct drm_encoder * encoder)1164 drm_atomic_add_encoder_bridges(struct drm_atomic_state *state,
1165 struct drm_encoder *encoder)
1166 {
1167 struct drm_bridge_state *bridge_state;
1168 struct drm_bridge *bridge;
1169
1170 if (!encoder)
1171 return 0;
1172
1173 DRM_DEBUG_ATOMIC("Adding all bridges for [encoder:%d:%s] to %p\n",
1174 encoder->base.id, encoder->name, state);
1175
1176 drm_for_each_bridge_in_chain(encoder, bridge) {
1177 /* Skip bridges that don't implement the atomic state hooks. */
1178 if (!bridge->funcs->atomic_duplicate_state)
1179 continue;
1180
1181 bridge_state = drm_atomic_get_bridge_state(state, bridge);
1182 if (IS_ERR(bridge_state))
1183 return PTR_ERR(bridge_state);
1184 }
1185
1186 return 0;
1187 }
1188 EXPORT_SYMBOL(drm_atomic_add_encoder_bridges);
1189
1190 /**
1191 * drm_atomic_add_affected_connectors - add connectors for CRTC
1192 * @state: atomic state
1193 * @crtc: DRM CRTC
1194 *
1195 * This function walks the current configuration and adds all connectors
1196 * currently using @crtc to the atomic configuration @state. Note that this
1197 * function must acquire the connection mutex. This can potentially cause
1198 * unneeded serialization if the update is just for the planes on one CRTC. Hence
1199 * drivers and helpers should only call this when really needed (e.g. when a
1200 * full modeset needs to happen due to some change).
1201 *
1202 * Returns:
1203 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1204 * then the w/w mutex code has detected a deadlock and the entire atomic
1205 * sequence must be restarted. All other errors are fatal.
1206 */
1207 int
drm_atomic_add_affected_connectors(struct drm_atomic_state * state,struct drm_crtc * crtc)1208 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1209 struct drm_crtc *crtc)
1210 {
1211 struct drm_mode_config *config = &state->dev->mode_config;
1212 struct drm_connector *connector;
1213 struct drm_connector_state *conn_state;
1214 struct drm_connector_list_iter conn_iter;
1215 struct drm_crtc_state *crtc_state;
1216 int ret;
1217
1218 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1219 if (IS_ERR(crtc_state))
1220 return PTR_ERR(crtc_state);
1221
1222 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1223 if (ret)
1224 return ret;
1225
1226 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1227 crtc->base.id, crtc->name, state);
1228
1229 /*
1230 * Changed connectors are already in @state, so only need to look
1231 * at the connector_mask in crtc_state.
1232 */
1233 drm_connector_list_iter_begin(state->dev, &conn_iter);
1234 drm_for_each_connector_iter(connector, &conn_iter) {
1235 if (!(crtc_state->connector_mask & drm_connector_mask(connector)))
1236 continue;
1237
1238 conn_state = drm_atomic_get_connector_state(state, connector);
1239 if (IS_ERR(conn_state)) {
1240 drm_connector_list_iter_end(&conn_iter);
1241 return PTR_ERR(conn_state);
1242 }
1243 }
1244 drm_connector_list_iter_end(&conn_iter);
1245
1246 return 0;
1247 }
1248 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1249
1250 /**
1251 * drm_atomic_add_affected_planes - add planes for CRTC
1252 * @state: atomic state
1253 * @crtc: DRM CRTC
1254 *
1255 * This function walks the current configuration and adds all planes
1256 * currently used by @crtc to the atomic configuration @state. This is useful
1257 * when an atomic commit also needs to check all currently enabled plane on
1258 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1259 * to avoid special code to force-enable all planes.
1260 *
1261 * Since acquiring a plane state will always also acquire the w/w mutex of the
1262 * current CRTC for that plane (if there is any) adding all the plane states for
1263 * a CRTC will not reduce parallelism of atomic updates.
1264 *
1265 * Returns:
1266 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1267 * then the w/w mutex code has detected a deadlock and the entire atomic
1268 * sequence must be restarted. All other errors are fatal.
1269 */
1270 int
drm_atomic_add_affected_planes(struct drm_atomic_state * state,struct drm_crtc * crtc)1271 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1272 struct drm_crtc *crtc)
1273 {
1274 const struct drm_crtc_state *old_crtc_state =
1275 drm_atomic_get_old_crtc_state(state, crtc);
1276 struct drm_plane *plane;
1277
1278 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
1279
1280 DRM_DEBUG_ATOMIC("Adding all current planes for [CRTC:%d:%s] to %p\n",
1281 crtc->base.id, crtc->name, state);
1282
1283 drm_for_each_plane_mask(plane, state->dev, old_crtc_state->plane_mask) {
1284 struct drm_plane_state *plane_state =
1285 drm_atomic_get_plane_state(state, plane);
1286
1287 if (IS_ERR(plane_state))
1288 return PTR_ERR(plane_state);
1289 }
1290 return 0;
1291 }
1292 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1293
1294 /**
1295 * drm_atomic_check_only - check whether a given config would work
1296 * @state: atomic configuration to check
1297 *
1298 * Note that this function can return -EDEADLK if the driver needed to acquire
1299 * more locks but encountered a deadlock. The caller must then do the usual w/w
1300 * backoff dance and restart. All other errors are fatal.
1301 *
1302 * Returns:
1303 * 0 on success, negative error code on failure.
1304 */
drm_atomic_check_only(struct drm_atomic_state * state)1305 int drm_atomic_check_only(struct drm_atomic_state *state)
1306 {
1307 struct drm_device *dev = state->dev;
1308 struct drm_mode_config *config = &dev->mode_config;
1309 struct drm_plane *plane;
1310 struct drm_plane_state *old_plane_state;
1311 struct drm_plane_state *new_plane_state;
1312 struct drm_crtc *crtc;
1313 struct drm_crtc_state *old_crtc_state;
1314 struct drm_crtc_state *new_crtc_state;
1315 struct drm_connector *conn;
1316 struct drm_connector_state *conn_state;
1317 unsigned int requested_crtc = 0;
1318 unsigned int affected_crtc = 0;
1319 int i, ret = 0;
1320
1321 DRM_DEBUG_ATOMIC("checking %p\n", state);
1322
1323 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1324 if (new_crtc_state->enable)
1325 requested_crtc |= drm_crtc_mask(crtc);
1326 }
1327
1328 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
1329 ret = drm_atomic_plane_check(old_plane_state, new_plane_state);
1330 if (ret) {
1331 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1332 plane->base.id, plane->name);
1333 return ret;
1334 }
1335 }
1336
1337 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1338 ret = drm_atomic_crtc_check(old_crtc_state, new_crtc_state);
1339 if (ret) {
1340 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1341 crtc->base.id, crtc->name);
1342 return ret;
1343 }
1344 }
1345
1346 for_each_new_connector_in_state(state, conn, conn_state, i) {
1347 ret = drm_atomic_connector_check(conn, conn_state);
1348 if (ret) {
1349 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] atomic core check failed\n",
1350 conn->base.id, conn->name);
1351 return ret;
1352 }
1353 }
1354
1355 if (config->funcs->atomic_check) {
1356 ret = config->funcs->atomic_check(state->dev, state);
1357
1358 if (ret) {
1359 DRM_DEBUG_ATOMIC("atomic driver check for %p failed: %d\n",
1360 state, ret);
1361 return ret;
1362 }
1363 }
1364
1365 if (!state->allow_modeset) {
1366 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1367 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
1368 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1369 crtc->base.id, crtc->name);
1370 return -EINVAL;
1371 }
1372 }
1373 }
1374
1375 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1376 if (new_crtc_state->enable)
1377 affected_crtc |= drm_crtc_mask(crtc);
1378 }
1379
1380 /*
1381 * For commits that allow modesets drivers can add other CRTCs to the
1382 * atomic commit, e.g. when they need to reallocate global resources.
1383 * This can cause spurious EBUSY, which robs compositors of a very
1384 * effective sanity check for their drawing loop. Therefor only allow
1385 * drivers to add unrelated CRTC states for modeset commits.
1386 *
1387 * FIXME: Should add affected_crtc mask to the ATOMIC IOCTL as an output
1388 * so compositors know what's going on.
1389 */
1390 if (affected_crtc != requested_crtc) {
1391 DRM_DEBUG_ATOMIC("driver added CRTC to commit: requested 0x%x, affected 0x%0x\n",
1392 requested_crtc, affected_crtc);
1393 WARN(!state->allow_modeset, "adding CRTC not allowed without modesets: requested 0x%x, affected 0x%0x\n",
1394 requested_crtc, affected_crtc);
1395 }
1396
1397 return 0;
1398 }
1399 EXPORT_SYMBOL(drm_atomic_check_only);
1400
1401 /**
1402 * drm_atomic_commit - commit configuration atomically
1403 * @state: atomic configuration to check
1404 *
1405 * Note that this function can return -EDEADLK if the driver needed to acquire
1406 * more locks but encountered a deadlock. The caller must then do the usual w/w
1407 * backoff dance and restart. All other errors are fatal.
1408 *
1409 * This function will take its own reference on @state.
1410 * Callers should always release their reference with drm_atomic_state_put().
1411 *
1412 * Returns:
1413 * 0 on success, negative error code on failure.
1414 */
drm_atomic_commit(struct drm_atomic_state * state)1415 int drm_atomic_commit(struct drm_atomic_state *state)
1416 {
1417 struct drm_mode_config *config = &state->dev->mode_config;
1418 int ret;
1419
1420 ret = drm_atomic_check_only(state);
1421 if (ret)
1422 return ret;
1423
1424 DRM_DEBUG_ATOMIC("committing %p\n", state);
1425
1426 return config->funcs->atomic_commit(state->dev, state, false);
1427 }
1428 EXPORT_SYMBOL(drm_atomic_commit);
1429
1430 /**
1431 * drm_atomic_nonblocking_commit - atomic nonblocking commit
1432 * @state: atomic configuration to check
1433 *
1434 * Note that this function can return -EDEADLK if the driver needed to acquire
1435 * more locks but encountered a deadlock. The caller must then do the usual w/w
1436 * backoff dance and restart. All other errors are fatal.
1437 *
1438 * This function will take its own reference on @state.
1439 * Callers should always release their reference with drm_atomic_state_put().
1440 *
1441 * Returns:
1442 * 0 on success, negative error code on failure.
1443 */
drm_atomic_nonblocking_commit(struct drm_atomic_state * state)1444 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1445 {
1446 struct drm_mode_config *config = &state->dev->mode_config;
1447 int ret;
1448
1449 ret = drm_atomic_check_only(state);
1450 if (ret)
1451 return ret;
1452
1453 DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
1454
1455 return config->funcs->atomic_commit(state->dev, state, true);
1456 }
1457 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1458
1459 /* just used from drm-client and atomic-helper: */
__drm_atomic_helper_disable_plane(struct drm_plane * plane,struct drm_plane_state * plane_state)1460 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1461 struct drm_plane_state *plane_state)
1462 {
1463 int ret;
1464
1465 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1466 if (ret != 0)
1467 return ret;
1468
1469 drm_atomic_set_fb_for_plane(plane_state, NULL);
1470 plane_state->crtc_x = 0;
1471 plane_state->crtc_y = 0;
1472 plane_state->crtc_w = 0;
1473 plane_state->crtc_h = 0;
1474 plane_state->src_x = 0;
1475 plane_state->src_y = 0;
1476 plane_state->src_w = 0;
1477 plane_state->src_h = 0;
1478
1479 return 0;
1480 }
1481 EXPORT_SYMBOL(__drm_atomic_helper_disable_plane);
1482
update_output_state(struct drm_atomic_state * state,struct drm_mode_set * set)1483 static int update_output_state(struct drm_atomic_state *state,
1484 struct drm_mode_set *set)
1485 {
1486 struct drm_device *dev = set->crtc->dev;
1487 struct drm_crtc *crtc;
1488 struct drm_crtc_state *new_crtc_state;
1489 struct drm_connector *connector;
1490 struct drm_connector_state *new_conn_state;
1491 int ret, i;
1492
1493 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1494 state->acquire_ctx);
1495 if (ret)
1496 return ret;
1497
1498 /* First disable all connectors on the target crtc. */
1499 ret = drm_atomic_add_affected_connectors(state, set->crtc);
1500 if (ret)
1501 return ret;
1502
1503 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
1504 if (new_conn_state->crtc == set->crtc) {
1505 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
1506 NULL);
1507 if (ret)
1508 return ret;
1509
1510 /* Make sure legacy setCrtc always re-trains */
1511 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
1512 }
1513 }
1514
1515 /* Then set all connectors from set->connectors on the target crtc */
1516 for (i = 0; i < set->num_connectors; i++) {
1517 new_conn_state = drm_atomic_get_connector_state(state,
1518 set->connectors[i]);
1519 if (IS_ERR(new_conn_state))
1520 return PTR_ERR(new_conn_state);
1521
1522 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
1523 set->crtc);
1524 if (ret)
1525 return ret;
1526 }
1527
1528 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1529 /*
1530 * Don't update ->enable for the CRTC in the set_config request,
1531 * since a mismatch would indicate a bug in the upper layers.
1532 * The actual modeset code later on will catch any
1533 * inconsistencies here.
1534 */
1535 if (crtc == set->crtc)
1536 continue;
1537
1538 if (!new_crtc_state->connector_mask) {
1539 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
1540 NULL);
1541 if (ret < 0)
1542 return ret;
1543
1544 new_crtc_state->active = false;
1545 }
1546 }
1547
1548 return 0;
1549 }
1550
1551 /* just used from drm-client and atomic-helper: */
__drm_atomic_helper_set_config(struct drm_mode_set * set,struct drm_atomic_state * state)1552 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1553 struct drm_atomic_state *state)
1554 {
1555 struct drm_crtc_state *crtc_state;
1556 struct drm_plane_state *primary_state;
1557 struct drm_crtc *crtc = set->crtc;
1558 int hdisplay, vdisplay;
1559 int ret;
1560
1561 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1562 if (IS_ERR(crtc_state))
1563 return PTR_ERR(crtc_state);
1564
1565 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1566 if (IS_ERR(primary_state))
1567 return PTR_ERR(primary_state);
1568
1569 if (!set->mode) {
1570 WARN_ON(set->fb);
1571 WARN_ON(set->num_connectors);
1572
1573 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1574 if (ret != 0)
1575 return ret;
1576
1577 crtc_state->active = false;
1578
1579 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1580 if (ret != 0)
1581 return ret;
1582
1583 drm_atomic_set_fb_for_plane(primary_state, NULL);
1584
1585 goto commit;
1586 }
1587
1588 WARN_ON(!set->fb);
1589 WARN_ON(!set->num_connectors);
1590
1591 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1592 if (ret != 0)
1593 return ret;
1594
1595 crtc_state->active = true;
1596
1597 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1598 if (ret != 0)
1599 return ret;
1600
1601 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1602
1603 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1604 primary_state->crtc_x = 0;
1605 primary_state->crtc_y = 0;
1606 primary_state->crtc_w = hdisplay;
1607 primary_state->crtc_h = vdisplay;
1608 primary_state->src_x = set->x << 16;
1609 primary_state->src_y = set->y << 16;
1610 if (drm_rotation_90_or_270(primary_state->rotation)) {
1611 primary_state->src_w = vdisplay << 16;
1612 primary_state->src_h = hdisplay << 16;
1613 } else {
1614 primary_state->src_w = hdisplay << 16;
1615 primary_state->src_h = vdisplay << 16;
1616 }
1617
1618 commit:
1619 ret = update_output_state(state, set);
1620 if (ret)
1621 return ret;
1622
1623 return 0;
1624 }
1625 EXPORT_SYMBOL(__drm_atomic_helper_set_config);
1626
1627 /**
1628 * drm_atomic_print_new_state - prints drm atomic state
1629 * @state: atomic configuration to check
1630 * @p: drm printer
1631 *
1632 * This functions prints the drm atomic state snapshot using the drm printer
1633 * which is passed to it. This snapshot can be used for debugging purposes.
1634 *
1635 * Note that this function looks into the new state objects and hence its not
1636 * safe to be used after the call to drm_atomic_helper_commit_hw_done().
1637 */
drm_atomic_print_new_state(const struct drm_atomic_state * state,struct drm_printer * p)1638 void drm_atomic_print_new_state(const struct drm_atomic_state *state,
1639 struct drm_printer *p)
1640 {
1641 struct drm_plane *plane;
1642 struct drm_plane_state *plane_state;
1643 struct drm_crtc *crtc;
1644 struct drm_crtc_state *crtc_state;
1645 struct drm_connector *connector;
1646 struct drm_connector_state *connector_state;
1647 int i;
1648
1649 if (!p) {
1650 DRM_ERROR("invalid drm printer\n");
1651 return;
1652 }
1653
1654 DRM_DEBUG_ATOMIC("checking %p\n", state);
1655
1656 for_each_new_plane_in_state(state, plane, plane_state, i)
1657 drm_atomic_plane_print_state(p, plane_state);
1658
1659 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
1660 drm_atomic_crtc_print_state(p, crtc_state);
1661
1662 for_each_new_connector_in_state(state, connector, connector_state, i)
1663 drm_atomic_connector_print_state(p, connector_state);
1664 }
1665 EXPORT_SYMBOL(drm_atomic_print_new_state);
1666
__drm_state_dump(struct drm_device * dev,struct drm_printer * p,bool take_locks)1667 static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1668 bool take_locks)
1669 {
1670 struct drm_mode_config *config = &dev->mode_config;
1671 struct drm_plane *plane;
1672 struct drm_crtc *crtc;
1673 struct drm_connector *connector;
1674 struct drm_connector_list_iter conn_iter;
1675
1676 if (!drm_drv_uses_atomic_modeset(dev))
1677 return;
1678
1679 list_for_each_entry(plane, &config->plane_list, head) {
1680 if (take_locks)
1681 drm_modeset_lock(&plane->mutex, NULL);
1682 drm_atomic_plane_print_state(p, plane->state);
1683 if (take_locks)
1684 drm_modeset_unlock(&plane->mutex);
1685 }
1686
1687 list_for_each_entry(crtc, &config->crtc_list, head) {
1688 if (take_locks)
1689 drm_modeset_lock(&crtc->mutex, NULL);
1690 drm_atomic_crtc_print_state(p, crtc->state);
1691 if (take_locks)
1692 drm_modeset_unlock(&crtc->mutex);
1693 }
1694
1695 drm_connector_list_iter_begin(dev, &conn_iter);
1696 if (take_locks)
1697 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1698 drm_for_each_connector_iter(connector, &conn_iter)
1699 drm_atomic_connector_print_state(p, connector->state);
1700 if (take_locks)
1701 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1702 drm_connector_list_iter_end(&conn_iter);
1703 }
1704
1705 /**
1706 * drm_state_dump - dump entire device atomic state
1707 * @dev: the drm device
1708 * @p: where to print the state to
1709 *
1710 * Just for debugging. Drivers might want an option to dump state
1711 * to dmesg in case of error irq's. (Hint, you probably want to
1712 * ratelimit this!)
1713 *
1714 * The caller must wrap this drm_modeset_lock_all_ctx() and
1715 * drm_modeset_drop_locks(). If this is called from error irq handler, it should
1716 * not be enabled by default - if you are debugging errors you might
1717 * not care that this is racey, but calling this without all modeset locks held
1718 * is inherently unsafe.
1719 */
drm_state_dump(struct drm_device * dev,struct drm_printer * p)1720 void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1721 {
1722 __drm_state_dump(dev, p, false);
1723 }
1724 EXPORT_SYMBOL(drm_state_dump);
1725
1726 #ifdef CONFIG_DEBUG_FS
drm_state_info(struct seq_file * m,void * data)1727 static int drm_state_info(struct seq_file *m, void *data)
1728 {
1729 struct drm_info_node *node = (struct drm_info_node *) m->private;
1730 struct drm_device *dev = node->minor->dev;
1731 struct drm_printer p = drm_seq_file_printer(m);
1732
1733 __drm_state_dump(dev, &p, true);
1734
1735 return 0;
1736 }
1737
1738 /* any use in debugfs files to dump individual planes/crtc/etc? */
1739 static const struct drm_info_list drm_atomic_debugfs_list[] = {
1740 {"state", drm_state_info, 0},
1741 };
1742
drm_atomic_debugfs_init(struct drm_minor * minor)1743 void drm_atomic_debugfs_init(struct drm_minor *minor)
1744 {
1745 drm_debugfs_create_files(drm_atomic_debugfs_list,
1746 ARRAY_SIZE(drm_atomic_debugfs_list),
1747 minor->debugfs_root, minor);
1748 }
1749 #endif
1750