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 "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_has_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_pick_cmdline_mode(struct drm_connector *connector)
164 struct drm_cmdline_mode *cmdline_mode;
165 struct drm_display_mode *mode;
166 bool prefer_non_interlace;
169 * Find a user-defined mode. If the user gave us a valid
170 * mode on the kernel command line, it will show up in this
174 list_for_each_entry(mode, &connector->modes, head) {
175 if (mode->type & DRM_MODE_TYPE_USERDEF)
179 cmdline_mode = &connector->cmdline_mode;
180 if (cmdline_mode->specified == false)
184 * Attempt to find a matching mode in the list of modes we
185 * have gotten so far.
188 prefer_non_interlace = !cmdline_mode->interlace;
190 list_for_each_entry(mode, &connector->modes, head) {
191 /* check width/height */
192 if (mode->hdisplay != cmdline_mode->xres ||
193 mode->vdisplay != cmdline_mode->yres)
196 if (cmdline_mode->refresh_specified) {
197 if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
201 if (cmdline_mode->interlace) {
202 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
204 } else if (prefer_non_interlace) {
205 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
211 if (prefer_non_interlace) {
212 prefer_non_interlace = false;
219 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
223 if (connector->display_info.non_desktop)
227 enable = connector->status == connector_status_connected;
229 enable = connector->status != connector_status_disconnected;
234 static void drm_client_connectors_enabled(struct drm_connector **connectors,
235 unsigned int connector_count,
238 bool any_enabled = false;
239 struct drm_connector *connector;
242 for (i = 0; i < connector_count; i++) {
243 connector = connectors[i];
244 enabled[i] = drm_connector_enabled(connector, true);
245 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
246 connector->display_info.non_desktop ? "non desktop" : str_yes_no(enabled[i]));
248 any_enabled |= enabled[i];
254 for (i = 0; i < connector_count; i++)
255 enabled[i] = drm_connector_enabled(connectors[i], false);
258 static bool drm_client_target_cloned(struct drm_device *dev,
259 struct drm_connector **connectors,
260 unsigned int connector_count,
261 struct drm_display_mode **modes,
262 struct drm_client_offset *offsets,
263 bool *enabled, int width, int height)
266 bool can_clone = false;
267 struct drm_display_mode *dmt_mode, *mode;
269 /* only contemplate cloning in the single crtc case */
270 if (dev->mode_config.num_crtc > 1)
274 for (i = 0; i < connector_count; i++) {
279 /* only contemplate cloning if more than one connector is enabled */
283 /* check the command line or if nothing common pick 1024x768 */
285 for (i = 0; i < connector_count; i++) {
288 modes[i] = drm_connector_pick_cmdline_mode(connectors[i]);
293 for (j = 0; j < i; j++) {
296 if (!drm_mode_match(modes[j], modes[i],
297 DRM_MODE_MATCH_TIMINGS |
298 DRM_MODE_MATCH_CLOCK |
299 DRM_MODE_MATCH_FLAGS |
300 DRM_MODE_MATCH_3D_FLAGS))
306 DRM_DEBUG_KMS("can clone using command line\n");
310 /* try and find a 1024x768 mode on each connector */
312 dmt_mode = drm_mode_find_dmt(dev, 1024, 768, 60, false);
317 for (i = 0; i < connector_count; i++) {
321 list_for_each_entry(mode, &connectors[i]->modes, head) {
322 if (drm_mode_match(mode, dmt_mode,
323 DRM_MODE_MATCH_TIMINGS |
324 DRM_MODE_MATCH_CLOCK |
325 DRM_MODE_MATCH_FLAGS |
326 DRM_MODE_MATCH_3D_FLAGS))
335 DRM_DEBUG_KMS("can clone using 1024x768\n");
339 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
343 static int drm_client_get_tile_offsets(struct drm_connector **connectors,
344 unsigned int connector_count,
345 struct drm_display_mode **modes,
346 struct drm_client_offset *offsets,
348 int h_idx, int v_idx)
350 struct drm_connector *connector;
352 int hoffset = 0, voffset = 0;
354 for (i = 0; i < connector_count; i++) {
355 connector = connectors[i];
356 if (!connector->has_tile)
359 if (!modes[i] && (h_idx || v_idx)) {
360 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
364 if (connector->tile_h_loc < h_idx)
365 hoffset += modes[i]->hdisplay;
367 if (connector->tile_v_loc < v_idx)
368 voffset += modes[i]->vdisplay;
370 offsets[idx].x = hoffset;
371 offsets[idx].y = voffset;
372 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
376 static bool drm_client_target_preferred(struct drm_connector **connectors,
377 unsigned int connector_count,
378 struct drm_display_mode **modes,
379 struct drm_client_offset *offsets,
380 bool *enabled, int width, int height)
382 const u64 mask = BIT_ULL(connector_count) - 1;
383 struct drm_connector *connector;
384 u64 conn_configured = 0;
386 int num_tiled_conns = 0;
389 for (i = 0; i < connector_count; i++) {
390 if (connectors[i]->has_tile &&
391 connectors[i]->status == connector_status_connected)
396 for (i = 0; i < connector_count; i++) {
397 connector = connectors[i];
399 if (conn_configured & BIT_ULL(i))
402 if (enabled[i] == false) {
403 conn_configured |= BIT_ULL(i);
407 /* first pass over all the untiled connectors */
408 if (tile_pass == 0 && connector->has_tile)
411 if (tile_pass == 1) {
412 if (connector->tile_h_loc != 0 ||
413 connector->tile_v_loc != 0)
417 if (connector->tile_h_loc != tile_pass - 1 &&
418 connector->tile_v_loc != tile_pass - 1)
419 /* if this tile_pass doesn't cover any of the tiles - keep going */
423 * find the tile offsets for this pass - need to find
424 * all tiles left and above
426 drm_client_get_tile_offsets(connectors, connector_count, modes, offsets, i,
427 connector->tile_h_loc, connector->tile_v_loc);
429 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
432 /* got for command line mode first */
433 modes[i] = drm_connector_pick_cmdline_mode(connector);
435 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
436 connector->base.id, connector->tile_group ? connector->tile_group->id : 0);
437 modes[i] = drm_connector_has_preferred_mode(connector, width, height);
439 /* No preferred modes, pick one off the list */
440 if (!modes[i] && !list_empty(&connector->modes)) {
441 list_for_each_entry(modes[i], &connector->modes, head)
445 * In case of tiled mode if all tiles not present fallback to
446 * first available non tiled mode.
447 * After all tiles are present, try to find the tiled mode
448 * for all and if tiled mode not present due to fbcon size
449 * limitations, use first non tiled mode only for
450 * tile 0,0 and set to no mode for all other tiles.
452 if (connector->has_tile) {
453 if (num_tiled_conns <
454 connector->num_h_tile * connector->num_v_tile ||
455 (connector->tile_h_loc == 0 &&
456 connector->tile_v_loc == 0 &&
457 !drm_connector_get_tiled_mode(connector))) {
458 DRM_DEBUG_KMS("Falling back to non tiled mode on Connector %d\n",
460 modes[i] = drm_connector_fallback_non_tiled_mode(connector);
462 modes[i] = drm_connector_get_tiled_mode(connector);
466 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
468 conn_configured |= BIT_ULL(i);
471 if ((conn_configured & mask) != mask) {
478 static bool connector_has_possible_crtc(struct drm_connector *connector,
479 struct drm_crtc *crtc)
481 struct drm_encoder *encoder;
483 drm_connector_for_each_possible_encoder(connector, encoder) {
484 if (encoder->possible_crtcs & drm_crtc_mask(crtc))
491 static int drm_client_pick_crtcs(struct drm_client_dev *client,
492 struct drm_connector **connectors,
493 unsigned int connector_count,
494 struct drm_crtc **best_crtcs,
495 struct drm_display_mode **modes,
496 int n, int width, int height)
498 struct drm_device *dev = client->dev;
499 struct drm_connector *connector;
500 int my_score, best_score, score;
501 struct drm_crtc **crtcs, *crtc;
502 struct drm_mode_set *modeset;
505 if (n == connector_count)
508 connector = connectors[n];
510 best_crtcs[n] = NULL;
511 best_score = drm_client_pick_crtcs(client, connectors, connector_count,
512 best_crtcs, modes, n + 1, width, height);
513 if (modes[n] == NULL)
516 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
521 if (connector->status == connector_status_connected)
523 if (connector->cmdline_mode.specified)
525 if (drm_connector_has_preferred_mode(connector, width, height))
529 * select a crtc for this connector and then attempt to configure
530 * remaining connectors
532 drm_client_for_each_modeset(modeset, client) {
533 crtc = modeset->crtc;
535 if (!connector_has_possible_crtc(connector, crtc))
538 for (o = 0; o < n; o++)
539 if (best_crtcs[o] == crtc)
543 /* ignore cloning unless only a single crtc */
544 if (dev->mode_config.num_crtc > 1)
547 if (!drm_mode_equal(modes[o], modes[n]))
552 memcpy(crtcs, best_crtcs, n * sizeof(*crtcs));
553 score = my_score + drm_client_pick_crtcs(client, connectors, connector_count,
554 crtcs, modes, n + 1, width, height);
555 if (score > best_score) {
557 memcpy(best_crtcs, crtcs, connector_count * sizeof(*crtcs));
565 /* Try to read the BIOS display configuration and use it for the initial config */
566 static bool drm_client_firmware_config(struct drm_client_dev *client,
567 struct drm_connector **connectors,
568 unsigned int connector_count,
569 struct drm_crtc **crtcs,
570 struct drm_display_mode **modes,
571 struct drm_client_offset *offsets,
572 bool *enabled, int width, int height)
574 const int count = min_t(unsigned int, connector_count, BITS_PER_LONG);
575 unsigned long conn_configured, conn_seq, mask;
576 struct drm_device *dev = client->dev;
579 bool fallback = true, ret = true;
580 int num_connectors_enabled = 0;
581 int num_connectors_detected = 0;
582 int num_tiled_conns = 0;
583 struct drm_modeset_acquire_ctx ctx;
585 if (!drm_drv_uses_atomic_modeset(dev))
588 if (WARN_ON(count <= 0))
591 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
595 drm_modeset_acquire_init(&ctx, 0);
597 while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
598 drm_modeset_backoff(&ctx);
600 memcpy(save_enabled, enabled, count);
601 mask = GENMASK(count - 1, 0);
603 for (i = 0; i < count; i++) {
604 if (connectors[i]->has_tile &&
605 connectors[i]->status == connector_status_connected)
609 conn_seq = conn_configured;
610 for (i = 0; i < count; i++) {
611 struct drm_connector *connector;
612 struct drm_encoder *encoder;
613 struct drm_crtc *new_crtc;
615 connector = connectors[i];
617 if (conn_configured & BIT(i))
620 if (conn_seq == 0 && !connector->has_tile)
623 if (connector->status == connector_status_connected)
624 num_connectors_detected++;
627 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
629 conn_configured |= BIT(i);
633 if (connector->force == DRM_FORCE_OFF) {
634 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
640 encoder = connector->state->best_encoder;
641 if (!encoder || WARN_ON(!connector->state->crtc)) {
642 if (connector->force > DRM_FORCE_OFF)
645 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
648 conn_configured |= BIT(i);
652 num_connectors_enabled++;
654 new_crtc = connector->state->crtc;
657 * Make sure we're not trying to drive multiple connectors
658 * with a single CRTC, since our cloning support may not
661 for (j = 0; j < count; j++) {
662 if (crtcs[j] == new_crtc) {
663 DRM_DEBUG_KMS("fallback: cloned configuration\n");
668 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
671 /* go for command line mode first */
672 modes[i] = drm_connector_pick_cmdline_mode(connector);
674 /* try for preferred next */
676 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
677 connector->name, connector->has_tile);
678 modes[i] = drm_connector_has_preferred_mode(connector, width, height);
681 /* No preferred mode marked by the EDID? Are there any modes? */
682 if (!modes[i] && !list_empty(&connector->modes)) {
683 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
685 modes[i] = list_first_entry(&connector->modes,
686 struct drm_display_mode,
690 /* last resort: use current mode */
693 * IMPORTANT: We want to use the adjusted mode (i.e.
694 * after the panel fitter upscaling) as the initial
695 * config, not the input mode, which is what crtc->mode
696 * usually contains. But since our current
697 * code puts a mode derived from the post-pfit timings
698 * into crtc->mode this works out correctly.
700 * This is crtc->mode and not crtc->state->mode for the
701 * fastboot check to work correctly.
703 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
705 modes[i] = &connector->state->crtc->mode;
708 * In case of tiled modes, if all tiles are not present
709 * then fallback to a non tiled mode.
711 if (connector->has_tile &&
712 num_tiled_conns < connector->num_h_tile * connector->num_v_tile) {
713 DRM_DEBUG_KMS("Falling back to non tiled mode on Connector %d\n",
715 modes[i] = drm_connector_fallback_non_tiled_mode(connector);
719 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
721 connector->state->crtc->base.id,
722 connector->state->crtc->name,
723 modes[i]->hdisplay, modes[i]->vdisplay,
724 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "");
727 conn_configured |= BIT(i);
730 if ((conn_configured & mask) != mask && conn_configured != conn_seq)
734 * If the BIOS didn't enable everything it could, fall back to have the
735 * same user experiencing of lighting up as much as possible like the
736 * fbdev helper library.
738 if (num_connectors_enabled != num_connectors_detected &&
739 num_connectors_enabled < dev->mode_config.num_crtc) {
740 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
741 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
742 num_connectors_detected);
748 DRM_DEBUG_KMS("Not using firmware configuration\n");
749 memcpy(enabled, save_enabled, count);
753 drm_modeset_drop_locks(&ctx);
754 drm_modeset_acquire_fini(&ctx);
761 * drm_client_modeset_probe() - Probe for displays
762 * @client: DRM client
763 * @width: Maximum display mode width (optional)
764 * @height: Maximum display mode height (optional)
766 * This function sets up display pipelines for enabled connectors and stores the
767 * config in the client's modeset array.
770 * Zero on success or negative error code on failure.
772 int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height)
774 struct drm_connector *connector, **connectors = NULL;
775 struct drm_connector_list_iter conn_iter;
776 struct drm_device *dev = client->dev;
777 unsigned int total_modes_count = 0;
778 struct drm_client_offset *offsets;
779 unsigned int connector_count = 0;
780 struct drm_display_mode **modes;
781 struct drm_crtc **crtcs;
788 width = dev->mode_config.max_width;
790 height = dev->mode_config.max_height;
792 drm_connector_list_iter_begin(dev, &conn_iter);
793 drm_client_for_each_connector_iter(connector, &conn_iter) {
794 struct drm_connector **tmp;
796 tmp = krealloc(connectors, (connector_count + 1) * sizeof(*connectors), GFP_KERNEL);
799 goto free_connectors;
803 drm_connector_get(connector);
804 connectors[connector_count++] = connector;
806 drm_connector_list_iter_end(&conn_iter);
808 if (!connector_count)
811 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
812 modes = kcalloc(connector_count, sizeof(*modes), GFP_KERNEL);
813 offsets = kcalloc(connector_count, sizeof(*offsets), GFP_KERNEL);
814 enabled = kcalloc(connector_count, sizeof(bool), GFP_KERNEL);
815 if (!crtcs || !modes || !enabled || !offsets) {
816 DRM_ERROR("Memory allocation failed\n");
821 mutex_lock(&client->modeset_mutex);
823 mutex_lock(&dev->mode_config.mutex);
824 for (i = 0; i < connector_count; i++)
825 total_modes_count += connectors[i]->funcs->fill_modes(connectors[i], width, height);
826 if (!total_modes_count)
827 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
828 drm_client_connectors_enabled(connectors, connector_count, enabled);
830 if (!drm_client_firmware_config(client, connectors, connector_count, crtcs,
831 modes, offsets, enabled, width, height)) {
832 memset(modes, 0, connector_count * sizeof(*modes));
833 memset(crtcs, 0, connector_count * sizeof(*crtcs));
834 memset(offsets, 0, connector_count * sizeof(*offsets));
836 if (!drm_client_target_cloned(dev, connectors, connector_count, modes,
837 offsets, enabled, width, height) &&
838 !drm_client_target_preferred(connectors, connector_count, modes,
839 offsets, enabled, width, height))
840 DRM_ERROR("Unable to find initial modes\n");
842 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
845 drm_client_pick_crtcs(client, connectors, connector_count,
846 crtcs, modes, 0, width, height);
848 mutex_unlock(&dev->mode_config.mutex);
850 drm_client_modeset_release(client);
852 for (i = 0; i < connector_count; i++) {
853 struct drm_display_mode *mode = modes[i];
854 struct drm_crtc *crtc = crtcs[i];
855 struct drm_client_offset *offset = &offsets[i];
858 struct drm_mode_set *modeset = drm_client_find_modeset(client, crtc);
859 struct drm_connector *connector = connectors[i];
861 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
862 mode->name, crtc->base.id, offset->x, offset->y);
864 if (WARN_ON_ONCE(modeset->num_connectors == DRM_CLIENT_MAX_CLONED_CONNECTORS ||
865 (dev->mode_config.num_crtc > 1 && modeset->num_connectors == 1))) {
870 kfree(modeset->mode);
871 modeset->mode = drm_mode_duplicate(dev, mode);
872 drm_connector_get(connector);
873 modeset->connectors[modeset->num_connectors++] = connector;
874 modeset->x = offset->x;
875 modeset->y = offset->y;
879 mutex_unlock(&client->modeset_mutex);
886 for (i = 0; i < connector_count; i++)
887 drm_connector_put(connectors[i]);
892 EXPORT_SYMBOL(drm_client_modeset_probe);
895 * drm_client_rotation() - Check the initial rotation value
896 * @modeset: DRM modeset
897 * @rotation: Returned rotation value
899 * This function checks if the primary plane in @modeset can hw rotate
900 * to match the rotation needed on its connector.
902 * Note: Currently only 0 and 180 degrees are supported.
905 * True if the plane can do the rotation, false otherwise.
907 bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation)
909 struct drm_connector *connector = modeset->connectors[0];
910 struct drm_plane *plane = modeset->crtc->primary;
911 struct drm_cmdline_mode *cmdline;
915 if (!modeset->num_connectors)
918 switch (connector->display_info.panel_orientation) {
919 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
920 *rotation = DRM_MODE_ROTATE_180;
922 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
923 *rotation = DRM_MODE_ROTATE_90;
925 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
926 *rotation = DRM_MODE_ROTATE_270;
929 *rotation = DRM_MODE_ROTATE_0;
933 * The panel already defined the default rotation
934 * through its orientation. Whatever has been provided
935 * on the command line needs to be added to that.
937 * Unfortunately, the rotations are at different bit
938 * indices, so the math to add them up are not as
939 * trivial as they could.
941 * Reflections on the other hand are pretty trivial to deal with, a
942 * simple XOR between the two handle the addition nicely.
944 cmdline = &connector->cmdline_mode;
945 if (cmdline->specified && cmdline->rotation_reflection) {
946 unsigned int cmdline_rest, panel_rest;
947 unsigned int cmdline_rot, panel_rot;
948 unsigned int sum_rot, sum_rest;
950 panel_rot = ilog2(*rotation & DRM_MODE_ROTATE_MASK);
951 cmdline_rot = ilog2(cmdline->rotation_reflection & DRM_MODE_ROTATE_MASK);
952 sum_rot = (panel_rot + cmdline_rot) % 4;
954 panel_rest = *rotation & ~DRM_MODE_ROTATE_MASK;
955 cmdline_rest = cmdline->rotation_reflection & ~DRM_MODE_ROTATE_MASK;
956 sum_rest = panel_rest ^ cmdline_rest;
958 *rotation = (1 << sum_rot) | sum_rest;
962 * TODO: support 90 / 270 degree hardware rotation,
963 * depending on the hardware this may require the framebuffer
964 * to be in a specific tiling format.
966 if (((*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0 &&
967 (*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_180) ||
968 !plane->rotation_property)
971 for (i = 0; i < plane->rotation_property->num_values; i++)
972 valid_mask |= (1ULL << plane->rotation_property->values[i]);
974 if (!(*rotation & valid_mask))
979 EXPORT_SYMBOL(drm_client_rotation);
981 static int drm_client_modeset_commit_atomic(struct drm_client_dev *client, bool active, bool check)
983 struct drm_device *dev = client->dev;
984 struct drm_plane *plane;
985 struct drm_atomic_state *state;
986 struct drm_modeset_acquire_ctx ctx;
987 struct drm_mode_set *mode_set;
990 drm_modeset_acquire_init(&ctx, 0);
992 state = drm_atomic_state_alloc(dev);
998 state->acquire_ctx = &ctx;
1000 drm_for_each_plane(plane, dev) {
1001 struct drm_plane_state *plane_state;
1003 plane_state = drm_atomic_get_plane_state(state, plane);
1004 if (IS_ERR(plane_state)) {
1005 ret = PTR_ERR(plane_state);
1009 plane_state->rotation = DRM_MODE_ROTATE_0;
1011 /* disable non-primary: */
1012 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
1015 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1020 drm_client_for_each_modeset(mode_set, client) {
1021 struct drm_plane *primary = mode_set->crtc->primary;
1022 unsigned int rotation;
1024 if (drm_client_rotation(mode_set, &rotation)) {
1025 struct drm_plane_state *plane_state;
1027 /* Cannot fail as we've already gotten the plane state above */
1028 plane_state = drm_atomic_get_new_plane_state(state, primary);
1029 plane_state->rotation = rotation;
1032 ret = __drm_atomic_helper_set_config(mode_set, state);
1037 * __drm_atomic_helper_set_config() sets active when a
1038 * mode is set, unconditionally clear it if we force DPMS off
1041 struct drm_crtc *crtc = mode_set->crtc;
1042 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1044 crtc_state->active = false;
1049 ret = drm_atomic_check_only(state);
1051 ret = drm_atomic_commit(state);
1054 if (ret == -EDEADLK)
1057 drm_atomic_state_put(state);
1059 drm_modeset_drop_locks(&ctx);
1060 drm_modeset_acquire_fini(&ctx);
1065 drm_atomic_state_clear(state);
1066 drm_modeset_backoff(&ctx);
1071 static int drm_client_modeset_commit_legacy(struct drm_client_dev *client)
1073 struct drm_device *dev = client->dev;
1074 struct drm_mode_set *mode_set;
1075 struct drm_plane *plane;
1078 drm_modeset_lock_all(dev);
1079 drm_for_each_plane(plane, dev) {
1080 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
1081 drm_plane_force_disable(plane);
1083 if (plane->rotation_property)
1084 drm_mode_plane_set_obj_prop(plane,
1085 plane->rotation_property,
1089 drm_client_for_each_modeset(mode_set, client) {
1090 struct drm_crtc *crtc = mode_set->crtc;
1092 if (crtc->funcs->cursor_set2) {
1093 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
1096 } else if (crtc->funcs->cursor_set) {
1097 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
1102 ret = drm_mode_set_config_internal(mode_set);
1107 drm_modeset_unlock_all(dev);
1113 * drm_client_modeset_check() - Check modeset configuration
1114 * @client: DRM client
1116 * Check modeset configuration.
1119 * Zero on success or negative error code on failure.
1121 int drm_client_modeset_check(struct drm_client_dev *client)
1125 if (!drm_drv_uses_atomic_modeset(client->dev))
1128 mutex_lock(&client->modeset_mutex);
1129 ret = drm_client_modeset_commit_atomic(client, true, true);
1130 mutex_unlock(&client->modeset_mutex);
1134 EXPORT_SYMBOL(drm_client_modeset_check);
1137 * drm_client_modeset_commit_locked() - Force commit CRTC configuration
1138 * @client: DRM client
1140 * Commit modeset configuration to crtcs without checking if there is a DRM
1141 * master. The assumption is that the caller already holds an internal DRM
1142 * master reference acquired with drm_master_internal_acquire().
1145 * Zero on success or negative error code on failure.
1147 int drm_client_modeset_commit_locked(struct drm_client_dev *client)
1149 struct drm_device *dev = client->dev;
1152 mutex_lock(&client->modeset_mutex);
1153 if (drm_drv_uses_atomic_modeset(dev))
1154 ret = drm_client_modeset_commit_atomic(client, true, false);
1156 ret = drm_client_modeset_commit_legacy(client);
1157 mutex_unlock(&client->modeset_mutex);
1161 EXPORT_SYMBOL(drm_client_modeset_commit_locked);
1164 * drm_client_modeset_commit() - Commit CRTC configuration
1165 * @client: DRM client
1167 * Commit modeset configuration to crtcs.
1170 * Zero on success or negative error code on failure.
1172 int drm_client_modeset_commit(struct drm_client_dev *client)
1174 struct drm_device *dev = client->dev;
1177 if (!drm_master_internal_acquire(dev))
1180 ret = drm_client_modeset_commit_locked(client);
1182 drm_master_internal_release(dev);
1186 EXPORT_SYMBOL(drm_client_modeset_commit);
1188 static void drm_client_modeset_dpms_legacy(struct drm_client_dev *client, int dpms_mode)
1190 struct drm_device *dev = client->dev;
1191 struct drm_connector *connector;
1192 struct drm_mode_set *modeset;
1193 struct drm_modeset_acquire_ctx ctx;
1197 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
1198 drm_client_for_each_modeset(modeset, client) {
1199 if (!modeset->crtc->enabled)
1202 for (j = 0; j < modeset->num_connectors; j++) {
1203 connector = modeset->connectors[j];
1204 connector->funcs->dpms(connector, dpms_mode);
1205 drm_object_property_set_value(&connector->base,
1206 dev->mode_config.dpms_property, dpms_mode);
1209 DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
1213 * drm_client_modeset_dpms() - Set DPMS mode
1214 * @client: DRM client
1217 * Note: For atomic drivers @mode is reduced to on/off.
1220 * Zero on success or negative error code on failure.
1222 int drm_client_modeset_dpms(struct drm_client_dev *client, int mode)
1224 struct drm_device *dev = client->dev;
1227 if (!drm_master_internal_acquire(dev))
1230 mutex_lock(&client->modeset_mutex);
1231 if (drm_drv_uses_atomic_modeset(dev))
1232 ret = drm_client_modeset_commit_atomic(client, mode == DRM_MODE_DPMS_ON, false);
1234 drm_client_modeset_dpms_legacy(client, mode);
1235 mutex_unlock(&client->modeset_mutex);
1237 drm_master_internal_release(dev);
1241 EXPORT_SYMBOL(drm_client_modeset_dpms);
1243 #ifdef CONFIG_DRM_KUNIT_TEST
1244 #include "tests/drm_client_modeset_test.c"