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