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>
15 #include <drm/drm_atomic.h>
16 #include <drm/drm_client.h>
17 #include <drm/drm_connector.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_device.h>
20 #include <drm/drm_drv.h>
21 #include <drm/drm_encoder.h>
22 #include <drm/drm_print.h>
24 #include "drm_crtc_internal.h"
25 #include "drm_internal.h"
27 #define DRM_CLIENT_MAX_CLONED_CONNECTORS 8
29 struct drm_client_offset {
33 int drm_client_modeset_create(struct drm_client_dev *client)
35 struct drm_device *dev = client->dev;
36 unsigned int num_crtc = dev->mode_config.num_crtc;
37 unsigned int max_connector_count = 1;
38 struct drm_mode_set *modeset;
39 struct drm_crtc *crtc;
42 /* Add terminating zero entry to enable index less iteration */
43 client->modesets = kcalloc(num_crtc + 1, sizeof(*client->modesets), GFP_KERNEL);
44 if (!client->modesets)
47 mutex_init(&client->modeset_mutex);
49 drm_for_each_crtc(crtc, dev)
50 client->modesets[i++].crtc = crtc;
52 /* Cloning is only supported in the single crtc case. */
54 max_connector_count = DRM_CLIENT_MAX_CLONED_CONNECTORS;
56 for (modeset = client->modesets; modeset->crtc; modeset++) {
57 modeset->connectors = kcalloc(max_connector_count,
58 sizeof(*modeset->connectors), GFP_KERNEL);
59 if (!modeset->connectors)
66 drm_client_modeset_free(client);
71 static void drm_client_modeset_release(struct drm_client_dev *client)
73 struct drm_mode_set *modeset;
76 drm_client_for_each_modeset(modeset, client) {
77 drm_mode_destroy(client->dev, modeset->mode);
81 for (i = 0; i < modeset->num_connectors; i++) {
82 drm_connector_put(modeset->connectors[i]);
83 modeset->connectors[i] = NULL;
85 modeset->num_connectors = 0;
89 void drm_client_modeset_free(struct drm_client_dev *client)
91 struct drm_mode_set *modeset;
93 mutex_lock(&client->modeset_mutex);
95 drm_client_modeset_release(client);
97 drm_client_for_each_modeset(modeset, client)
98 kfree(modeset->connectors);
100 mutex_unlock(&client->modeset_mutex);
102 mutex_destroy(&client->modeset_mutex);
103 kfree(client->modesets);
106 static struct drm_mode_set *
107 drm_client_find_modeset(struct drm_client_dev *client, struct drm_crtc *crtc)
109 struct drm_mode_set *modeset;
111 drm_client_for_each_modeset(modeset, client)
112 if (modeset->crtc == crtc)
118 static struct drm_display_mode *
119 drm_connector_get_tiled_mode(struct drm_connector *connector)
121 struct drm_display_mode *mode;
123 list_for_each_entry(mode, &connector->modes, head) {
124 if (mode->hdisplay == connector->tile_h_size &&
125 mode->vdisplay == connector->tile_v_size)
131 static struct drm_display_mode *
132 drm_connector_fallback_non_tiled_mode(struct drm_connector *connector)
134 struct drm_display_mode *mode;
136 list_for_each_entry(mode, &connector->modes, head) {
137 if (mode->hdisplay == connector->tile_h_size &&
138 mode->vdisplay == connector->tile_v_size)
145 static struct drm_display_mode *
146 drm_connector_has_preferred_mode(struct drm_connector *connector, int width, int height)
148 struct drm_display_mode *mode;
150 list_for_each_entry(mode, &connector->modes, head) {
151 if (mode->hdisplay > width ||
152 mode->vdisplay > height)
154 if (mode->type & DRM_MODE_TYPE_PREFERRED)
160 static struct drm_display_mode *
161 drm_connector_pick_cmdline_mode(struct drm_connector *connector)
163 struct drm_cmdline_mode *cmdline_mode;
164 struct drm_display_mode *mode;
165 bool prefer_non_interlace;
167 cmdline_mode = &connector->cmdline_mode;
168 if (cmdline_mode->specified == false)
171 /* attempt to find a matching mode in the list of modes
172 * we have gotten so far, if not add a CVT mode that conforms
174 if (cmdline_mode->rb || cmdline_mode->margins)
177 prefer_non_interlace = !cmdline_mode->interlace;
179 list_for_each_entry(mode, &connector->modes, head) {
180 /* Check (optional) mode name first */
181 if (!strcmp(mode->name, cmdline_mode->name))
184 /* check width/height */
185 if (mode->hdisplay != cmdline_mode->xres ||
186 mode->vdisplay != cmdline_mode->yres)
189 if (cmdline_mode->refresh_specified) {
190 if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
194 if (cmdline_mode->interlace) {
195 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
197 } else if (prefer_non_interlace) {
198 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
204 if (prefer_non_interlace) {
205 prefer_non_interlace = false;
210 mode = drm_mode_create_from_cmdline_mode(connector->dev, cmdline_mode);
212 list_add(&mode->head, &connector->modes);
217 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
221 if (connector->display_info.non_desktop)
225 enable = connector->status == connector_status_connected;
227 enable = connector->status != connector_status_disconnected;
232 static void drm_client_connectors_enabled(struct drm_connector **connectors,
233 unsigned int connector_count,
236 bool any_enabled = false;
237 struct drm_connector *connector;
240 for (i = 0; i < connector_count; i++) {
241 connector = connectors[i];
242 enabled[i] = drm_connector_enabled(connector, true);
243 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
244 connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no");
246 any_enabled |= enabled[i];
252 for (i = 0; i < connector_count; i++)
253 enabled[i] = drm_connector_enabled(connectors[i], false);
256 static bool drm_client_target_cloned(struct drm_device *dev,
257 struct drm_connector **connectors,
258 unsigned int connector_count,
259 struct drm_display_mode **modes,
260 struct drm_client_offset *offsets,
261 bool *enabled, int width, int height)
264 bool can_clone = false;
265 struct drm_display_mode *dmt_mode, *mode;
267 /* only contemplate cloning in the single crtc case */
268 if (dev->mode_config.num_crtc > 1)
272 for (i = 0; i < connector_count; i++) {
277 /* only contemplate cloning if more than one connector is enabled */
281 /* check the command line or if nothing common pick 1024x768 */
283 for (i = 0; i < connector_count; i++) {
286 modes[i] = drm_connector_pick_cmdline_mode(connectors[i]);
291 for (j = 0; j < i; j++) {
294 if (!drm_mode_match(modes[j], modes[i],
295 DRM_MODE_MATCH_TIMINGS |
296 DRM_MODE_MATCH_CLOCK |
297 DRM_MODE_MATCH_FLAGS |
298 DRM_MODE_MATCH_3D_FLAGS))
304 DRM_DEBUG_KMS("can clone using command line\n");
308 /* try and find a 1024x768 mode on each connector */
310 dmt_mode = drm_mode_find_dmt(dev, 1024, 768, 60, false);
312 for (i = 0; i < connector_count; i++) {
316 list_for_each_entry(mode, &connectors[i]->modes, head) {
317 if (drm_mode_match(mode, dmt_mode,
318 DRM_MODE_MATCH_TIMINGS |
319 DRM_MODE_MATCH_CLOCK |
320 DRM_MODE_MATCH_FLAGS |
321 DRM_MODE_MATCH_3D_FLAGS))
329 DRM_DEBUG_KMS("can clone using 1024x768\n");
332 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
336 static int drm_client_get_tile_offsets(struct drm_connector **connectors,
337 unsigned int connector_count,
338 struct drm_display_mode **modes,
339 struct drm_client_offset *offsets,
341 int h_idx, int v_idx)
343 struct drm_connector *connector;
345 int hoffset = 0, voffset = 0;
347 for (i = 0; i < connector_count; i++) {
348 connector = connectors[i];
349 if (!connector->has_tile)
352 if (!modes[i] && (h_idx || v_idx)) {
353 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
357 if (connector->tile_h_loc < h_idx)
358 hoffset += modes[i]->hdisplay;
360 if (connector->tile_v_loc < v_idx)
361 voffset += modes[i]->vdisplay;
363 offsets[idx].x = hoffset;
364 offsets[idx].y = voffset;
365 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
369 static bool drm_client_target_preferred(struct drm_connector **connectors,
370 unsigned int connector_count,
371 struct drm_display_mode **modes,
372 struct drm_client_offset *offsets,
373 bool *enabled, int width, int height)
375 const u64 mask = BIT_ULL(connector_count) - 1;
376 struct drm_connector *connector;
377 u64 conn_configured = 0;
379 int num_tiled_conns = 0;
382 for (i = 0; i < connector_count; i++) {
383 if (connectors[i]->has_tile &&
384 connectors[i]->status == connector_status_connected)
389 for (i = 0; i < connector_count; i++) {
390 connector = connectors[i];
392 if (conn_configured & BIT_ULL(i))
395 if (enabled[i] == false) {
396 conn_configured |= BIT_ULL(i);
400 /* first pass over all the untiled connectors */
401 if (tile_pass == 0 && connector->has_tile)
404 if (tile_pass == 1) {
405 if (connector->tile_h_loc != 0 ||
406 connector->tile_v_loc != 0)
410 if (connector->tile_h_loc != tile_pass - 1 &&
411 connector->tile_v_loc != tile_pass - 1)
412 /* if this tile_pass doesn't cover any of the tiles - keep going */
416 * find the tile offsets for this pass - need to find
417 * all tiles left and above
419 drm_client_get_tile_offsets(connectors, connector_count, modes, offsets, i,
420 connector->tile_h_loc, connector->tile_v_loc);
422 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
425 /* got for command line mode first */
426 modes[i] = drm_connector_pick_cmdline_mode(connector);
428 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
429 connector->base.id, connector->tile_group ? connector->tile_group->id : 0);
430 modes[i] = drm_connector_has_preferred_mode(connector, width, height);
432 /* No preferred modes, pick one off the list */
433 if (!modes[i] && !list_empty(&connector->modes)) {
434 list_for_each_entry(modes[i], &connector->modes, head)
438 * In case of tiled mode if all tiles not present fallback to
439 * first available non tiled mode.
440 * After all tiles are present, try to find the tiled mode
441 * for all and if tiled mode not present due to fbcon size
442 * limitations, use first non tiled mode only for
443 * tile 0,0 and set to no mode for all other tiles.
445 if (connector->has_tile) {
446 if (num_tiled_conns <
447 connector->num_h_tile * connector->num_v_tile ||
448 (connector->tile_h_loc == 0 &&
449 connector->tile_v_loc == 0 &&
450 !drm_connector_get_tiled_mode(connector))) {
451 DRM_DEBUG_KMS("Falling back to non tiled mode on Connector %d\n",
453 modes[i] = drm_connector_fallback_non_tiled_mode(connector);
455 modes[i] = drm_connector_get_tiled_mode(connector);
459 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
461 conn_configured |= BIT_ULL(i);
464 if ((conn_configured & mask) != mask) {
471 static bool connector_has_possible_crtc(struct drm_connector *connector,
472 struct drm_crtc *crtc)
474 struct drm_encoder *encoder;
476 drm_connector_for_each_possible_encoder(connector, encoder) {
477 if (encoder->possible_crtcs & drm_crtc_mask(crtc))
484 static int drm_client_pick_crtcs(struct drm_client_dev *client,
485 struct drm_connector **connectors,
486 unsigned int connector_count,
487 struct drm_crtc **best_crtcs,
488 struct drm_display_mode **modes,
489 int n, int width, int height)
491 struct drm_device *dev = client->dev;
492 struct drm_connector *connector;
493 int my_score, best_score, score;
494 struct drm_crtc **crtcs, *crtc;
495 struct drm_mode_set *modeset;
498 if (n == connector_count)
501 connector = connectors[n];
503 best_crtcs[n] = NULL;
504 best_score = drm_client_pick_crtcs(client, connectors, connector_count,
505 best_crtcs, modes, n + 1, width, height);
506 if (modes[n] == NULL)
509 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
514 if (connector->status == connector_status_connected)
516 if (connector->cmdline_mode.specified)
518 if (drm_connector_has_preferred_mode(connector, width, height))
522 * select a crtc for this connector and then attempt to configure
523 * remaining connectors
525 drm_client_for_each_modeset(modeset, client) {
526 crtc = modeset->crtc;
528 if (!connector_has_possible_crtc(connector, crtc))
531 for (o = 0; o < n; o++)
532 if (best_crtcs[o] == crtc)
536 /* ignore cloning unless only a single crtc */
537 if (dev->mode_config.num_crtc > 1)
540 if (!drm_mode_equal(modes[o], modes[n]))
545 memcpy(crtcs, best_crtcs, n * sizeof(*crtcs));
546 score = my_score + drm_client_pick_crtcs(client, connectors, connector_count,
547 crtcs, modes, n + 1, width, height);
548 if (score > best_score) {
550 memcpy(best_crtcs, crtcs, connector_count * sizeof(*crtcs));
558 /* Try to read the BIOS display configuration and use it for the initial config */
559 static bool drm_client_firmware_config(struct drm_client_dev *client,
560 struct drm_connector **connectors,
561 unsigned int connector_count,
562 struct drm_crtc **crtcs,
563 struct drm_display_mode **modes,
564 struct drm_client_offset *offsets,
565 bool *enabled, int width, int height)
567 const int count = min_t(unsigned int, connector_count, BITS_PER_LONG);
568 unsigned long conn_configured, conn_seq, mask;
569 struct drm_device *dev = client->dev;
572 bool fallback = true, ret = true;
573 int num_connectors_enabled = 0;
574 int num_connectors_detected = 0;
575 int num_tiled_conns = 0;
576 struct drm_modeset_acquire_ctx ctx;
578 if (!drm_drv_uses_atomic_modeset(dev))
581 if (WARN_ON(count <= 0))
584 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
588 drm_modeset_acquire_init(&ctx, 0);
590 while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
591 drm_modeset_backoff(&ctx);
593 memcpy(save_enabled, enabled, count);
594 mask = GENMASK(count - 1, 0);
596 for (i = 0; i < count; i++) {
597 if (connectors[i]->has_tile &&
598 connectors[i]->status == connector_status_connected)
602 conn_seq = conn_configured;
603 for (i = 0; i < count; i++) {
604 struct drm_connector *connector;
605 struct drm_encoder *encoder;
606 struct drm_crtc *new_crtc;
608 connector = connectors[i];
610 if (conn_configured & BIT(i))
613 if (conn_seq == 0 && !connector->has_tile)
616 if (connector->status == connector_status_connected)
617 num_connectors_detected++;
620 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
622 conn_configured |= BIT(i);
626 if (connector->force == DRM_FORCE_OFF) {
627 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
633 encoder = connector->state->best_encoder;
634 if (!encoder || WARN_ON(!connector->state->crtc)) {
635 if (connector->force > DRM_FORCE_OFF)
638 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
641 conn_configured |= BIT(i);
645 num_connectors_enabled++;
647 new_crtc = connector->state->crtc;
650 * Make sure we're not trying to drive multiple connectors
651 * with a single CRTC, since our cloning support may not
654 for (j = 0; j < count; j++) {
655 if (crtcs[j] == new_crtc) {
656 DRM_DEBUG_KMS("fallback: cloned configuration\n");
661 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
664 /* go for command line mode first */
665 modes[i] = drm_connector_pick_cmdline_mode(connector);
667 /* try for preferred next */
669 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
670 connector->name, connector->has_tile);
671 modes[i] = drm_connector_has_preferred_mode(connector, width, height);
674 /* No preferred mode marked by the EDID? Are there any modes? */
675 if (!modes[i] && !list_empty(&connector->modes)) {
676 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
678 modes[i] = list_first_entry(&connector->modes,
679 struct drm_display_mode,
683 /* last resort: use current mode */
686 * IMPORTANT: We want to use the adjusted mode (i.e.
687 * after the panel fitter upscaling) as the initial
688 * config, not the input mode, which is what crtc->mode
689 * usually contains. But since our current
690 * code puts a mode derived from the post-pfit timings
691 * into crtc->mode this works out correctly.
693 * This is crtc->mode and not crtc->state->mode for the
694 * fastboot check to work correctly.
696 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
698 modes[i] = &connector->state->crtc->mode;
701 * In case of tiled modes, if all tiles are not present
702 * then fallback to a non tiled mode.
704 if (connector->has_tile &&
705 num_tiled_conns < connector->num_h_tile * connector->num_v_tile) {
706 DRM_DEBUG_KMS("Falling back to non tiled mode on Connector %d\n",
708 modes[i] = drm_connector_fallback_non_tiled_mode(connector);
712 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
714 connector->state->crtc->base.id,
715 connector->state->crtc->name,
716 modes[i]->hdisplay, modes[i]->vdisplay,
717 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "");
720 conn_configured |= BIT(i);
723 if ((conn_configured & mask) != mask && conn_configured != conn_seq)
727 * If the BIOS didn't enable everything it could, fall back to have the
728 * same user experiencing of lighting up as much as possible like the
729 * fbdev helper library.
731 if (num_connectors_enabled != num_connectors_detected &&
732 num_connectors_enabled < dev->mode_config.num_crtc) {
733 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
734 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
735 num_connectors_detected);
741 DRM_DEBUG_KMS("Not using firmware configuration\n");
742 memcpy(enabled, save_enabled, count);
746 drm_modeset_drop_locks(&ctx);
747 drm_modeset_acquire_fini(&ctx);
754 * drm_client_modeset_probe() - Probe for displays
755 * @client: DRM client
756 * @width: Maximum display mode width (optional)
757 * @height: Maximum display mode height (optional)
759 * This function sets up display pipelines for enabled connectors and stores the
760 * config in the client's modeset array.
763 * Zero on success or negative error code on failure.
765 int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height)
767 struct drm_connector *connector, **connectors = NULL;
768 struct drm_connector_list_iter conn_iter;
769 struct drm_device *dev = client->dev;
770 unsigned int total_modes_count = 0;
771 struct drm_client_offset *offsets;
772 unsigned int connector_count = 0;
773 struct drm_display_mode **modes;
774 struct drm_crtc **crtcs;
781 width = dev->mode_config.max_width;
783 height = dev->mode_config.max_height;
785 drm_connector_list_iter_begin(dev, &conn_iter);
786 drm_client_for_each_connector_iter(connector, &conn_iter) {
787 struct drm_connector **tmp;
789 tmp = krealloc(connectors, (connector_count + 1) * sizeof(*connectors), GFP_KERNEL);
792 goto free_connectors;
796 drm_connector_get(connector);
797 connectors[connector_count++] = connector;
799 drm_connector_list_iter_end(&conn_iter);
801 if (!connector_count)
804 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
805 modes = kcalloc(connector_count, sizeof(*modes), GFP_KERNEL);
806 offsets = kcalloc(connector_count, sizeof(*offsets), GFP_KERNEL);
807 enabled = kcalloc(connector_count, sizeof(bool), GFP_KERNEL);
808 if (!crtcs || !modes || !enabled || !offsets) {
809 DRM_ERROR("Memory allocation failed\n");
814 mutex_lock(&client->modeset_mutex);
816 mutex_lock(&dev->mode_config.mutex);
817 for (i = 0; i < connector_count; i++)
818 total_modes_count += connectors[i]->funcs->fill_modes(connectors[i], width, height);
819 if (!total_modes_count)
820 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
821 drm_client_connectors_enabled(connectors, connector_count, enabled);
823 if (!drm_client_firmware_config(client, connectors, connector_count, crtcs,
824 modes, offsets, enabled, width, height)) {
825 memset(modes, 0, connector_count * sizeof(*modes));
826 memset(crtcs, 0, connector_count * sizeof(*crtcs));
827 memset(offsets, 0, connector_count * sizeof(*offsets));
829 if (!drm_client_target_cloned(dev, connectors, connector_count, modes,
830 offsets, enabled, width, height) &&
831 !drm_client_target_preferred(connectors, connector_count, modes,
832 offsets, enabled, width, height))
833 DRM_ERROR("Unable to find initial modes\n");
835 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
838 drm_client_pick_crtcs(client, connectors, connector_count,
839 crtcs, modes, 0, width, height);
841 mutex_unlock(&dev->mode_config.mutex);
843 drm_client_modeset_release(client);
845 for (i = 0; i < connector_count; i++) {
846 struct drm_display_mode *mode = modes[i];
847 struct drm_crtc *crtc = crtcs[i];
848 struct drm_client_offset *offset = &offsets[i];
851 struct drm_mode_set *modeset = drm_client_find_modeset(client, crtc);
852 struct drm_connector *connector = connectors[i];
854 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
855 mode->name, crtc->base.id, offset->x, offset->y);
857 if (WARN_ON_ONCE(modeset->num_connectors == DRM_CLIENT_MAX_CLONED_CONNECTORS ||
858 (dev->mode_config.num_crtc > 1 && modeset->num_connectors == 1))) {
863 modeset->mode = drm_mode_duplicate(dev, mode);
864 drm_connector_get(connector);
865 modeset->connectors[modeset->num_connectors++] = connector;
866 modeset->x = offset->x;
867 modeset->y = offset->y;
871 mutex_unlock(&client->modeset_mutex);
878 for (i = 0; i < connector_count; i++)
879 drm_connector_put(connectors[i]);
884 EXPORT_SYMBOL(drm_client_modeset_probe);
887 * drm_client_rotation() - Check the initial rotation value
888 * @modeset: DRM modeset
889 * @rotation: Returned rotation value
891 * This function checks if the primary plane in @modeset can hw rotate
892 * to match the rotation needed on its connector.
894 * Note: Currently only 0 and 180 degrees are supported.
897 * True if the plane can do the rotation, false otherwise.
899 bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation)
901 struct drm_connector *connector = modeset->connectors[0];
902 struct drm_plane *plane = modeset->crtc->primary;
903 struct drm_cmdline_mode *cmdline;
907 if (!modeset->num_connectors)
910 switch (connector->display_info.panel_orientation) {
911 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
912 *rotation = DRM_MODE_ROTATE_180;
914 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
915 *rotation = DRM_MODE_ROTATE_90;
917 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
918 *rotation = DRM_MODE_ROTATE_270;
921 *rotation = DRM_MODE_ROTATE_0;
925 * The panel already defined the default rotation
926 * through its orientation. Whatever has been provided
927 * on the command line needs to be added to that.
929 * Unfortunately, the rotations are at different bit
930 * indices, so the math to add them up are not as
931 * trivial as they could.
933 * Reflections on the other hand are pretty trivial to deal with, a
934 * simple XOR between the two handle the addition nicely.
936 cmdline = &connector->cmdline_mode;
937 if (cmdline->specified && cmdline->rotation_reflection) {
938 unsigned int cmdline_rest, panel_rest;
939 unsigned int cmdline_rot, panel_rot;
940 unsigned int sum_rot, sum_rest;
942 panel_rot = ilog2(*rotation & DRM_MODE_ROTATE_MASK);
943 cmdline_rot = ilog2(cmdline->rotation_reflection & DRM_MODE_ROTATE_MASK);
944 sum_rot = (panel_rot + cmdline_rot) % 4;
946 panel_rest = *rotation & ~DRM_MODE_ROTATE_MASK;
947 cmdline_rest = cmdline->rotation_reflection & ~DRM_MODE_ROTATE_MASK;
948 sum_rest = panel_rest ^ cmdline_rest;
950 *rotation = (1 << sum_rot) | sum_rest;
954 * TODO: support 90 / 270 degree hardware rotation,
955 * depending on the hardware this may require the framebuffer
956 * to be in a specific tiling format.
958 if (((*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0 &&
959 (*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_180) ||
960 !plane->rotation_property)
963 for (i = 0; i < plane->rotation_property->num_values; i++)
964 valid_mask |= (1ULL << plane->rotation_property->values[i]);
966 if (!(*rotation & valid_mask))
971 EXPORT_SYMBOL(drm_client_rotation);
973 static int drm_client_modeset_commit_atomic(struct drm_client_dev *client, bool active, bool check)
975 struct drm_device *dev = client->dev;
976 struct drm_plane *plane;
977 struct drm_atomic_state *state;
978 struct drm_modeset_acquire_ctx ctx;
979 struct drm_mode_set *mode_set;
982 drm_modeset_acquire_init(&ctx, 0);
984 state = drm_atomic_state_alloc(dev);
990 state->acquire_ctx = &ctx;
992 drm_for_each_plane(plane, dev) {
993 struct drm_plane_state *plane_state;
995 plane_state = drm_atomic_get_plane_state(state, plane);
996 if (IS_ERR(plane_state)) {
997 ret = PTR_ERR(plane_state);
1001 plane_state->rotation = DRM_MODE_ROTATE_0;
1003 /* disable non-primary: */
1004 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
1007 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1012 drm_client_for_each_modeset(mode_set, client) {
1013 struct drm_plane *primary = mode_set->crtc->primary;
1014 unsigned int rotation;
1016 if (drm_client_rotation(mode_set, &rotation)) {
1017 struct drm_plane_state *plane_state;
1019 /* Cannot fail as we've already gotten the plane state above */
1020 plane_state = drm_atomic_get_new_plane_state(state, primary);
1021 plane_state->rotation = rotation;
1024 ret = __drm_atomic_helper_set_config(mode_set, state);
1029 * __drm_atomic_helper_set_config() sets active when a
1030 * mode is set, unconditionally clear it if we force DPMS off
1033 struct drm_crtc *crtc = mode_set->crtc;
1034 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1036 crtc_state->active = false;
1041 ret = drm_atomic_check_only(state);
1043 ret = drm_atomic_commit(state);
1046 if (ret == -EDEADLK)
1049 drm_atomic_state_put(state);
1051 drm_modeset_drop_locks(&ctx);
1052 drm_modeset_acquire_fini(&ctx);
1057 drm_atomic_state_clear(state);
1058 drm_modeset_backoff(&ctx);
1063 static int drm_client_modeset_commit_legacy(struct drm_client_dev *client)
1065 struct drm_device *dev = client->dev;
1066 struct drm_mode_set *mode_set;
1067 struct drm_plane *plane;
1070 drm_modeset_lock_all(dev);
1071 drm_for_each_plane(plane, dev) {
1072 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
1073 drm_plane_force_disable(plane);
1075 if (plane->rotation_property)
1076 drm_mode_plane_set_obj_prop(plane,
1077 plane->rotation_property,
1081 drm_client_for_each_modeset(mode_set, client) {
1082 struct drm_crtc *crtc = mode_set->crtc;
1084 if (crtc->funcs->cursor_set2) {
1085 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
1088 } else if (crtc->funcs->cursor_set) {
1089 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
1094 ret = drm_mode_set_config_internal(mode_set);
1099 drm_modeset_unlock_all(dev);
1105 * drm_client_modeset_check() - Check modeset configuration
1106 * @client: DRM client
1108 * Check modeset configuration.
1111 * Zero on success or negative error code on failure.
1113 int drm_client_modeset_check(struct drm_client_dev *client)
1117 if (!drm_drv_uses_atomic_modeset(client->dev))
1120 mutex_lock(&client->modeset_mutex);
1121 ret = drm_client_modeset_commit_atomic(client, true, true);
1122 mutex_unlock(&client->modeset_mutex);
1126 EXPORT_SYMBOL(drm_client_modeset_check);
1129 * drm_client_modeset_commit_locked() - Force commit CRTC configuration
1130 * @client: DRM client
1132 * Commit modeset configuration to crtcs without checking if there is a DRM
1133 * master. The assumption is that the caller already holds an internal DRM
1134 * master reference acquired with drm_master_internal_acquire().
1137 * Zero on success or negative error code on failure.
1139 int drm_client_modeset_commit_locked(struct drm_client_dev *client)
1141 struct drm_device *dev = client->dev;
1144 mutex_lock(&client->modeset_mutex);
1145 if (drm_drv_uses_atomic_modeset(dev))
1146 ret = drm_client_modeset_commit_atomic(client, true, false);
1148 ret = drm_client_modeset_commit_legacy(client);
1149 mutex_unlock(&client->modeset_mutex);
1153 EXPORT_SYMBOL(drm_client_modeset_commit_locked);
1156 * drm_client_modeset_commit() - Commit CRTC configuration
1157 * @client: DRM client
1159 * Commit modeset configuration to crtcs.
1162 * Zero on success or negative error code on failure.
1164 int drm_client_modeset_commit(struct drm_client_dev *client)
1166 struct drm_device *dev = client->dev;
1169 if (!drm_master_internal_acquire(dev))
1172 ret = drm_client_modeset_commit_locked(client);
1174 drm_master_internal_release(dev);
1178 EXPORT_SYMBOL(drm_client_modeset_commit);
1180 static void drm_client_modeset_dpms_legacy(struct drm_client_dev *client, int dpms_mode)
1182 struct drm_device *dev = client->dev;
1183 struct drm_connector *connector;
1184 struct drm_mode_set *modeset;
1185 struct drm_modeset_acquire_ctx ctx;
1189 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
1190 drm_client_for_each_modeset(modeset, client) {
1191 if (!modeset->crtc->enabled)
1194 for (j = 0; j < modeset->num_connectors; j++) {
1195 connector = modeset->connectors[j];
1196 connector->funcs->dpms(connector, dpms_mode);
1197 drm_object_property_set_value(&connector->base,
1198 dev->mode_config.dpms_property, dpms_mode);
1201 DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
1205 * drm_client_modeset_dpms() - Set DPMS mode
1206 * @client: DRM client
1209 * Note: For atomic drivers @mode is reduced to on/off.
1212 * Zero on success or negative error code on failure.
1214 int drm_client_modeset_dpms(struct drm_client_dev *client, int mode)
1216 struct drm_device *dev = client->dev;
1219 if (!drm_master_internal_acquire(dev))
1222 mutex_lock(&client->modeset_mutex);
1223 if (drm_drv_uses_atomic_modeset(dev))
1224 ret = drm_client_modeset_commit_atomic(client, mode == DRM_MODE_DPMS_ON, false);
1226 drm_client_modeset_dpms_legacy(client, mode);
1227 mutex_unlock(&client->modeset_mutex);
1229 drm_master_internal_release(dev);
1233 EXPORT_SYMBOL(drm_client_modeset_dpms);