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
10 #include "drm/drm_modeset_lock.h"
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/slab.h>
14 #include <linux/string_helpers.h>
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_client.h>
18 #include <drm/drm_connector.h>
19 #include <drm/drm_crtc.h>
20 #include <drm/drm_device.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_edid.h>
23 #include <drm/drm_encoder.h>
24 #include <drm/drm_print.h>
26 #include "drm_crtc_internal.h"
27 #include "drm_internal.h"
29 #define DRM_CLIENT_MAX_CLONED_CONNECTORS 8
31 struct drm_client_offset {
35 int drm_client_modeset_create(struct drm_client_dev *client)
37 struct drm_device *dev = client->dev;
38 unsigned int num_crtc = dev->mode_config.num_crtc;
39 unsigned int max_connector_count = 1;
40 struct drm_mode_set *modeset;
41 struct drm_crtc *crtc;
44 /* Add terminating zero entry to enable index less iteration */
45 client->modesets = kcalloc(num_crtc + 1, sizeof(*client->modesets), GFP_KERNEL);
46 if (!client->modesets)
49 mutex_init(&client->modeset_mutex);
51 drm_for_each_crtc(crtc, dev)
52 client->modesets[i++].crtc = crtc;
54 /* Cloning is only supported in the single crtc case. */
56 max_connector_count = DRM_CLIENT_MAX_CLONED_CONNECTORS;
58 for (modeset = client->modesets; modeset->crtc; modeset++) {
59 modeset->connectors = kcalloc(max_connector_count,
60 sizeof(*modeset->connectors), GFP_KERNEL);
61 if (!modeset->connectors)
68 drm_client_modeset_free(client);
73 static void drm_client_modeset_release(struct drm_client_dev *client)
75 struct drm_mode_set *modeset;
78 drm_client_for_each_modeset(modeset, client) {
79 drm_mode_destroy(client->dev, modeset->mode);
83 for (i = 0; i < modeset->num_connectors; i++) {
84 drm_connector_put(modeset->connectors[i]);
85 modeset->connectors[i] = NULL;
87 modeset->num_connectors = 0;
91 void drm_client_modeset_free(struct drm_client_dev *client)
93 struct drm_mode_set *modeset;
95 mutex_lock(&client->modeset_mutex);
97 drm_client_modeset_release(client);
99 drm_client_for_each_modeset(modeset, client)
100 kfree(modeset->connectors);
102 mutex_unlock(&client->modeset_mutex);
104 mutex_destroy(&client->modeset_mutex);
105 kfree(client->modesets);
108 static struct drm_mode_set *
109 drm_client_find_modeset(struct drm_client_dev *client, struct drm_crtc *crtc)
111 struct drm_mode_set *modeset;
113 drm_client_for_each_modeset(modeset, client)
114 if (modeset->crtc == crtc)
120 static struct drm_display_mode *
121 drm_connector_get_tiled_mode(struct drm_connector *connector)
123 struct drm_display_mode *mode;
125 list_for_each_entry(mode, &connector->modes, head) {
126 if (mode->hdisplay == connector->tile_h_size &&
127 mode->vdisplay == connector->tile_v_size)
133 static struct drm_display_mode *
134 drm_connector_fallback_non_tiled_mode(struct drm_connector *connector)
136 struct drm_display_mode *mode;
138 list_for_each_entry(mode, &connector->modes, head) {
139 if (mode->hdisplay == connector->tile_h_size &&
140 mode->vdisplay == connector->tile_v_size)
147 static struct drm_display_mode *
148 drm_connector_preferred_mode(struct drm_connector *connector, int width, int height)
150 struct drm_display_mode *mode;
152 list_for_each_entry(mode, &connector->modes, head) {
153 if (mode->hdisplay > width ||
154 mode->vdisplay > height)
156 if (mode->type & DRM_MODE_TYPE_PREFERRED)
162 static struct drm_display_mode *drm_connector_first_mode(struct drm_connector *connector)
164 return list_first_entry_or_null(&connector->modes,
165 struct drm_display_mode, head);
168 static struct drm_display_mode *drm_connector_pick_cmdline_mode(struct drm_connector *connector)
170 struct drm_cmdline_mode *cmdline_mode;
171 struct drm_display_mode *mode;
172 bool prefer_non_interlace;
175 * Find a user-defined mode. If the user gave us a valid
176 * mode on the kernel command line, it will show up in this
180 list_for_each_entry(mode, &connector->modes, head) {
181 if (mode->type & DRM_MODE_TYPE_USERDEF)
185 cmdline_mode = &connector->cmdline_mode;
186 if (cmdline_mode->specified == false)
190 * Attempt to find a matching mode in the list of modes we
191 * have gotten so far.
194 prefer_non_interlace = !cmdline_mode->interlace;
196 list_for_each_entry(mode, &connector->modes, head) {
197 /* check width/height */
198 if (mode->hdisplay != cmdline_mode->xres ||
199 mode->vdisplay != cmdline_mode->yres)
202 if (cmdline_mode->refresh_specified) {
203 if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
207 if (cmdline_mode->interlace) {
208 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
210 } else if (prefer_non_interlace) {
211 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
217 if (prefer_non_interlace) {
218 prefer_non_interlace = false;
225 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
229 if (connector->display_info.non_desktop)
233 enable = connector->status == connector_status_connected;
235 enable = connector->status != connector_status_disconnected;
240 static void drm_client_connectors_enabled(struct drm_connector **connectors,
241 unsigned int connector_count,
244 bool any_enabled = false;
245 struct drm_connector *connector;
248 for (i = 0; i < connector_count; i++) {
249 connector = connectors[i];
250 enabled[i] = drm_connector_enabled(connector, true);
251 drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] enabled? %s\n",
252 connector->base.id, connector->name,
253 connector->display_info.non_desktop ?
254 "non desktop" : str_yes_no(enabled[i]));
256 any_enabled |= enabled[i];
262 for (i = 0; i < connector_count; i++)
263 enabled[i] = drm_connector_enabled(connectors[i], false);
266 static bool drm_client_target_cloned(struct drm_device *dev,
267 struct drm_connector **connectors,
268 unsigned int connector_count,
269 struct drm_display_mode **modes,
270 struct drm_client_offset *offsets,
271 bool *enabled, int width, int height)
274 bool can_clone = false;
275 struct drm_display_mode *dmt_mode, *mode;
277 /* only contemplate cloning in the single crtc case */
278 if (dev->mode_config.num_crtc > 1)
282 for (i = 0; i < connector_count; i++) {
287 /* only contemplate cloning if more than one connector is enabled */
291 /* check the command line or if nothing common pick 1024x768 */
293 for (i = 0; i < connector_count; i++) {
296 modes[i] = drm_connector_pick_cmdline_mode(connectors[i]);
301 for (j = 0; j < i; j++) {
304 if (!drm_mode_match(modes[j], modes[i],
305 DRM_MODE_MATCH_TIMINGS |
306 DRM_MODE_MATCH_CLOCK |
307 DRM_MODE_MATCH_FLAGS |
308 DRM_MODE_MATCH_3D_FLAGS))
314 drm_dbg_kms(dev, "can clone using command line\n");
318 /* try and find a 1024x768 mode on each connector */
320 dmt_mode = drm_mode_find_dmt(dev, 1024, 768, 60, false);
325 for (i = 0; i < connector_count; i++) {
329 list_for_each_entry(mode, &connectors[i]->modes, head) {
330 if (drm_mode_match(mode, dmt_mode,
331 DRM_MODE_MATCH_TIMINGS |
332 DRM_MODE_MATCH_CLOCK |
333 DRM_MODE_MATCH_FLAGS |
334 DRM_MODE_MATCH_3D_FLAGS))
340 drm_mode_destroy(dev, dmt_mode);
343 drm_dbg_kms(dev, "can clone using 1024x768\n");
347 drm_info(dev, "kms: can't enable cloning when we probably wanted to.\n");
351 static int drm_client_get_tile_offsets(struct drm_device *dev,
352 struct drm_connector **connectors,
353 unsigned int connector_count,
354 struct drm_display_mode **modes,
355 struct drm_client_offset *offsets,
357 int h_idx, int v_idx)
359 struct drm_connector *connector;
361 int hoffset = 0, voffset = 0;
363 for (i = 0; i < connector_count; i++) {
364 connector = connectors[i];
365 if (!connector->has_tile)
368 if (!modes[i] && (h_idx || v_idx)) {
370 "[CONNECTOR:%d:%s] no modes for connector tiled %d\n",
371 connector->base.id, connector->name, i);
374 if (connector->tile_h_loc < h_idx)
375 hoffset += modes[i]->hdisplay;
377 if (connector->tile_v_loc < v_idx)
378 voffset += modes[i]->vdisplay;
380 offsets[idx].x = hoffset;
381 offsets[idx].y = voffset;
382 drm_dbg_kms(dev, "returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
386 static bool drm_client_target_preferred(struct drm_device *dev,
387 struct drm_connector **connectors,
388 unsigned int connector_count,
389 struct drm_display_mode **modes,
390 struct drm_client_offset *offsets,
391 bool *enabled, int width, int height)
393 const u64 mask = BIT_ULL(connector_count) - 1;
394 struct drm_connector *connector;
395 u64 conn_configured = 0;
397 int num_tiled_conns = 0;
400 for (i = 0; i < connector_count; i++) {
401 if (connectors[i]->has_tile &&
402 connectors[i]->status == connector_status_connected)
407 for (i = 0; i < connector_count; i++) {
408 connector = connectors[i];
410 if (conn_configured & BIT_ULL(i))
413 if (enabled[i] == false) {
414 conn_configured |= BIT_ULL(i);
418 /* first pass over all the untiled connectors */
419 if (tile_pass == 0 && connector->has_tile)
422 if (tile_pass == 1) {
423 if (connector->tile_h_loc != 0 ||
424 connector->tile_v_loc != 0)
428 if (connector->tile_h_loc != tile_pass - 1 &&
429 connector->tile_v_loc != tile_pass - 1)
430 /* if this tile_pass doesn't cover any of the tiles - keep going */
434 * find the tile offsets for this pass - need to find
435 * all tiles left and above
437 drm_client_get_tile_offsets(dev, connectors, connector_count,
439 connector->tile_h_loc, connector->tile_v_loc);
441 drm_dbg_kms(dev, "[CONNECTOR:%d:%s] looking for cmdline mode\n",
442 connector->base.id, connector->name);
444 /* got for command line mode first */
445 modes[i] = drm_connector_pick_cmdline_mode(connector);
447 drm_dbg_kms(dev, "[CONNECTOR:%d:%s] looking for preferred mode, tile %d\n",
448 connector->base.id, connector->name,
449 connector->tile_group ? connector->tile_group->id : 0);
450 modes[i] = drm_connector_preferred_mode(connector, width, height);
452 /* No preferred modes, pick one off the list */
454 modes[i] = drm_connector_first_mode(connector);
456 * In case of tiled mode if all tiles not present fallback to
457 * first available non tiled mode.
458 * After all tiles are present, try to find the tiled mode
459 * for all and if tiled mode not present due to fbcon size
460 * limitations, use first non tiled mode only for
461 * tile 0,0 and set to no mode for all other tiles.
463 if (connector->has_tile) {
464 if (num_tiled_conns <
465 connector->num_h_tile * connector->num_v_tile ||
466 (connector->tile_h_loc == 0 &&
467 connector->tile_v_loc == 0 &&
468 !drm_connector_get_tiled_mode(connector))) {
470 "[CONNECTOR:%d:%s] Falling back to non-tiled mode\n",
471 connector->base.id, connector->name);
472 modes[i] = drm_connector_fallback_non_tiled_mode(connector);
474 modes[i] = drm_connector_get_tiled_mode(connector);
478 drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Found mode %s\n",
479 connector->base.id, connector->name,
480 modes[i] ? modes[i]->name : "none");
481 conn_configured |= BIT_ULL(i);
484 if ((conn_configured & mask) != mask) {
491 static bool connector_has_possible_crtc(struct drm_connector *connector,
492 struct drm_crtc *crtc)
494 struct drm_encoder *encoder;
496 drm_connector_for_each_possible_encoder(connector, encoder) {
497 if (encoder->possible_crtcs & drm_crtc_mask(crtc))
504 static int drm_client_pick_crtcs(struct drm_client_dev *client,
505 struct drm_connector **connectors,
506 unsigned int connector_count,
507 struct drm_crtc **best_crtcs,
508 struct drm_display_mode **modes,
509 int n, int width, int height)
511 struct drm_device *dev = client->dev;
512 struct drm_connector *connector;
513 int my_score, best_score, score;
514 struct drm_crtc **crtcs, *crtc;
515 struct drm_mode_set *modeset;
518 if (n == connector_count)
521 connector = connectors[n];
523 best_crtcs[n] = NULL;
524 best_score = drm_client_pick_crtcs(client, connectors, connector_count,
525 best_crtcs, modes, n + 1, width, height);
526 if (modes[n] == NULL)
529 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
534 if (connector->status == connector_status_connected)
536 if (connector->cmdline_mode.specified)
538 if (drm_connector_preferred_mode(connector, width, height))
542 * select a crtc for this connector and then attempt to configure
543 * remaining connectors
545 drm_client_for_each_modeset(modeset, client) {
546 crtc = modeset->crtc;
548 if (!connector_has_possible_crtc(connector, crtc))
551 for (o = 0; o < n; o++)
552 if (best_crtcs[o] == crtc)
556 /* ignore cloning unless only a single crtc */
557 if (dev->mode_config.num_crtc > 1)
560 if (!drm_mode_equal(modes[o], modes[n]))
565 memcpy(crtcs, best_crtcs, n * sizeof(*crtcs));
566 score = my_score + drm_client_pick_crtcs(client, connectors, connector_count,
567 crtcs, modes, n + 1, width, height);
568 if (score > best_score) {
570 memcpy(best_crtcs, crtcs, connector_count * sizeof(*crtcs));
578 /* Try to read the BIOS display configuration and use it for the initial config */
579 static bool drm_client_firmware_config(struct drm_client_dev *client,
580 struct drm_connector **connectors,
581 unsigned int connector_count,
582 struct drm_crtc **crtcs,
583 struct drm_display_mode **modes,
584 struct drm_client_offset *offsets,
585 bool *enabled, int width, int height)
587 const int count = min_t(unsigned int, connector_count, BITS_PER_LONG);
588 unsigned long conn_configured, conn_seq, mask;
589 struct drm_device *dev = client->dev;
592 bool fallback = true, ret = true;
593 int num_connectors_enabled = 0;
594 int num_connectors_detected = 0;
595 int num_tiled_conns = 0;
596 struct drm_modeset_acquire_ctx ctx;
598 if (!drm_drv_uses_atomic_modeset(dev))
601 if (drm_WARN_ON(dev, count <= 0))
604 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
608 drm_modeset_acquire_init(&ctx, 0);
610 while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
611 drm_modeset_backoff(&ctx);
613 memcpy(save_enabled, enabled, count);
614 mask = GENMASK(count - 1, 0);
616 for (i = 0; i < count; i++) {
617 if (connectors[i]->has_tile &&
618 connectors[i]->status == connector_status_connected)
622 conn_seq = conn_configured;
623 for (i = 0; i < count; i++) {
624 struct drm_connector *connector;
625 struct drm_encoder *encoder;
626 struct drm_crtc *new_crtc;
628 connector = connectors[i];
630 if (conn_configured & BIT(i))
633 if (conn_seq == 0 && !connector->has_tile)
636 if (connector->status == connector_status_connected)
637 num_connectors_detected++;
640 drm_dbg_kms(dev, "[CONNECTOR:%d:%s] not enabled, skipping\n",
641 connector->base.id, connector->name);
642 conn_configured |= BIT(i);
646 if (connector->force == DRM_FORCE_OFF) {
647 drm_dbg_kms(dev, "[CONNECTOR:%d:%s] disabled by user, skipping\n",
648 connector->base.id, connector->name);
653 encoder = connector->state->best_encoder;
654 if (!encoder || drm_WARN_ON(dev, !connector->state->crtc)) {
655 if (connector->force > DRM_FORCE_OFF)
658 drm_dbg_kms(dev, "[CONNECTOR:%d:%s] has no encoder or crtc, skipping\n",
659 connector->base.id, connector->name);
661 conn_configured |= BIT(i);
665 num_connectors_enabled++;
667 new_crtc = connector->state->crtc;
670 * Make sure we're not trying to drive multiple connectors
671 * with a single CRTC, since our cloning support may not
674 for (j = 0; j < count; j++) {
675 if (crtcs[j] == new_crtc) {
676 drm_dbg_kms(dev, "fallback: cloned configuration\n");
681 drm_dbg_kms(dev, "[CONNECTOR:%d:%s] looking for cmdline mode\n",
682 connector->base.id, connector->name);
684 /* go for command line mode first */
685 modes[i] = drm_connector_pick_cmdline_mode(connector);
687 /* try for preferred next */
690 "[CONNECTOR:%d:%s] looking for preferred mode, has tile: %s\n",
691 connector->base.id, connector->name,
692 str_yes_no(connector->has_tile));
693 modes[i] = drm_connector_preferred_mode(connector, width, height);
696 /* No preferred mode marked by the EDID? Are there any modes? */
697 if (!modes[i] && !list_empty(&connector->modes)) {
698 drm_dbg_kms(dev, "[CONNECTOR:%d:%s] using first listed mode\n",
699 connector->base.id, connector->name);
700 modes[i] = drm_connector_first_mode(connector);
703 /* last resort: use current mode */
706 * IMPORTANT: We want to use the adjusted mode (i.e.
707 * after the panel fitter upscaling) as the initial
708 * config, not the input mode, which is what crtc->mode
709 * usually contains. But since our current
710 * code puts a mode derived from the post-pfit timings
711 * into crtc->mode this works out correctly.
713 * This is crtc->mode and not crtc->state->mode for the
714 * fastboot check to work correctly.
716 drm_dbg_kms(dev, "[CONNECTOR:%d:%s] looking for current mode\n",
717 connector->base.id, connector->name);
718 modes[i] = &connector->state->crtc->mode;
721 * In case of tiled modes, if all tiles are not present
722 * then fallback to a non tiled mode.
724 if (connector->has_tile &&
725 num_tiled_conns < connector->num_h_tile * connector->num_v_tile) {
726 drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Falling back to non-tiled mode\n",
727 connector->base.id, connector->name);
728 modes[i] = drm_connector_fallback_non_tiled_mode(connector);
732 drm_dbg_kms(dev, "[CONNECTOR:%d:%s] on [CRTC:%d:%s]: %dx%d%s\n",
733 connector->base.id, connector->name,
734 connector->state->crtc->base.id,
735 connector->state->crtc->name,
736 modes[i]->hdisplay, modes[i]->vdisplay,
737 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "");
740 conn_configured |= BIT(i);
743 if ((conn_configured & mask) != mask && conn_configured != conn_seq)
746 for (i = 0; i < count; i++) {
747 struct drm_connector *connector = connectors[i];
749 if (connector->has_tile)
750 drm_client_get_tile_offsets(dev, connectors, connector_count,
752 connector->tile_h_loc, connector->tile_v_loc);
756 * If the BIOS didn't enable everything it could, fall back to have the
757 * same user experiencing of lighting up as much as possible like the
758 * fbdev helper library.
760 if (num_connectors_enabled != num_connectors_detected &&
761 num_connectors_enabled < dev->mode_config.num_crtc) {
762 drm_dbg_kms(dev, "fallback: Not all outputs enabled\n");
763 drm_dbg_kms(dev, "Enabled: %i, detected: %i\n",
764 num_connectors_enabled, num_connectors_detected);
770 drm_dbg_kms(dev, "Not using firmware configuration\n");
771 memcpy(enabled, save_enabled, count);
775 drm_modeset_drop_locks(&ctx);
776 drm_modeset_acquire_fini(&ctx);
783 * drm_client_modeset_probe() - Probe for displays
784 * @client: DRM client
785 * @width: Maximum display mode width (optional)
786 * @height: Maximum display mode height (optional)
788 * This function sets up display pipelines for enabled connectors and stores the
789 * config in the client's modeset array.
792 * Zero on success or negative error code on failure.
794 int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height)
796 struct drm_connector *connector, **connectors = NULL;
797 struct drm_connector_list_iter conn_iter;
798 struct drm_device *dev = client->dev;
799 unsigned int total_modes_count = 0;
800 struct drm_client_offset *offsets;
801 unsigned int connector_count = 0;
802 /* points to modes protected by mode_config.mutex */
803 struct drm_display_mode **modes;
804 struct drm_crtc **crtcs;
808 drm_dbg_kms(dev, "\n");
811 width = dev->mode_config.max_width;
813 height = dev->mode_config.max_height;
815 drm_connector_list_iter_begin(dev, &conn_iter);
816 drm_client_for_each_connector_iter(connector, &conn_iter) {
817 struct drm_connector **tmp;
819 tmp = krealloc(connectors, (connector_count + 1) * sizeof(*connectors), GFP_KERNEL);
822 goto free_connectors;
826 drm_connector_get(connector);
827 connectors[connector_count++] = connector;
829 drm_connector_list_iter_end(&conn_iter);
831 if (!connector_count)
834 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
835 modes = kcalloc(connector_count, sizeof(*modes), GFP_KERNEL);
836 offsets = kcalloc(connector_count, sizeof(*offsets), GFP_KERNEL);
837 enabled = kcalloc(connector_count, sizeof(bool), GFP_KERNEL);
838 if (!crtcs || !modes || !enabled || !offsets) {
843 mutex_lock(&client->modeset_mutex);
845 mutex_lock(&dev->mode_config.mutex);
846 for (i = 0; i < connector_count; i++)
847 total_modes_count += connectors[i]->funcs->fill_modes(connectors[i], width, height);
848 if (!total_modes_count)
849 drm_dbg_kms(dev, "No connectors reported connected with modes\n");
850 drm_client_connectors_enabled(connectors, connector_count, enabled);
852 if (!drm_client_firmware_config(client, connectors, connector_count, crtcs,
853 modes, offsets, enabled, width, height)) {
854 memset(modes, 0, connector_count * sizeof(*modes));
855 memset(crtcs, 0, connector_count * sizeof(*crtcs));
856 memset(offsets, 0, connector_count * sizeof(*offsets));
858 if (!drm_client_target_cloned(dev, connectors, connector_count, modes,
859 offsets, enabled, width, height) &&
860 !drm_client_target_preferred(dev, connectors, connector_count, modes,
861 offsets, enabled, width, height))
862 drm_err(dev, "Unable to find initial modes\n");
864 drm_dbg_kms(dev, "picking CRTCs for %dx%d config\n",
867 drm_client_pick_crtcs(client, connectors, connector_count,
868 crtcs, modes, 0, width, height);
871 drm_client_modeset_release(client);
873 for (i = 0; i < connector_count; i++) {
874 struct drm_display_mode *mode = modes[i];
875 struct drm_crtc *crtc = crtcs[i];
876 struct drm_client_offset *offset = &offsets[i];
879 struct drm_mode_set *modeset = drm_client_find_modeset(client, crtc);
880 struct drm_connector *connector = connectors[i];
882 drm_dbg_kms(dev, "[CRTC:%d:%s] desired mode %s set (%d,%d)\n",
883 crtc->base.id, crtc->name,
884 mode->name, offset->x, offset->y);
886 if (drm_WARN_ON_ONCE(dev, modeset->num_connectors == DRM_CLIENT_MAX_CLONED_CONNECTORS ||
887 (dev->mode_config.num_crtc > 1 && modeset->num_connectors == 1))) {
892 drm_mode_destroy(dev, modeset->mode);
893 modeset->mode = drm_mode_duplicate(dev, mode);
894 if (!modeset->mode) {
899 drm_connector_get(connector);
900 modeset->connectors[modeset->num_connectors++] = connector;
901 modeset->x = offset->x;
902 modeset->y = offset->y;
905 mutex_unlock(&dev->mode_config.mutex);
907 mutex_unlock(&client->modeset_mutex);
914 for (i = 0; i < connector_count; i++)
915 drm_connector_put(connectors[i]);
920 EXPORT_SYMBOL(drm_client_modeset_probe);
923 * drm_client_rotation() - Check the initial rotation value
924 * @modeset: DRM modeset
925 * @rotation: Returned rotation value
927 * This function checks if the primary plane in @modeset can hw rotate
928 * to match the rotation needed on its connector.
930 * Note: Currently only 0 and 180 degrees are supported.
933 * True if the plane can do the rotation, false otherwise.
935 bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation)
937 struct drm_connector *connector = modeset->connectors[0];
938 struct drm_plane *plane = modeset->crtc->primary;
939 struct drm_cmdline_mode *cmdline;
943 if (!modeset->num_connectors)
946 switch (connector->display_info.panel_orientation) {
947 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
948 *rotation = DRM_MODE_ROTATE_180;
950 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
951 *rotation = DRM_MODE_ROTATE_90;
953 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
954 *rotation = DRM_MODE_ROTATE_270;
957 *rotation = DRM_MODE_ROTATE_0;
961 * The panel already defined the default rotation
962 * through its orientation. Whatever has been provided
963 * on the command line needs to be added to that.
965 * Unfortunately, the rotations are at different bit
966 * indices, so the math to add them up are not as
967 * trivial as they could.
969 * Reflections on the other hand are pretty trivial to deal with, a
970 * simple XOR between the two handle the addition nicely.
972 cmdline = &connector->cmdline_mode;
973 if (cmdline->specified && cmdline->rotation_reflection) {
974 unsigned int cmdline_rest, panel_rest;
975 unsigned int cmdline_rot, panel_rot;
976 unsigned int sum_rot, sum_rest;
978 panel_rot = ilog2(*rotation & DRM_MODE_ROTATE_MASK);
979 cmdline_rot = ilog2(cmdline->rotation_reflection & DRM_MODE_ROTATE_MASK);
980 sum_rot = (panel_rot + cmdline_rot) % 4;
982 panel_rest = *rotation & ~DRM_MODE_ROTATE_MASK;
983 cmdline_rest = cmdline->rotation_reflection & ~DRM_MODE_ROTATE_MASK;
984 sum_rest = panel_rest ^ cmdline_rest;
986 *rotation = (1 << sum_rot) | sum_rest;
990 * TODO: support 90 / 270 degree hardware rotation,
991 * depending on the hardware this may require the framebuffer
992 * to be in a specific tiling format.
994 if (((*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0 &&
995 (*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_180) ||
996 !plane->rotation_property)
999 for (i = 0; i < plane->rotation_property->num_values; i++)
1000 valid_mask |= (1ULL << plane->rotation_property->values[i]);
1002 if (!(*rotation & valid_mask))
1007 EXPORT_SYMBOL(drm_client_rotation);
1009 static int drm_client_modeset_commit_atomic(struct drm_client_dev *client, bool active, bool check)
1011 struct drm_device *dev = client->dev;
1012 struct drm_plane *plane;
1013 struct drm_atomic_state *state;
1014 struct drm_modeset_acquire_ctx ctx;
1015 struct drm_mode_set *mode_set;
1018 drm_modeset_acquire_init(&ctx, 0);
1020 state = drm_atomic_state_alloc(dev);
1026 state->acquire_ctx = &ctx;
1028 drm_for_each_plane(plane, dev) {
1029 struct drm_plane_state *plane_state;
1031 plane_state = drm_atomic_get_plane_state(state, plane);
1032 if (IS_ERR(plane_state)) {
1033 ret = PTR_ERR(plane_state);
1037 plane_state->rotation = DRM_MODE_ROTATE_0;
1039 /* disable non-primary: */
1040 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
1043 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1048 drm_client_for_each_modeset(mode_set, client) {
1049 struct drm_plane *primary = mode_set->crtc->primary;
1050 unsigned int rotation;
1052 if (drm_client_rotation(mode_set, &rotation)) {
1053 struct drm_plane_state *plane_state;
1055 /* Cannot fail as we've already gotten the plane state above */
1056 plane_state = drm_atomic_get_new_plane_state(state, primary);
1057 plane_state->rotation = rotation;
1060 ret = __drm_atomic_helper_set_config(mode_set, state);
1065 * __drm_atomic_helper_set_config() sets active when a
1066 * mode is set, unconditionally clear it if we force DPMS off
1069 struct drm_crtc *crtc = mode_set->crtc;
1070 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1072 crtc_state->active = false;
1077 ret = drm_atomic_check_only(state);
1079 ret = drm_atomic_commit(state);
1082 if (ret == -EDEADLK)
1085 drm_atomic_state_put(state);
1087 drm_modeset_drop_locks(&ctx);
1088 drm_modeset_acquire_fini(&ctx);
1093 drm_atomic_state_clear(state);
1094 drm_modeset_backoff(&ctx);
1099 static int drm_client_modeset_commit_legacy(struct drm_client_dev *client)
1101 struct drm_device *dev = client->dev;
1102 struct drm_mode_set *mode_set;
1103 struct drm_plane *plane;
1106 drm_modeset_lock_all(dev);
1107 drm_for_each_plane(plane, dev) {
1108 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
1109 drm_plane_force_disable(plane);
1111 if (plane->rotation_property)
1112 drm_mode_plane_set_obj_prop(plane,
1113 plane->rotation_property,
1117 drm_client_for_each_modeset(mode_set, client) {
1118 struct drm_crtc *crtc = mode_set->crtc;
1120 if (crtc->funcs->cursor_set2) {
1121 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
1124 } else if (crtc->funcs->cursor_set) {
1125 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
1130 ret = drm_mode_set_config_internal(mode_set);
1135 drm_modeset_unlock_all(dev);
1141 * drm_client_modeset_check() - Check modeset configuration
1142 * @client: DRM client
1144 * Check modeset configuration.
1147 * Zero on success or negative error code on failure.
1149 int drm_client_modeset_check(struct drm_client_dev *client)
1153 if (!drm_drv_uses_atomic_modeset(client->dev))
1156 mutex_lock(&client->modeset_mutex);
1157 ret = drm_client_modeset_commit_atomic(client, true, true);
1158 mutex_unlock(&client->modeset_mutex);
1162 EXPORT_SYMBOL(drm_client_modeset_check);
1165 * drm_client_modeset_commit_locked() - Force commit CRTC configuration
1166 * @client: DRM client
1168 * Commit modeset configuration to crtcs without checking if there is a DRM
1169 * master. The assumption is that the caller already holds an internal DRM
1170 * master reference acquired with drm_master_internal_acquire().
1173 * Zero on success or negative error code on failure.
1175 int drm_client_modeset_commit_locked(struct drm_client_dev *client)
1177 struct drm_device *dev = client->dev;
1180 mutex_lock(&client->modeset_mutex);
1181 if (drm_drv_uses_atomic_modeset(dev))
1182 ret = drm_client_modeset_commit_atomic(client, true, false);
1184 ret = drm_client_modeset_commit_legacy(client);
1185 mutex_unlock(&client->modeset_mutex);
1189 EXPORT_SYMBOL(drm_client_modeset_commit_locked);
1192 * drm_client_modeset_commit() - Commit CRTC configuration
1193 * @client: DRM client
1195 * Commit modeset configuration to crtcs.
1198 * Zero on success or negative error code on failure.
1200 int drm_client_modeset_commit(struct drm_client_dev *client)
1202 struct drm_device *dev = client->dev;
1205 if (!drm_master_internal_acquire(dev))
1208 ret = drm_client_modeset_commit_locked(client);
1210 drm_master_internal_release(dev);
1214 EXPORT_SYMBOL(drm_client_modeset_commit);
1216 static void drm_client_modeset_dpms_legacy(struct drm_client_dev *client, int dpms_mode)
1218 struct drm_device *dev = client->dev;
1219 struct drm_connector *connector;
1220 struct drm_mode_set *modeset;
1221 struct drm_modeset_acquire_ctx ctx;
1225 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
1226 drm_client_for_each_modeset(modeset, client) {
1227 if (!modeset->crtc->enabled)
1230 for (j = 0; j < modeset->num_connectors; j++) {
1231 connector = modeset->connectors[j];
1232 connector->funcs->dpms(connector, dpms_mode);
1233 drm_object_property_set_value(&connector->base,
1234 dev->mode_config.dpms_property, dpms_mode);
1237 DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
1241 * drm_client_modeset_dpms() - Set DPMS mode
1242 * @client: DRM client
1245 * Note: For atomic drivers @mode is reduced to on/off.
1248 * Zero on success or negative error code on failure.
1250 int drm_client_modeset_dpms(struct drm_client_dev *client, int mode)
1252 struct drm_device *dev = client->dev;
1255 if (!drm_master_internal_acquire(dev))
1258 mutex_lock(&client->modeset_mutex);
1259 if (drm_drv_uses_atomic_modeset(dev))
1260 ret = drm_client_modeset_commit_atomic(client, mode == DRM_MODE_DPMS_ON, false);
1262 drm_client_modeset_dpms_legacy(client, mode);
1263 mutex_unlock(&client->modeset_mutex);
1265 drm_master_internal_release(dev);
1269 EXPORT_SYMBOL(drm_client_modeset_dpms);
1271 #ifdef CONFIG_DRM_KUNIT_TEST
1272 #include "tests/drm_client_modeset_test.c"