1 // SPDX-License-Identifier: MIT
3 * Copyright 2018 Noralf Trønnes
4 * Copyright (c) 2006-2009 Red Hat Inc.
5 * Copyright (c) 2006-2008 Intel Corporation
6 * Jesse Barnes <jesse.barnes@intel.com>
7 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
14 #include <drm/drm_atomic.h>
15 #include <drm/drm_client.h>
16 #include <drm/drm_connector.h>
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_device.h>
19 #include <drm/drm_drv.h>
20 #include <drm/drm_encoder.h>
21 #include <drm/drm_print.h>
23 #include "drm_crtc_internal.h"
24 #include "drm_internal.h"
26 #define DRM_CLIENT_MAX_CLONED_CONNECTORS 8
28 struct drm_client_offset
{
32 int drm_client_modeset_create(struct drm_client_dev
*client
)
34 struct drm_device
*dev
= client
->dev
;
35 unsigned int num_crtc
= dev
->mode_config
.num_crtc
;
36 unsigned int max_connector_count
= 1;
37 struct drm_mode_set
*modeset
;
38 struct drm_crtc
*crtc
;
41 /* Add terminating zero entry to enable index less iteration */
42 client
->modesets
= kcalloc(num_crtc
+ 1, sizeof(*client
->modesets
), GFP_KERNEL
);
43 if (!client
->modesets
)
46 mutex_init(&client
->modeset_mutex
);
48 drm_for_each_crtc(crtc
, dev
)
49 client
->modesets
[i
++].crtc
= crtc
;
51 /* Cloning is only supported in the single crtc case. */
53 max_connector_count
= DRM_CLIENT_MAX_CLONED_CONNECTORS
;
55 for (modeset
= client
->modesets
; modeset
->crtc
; modeset
++) {
56 modeset
->connectors
= kcalloc(max_connector_count
,
57 sizeof(*modeset
->connectors
), GFP_KERNEL
);
58 if (!modeset
->connectors
)
65 drm_client_modeset_free(client
);
70 static void drm_client_modeset_release(struct drm_client_dev
*client
)
72 struct drm_mode_set
*modeset
;
75 drm_client_for_each_modeset(modeset
, client
) {
76 drm_mode_destroy(client
->dev
, modeset
->mode
);
80 for (i
= 0; i
< modeset
->num_connectors
; i
++) {
81 drm_connector_put(modeset
->connectors
[i
]);
82 modeset
->connectors
[i
] = NULL
;
84 modeset
->num_connectors
= 0;
88 void drm_client_modeset_free(struct drm_client_dev
*client
)
90 struct drm_mode_set
*modeset
;
92 mutex_lock(&client
->modeset_mutex
);
94 drm_client_modeset_release(client
);
96 drm_client_for_each_modeset(modeset
, client
)
97 kfree(modeset
->connectors
);
99 mutex_unlock(&client
->modeset_mutex
);
101 mutex_destroy(&client
->modeset_mutex
);
102 kfree(client
->modesets
);
105 static struct drm_mode_set
*
106 drm_client_find_modeset(struct drm_client_dev
*client
, struct drm_crtc
*crtc
)
108 struct drm_mode_set
*modeset
;
110 drm_client_for_each_modeset(modeset
, client
)
111 if (modeset
->crtc
== crtc
)
117 static struct drm_display_mode
*
118 drm_connector_get_tiled_mode(struct drm_connector
*connector
)
120 struct drm_display_mode
*mode
;
122 list_for_each_entry(mode
, &connector
->modes
, head
) {
123 if (mode
->hdisplay
== connector
->tile_h_size
&&
124 mode
->vdisplay
== connector
->tile_v_size
)
130 static struct drm_display_mode
*
131 drm_connector_fallback_non_tiled_mode(struct drm_connector
*connector
)
133 struct drm_display_mode
*mode
;
135 list_for_each_entry(mode
, &connector
->modes
, head
) {
136 if (mode
->hdisplay
== connector
->tile_h_size
&&
137 mode
->vdisplay
== connector
->tile_v_size
)
144 static struct drm_display_mode
*
145 drm_connector_has_preferred_mode(struct drm_connector
*connector
, int width
, int height
)
147 struct drm_display_mode
*mode
;
149 list_for_each_entry(mode
, &connector
->modes
, head
) {
150 if (mode
->hdisplay
> width
||
151 mode
->vdisplay
> height
)
153 if (mode
->type
& DRM_MODE_TYPE_PREFERRED
)
159 static struct drm_display_mode
*
160 drm_connector_pick_cmdline_mode(struct drm_connector
*connector
)
162 struct drm_cmdline_mode
*cmdline_mode
;
163 struct drm_display_mode
*mode
;
164 bool prefer_non_interlace
;
166 cmdline_mode
= &connector
->cmdline_mode
;
167 if (cmdline_mode
->specified
== false)
170 /* attempt to find a matching mode in the list of modes
171 * we have gotten so far, if not add a CVT mode that conforms
173 if (cmdline_mode
->rb
|| cmdline_mode
->margins
)
176 prefer_non_interlace
= !cmdline_mode
->interlace
;
178 list_for_each_entry(mode
, &connector
->modes
, head
) {
179 /* Check (optional) mode name first */
180 if (!strcmp(mode
->name
, cmdline_mode
->name
))
183 /* check width/height */
184 if (mode
->hdisplay
!= cmdline_mode
->xres
||
185 mode
->vdisplay
!= cmdline_mode
->yres
)
188 if (cmdline_mode
->refresh_specified
) {
189 if (drm_mode_vrefresh(mode
) != cmdline_mode
->refresh
)
193 if (cmdline_mode
->interlace
) {
194 if (!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
))
196 } else if (prefer_non_interlace
) {
197 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
203 if (prefer_non_interlace
) {
204 prefer_non_interlace
= false;
209 mode
= drm_mode_create_from_cmdline_mode(connector
->dev
, cmdline_mode
);
211 list_add(&mode
->head
, &connector
->modes
);
216 static bool drm_connector_enabled(struct drm_connector
*connector
, bool strict
)
220 if (connector
->display_info
.non_desktop
)
224 enable
= connector
->status
== connector_status_connected
;
226 enable
= connector
->status
!= connector_status_disconnected
;
231 static void drm_client_connectors_enabled(struct drm_connector
**connectors
,
232 unsigned int connector_count
,
235 bool any_enabled
= false;
236 struct drm_connector
*connector
;
239 for (i
= 0; i
< connector_count
; i
++) {
240 connector
= connectors
[i
];
241 enabled
[i
] = drm_connector_enabled(connector
, true);
242 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector
->base
.id
,
243 connector
->display_info
.non_desktop
? "non desktop" : enabled
[i
] ? "yes" : "no");
245 any_enabled
|= enabled
[i
];
251 for (i
= 0; i
< connector_count
; i
++)
252 enabled
[i
] = drm_connector_enabled(connectors
[i
], false);
255 static bool drm_client_target_cloned(struct drm_device
*dev
,
256 struct drm_connector
**connectors
,
257 unsigned int connector_count
,
258 struct drm_display_mode
**modes
,
259 struct drm_client_offset
*offsets
,
260 bool *enabled
, int width
, int height
)
263 bool can_clone
= false;
264 struct drm_display_mode
*dmt_mode
, *mode
;
266 /* only contemplate cloning in the single crtc case */
267 if (dev
->mode_config
.num_crtc
> 1)
271 for (i
= 0; i
< connector_count
; i
++) {
276 /* only contemplate cloning if more than one connector is enabled */
280 /* check the command line or if nothing common pick 1024x768 */
282 for (i
= 0; i
< connector_count
; i
++) {
285 modes
[i
] = drm_connector_pick_cmdline_mode(connectors
[i
]);
290 for (j
= 0; j
< i
; j
++) {
293 if (!drm_mode_match(modes
[j
], modes
[i
],
294 DRM_MODE_MATCH_TIMINGS
|
295 DRM_MODE_MATCH_CLOCK
|
296 DRM_MODE_MATCH_FLAGS
|
297 DRM_MODE_MATCH_3D_FLAGS
))
303 DRM_DEBUG_KMS("can clone using command line\n");
307 /* try and find a 1024x768 mode on each connector */
309 dmt_mode
= drm_mode_find_dmt(dev
, 1024, 768, 60, false);
311 for (i
= 0; i
< connector_count
; i
++) {
315 list_for_each_entry(mode
, &connectors
[i
]->modes
, head
) {
316 if (drm_mode_match(mode
, dmt_mode
,
317 DRM_MODE_MATCH_TIMINGS
|
318 DRM_MODE_MATCH_CLOCK
|
319 DRM_MODE_MATCH_FLAGS
|
320 DRM_MODE_MATCH_3D_FLAGS
))
328 DRM_DEBUG_KMS("can clone using 1024x768\n");
331 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
335 static int drm_client_get_tile_offsets(struct drm_connector
**connectors
,
336 unsigned int connector_count
,
337 struct drm_display_mode
**modes
,
338 struct drm_client_offset
*offsets
,
340 int h_idx
, int v_idx
)
342 struct drm_connector
*connector
;
344 int hoffset
= 0, voffset
= 0;
346 for (i
= 0; i
< connector_count
; i
++) {
347 connector
= connectors
[i
];
348 if (!connector
->has_tile
)
351 if (!modes
[i
] && (h_idx
|| v_idx
)) {
352 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i
,
356 if (connector
->tile_h_loc
< h_idx
)
357 hoffset
+= modes
[i
]->hdisplay
;
359 if (connector
->tile_v_loc
< v_idx
)
360 voffset
+= modes
[i
]->vdisplay
;
362 offsets
[idx
].x
= hoffset
;
363 offsets
[idx
].y
= voffset
;
364 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset
, voffset
, h_idx
, v_idx
);
368 static bool drm_client_target_preferred(struct drm_connector
**connectors
,
369 unsigned int connector_count
,
370 struct drm_display_mode
**modes
,
371 struct drm_client_offset
*offsets
,
372 bool *enabled
, int width
, int height
)
374 const u64 mask
= BIT_ULL(connector_count
) - 1;
375 struct drm_connector
*connector
;
376 u64 conn_configured
= 0;
378 int num_tiled_conns
= 0;
381 for (i
= 0; i
< connector_count
; i
++) {
382 if (connectors
[i
]->has_tile
&&
383 connectors
[i
]->status
== connector_status_connected
)
388 for (i
= 0; i
< connector_count
; i
++) {
389 connector
= connectors
[i
];
391 if (conn_configured
& BIT_ULL(i
))
394 if (enabled
[i
] == false) {
395 conn_configured
|= BIT_ULL(i
);
399 /* first pass over all the untiled connectors */
400 if (tile_pass
== 0 && connector
->has_tile
)
403 if (tile_pass
== 1) {
404 if (connector
->tile_h_loc
!= 0 ||
405 connector
->tile_v_loc
!= 0)
409 if (connector
->tile_h_loc
!= tile_pass
- 1 &&
410 connector
->tile_v_loc
!= tile_pass
- 1)
411 /* if this tile_pass doesn't cover any of the tiles - keep going */
415 * find the tile offsets for this pass - need to find
416 * all tiles left and above
418 drm_client_get_tile_offsets(connectors
, connector_count
, modes
, offsets
, i
,
419 connector
->tile_h_loc
, connector
->tile_v_loc
);
421 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
424 /* got for command line mode first */
425 modes
[i
] = drm_connector_pick_cmdline_mode(connector
);
427 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
428 connector
->base
.id
, connector
->tile_group
? connector
->tile_group
->id
: 0);
429 modes
[i
] = drm_connector_has_preferred_mode(connector
, width
, height
);
431 /* No preferred modes, pick one off the list */
432 if (!modes
[i
] && !list_empty(&connector
->modes
)) {
433 list_for_each_entry(modes
[i
], &connector
->modes
, head
)
437 * In case of tiled mode if all tiles not present fallback to
438 * first available non tiled mode.
439 * After all tiles are present, try to find the tiled mode
440 * for all and if tiled mode not present due to fbcon size
441 * limitations, use first non tiled mode only for
442 * tile 0,0 and set to no mode for all other tiles.
444 if (connector
->has_tile
) {
445 if (num_tiled_conns
<
446 connector
->num_h_tile
* connector
->num_v_tile
||
447 (connector
->tile_h_loc
== 0 &&
448 connector
->tile_v_loc
== 0 &&
449 !drm_connector_get_tiled_mode(connector
))) {
450 DRM_DEBUG_KMS("Falling back to non tiled mode on Connector %d\n",
452 modes
[i
] = drm_connector_fallback_non_tiled_mode(connector
);
454 modes
[i
] = drm_connector_get_tiled_mode(connector
);
458 DRM_DEBUG_KMS("found mode %s\n", modes
[i
] ? modes
[i
]->name
:
460 conn_configured
|= BIT_ULL(i
);
463 if ((conn_configured
& mask
) != mask
) {
470 static bool connector_has_possible_crtc(struct drm_connector
*connector
,
471 struct drm_crtc
*crtc
)
473 struct drm_encoder
*encoder
;
475 drm_connector_for_each_possible_encoder(connector
, encoder
) {
476 if (encoder
->possible_crtcs
& drm_crtc_mask(crtc
))
483 static int drm_client_pick_crtcs(struct drm_client_dev
*client
,
484 struct drm_connector
**connectors
,
485 unsigned int connector_count
,
486 struct drm_crtc
**best_crtcs
,
487 struct drm_display_mode
**modes
,
488 int n
, int width
, int height
)
490 struct drm_device
*dev
= client
->dev
;
491 struct drm_connector
*connector
;
492 int my_score
, best_score
, score
;
493 struct drm_crtc
**crtcs
, *crtc
;
494 struct drm_mode_set
*modeset
;
497 if (n
== connector_count
)
500 connector
= connectors
[n
];
502 best_crtcs
[n
] = NULL
;
503 best_score
= drm_client_pick_crtcs(client
, connectors
, connector_count
,
504 best_crtcs
, modes
, n
+ 1, width
, height
);
505 if (modes
[n
] == NULL
)
508 crtcs
= kcalloc(connector_count
, sizeof(*crtcs
), GFP_KERNEL
);
513 if (connector
->status
== connector_status_connected
)
515 if (connector
->cmdline_mode
.specified
)
517 if (drm_connector_has_preferred_mode(connector
, width
, height
))
521 * select a crtc for this connector and then attempt to configure
522 * remaining connectors
524 drm_client_for_each_modeset(modeset
, client
) {
525 crtc
= modeset
->crtc
;
527 if (!connector_has_possible_crtc(connector
, crtc
))
530 for (o
= 0; o
< n
; o
++)
531 if (best_crtcs
[o
] == crtc
)
535 /* ignore cloning unless only a single crtc */
536 if (dev
->mode_config
.num_crtc
> 1)
539 if (!drm_mode_equal(modes
[o
], modes
[n
]))
544 memcpy(crtcs
, best_crtcs
, n
* sizeof(*crtcs
));
545 score
= my_score
+ drm_client_pick_crtcs(client
, connectors
, connector_count
,
546 crtcs
, modes
, n
+ 1, width
, height
);
547 if (score
> best_score
) {
549 memcpy(best_crtcs
, crtcs
, connector_count
* sizeof(*crtcs
));
557 /* Try to read the BIOS display configuration and use it for the initial config */
558 static bool drm_client_firmware_config(struct drm_client_dev
*client
,
559 struct drm_connector
**connectors
,
560 unsigned int connector_count
,
561 struct drm_crtc
**crtcs
,
562 struct drm_display_mode
**modes
,
563 struct drm_client_offset
*offsets
,
564 bool *enabled
, int width
, int height
)
566 const int count
= min_t(unsigned int, connector_count
, BITS_PER_LONG
);
567 unsigned long conn_configured
, conn_seq
, mask
;
568 struct drm_device
*dev
= client
->dev
;
571 bool fallback
= true, ret
= true;
572 int num_connectors_enabled
= 0;
573 int num_connectors_detected
= 0;
574 int num_tiled_conns
= 0;
575 struct drm_modeset_acquire_ctx ctx
;
577 if (!drm_drv_uses_atomic_modeset(dev
))
580 if (WARN_ON(count
<= 0))
583 save_enabled
= kcalloc(count
, sizeof(bool), GFP_KERNEL
);
587 drm_modeset_acquire_init(&ctx
, 0);
589 while (drm_modeset_lock_all_ctx(dev
, &ctx
) != 0)
590 drm_modeset_backoff(&ctx
);
592 memcpy(save_enabled
, enabled
, count
);
593 mask
= GENMASK(count
- 1, 0);
595 for (i
= 0; i
< count
; i
++) {
596 if (connectors
[i
]->has_tile
&&
597 connectors
[i
]->status
== connector_status_connected
)
601 conn_seq
= conn_configured
;
602 for (i
= 0; i
< count
; i
++) {
603 struct drm_connector
*connector
;
604 struct drm_encoder
*encoder
;
605 struct drm_crtc
*new_crtc
;
607 connector
= connectors
[i
];
609 if (conn_configured
& BIT(i
))
612 if (conn_seq
== 0 && !connector
->has_tile
)
615 if (connector
->status
== connector_status_connected
)
616 num_connectors_detected
++;
619 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
621 conn_configured
|= BIT(i
);
625 if (connector
->force
== DRM_FORCE_OFF
) {
626 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
632 encoder
= connector
->state
->best_encoder
;
633 if (!encoder
|| WARN_ON(!connector
->state
->crtc
)) {
634 if (connector
->force
> DRM_FORCE_OFF
)
637 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
640 conn_configured
|= BIT(i
);
644 num_connectors_enabled
++;
646 new_crtc
= connector
->state
->crtc
;
649 * Make sure we're not trying to drive multiple connectors
650 * with a single CRTC, since our cloning support may not
653 for (j
= 0; j
< count
; j
++) {
654 if (crtcs
[j
] == new_crtc
) {
655 DRM_DEBUG_KMS("fallback: cloned configuration\n");
660 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
663 /* go for command line mode first */
664 modes
[i
] = drm_connector_pick_cmdline_mode(connector
);
666 /* try for preferred next */
668 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
669 connector
->name
, connector
->has_tile
);
670 modes
[i
] = drm_connector_has_preferred_mode(connector
, width
, height
);
673 /* No preferred mode marked by the EDID? Are there any modes? */
674 if (!modes
[i
] && !list_empty(&connector
->modes
)) {
675 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
677 modes
[i
] = list_first_entry(&connector
->modes
,
678 struct drm_display_mode
,
682 /* last resort: use current mode */
685 * IMPORTANT: We want to use the adjusted mode (i.e.
686 * after the panel fitter upscaling) as the initial
687 * config, not the input mode, which is what crtc->mode
688 * usually contains. But since our current
689 * code puts a mode derived from the post-pfit timings
690 * into crtc->mode this works out correctly.
692 * This is crtc->mode and not crtc->state->mode for the
693 * fastboot check to work correctly.
695 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
697 modes
[i
] = &connector
->state
->crtc
->mode
;
700 * In case of tiled modes, if all tiles are not present
701 * then fallback to a non tiled mode.
703 if (connector
->has_tile
&&
704 num_tiled_conns
< connector
->num_h_tile
* connector
->num_v_tile
) {
705 DRM_DEBUG_KMS("Falling back to non tiled mode on Connector %d\n",
707 modes
[i
] = drm_connector_fallback_non_tiled_mode(connector
);
711 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
713 connector
->state
->crtc
->base
.id
,
714 connector
->state
->crtc
->name
,
715 modes
[i
]->hdisplay
, modes
[i
]->vdisplay
,
716 modes
[i
]->flags
& DRM_MODE_FLAG_INTERLACE
? "i" : "");
719 conn_configured
|= BIT(i
);
722 if ((conn_configured
& mask
) != mask
&& conn_configured
!= conn_seq
)
726 * If the BIOS didn't enable everything it could, fall back to have the
727 * same user experiencing of lighting up as much as possible like the
728 * fbdev helper library.
730 if (num_connectors_enabled
!= num_connectors_detected
&&
731 num_connectors_enabled
< dev
->mode_config
.num_crtc
) {
732 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
733 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled
,
734 num_connectors_detected
);
740 DRM_DEBUG_KMS("Not using firmware configuration\n");
741 memcpy(enabled
, save_enabled
, count
);
745 drm_modeset_drop_locks(&ctx
);
746 drm_modeset_acquire_fini(&ctx
);
753 * drm_client_modeset_probe() - Probe for displays
754 * @client: DRM client
755 * @width: Maximum display mode width (optional)
756 * @height: Maximum display mode height (optional)
758 * This function sets up display pipelines for enabled connectors and stores the
759 * config in the client's modeset array.
762 * Zero on success or negative error code on failure.
764 int drm_client_modeset_probe(struct drm_client_dev
*client
, unsigned int width
, unsigned int height
)
766 struct drm_connector
*connector
, **connectors
= NULL
;
767 struct drm_connector_list_iter conn_iter
;
768 struct drm_device
*dev
= client
->dev
;
769 unsigned int total_modes_count
= 0;
770 struct drm_client_offset
*offsets
;
771 unsigned int connector_count
= 0;
772 struct drm_display_mode
**modes
;
773 struct drm_crtc
**crtcs
;
780 width
= dev
->mode_config
.max_width
;
782 height
= dev
->mode_config
.max_height
;
784 drm_connector_list_iter_begin(dev
, &conn_iter
);
785 drm_client_for_each_connector_iter(connector
, &conn_iter
) {
786 struct drm_connector
**tmp
;
788 tmp
= krealloc(connectors
, (connector_count
+ 1) * sizeof(*connectors
), GFP_KERNEL
);
791 goto free_connectors
;
795 drm_connector_get(connector
);
796 connectors
[connector_count
++] = connector
;
798 drm_connector_list_iter_end(&conn_iter
);
800 if (!connector_count
)
803 crtcs
= kcalloc(connector_count
, sizeof(*crtcs
), GFP_KERNEL
);
804 modes
= kcalloc(connector_count
, sizeof(*modes
), GFP_KERNEL
);
805 offsets
= kcalloc(connector_count
, sizeof(*offsets
), GFP_KERNEL
);
806 enabled
= kcalloc(connector_count
, sizeof(bool), GFP_KERNEL
);
807 if (!crtcs
|| !modes
|| !enabled
|| !offsets
) {
808 DRM_ERROR("Memory allocation failed\n");
813 mutex_lock(&client
->modeset_mutex
);
815 mutex_lock(&dev
->mode_config
.mutex
);
816 for (i
= 0; i
< connector_count
; i
++)
817 total_modes_count
+= connectors
[i
]->funcs
->fill_modes(connectors
[i
], width
, height
);
818 if (!total_modes_count
)
819 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
820 drm_client_connectors_enabled(connectors
, connector_count
, enabled
);
822 if (!drm_client_firmware_config(client
, connectors
, connector_count
, crtcs
,
823 modes
, offsets
, enabled
, width
, height
)) {
824 memset(modes
, 0, connector_count
* sizeof(*modes
));
825 memset(crtcs
, 0, connector_count
* sizeof(*crtcs
));
826 memset(offsets
, 0, connector_count
* sizeof(*offsets
));
828 if (!drm_client_target_cloned(dev
, connectors
, connector_count
, modes
,
829 offsets
, enabled
, width
, height
) &&
830 !drm_client_target_preferred(connectors
, connector_count
, modes
,
831 offsets
, enabled
, width
, height
))
832 DRM_ERROR("Unable to find initial modes\n");
834 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
837 drm_client_pick_crtcs(client
, connectors
, connector_count
,
838 crtcs
, modes
, 0, width
, height
);
840 mutex_unlock(&dev
->mode_config
.mutex
);
842 drm_client_modeset_release(client
);
844 for (i
= 0; i
< connector_count
; i
++) {
845 struct drm_display_mode
*mode
= modes
[i
];
846 struct drm_crtc
*crtc
= crtcs
[i
];
847 struct drm_client_offset
*offset
= &offsets
[i
];
850 struct drm_mode_set
*modeset
= drm_client_find_modeset(client
, crtc
);
851 struct drm_connector
*connector
= connectors
[i
];
853 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
854 mode
->name
, crtc
->base
.id
, offset
->x
, offset
->y
);
856 if (WARN_ON_ONCE(modeset
->num_connectors
== DRM_CLIENT_MAX_CLONED_CONNECTORS
||
857 (dev
->mode_config
.num_crtc
> 1 && modeset
->num_connectors
== 1))) {
862 modeset
->mode
= drm_mode_duplicate(dev
, mode
);
863 drm_connector_get(connector
);
864 modeset
->connectors
[modeset
->num_connectors
++] = connector
;
865 modeset
->x
= offset
->x
;
866 modeset
->y
= offset
->y
;
870 mutex_unlock(&client
->modeset_mutex
);
877 for (i
= 0; i
< connector_count
; i
++)
878 drm_connector_put(connectors
[i
]);
883 EXPORT_SYMBOL(drm_client_modeset_probe
);
886 * drm_client_rotation() - Check the initial rotation value
887 * @modeset: DRM modeset
888 * @rotation: Returned rotation value
890 * This function checks if the primary plane in @modeset can hw rotate
891 * to match the rotation needed on its connector.
893 * Note: Currently only 0 and 180 degrees are supported.
896 * True if the plane can do the rotation, false otherwise.
898 bool drm_client_rotation(struct drm_mode_set
*modeset
, unsigned int *rotation
)
900 struct drm_connector
*connector
= modeset
->connectors
[0];
901 struct drm_plane
*plane
= modeset
->crtc
->primary
;
902 struct drm_cmdline_mode
*cmdline
;
906 if (!modeset
->num_connectors
)
909 switch (connector
->display_info
.panel_orientation
) {
910 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP
:
911 *rotation
= DRM_MODE_ROTATE_180
;
913 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP
:
914 *rotation
= DRM_MODE_ROTATE_90
;
916 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP
:
917 *rotation
= DRM_MODE_ROTATE_270
;
920 *rotation
= DRM_MODE_ROTATE_0
;
924 * The panel already defined the default rotation
925 * through its orientation. Whatever has been provided
926 * on the command line needs to be added to that.
928 * Unfortunately, the rotations are at different bit
929 * indices, so the math to add them up are not as
930 * trivial as they could.
932 * Reflections on the other hand are pretty trivial to deal with, a
933 * simple XOR between the two handle the addition nicely.
935 cmdline
= &connector
->cmdline_mode
;
936 if (cmdline
->specified
&& cmdline
->rotation_reflection
) {
937 unsigned int cmdline_rest
, panel_rest
;
938 unsigned int cmdline_rot
, panel_rot
;
939 unsigned int sum_rot
, sum_rest
;
941 panel_rot
= ilog2(*rotation
& DRM_MODE_ROTATE_MASK
);
942 cmdline_rot
= ilog2(cmdline
->rotation_reflection
& DRM_MODE_ROTATE_MASK
);
943 sum_rot
= (panel_rot
+ cmdline_rot
) % 4;
945 panel_rest
= *rotation
& ~DRM_MODE_ROTATE_MASK
;
946 cmdline_rest
= cmdline
->rotation_reflection
& ~DRM_MODE_ROTATE_MASK
;
947 sum_rest
= panel_rest
^ cmdline_rest
;
949 *rotation
= (1 << sum_rot
) | sum_rest
;
953 * TODO: support 90 / 270 degree hardware rotation,
954 * depending on the hardware this may require the framebuffer
955 * to be in a specific tiling format.
957 if (((*rotation
& DRM_MODE_ROTATE_MASK
) != DRM_MODE_ROTATE_0
&&
958 (*rotation
& DRM_MODE_ROTATE_MASK
) != DRM_MODE_ROTATE_180
) ||
959 !plane
->rotation_property
)
962 for (i
= 0; i
< plane
->rotation_property
->num_values
; i
++)
963 valid_mask
|= (1ULL << plane
->rotation_property
->values
[i
]);
965 if (!(*rotation
& valid_mask
))
970 EXPORT_SYMBOL(drm_client_rotation
);
972 static int drm_client_modeset_commit_atomic(struct drm_client_dev
*client
, bool active
, bool check
)
974 struct drm_device
*dev
= client
->dev
;
975 struct drm_plane
*plane
;
976 struct drm_atomic_state
*state
;
977 struct drm_modeset_acquire_ctx ctx
;
978 struct drm_mode_set
*mode_set
;
981 drm_modeset_acquire_init(&ctx
, 0);
983 state
= drm_atomic_state_alloc(dev
);
989 state
->acquire_ctx
= &ctx
;
991 drm_for_each_plane(plane
, dev
) {
992 struct drm_plane_state
*plane_state
;
994 plane_state
= drm_atomic_get_plane_state(state
, plane
);
995 if (IS_ERR(plane_state
)) {
996 ret
= PTR_ERR(plane_state
);
1000 plane_state
->rotation
= DRM_MODE_ROTATE_0
;
1002 /* disable non-primary: */
1003 if (plane
->type
== DRM_PLANE_TYPE_PRIMARY
)
1006 ret
= __drm_atomic_helper_disable_plane(plane
, plane_state
);
1011 drm_client_for_each_modeset(mode_set
, client
) {
1012 struct drm_plane
*primary
= mode_set
->crtc
->primary
;
1013 unsigned int rotation
;
1015 if (drm_client_rotation(mode_set
, &rotation
)) {
1016 struct drm_plane_state
*plane_state
;
1018 /* Cannot fail as we've already gotten the plane state above */
1019 plane_state
= drm_atomic_get_new_plane_state(state
, primary
);
1020 plane_state
->rotation
= rotation
;
1023 ret
= __drm_atomic_helper_set_config(mode_set
, state
);
1028 * __drm_atomic_helper_set_config() sets active when a
1029 * mode is set, unconditionally clear it if we force DPMS off
1032 struct drm_crtc
*crtc
= mode_set
->crtc
;
1033 struct drm_crtc_state
*crtc_state
= drm_atomic_get_new_crtc_state(state
, crtc
);
1035 crtc_state
->active
= false;
1040 ret
= drm_atomic_check_only(state
);
1042 ret
= drm_atomic_commit(state
);
1045 if (ret
== -EDEADLK
)
1048 drm_atomic_state_put(state
);
1050 drm_modeset_drop_locks(&ctx
);
1051 drm_modeset_acquire_fini(&ctx
);
1056 drm_atomic_state_clear(state
);
1057 drm_modeset_backoff(&ctx
);
1062 static int drm_client_modeset_commit_legacy(struct drm_client_dev
*client
)
1064 struct drm_device
*dev
= client
->dev
;
1065 struct drm_mode_set
*mode_set
;
1066 struct drm_plane
*plane
;
1069 drm_modeset_lock_all(dev
);
1070 drm_for_each_plane(plane
, dev
) {
1071 if (plane
->type
!= DRM_PLANE_TYPE_PRIMARY
)
1072 drm_plane_force_disable(plane
);
1074 if (plane
->rotation_property
)
1075 drm_mode_plane_set_obj_prop(plane
,
1076 plane
->rotation_property
,
1080 drm_client_for_each_modeset(mode_set
, client
) {
1081 struct drm_crtc
*crtc
= mode_set
->crtc
;
1083 if (crtc
->funcs
->cursor_set2
) {
1084 ret
= crtc
->funcs
->cursor_set2(crtc
, NULL
, 0, 0, 0, 0, 0);
1087 } else if (crtc
->funcs
->cursor_set
) {
1088 ret
= crtc
->funcs
->cursor_set(crtc
, NULL
, 0, 0, 0);
1093 ret
= drm_mode_set_config_internal(mode_set
);
1098 drm_modeset_unlock_all(dev
);
1104 * drm_client_modeset_check() - Check modeset configuration
1105 * @client: DRM client
1107 * Check modeset configuration.
1110 * Zero on success or negative error code on failure.
1112 int drm_client_modeset_check(struct drm_client_dev
*client
)
1116 if (!drm_drv_uses_atomic_modeset(client
->dev
))
1119 mutex_lock(&client
->modeset_mutex
);
1120 ret
= drm_client_modeset_commit_atomic(client
, true, true);
1121 mutex_unlock(&client
->modeset_mutex
);
1125 EXPORT_SYMBOL(drm_client_modeset_check
);
1128 * drm_client_modeset_commit_locked() - Force commit CRTC configuration
1129 * @client: DRM client
1131 * Commit modeset configuration to crtcs without checking if there is a DRM
1132 * master. The assumption is that the caller already holds an internal DRM
1133 * master reference acquired with drm_master_internal_acquire().
1136 * Zero on success or negative error code on failure.
1138 int drm_client_modeset_commit_locked(struct drm_client_dev
*client
)
1140 struct drm_device
*dev
= client
->dev
;
1143 mutex_lock(&client
->modeset_mutex
);
1144 if (drm_drv_uses_atomic_modeset(dev
))
1145 ret
= drm_client_modeset_commit_atomic(client
, true, false);
1147 ret
= drm_client_modeset_commit_legacy(client
);
1148 mutex_unlock(&client
->modeset_mutex
);
1152 EXPORT_SYMBOL(drm_client_modeset_commit_locked
);
1155 * drm_client_modeset_commit() - Commit CRTC configuration
1156 * @client: DRM client
1158 * Commit modeset configuration to crtcs.
1161 * Zero on success or negative error code on failure.
1163 int drm_client_modeset_commit(struct drm_client_dev
*client
)
1165 struct drm_device
*dev
= client
->dev
;
1168 if (!drm_master_internal_acquire(dev
))
1171 ret
= drm_client_modeset_commit_locked(client
);
1173 drm_master_internal_release(dev
);
1177 EXPORT_SYMBOL(drm_client_modeset_commit
);
1179 static void drm_client_modeset_dpms_legacy(struct drm_client_dev
*client
, int dpms_mode
)
1181 struct drm_device
*dev
= client
->dev
;
1182 struct drm_connector
*connector
;
1183 struct drm_mode_set
*modeset
;
1186 drm_modeset_lock_all(dev
);
1187 drm_client_for_each_modeset(modeset
, client
) {
1188 if (!modeset
->crtc
->enabled
)
1191 for (j
= 0; j
< modeset
->num_connectors
; j
++) {
1192 connector
= modeset
->connectors
[j
];
1193 connector
->funcs
->dpms(connector
, dpms_mode
);
1194 drm_object_property_set_value(&connector
->base
,
1195 dev
->mode_config
.dpms_property
, dpms_mode
);
1198 drm_modeset_unlock_all(dev
);
1202 * drm_client_modeset_dpms() - Set DPMS mode
1203 * @client: DRM client
1206 * Note: For atomic drivers @mode is reduced to on/off.
1209 * Zero on success or negative error code on failure.
1211 int drm_client_modeset_dpms(struct drm_client_dev
*client
, int mode
)
1213 struct drm_device
*dev
= client
->dev
;
1216 if (!drm_master_internal_acquire(dev
))
1219 mutex_lock(&client
->modeset_mutex
);
1220 if (drm_drv_uses_atomic_modeset(dev
))
1221 ret
= drm_client_modeset_commit_atomic(client
, mode
== DRM_MODE_DPMS_ON
, false);
1223 drm_client_modeset_dpms_legacy(client
, mode
);
1224 mutex_unlock(&client
->modeset_mutex
);
1226 drm_master_internal_release(dev
);
1230 EXPORT_SYMBOL(drm_client_modeset_dpms
);