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_encoder.h>
23 #include <drm/drm_print.h>
25 #include "drm_crtc_internal.h"
26 #include "drm_internal.h"
28 #define DRM_CLIENT_MAX_CLONED_CONNECTORS 8
30 struct drm_client_offset {
34 int drm_client_modeset_create(struct drm_client_dev *client)
36 struct drm_device *dev = client->dev;
37 unsigned int num_crtc = dev->mode_config.num_crtc;
38 unsigned int max_connector_count = 1;
39 struct drm_mode_set *modeset;
40 struct drm_crtc *crtc;
43 /* Add terminating zero entry to enable index less iteration */
44 client->modesets = kcalloc(num_crtc + 1, sizeof(*client->modesets), GFP_KERNEL);
45 if (!client->modesets)
48 mutex_init(&client->modeset_mutex);
50 drm_for_each_crtc(crtc, dev)
51 client->modesets[i++].crtc = crtc;
53 /* Cloning is only supported in the single crtc case. */
55 max_connector_count = DRM_CLIENT_MAX_CLONED_CONNECTORS;
57 for (modeset = client->modesets; modeset->crtc; modeset++) {
58 modeset->connectors = kcalloc(max_connector_count,
59 sizeof(*modeset->connectors), GFP_KERNEL);
60 if (!modeset->connectors)
67 drm_client_modeset_free(client);
72 static void drm_client_modeset_release(struct drm_client_dev *client)
74 struct drm_mode_set *modeset;
77 drm_client_for_each_modeset(modeset, client) {
78 drm_mode_destroy(client->dev, modeset->mode);
82 for (i = 0; i < modeset->num_connectors; i++) {
83 drm_connector_put(modeset->connectors[i]);
84 modeset->connectors[i] = NULL;
86 modeset->num_connectors = 0;
90 void drm_client_modeset_free(struct drm_client_dev *client)
92 struct drm_mode_set *modeset;
94 mutex_lock(&client->modeset_mutex);
96 drm_client_modeset_release(client);
98 drm_client_for_each_modeset(modeset, client)
99 kfree(modeset->connectors);
101 mutex_unlock(&client->modeset_mutex);
103 mutex_destroy(&client->modeset_mutex);
104 kfree(client->modesets);
107 static struct drm_mode_set *
108 drm_client_find_modeset(struct drm_client_dev *client, struct drm_crtc *crtc)
110 struct drm_mode_set *modeset;
112 drm_client_for_each_modeset(modeset, client)
113 if (modeset->crtc == crtc)
119 static struct drm_display_mode *
120 drm_connector_get_tiled_mode(struct drm_connector *connector)
122 struct drm_display_mode *mode;
124 list_for_each_entry(mode, &connector->modes, head) {
125 if (mode->hdisplay == connector->tile_h_size &&
126 mode->vdisplay == connector->tile_v_size)
132 static struct drm_display_mode *
133 drm_connector_fallback_non_tiled_mode(struct drm_connector *connector)
135 struct drm_display_mode *mode;
137 list_for_each_entry(mode, &connector->modes, head) {
138 if (mode->hdisplay == connector->tile_h_size &&
139 mode->vdisplay == connector->tile_v_size)
146 static struct drm_display_mode *
147 drm_connector_has_preferred_mode(struct drm_connector *connector, int width, int height)
149 struct drm_display_mode *mode;
151 list_for_each_entry(mode, &connector->modes, head) {
152 if (mode->hdisplay > width ||
153 mode->vdisplay > height)
155 if (mode->type & DRM_MODE_TYPE_PREFERRED)
161 static struct drm_display_mode *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;
168 * Find a user-defined mode. If the user gave us a valid
169 * mode on the kernel command line, it will show up in this
173 list_for_each_entry(mode, &connector->modes, head) {
174 if (mode->type & DRM_MODE_TYPE_USERDEF)
178 cmdline_mode = &connector->cmdline_mode;
179 if (cmdline_mode->specified == false)
183 * Attempt to find a matching mode in the list of modes we
184 * have gotten so far.
187 prefer_non_interlace = !cmdline_mode->interlace;
189 list_for_each_entry(mode, &connector->modes, head) {
190 /* Check (optional) mode name first */
191 if (!strcmp(mode->name, cmdline_mode->name))
194 /* check width/height */
195 if (mode->hdisplay != cmdline_mode->xres ||
196 mode->vdisplay != cmdline_mode->yres)
199 if (cmdline_mode->refresh_specified) {
200 if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
204 if (cmdline_mode->interlace) {
205 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
207 } else if (prefer_non_interlace) {
208 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
214 if (prefer_non_interlace) {
215 prefer_non_interlace = false;
222 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
226 if (connector->display_info.non_desktop)
230 enable = connector->status == connector_status_connected;
232 enable = connector->status != connector_status_disconnected;
237 static void drm_client_connectors_enabled(struct drm_connector **connectors,
238 unsigned int connector_count,
241 bool any_enabled = false;
242 struct drm_connector *connector;
245 for (i = 0; i < connector_count; i++) {
246 connector = connectors[i];
247 enabled[i] = drm_connector_enabled(connector, true);
248 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
249 connector->display_info.non_desktop ? "non desktop" : str_yes_no(enabled[i]));
251 any_enabled |= enabled[i];
257 for (i = 0; i < connector_count; i++)
258 enabled[i] = drm_connector_enabled(connectors[i], false);
261 static bool drm_client_target_cloned(struct drm_device *dev,
262 struct drm_connector **connectors,
263 unsigned int connector_count,
264 struct drm_display_mode **modes,
265 struct drm_client_offset *offsets,
266 bool *enabled, int width, int height)
269 bool can_clone = false;
270 struct drm_display_mode *dmt_mode, *mode;
272 /* only contemplate cloning in the single crtc case */
273 if (dev->mode_config.num_crtc > 1)
277 for (i = 0; i < connector_count; i++) {
282 /* only contemplate cloning if more than one connector is enabled */
286 /* check the command line or if nothing common pick 1024x768 */
288 for (i = 0; i < connector_count; i++) {
291 modes[i] = drm_connector_pick_cmdline_mode(connectors[i]);
296 for (j = 0; j < i; j++) {
299 if (!drm_mode_match(modes[j], modes[i],
300 DRM_MODE_MATCH_TIMINGS |
301 DRM_MODE_MATCH_CLOCK |
302 DRM_MODE_MATCH_FLAGS |
303 DRM_MODE_MATCH_3D_FLAGS))
309 DRM_DEBUG_KMS("can clone using command line\n");
313 /* try and find a 1024x768 mode on each connector */
315 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))
334 DRM_DEBUG_KMS("can clone using 1024x768\n");
337 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
341 static int drm_client_get_tile_offsets(struct drm_connector **connectors,
342 unsigned int connector_count,
343 struct drm_display_mode **modes,
344 struct drm_client_offset *offsets,
346 int h_idx, int v_idx)
348 struct drm_connector *connector;
350 int hoffset = 0, voffset = 0;
352 for (i = 0; i < connector_count; i++) {
353 connector = connectors[i];
354 if (!connector->has_tile)
357 if (!modes[i] && (h_idx || v_idx)) {
358 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
362 if (connector->tile_h_loc < h_idx)
363 hoffset += modes[i]->hdisplay;
365 if (connector->tile_v_loc < v_idx)
366 voffset += modes[i]->vdisplay;
368 offsets[idx].x = hoffset;
369 offsets[idx].y = voffset;
370 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
374 static bool drm_client_target_preferred(struct drm_connector **connectors,
375 unsigned int connector_count,
376 struct drm_display_mode **modes,
377 struct drm_client_offset *offsets,
378 bool *enabled, int width, int height)
380 const u64 mask = BIT_ULL(connector_count) - 1;
381 struct drm_connector *connector;
382 u64 conn_configured = 0;
384 int num_tiled_conns = 0;
387 for (i = 0; i < connector_count; i++) {
388 if (connectors[i]->has_tile &&
389 connectors[i]->status == connector_status_connected)
394 for (i = 0; i < connector_count; i++) {
395 connector = connectors[i];
397 if (conn_configured & BIT_ULL(i))
400 if (enabled[i] == false) {
401 conn_configured |= BIT_ULL(i);
405 /* first pass over all the untiled connectors */
406 if (tile_pass == 0 && connector->has_tile)
409 if (tile_pass == 1) {
410 if (connector->tile_h_loc != 0 ||
411 connector->tile_v_loc != 0)
415 if (connector->tile_h_loc != tile_pass - 1 &&
416 connector->tile_v_loc != tile_pass - 1)
417 /* if this tile_pass doesn't cover any of the tiles - keep going */
421 * find the tile offsets for this pass - need to find
422 * all tiles left and above
424 drm_client_get_tile_offsets(connectors, connector_count, modes, offsets, i,
425 connector->tile_h_loc, connector->tile_v_loc);
427 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
430 /* got for command line mode first */
431 modes[i] = drm_connector_pick_cmdline_mode(connector);
433 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
434 connector->base.id, connector->tile_group ? connector->tile_group->id : 0);
435 modes[i] = drm_connector_has_preferred_mode(connector, width, height);
437 /* No preferred modes, pick one off the list */
438 if (!modes[i] && !list_empty(&connector->modes)) {
439 list_for_each_entry(modes[i], &connector->modes, head)
443 * In case of tiled mode if all tiles not present fallback to
444 * first available non tiled mode.
445 * After all tiles are present, try to find the tiled mode
446 * for all and if tiled mode not present due to fbcon size
447 * limitations, use first non tiled mode only for
448 * tile 0,0 and set to no mode for all other tiles.
450 if (connector->has_tile) {
451 if (num_tiled_conns <
452 connector->num_h_tile * connector->num_v_tile ||
453 (connector->tile_h_loc == 0 &&
454 connector->tile_v_loc == 0 &&
455 !drm_connector_get_tiled_mode(connector))) {
456 DRM_DEBUG_KMS("Falling back to non tiled mode on Connector %d\n",
458 modes[i] = drm_connector_fallback_non_tiled_mode(connector);
460 modes[i] = drm_connector_get_tiled_mode(connector);
464 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
466 conn_configured |= BIT_ULL(i);
469 if ((conn_configured & mask) != mask) {
476 static bool connector_has_possible_crtc(struct drm_connector *connector,
477 struct drm_crtc *crtc)
479 struct drm_encoder *encoder;
481 drm_connector_for_each_possible_encoder(connector, encoder) {
482 if (encoder->possible_crtcs & drm_crtc_mask(crtc))
489 static int drm_client_pick_crtcs(struct drm_client_dev *client,
490 struct drm_connector **connectors,
491 unsigned int connector_count,
492 struct drm_crtc **best_crtcs,
493 struct drm_display_mode **modes,
494 int n, int width, int height)
496 struct drm_device *dev = client->dev;
497 struct drm_connector *connector;
498 int my_score, best_score, score;
499 struct drm_crtc **crtcs, *crtc;
500 struct drm_mode_set *modeset;
503 if (n == connector_count)
506 connector = connectors[n];
508 best_crtcs[n] = NULL;
509 best_score = drm_client_pick_crtcs(client, connectors, connector_count,
510 best_crtcs, modes, n + 1, width, height);
511 if (modes[n] == NULL)
514 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
519 if (connector->status == connector_status_connected)
521 if (connector->cmdline_mode.specified)
523 if (drm_connector_has_preferred_mode(connector, width, height))
527 * select a crtc for this connector and then attempt to configure
528 * remaining connectors
530 drm_client_for_each_modeset(modeset, client) {
531 crtc = modeset->crtc;
533 if (!connector_has_possible_crtc(connector, crtc))
536 for (o = 0; o < n; o++)
537 if (best_crtcs[o] == crtc)
541 /* ignore cloning unless only a single crtc */
542 if (dev->mode_config.num_crtc > 1)
545 if (!drm_mode_equal(modes[o], modes[n]))
550 memcpy(crtcs, best_crtcs, n * sizeof(*crtcs));
551 score = my_score + drm_client_pick_crtcs(client, connectors, connector_count,
552 crtcs, modes, n + 1, width, height);
553 if (score > best_score) {
555 memcpy(best_crtcs, crtcs, connector_count * sizeof(*crtcs));
563 /* Try to read the BIOS display configuration and use it for the initial config */
564 static bool drm_client_firmware_config(struct drm_client_dev *client,
565 struct drm_connector **connectors,
566 unsigned int connector_count,
567 struct drm_crtc **crtcs,
568 struct drm_display_mode **modes,
569 struct drm_client_offset *offsets,
570 bool *enabled, int width, int height)
572 const int count = min_t(unsigned int, connector_count, BITS_PER_LONG);
573 unsigned long conn_configured, conn_seq, mask;
574 struct drm_device *dev = client->dev;
577 bool fallback = true, ret = true;
578 int num_connectors_enabled = 0;
579 int num_connectors_detected = 0;
580 int num_tiled_conns = 0;
581 struct drm_modeset_acquire_ctx ctx;
583 if (!drm_drv_uses_atomic_modeset(dev))
586 if (WARN_ON(count <= 0))
589 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
593 drm_modeset_acquire_init(&ctx, 0);
595 while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
596 drm_modeset_backoff(&ctx);
598 memcpy(save_enabled, enabled, count);
599 mask = GENMASK(count - 1, 0);
601 for (i = 0; i < count; i++) {
602 if (connectors[i]->has_tile &&
603 connectors[i]->status == connector_status_connected)
607 conn_seq = conn_configured;
608 for (i = 0; i < count; i++) {
609 struct drm_connector *connector;
610 struct drm_encoder *encoder;
611 struct drm_crtc *new_crtc;
613 connector = connectors[i];
615 if (conn_configured & BIT(i))
618 if (conn_seq == 0 && !connector->has_tile)
621 if (connector->status == connector_status_connected)
622 num_connectors_detected++;
625 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
627 conn_configured |= BIT(i);
631 if (connector->force == DRM_FORCE_OFF) {
632 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
638 encoder = connector->state->best_encoder;
639 if (!encoder || WARN_ON(!connector->state->crtc)) {
640 if (connector->force > DRM_FORCE_OFF)
643 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
646 conn_configured |= BIT(i);
650 num_connectors_enabled++;
652 new_crtc = connector->state->crtc;
655 * Make sure we're not trying to drive multiple connectors
656 * with a single CRTC, since our cloning support may not
659 for (j = 0; j < count; j++) {
660 if (crtcs[j] == new_crtc) {
661 DRM_DEBUG_KMS("fallback: cloned configuration\n");
666 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
669 /* go for command line mode first */
670 modes[i] = drm_connector_pick_cmdline_mode(connector);
672 /* try for preferred next */
674 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
675 connector->name, connector->has_tile);
676 modes[i] = drm_connector_has_preferred_mode(connector, width, height);
679 /* No preferred mode marked by the EDID? Are there any modes? */
680 if (!modes[i] && !list_empty(&connector->modes)) {
681 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
683 modes[i] = list_first_entry(&connector->modes,
684 struct drm_display_mode,
688 /* last resort: use current mode */
691 * IMPORTANT: We want to use the adjusted mode (i.e.
692 * after the panel fitter upscaling) as the initial
693 * config, not the input mode, which is what crtc->mode
694 * usually contains. But since our current
695 * code puts a mode derived from the post-pfit timings
696 * into crtc->mode this works out correctly.
698 * This is crtc->mode and not crtc->state->mode for the
699 * fastboot check to work correctly.
701 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
703 modes[i] = &connector->state->crtc->mode;
706 * In case of tiled modes, if all tiles are not present
707 * then fallback to a non tiled mode.
709 if (connector->has_tile &&
710 num_tiled_conns < connector->num_h_tile * connector->num_v_tile) {
711 DRM_DEBUG_KMS("Falling back to non tiled mode on Connector %d\n",
713 modes[i] = drm_connector_fallback_non_tiled_mode(connector);
717 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
719 connector->state->crtc->base.id,
720 connector->state->crtc->name,
721 modes[i]->hdisplay, modes[i]->vdisplay,
722 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "");
725 conn_configured |= BIT(i);
728 if ((conn_configured & mask) != mask && conn_configured != conn_seq)
732 * If the BIOS didn't enable everything it could, fall back to have the
733 * same user experiencing of lighting up as much as possible like the
734 * fbdev helper library.
736 if (num_connectors_enabled != num_connectors_detected &&
737 num_connectors_enabled < dev->mode_config.num_crtc) {
738 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
739 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
740 num_connectors_detected);
746 DRM_DEBUG_KMS("Not using firmware configuration\n");
747 memcpy(enabled, save_enabled, count);
751 drm_modeset_drop_locks(&ctx);
752 drm_modeset_acquire_fini(&ctx);
759 * drm_client_modeset_probe() - Probe for displays
760 * @client: DRM client
761 * @width: Maximum display mode width (optional)
762 * @height: Maximum display mode height (optional)
764 * This function sets up display pipelines for enabled connectors and stores the
765 * config in the client's modeset array.
768 * Zero on success or negative error code on failure.
770 int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height)
772 struct drm_connector *connector, **connectors = NULL;
773 struct drm_connector_list_iter conn_iter;
774 struct drm_device *dev = client->dev;
775 unsigned int total_modes_count = 0;
776 struct drm_client_offset *offsets;
777 unsigned int connector_count = 0;
778 struct drm_display_mode **modes;
779 struct drm_crtc **crtcs;
786 width = dev->mode_config.max_width;
788 height = dev->mode_config.max_height;
790 drm_connector_list_iter_begin(dev, &conn_iter);
791 drm_client_for_each_connector_iter(connector, &conn_iter) {
792 struct drm_connector **tmp;
794 tmp = krealloc(connectors, (connector_count + 1) * sizeof(*connectors), GFP_KERNEL);
797 goto free_connectors;
801 drm_connector_get(connector);
802 connectors[connector_count++] = connector;
804 drm_connector_list_iter_end(&conn_iter);
806 if (!connector_count)
809 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
810 modes = kcalloc(connector_count, sizeof(*modes), GFP_KERNEL);
811 offsets = kcalloc(connector_count, sizeof(*offsets), GFP_KERNEL);
812 enabled = kcalloc(connector_count, sizeof(bool), GFP_KERNEL);
813 if (!crtcs || !modes || !enabled || !offsets) {
814 DRM_ERROR("Memory allocation failed\n");
819 mutex_lock(&client->modeset_mutex);
821 mutex_lock(&dev->mode_config.mutex);
822 for (i = 0; i < connector_count; i++)
823 total_modes_count += connectors[i]->funcs->fill_modes(connectors[i], width, height);
824 if (!total_modes_count)
825 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
826 drm_client_connectors_enabled(connectors, connector_count, enabled);
828 if (!drm_client_firmware_config(client, connectors, connector_count, crtcs,
829 modes, offsets, enabled, width, height)) {
830 memset(modes, 0, connector_count * sizeof(*modes));
831 memset(crtcs, 0, connector_count * sizeof(*crtcs));
832 memset(offsets, 0, connector_count * sizeof(*offsets));
834 if (!drm_client_target_cloned(dev, connectors, connector_count, modes,
835 offsets, enabled, width, height) &&
836 !drm_client_target_preferred(connectors, connector_count, modes,
837 offsets, enabled, width, height))
838 DRM_ERROR("Unable to find initial modes\n");
840 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
843 drm_client_pick_crtcs(client, connectors, connector_count,
844 crtcs, modes, 0, width, height);
846 mutex_unlock(&dev->mode_config.mutex);
848 drm_client_modeset_release(client);
850 for (i = 0; i < connector_count; i++) {
851 struct drm_display_mode *mode = modes[i];
852 struct drm_crtc *crtc = crtcs[i];
853 struct drm_client_offset *offset = &offsets[i];
856 struct drm_mode_set *modeset = drm_client_find_modeset(client, crtc);
857 struct drm_connector *connector = connectors[i];
859 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
860 mode->name, crtc->base.id, offset->x, offset->y);
862 if (WARN_ON_ONCE(modeset->num_connectors == DRM_CLIENT_MAX_CLONED_CONNECTORS ||
863 (dev->mode_config.num_crtc > 1 && modeset->num_connectors == 1))) {
868 modeset->mode = drm_mode_duplicate(dev, mode);
869 drm_connector_get(connector);
870 modeset->connectors[modeset->num_connectors++] = connector;
871 modeset->x = offset->x;
872 modeset->y = offset->y;
876 mutex_unlock(&client->modeset_mutex);
883 for (i = 0; i < connector_count; i++)
884 drm_connector_put(connectors[i]);
889 EXPORT_SYMBOL(drm_client_modeset_probe);
892 * drm_client_rotation() - Check the initial rotation value
893 * @modeset: DRM modeset
894 * @rotation: Returned rotation value
896 * This function checks if the primary plane in @modeset can hw rotate
897 * to match the rotation needed on its connector.
899 * Note: Currently only 0 and 180 degrees are supported.
902 * True if the plane can do the rotation, false otherwise.
904 bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation)
906 struct drm_connector *connector = modeset->connectors[0];
907 struct drm_plane *plane = modeset->crtc->primary;
908 struct drm_cmdline_mode *cmdline;
912 if (!modeset->num_connectors)
915 switch (connector->display_info.panel_orientation) {
916 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
917 *rotation = DRM_MODE_ROTATE_180;
919 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
920 *rotation = DRM_MODE_ROTATE_90;
922 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
923 *rotation = DRM_MODE_ROTATE_270;
926 *rotation = DRM_MODE_ROTATE_0;
930 * The panel already defined the default rotation
931 * through its orientation. Whatever has been provided
932 * on the command line needs to be added to that.
934 * Unfortunately, the rotations are at different bit
935 * indices, so the math to add them up are not as
936 * trivial as they could.
938 * Reflections on the other hand are pretty trivial to deal with, a
939 * simple XOR between the two handle the addition nicely.
941 cmdline = &connector->cmdline_mode;
942 if (cmdline->specified && cmdline->rotation_reflection) {
943 unsigned int cmdline_rest, panel_rest;
944 unsigned int cmdline_rot, panel_rot;
945 unsigned int sum_rot, sum_rest;
947 panel_rot = ilog2(*rotation & DRM_MODE_ROTATE_MASK);
948 cmdline_rot = ilog2(cmdline->rotation_reflection & DRM_MODE_ROTATE_MASK);
949 sum_rot = (panel_rot + cmdline_rot) % 4;
951 panel_rest = *rotation & ~DRM_MODE_ROTATE_MASK;
952 cmdline_rest = cmdline->rotation_reflection & ~DRM_MODE_ROTATE_MASK;
953 sum_rest = panel_rest ^ cmdline_rest;
955 *rotation = (1 << sum_rot) | sum_rest;
959 * TODO: support 90 / 270 degree hardware rotation,
960 * depending on the hardware this may require the framebuffer
961 * to be in a specific tiling format.
963 if (((*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0 &&
964 (*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_180) ||
965 !plane->rotation_property)
968 for (i = 0; i < plane->rotation_property->num_values; i++)
969 valid_mask |= (1ULL << plane->rotation_property->values[i]);
971 if (!(*rotation & valid_mask))
976 EXPORT_SYMBOL(drm_client_rotation);
978 static int drm_client_modeset_commit_atomic(struct drm_client_dev *client, bool active, bool check)
980 struct drm_device *dev = client->dev;
981 struct drm_plane *plane;
982 struct drm_atomic_state *state;
983 struct drm_modeset_acquire_ctx ctx;
984 struct drm_mode_set *mode_set;
987 drm_modeset_acquire_init(&ctx, 0);
989 state = drm_atomic_state_alloc(dev);
995 state->acquire_ctx = &ctx;
997 drm_for_each_plane(plane, dev) {
998 struct drm_plane_state *plane_state;
1000 plane_state = drm_atomic_get_plane_state(state, plane);
1001 if (IS_ERR(plane_state)) {
1002 ret = PTR_ERR(plane_state);
1006 plane_state->rotation = DRM_MODE_ROTATE_0;
1008 /* disable non-primary: */
1009 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
1012 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1017 drm_client_for_each_modeset(mode_set, client) {
1018 struct drm_plane *primary = mode_set->crtc->primary;
1019 unsigned int rotation;
1021 if (drm_client_rotation(mode_set, &rotation)) {
1022 struct drm_plane_state *plane_state;
1024 /* Cannot fail as we've already gotten the plane state above */
1025 plane_state = drm_atomic_get_new_plane_state(state, primary);
1026 plane_state->rotation = rotation;
1029 ret = __drm_atomic_helper_set_config(mode_set, state);
1034 * __drm_atomic_helper_set_config() sets active when a
1035 * mode is set, unconditionally clear it if we force DPMS off
1038 struct drm_crtc *crtc = mode_set->crtc;
1039 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1041 crtc_state->active = false;
1046 ret = drm_atomic_check_only(state);
1048 ret = drm_atomic_commit(state);
1051 if (ret == -EDEADLK)
1054 drm_atomic_state_put(state);
1056 drm_modeset_drop_locks(&ctx);
1057 drm_modeset_acquire_fini(&ctx);
1062 drm_atomic_state_clear(state);
1063 drm_modeset_backoff(&ctx);
1068 static int drm_client_modeset_commit_legacy(struct drm_client_dev *client)
1070 struct drm_device *dev = client->dev;
1071 struct drm_mode_set *mode_set;
1072 struct drm_plane *plane;
1075 drm_modeset_lock_all(dev);
1076 drm_for_each_plane(plane, dev) {
1077 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
1078 drm_plane_force_disable(plane);
1080 if (plane->rotation_property)
1081 drm_mode_plane_set_obj_prop(plane,
1082 plane->rotation_property,
1086 drm_client_for_each_modeset(mode_set, client) {
1087 struct drm_crtc *crtc = mode_set->crtc;
1089 if (crtc->funcs->cursor_set2) {
1090 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
1093 } else if (crtc->funcs->cursor_set) {
1094 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
1099 ret = drm_mode_set_config_internal(mode_set);
1104 drm_modeset_unlock_all(dev);
1110 * drm_client_modeset_check() - Check modeset configuration
1111 * @client: DRM client
1113 * Check modeset configuration.
1116 * Zero on success or negative error code on failure.
1118 int drm_client_modeset_check(struct drm_client_dev *client)
1122 if (!drm_drv_uses_atomic_modeset(client->dev))
1125 mutex_lock(&client->modeset_mutex);
1126 ret = drm_client_modeset_commit_atomic(client, true, true);
1127 mutex_unlock(&client->modeset_mutex);
1131 EXPORT_SYMBOL(drm_client_modeset_check);
1134 * drm_client_modeset_commit_locked() - Force commit CRTC configuration
1135 * @client: DRM client
1137 * Commit modeset configuration to crtcs without checking if there is a DRM
1138 * master. The assumption is that the caller already holds an internal DRM
1139 * master reference acquired with drm_master_internal_acquire().
1142 * Zero on success or negative error code on failure.
1144 int drm_client_modeset_commit_locked(struct drm_client_dev *client)
1146 struct drm_device *dev = client->dev;
1149 mutex_lock(&client->modeset_mutex);
1150 if (drm_drv_uses_atomic_modeset(dev))
1151 ret = drm_client_modeset_commit_atomic(client, true, false);
1153 ret = drm_client_modeset_commit_legacy(client);
1154 mutex_unlock(&client->modeset_mutex);
1158 EXPORT_SYMBOL(drm_client_modeset_commit_locked);
1161 * drm_client_modeset_commit() - Commit CRTC configuration
1162 * @client: DRM client
1164 * Commit modeset configuration to crtcs.
1167 * Zero on success or negative error code on failure.
1169 int drm_client_modeset_commit(struct drm_client_dev *client)
1171 struct drm_device *dev = client->dev;
1174 if (!drm_master_internal_acquire(dev))
1177 ret = drm_client_modeset_commit_locked(client);
1179 drm_master_internal_release(dev);
1183 EXPORT_SYMBOL(drm_client_modeset_commit);
1185 static void drm_client_modeset_dpms_legacy(struct drm_client_dev *client, int dpms_mode)
1187 struct drm_device *dev = client->dev;
1188 struct drm_connector *connector;
1189 struct drm_mode_set *modeset;
1190 struct drm_modeset_acquire_ctx ctx;
1194 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
1195 drm_client_for_each_modeset(modeset, client) {
1196 if (!modeset->crtc->enabled)
1199 for (j = 0; j < modeset->num_connectors; j++) {
1200 connector = modeset->connectors[j];
1201 connector->funcs->dpms(connector, dpms_mode);
1202 drm_object_property_set_value(&connector->base,
1203 dev->mode_config.dpms_property, dpms_mode);
1206 DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
1210 * drm_client_modeset_dpms() - Set DPMS mode
1211 * @client: DRM client
1214 * Note: For atomic drivers @mode is reduced to on/off.
1217 * Zero on success or negative error code on failure.
1219 int drm_client_modeset_dpms(struct drm_client_dev *client, int mode)
1221 struct drm_device *dev = client->dev;
1224 if (!drm_master_internal_acquire(dev))
1227 mutex_lock(&client->modeset_mutex);
1228 if (drm_drv_uses_atomic_modeset(dev))
1229 ret = drm_client_modeset_commit_atomic(client, mode == DRM_MODE_DPMS_ON, false);
1231 drm_client_modeset_dpms_legacy(client, mode);
1232 mutex_unlock(&client->modeset_mutex);
1234 drm_master_internal_release(dev);
1238 EXPORT_SYMBOL(drm_client_modeset_dpms);