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 <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
14 #include <drm/drm_atomic.h>
15 #include <drm/drm_client.h>
16 #include <drm/drm_connector.h>
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_device.h>
19 #include <drm/drm_drv.h>
20 #include <drm/drm_encoder.h>
21 #include <drm/drm_print.h>
23 #include "drm_crtc_internal.h"
24 #include "drm_internal.h"
26 #define DRM_CLIENT_MAX_CLONED_CONNECTORS 8
28 struct drm_client_offset {
32 int drm_client_modeset_create(struct drm_client_dev *client)
34 struct drm_device *dev = client->dev;
35 unsigned int num_crtc = dev->mode_config.num_crtc;
36 unsigned int max_connector_count = 1;
37 struct drm_mode_set *modeset;
38 struct drm_crtc *crtc;
41 /* Add terminating zero entry to enable index less iteration */
42 client->modesets = kcalloc(num_crtc + 1, sizeof(*client->modesets), GFP_KERNEL);
43 if (!client->modesets)
46 mutex_init(&client->modeset_mutex);
48 drm_for_each_crtc(crtc, dev)
49 client->modesets[i++].crtc = crtc;
51 /* Cloning is only supported in the single crtc case. */
53 max_connector_count = DRM_CLIENT_MAX_CLONED_CONNECTORS;
55 for (modeset = client->modesets; modeset->crtc; modeset++) {
56 modeset->connectors = kcalloc(max_connector_count,
57 sizeof(*modeset->connectors), GFP_KERNEL);
58 if (!modeset->connectors)
65 drm_client_modeset_free(client);
70 static void drm_client_modeset_release(struct drm_client_dev *client)
72 struct drm_mode_set *modeset;
75 drm_client_for_each_modeset(modeset, client) {
76 drm_mode_destroy(client->dev, modeset->mode);
80 for (i = 0; i < modeset->num_connectors; i++) {
81 drm_connector_put(modeset->connectors[i]);
82 modeset->connectors[i] = NULL;
84 modeset->num_connectors = 0;
88 void drm_client_modeset_free(struct drm_client_dev *client)
90 struct drm_mode_set *modeset;
92 mutex_lock(&client->modeset_mutex);
94 drm_client_modeset_release(client);
96 drm_client_for_each_modeset(modeset, client)
97 kfree(modeset->connectors);
99 mutex_unlock(&client->modeset_mutex);
101 mutex_destroy(&client->modeset_mutex);
102 kfree(client->modesets);
105 static struct drm_mode_set *
106 drm_client_find_modeset(struct drm_client_dev *client, struct drm_crtc *crtc)
108 struct drm_mode_set *modeset;
110 drm_client_for_each_modeset(modeset, client)
111 if (modeset->crtc == crtc)
117 static struct drm_display_mode *
118 drm_connector_has_preferred_mode(struct drm_connector *connector, int width, int height)
120 struct drm_display_mode *mode;
122 list_for_each_entry(mode, &connector->modes, head) {
123 if (mode->hdisplay > width ||
124 mode->vdisplay > height)
126 if (mode->type & DRM_MODE_TYPE_PREFERRED)
132 static struct drm_display_mode *
133 drm_connector_pick_cmdline_mode(struct drm_connector *connector)
135 struct drm_cmdline_mode *cmdline_mode;
136 struct drm_display_mode *mode;
137 bool prefer_non_interlace;
139 cmdline_mode = &connector->cmdline_mode;
140 if (cmdline_mode->specified == false)
143 /* attempt to find a matching mode in the list of modes
144 * we have gotten so far, if not add a CVT mode that conforms
146 if (cmdline_mode->rb || cmdline_mode->margins)
149 prefer_non_interlace = !cmdline_mode->interlace;
151 list_for_each_entry(mode, &connector->modes, head) {
152 /* Check (optional) mode name first */
153 if (!strcmp(mode->name, cmdline_mode->name))
156 /* check width/height */
157 if (mode->hdisplay != cmdline_mode->xres ||
158 mode->vdisplay != cmdline_mode->yres)
161 if (cmdline_mode->refresh_specified) {
162 if (mode->vrefresh != cmdline_mode->refresh)
166 if (cmdline_mode->interlace) {
167 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
169 } else if (prefer_non_interlace) {
170 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
176 if (prefer_non_interlace) {
177 prefer_non_interlace = false;
182 mode = drm_mode_create_from_cmdline_mode(connector->dev, cmdline_mode);
184 list_add(&mode->head, &connector->modes);
189 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
193 if (connector->display_info.non_desktop)
197 enable = connector->status == connector_status_connected;
199 enable = connector->status != connector_status_disconnected;
204 static void drm_client_connectors_enabled(struct drm_connector **connectors,
205 unsigned int connector_count,
208 bool any_enabled = false;
209 struct drm_connector *connector;
212 for (i = 0; i < connector_count; i++) {
213 connector = connectors[i];
214 enabled[i] = drm_connector_enabled(connector, true);
215 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
216 connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no");
218 any_enabled |= enabled[i];
224 for (i = 0; i < connector_count; i++)
225 enabled[i] = drm_connector_enabled(connectors[i], false);
228 static bool drm_client_target_cloned(struct drm_device *dev,
229 struct drm_connector **connectors,
230 unsigned int connector_count,
231 struct drm_display_mode **modes,
232 struct drm_client_offset *offsets,
233 bool *enabled, int width, int height)
236 bool can_clone = false;
237 struct drm_display_mode *dmt_mode, *mode;
239 /* only contemplate cloning in the single crtc case */
240 if (dev->mode_config.num_crtc > 1)
244 for (i = 0; i < connector_count; i++) {
249 /* only contemplate cloning if more than one connector is enabled */
253 /* check the command line or if nothing common pick 1024x768 */
255 for (i = 0; i < connector_count; i++) {
258 modes[i] = drm_connector_pick_cmdline_mode(connectors[i]);
263 for (j = 0; j < i; j++) {
266 if (!drm_mode_match(modes[j], modes[i],
267 DRM_MODE_MATCH_TIMINGS |
268 DRM_MODE_MATCH_CLOCK |
269 DRM_MODE_MATCH_FLAGS |
270 DRM_MODE_MATCH_3D_FLAGS))
276 DRM_DEBUG_KMS("can clone using command line\n");
280 /* try and find a 1024x768 mode on each connector */
282 dmt_mode = drm_mode_find_dmt(dev, 1024, 768, 60, false);
284 for (i = 0; i < connector_count; i++) {
288 list_for_each_entry(mode, &connectors[i]->modes, head) {
289 if (drm_mode_match(mode, dmt_mode,
290 DRM_MODE_MATCH_TIMINGS |
291 DRM_MODE_MATCH_CLOCK |
292 DRM_MODE_MATCH_FLAGS |
293 DRM_MODE_MATCH_3D_FLAGS))
301 DRM_DEBUG_KMS("can clone using 1024x768\n");
304 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
308 static int drm_client_get_tile_offsets(struct drm_connector **connectors,
309 unsigned int connector_count,
310 struct drm_display_mode **modes,
311 struct drm_client_offset *offsets,
313 int h_idx, int v_idx)
315 struct drm_connector *connector;
317 int hoffset = 0, voffset = 0;
319 for (i = 0; i < connector_count; i++) {
320 connector = connectors[i];
321 if (!connector->has_tile)
324 if (!modes[i] && (h_idx || v_idx)) {
325 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
329 if (connector->tile_h_loc < h_idx)
330 hoffset += modes[i]->hdisplay;
332 if (connector->tile_v_loc < v_idx)
333 voffset += modes[i]->vdisplay;
335 offsets[idx].x = hoffset;
336 offsets[idx].y = voffset;
337 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
341 static bool drm_client_target_preferred(struct drm_connector **connectors,
342 unsigned int connector_count,
343 struct drm_display_mode **modes,
344 struct drm_client_offset *offsets,
345 bool *enabled, int width, int height)
347 const u64 mask = BIT_ULL(connector_count) - 1;
348 struct drm_connector *connector;
349 u64 conn_configured = 0;
354 for (i = 0; i < connector_count; i++) {
355 connector = connectors[i];
357 if (conn_configured & BIT_ULL(i))
360 if (enabled[i] == false) {
361 conn_configured |= BIT_ULL(i);
365 /* first pass over all the untiled connectors */
366 if (tile_pass == 0 && connector->has_tile)
369 if (tile_pass == 1) {
370 if (connector->tile_h_loc != 0 ||
371 connector->tile_v_loc != 0)
375 if (connector->tile_h_loc != tile_pass - 1 &&
376 connector->tile_v_loc != tile_pass - 1)
377 /* if this tile_pass doesn't cover any of the tiles - keep going */
381 * find the tile offsets for this pass - need to find
382 * all tiles left and above
384 drm_client_get_tile_offsets(connectors, connector_count, modes, offsets, i,
385 connector->tile_h_loc, connector->tile_v_loc);
387 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
390 /* got for command line mode first */
391 modes[i] = drm_connector_pick_cmdline_mode(connector);
393 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
394 connector->base.id, connector->tile_group ? connector->tile_group->id : 0);
395 modes[i] = drm_connector_has_preferred_mode(connector, width, height);
397 /* No preferred modes, pick one off the list */
398 if (!modes[i] && !list_empty(&connector->modes)) {
399 list_for_each_entry(modes[i], &connector->modes, head)
402 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
404 conn_configured |= BIT_ULL(i);
407 if ((conn_configured & mask) != mask) {
414 static bool connector_has_possible_crtc(struct drm_connector *connector,
415 struct drm_crtc *crtc)
417 struct drm_encoder *encoder;
420 drm_connector_for_each_possible_encoder(connector, encoder, i) {
421 if (encoder->possible_crtcs & drm_crtc_mask(crtc))
428 static int drm_client_pick_crtcs(struct drm_client_dev *client,
429 struct drm_connector **connectors,
430 unsigned int connector_count,
431 struct drm_crtc **best_crtcs,
432 struct drm_display_mode **modes,
433 int n, int width, int height)
435 struct drm_device *dev = client->dev;
436 struct drm_connector *connector;
437 int my_score, best_score, score;
438 struct drm_crtc **crtcs, *crtc;
439 struct drm_mode_set *modeset;
442 if (n == connector_count)
445 connector = connectors[n];
447 best_crtcs[n] = NULL;
448 best_score = drm_client_pick_crtcs(client, connectors, connector_count,
449 best_crtcs, modes, n + 1, width, height);
450 if (modes[n] == NULL)
453 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
458 if (connector->status == connector_status_connected)
460 if (connector->cmdline_mode.specified)
462 if (drm_connector_has_preferred_mode(connector, width, height))
466 * select a crtc for this connector and then attempt to configure
467 * remaining connectors
469 drm_client_for_each_modeset(modeset, client) {
470 crtc = modeset->crtc;
472 if (!connector_has_possible_crtc(connector, crtc))
475 for (o = 0; o < n; o++)
476 if (best_crtcs[o] == crtc)
480 /* ignore cloning unless only a single crtc */
481 if (dev->mode_config.num_crtc > 1)
484 if (!drm_mode_equal(modes[o], modes[n]))
489 memcpy(crtcs, best_crtcs, n * sizeof(*crtcs));
490 score = my_score + drm_client_pick_crtcs(client, connectors, connector_count,
491 crtcs, modes, n + 1, width, height);
492 if (score > best_score) {
494 memcpy(best_crtcs, crtcs, connector_count * sizeof(*crtcs));
502 /* Try to read the BIOS display configuration and use it for the initial config */
503 static bool drm_client_firmware_config(struct drm_client_dev *client,
504 struct drm_connector **connectors,
505 unsigned int connector_count,
506 struct drm_crtc **crtcs,
507 struct drm_display_mode **modes,
508 struct drm_client_offset *offsets,
509 bool *enabled, int width, int height)
511 unsigned int count = min_t(unsigned int, connector_count, BITS_PER_LONG);
512 unsigned long conn_configured, conn_seq, mask;
513 struct drm_device *dev = client->dev;
516 bool fallback = true, ret = true;
517 int num_connectors_enabled = 0;
518 int num_connectors_detected = 0;
519 struct drm_modeset_acquire_ctx ctx;
521 if (!drm_drv_uses_atomic_modeset(dev))
524 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
528 drm_modeset_acquire_init(&ctx, 0);
530 while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
531 drm_modeset_backoff(&ctx);
533 memcpy(save_enabled, enabled, count);
534 mask = GENMASK(count - 1, 0);
537 conn_seq = conn_configured;
538 for (i = 0; i < count; i++) {
539 struct drm_connector *connector;
540 struct drm_encoder *encoder;
541 struct drm_crtc *new_crtc;
543 connector = connectors[i];
545 if (conn_configured & BIT(i))
548 if (conn_seq == 0 && !connector->has_tile)
551 if (connector->status == connector_status_connected)
552 num_connectors_detected++;
555 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
557 conn_configured |= BIT(i);
561 if (connector->force == DRM_FORCE_OFF) {
562 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
568 encoder = connector->state->best_encoder;
569 if (!encoder || WARN_ON(!connector->state->crtc)) {
570 if (connector->force > DRM_FORCE_OFF)
573 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
576 conn_configured |= BIT(i);
580 num_connectors_enabled++;
582 new_crtc = connector->state->crtc;
585 * Make sure we're not trying to drive multiple connectors
586 * with a single CRTC, since our cloning support may not
589 for (j = 0; j < count; j++) {
590 if (crtcs[j] == new_crtc) {
591 DRM_DEBUG_KMS("fallback: cloned configuration\n");
596 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
599 /* go for command line mode first */
600 modes[i] = drm_connector_pick_cmdline_mode(connector);
602 /* try for preferred next */
604 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
605 connector->name, connector->has_tile);
606 modes[i] = drm_connector_has_preferred_mode(connector, width, height);
609 /* No preferred mode marked by the EDID? Are there any modes? */
610 if (!modes[i] && !list_empty(&connector->modes)) {
611 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
613 modes[i] = list_first_entry(&connector->modes,
614 struct drm_display_mode,
618 /* last resort: use current mode */
621 * IMPORTANT: We want to use the adjusted mode (i.e.
622 * after the panel fitter upscaling) as the initial
623 * config, not the input mode, which is what crtc->mode
624 * usually contains. But since our current
625 * code puts a mode derived from the post-pfit timings
626 * into crtc->mode this works out correctly.
628 * This is crtc->mode and not crtc->state->mode for the
629 * fastboot check to work correctly.
631 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
633 modes[i] = &connector->state->crtc->mode;
637 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
639 connector->state->crtc->base.id,
640 connector->state->crtc->name,
641 modes[i]->hdisplay, modes[i]->vdisplay,
642 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "");
645 conn_configured |= BIT(i);
648 if ((conn_configured & mask) != mask && conn_configured != conn_seq)
652 * If the BIOS didn't enable everything it could, fall back to have the
653 * same user experiencing of lighting up as much as possible like the
654 * fbdev helper library.
656 if (num_connectors_enabled != num_connectors_detected &&
657 num_connectors_enabled < dev->mode_config.num_crtc) {
658 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
659 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
660 num_connectors_detected);
666 DRM_DEBUG_KMS("Not using firmware configuration\n");
667 memcpy(enabled, save_enabled, count);
671 drm_modeset_drop_locks(&ctx);
672 drm_modeset_acquire_fini(&ctx);
679 * drm_client_modeset_probe() - Probe for displays
680 * @client: DRM client
681 * @width: Maximum display mode width (optional)
682 * @height: Maximum display mode height (optional)
684 * This function sets up display pipelines for enabled connectors and stores the
685 * config in the client's modeset array.
688 * Zero on success or negative error code on failure.
690 int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height)
692 struct drm_connector *connector, **connectors = NULL;
693 struct drm_connector_list_iter conn_iter;
694 struct drm_device *dev = client->dev;
695 unsigned int total_modes_count = 0;
696 struct drm_client_offset *offsets;
697 unsigned int connector_count = 0;
698 struct drm_display_mode **modes;
699 struct drm_crtc **crtcs;
706 width = dev->mode_config.max_width;
708 height = dev->mode_config.max_height;
710 drm_connector_list_iter_begin(dev, &conn_iter);
711 drm_client_for_each_connector_iter(connector, &conn_iter) {
712 struct drm_connector **tmp;
714 tmp = krealloc(connectors, (connector_count + 1) * sizeof(*connectors), GFP_KERNEL);
717 goto free_connectors;
721 drm_connector_get(connector);
722 connectors[connector_count++] = connector;
724 drm_connector_list_iter_end(&conn_iter);
726 if (!connector_count)
729 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
730 modes = kcalloc(connector_count, sizeof(*modes), GFP_KERNEL);
731 offsets = kcalloc(connector_count, sizeof(*offsets), GFP_KERNEL);
732 enabled = kcalloc(connector_count, sizeof(bool), GFP_KERNEL);
733 if (!crtcs || !modes || !enabled || !offsets) {
734 DRM_ERROR("Memory allocation failed\n");
739 mutex_lock(&client->modeset_mutex);
741 mutex_lock(&dev->mode_config.mutex);
742 for (i = 0; i < connector_count; i++)
743 total_modes_count += connectors[i]->funcs->fill_modes(connectors[i], width, height);
744 if (!total_modes_count)
745 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
746 drm_client_connectors_enabled(connectors, connector_count, enabled);
748 if (!drm_client_firmware_config(client, connectors, connector_count, crtcs,
749 modes, offsets, enabled, width, height)) {
750 memset(modes, 0, connector_count * sizeof(*modes));
751 memset(crtcs, 0, connector_count * sizeof(*crtcs));
752 memset(offsets, 0, connector_count * sizeof(*offsets));
754 if (!drm_client_target_cloned(dev, connectors, connector_count, modes,
755 offsets, enabled, width, height) &&
756 !drm_client_target_preferred(connectors, connector_count, modes,
757 offsets, enabled, width, height))
758 DRM_ERROR("Unable to find initial modes\n");
760 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
763 drm_client_pick_crtcs(client, connectors, connector_count,
764 crtcs, modes, 0, width, height);
766 mutex_unlock(&dev->mode_config.mutex);
768 drm_client_modeset_release(client);
770 for (i = 0; i < connector_count; i++) {
771 struct drm_display_mode *mode = modes[i];
772 struct drm_crtc *crtc = crtcs[i];
773 struct drm_client_offset *offset = &offsets[i];
776 struct drm_mode_set *modeset = drm_client_find_modeset(client, crtc);
777 struct drm_connector *connector = connectors[i];
779 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
780 mode->name, crtc->base.id, offset->x, offset->y);
782 if (WARN_ON_ONCE(modeset->num_connectors == DRM_CLIENT_MAX_CLONED_CONNECTORS ||
783 (dev->mode_config.num_crtc > 1 && modeset->num_connectors == 1))) {
788 modeset->mode = drm_mode_duplicate(dev, mode);
789 drm_connector_get(connector);
790 modeset->connectors[modeset->num_connectors++] = connector;
791 modeset->x = offset->x;
792 modeset->y = offset->y;
796 mutex_unlock(&client->modeset_mutex);
803 for (i = 0; i < connector_count; i++)
804 drm_connector_put(connectors[i]);
809 EXPORT_SYMBOL(drm_client_modeset_probe);
812 * drm_client_rotation() - Check the initial rotation value
813 * @modeset: DRM modeset
814 * @rotation: Returned rotation value
816 * This function checks if the primary plane in @modeset can hw rotate
817 * to match the rotation needed on its connector.
819 * Note: Currently only 0 and 180 degrees are supported.
822 * True if the plane can do the rotation, false otherwise.
824 bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation)
826 struct drm_connector *connector = modeset->connectors[0];
827 struct drm_plane *plane = modeset->crtc->primary;
828 struct drm_cmdline_mode *cmdline;
832 if (!modeset->num_connectors)
835 switch (connector->display_info.panel_orientation) {
836 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
837 *rotation = DRM_MODE_ROTATE_180;
839 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
840 *rotation = DRM_MODE_ROTATE_90;
842 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
843 *rotation = DRM_MODE_ROTATE_270;
846 *rotation = DRM_MODE_ROTATE_0;
850 * The panel already defined the default rotation
851 * through its orientation. Whatever has been provided
852 * on the command line needs to be added to that.
854 * Unfortunately, the rotations are at different bit
855 * indices, so the math to add them up are not as
856 * trivial as they could.
858 * Reflections on the other hand are pretty trivial to deal with, a
859 * simple XOR between the two handle the addition nicely.
861 cmdline = &connector->cmdline_mode;
862 if (cmdline->specified && cmdline->rotation_reflection) {
863 unsigned int cmdline_rest, panel_rest;
864 unsigned int cmdline_rot, panel_rot;
865 unsigned int sum_rot, sum_rest;
867 panel_rot = ilog2(*rotation & DRM_MODE_ROTATE_MASK);
868 cmdline_rot = ilog2(cmdline->rotation_reflection & DRM_MODE_ROTATE_MASK);
869 sum_rot = (panel_rot + cmdline_rot) % 4;
871 panel_rest = *rotation & ~DRM_MODE_ROTATE_MASK;
872 cmdline_rest = cmdline->rotation_reflection & ~DRM_MODE_ROTATE_MASK;
873 sum_rest = panel_rest ^ cmdline_rest;
875 *rotation = (1 << sum_rot) | sum_rest;
879 * TODO: support 90 / 270 degree hardware rotation,
880 * depending on the hardware this may require the framebuffer
881 * to be in a specific tiling format.
883 if ((*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_180 ||
884 !plane->rotation_property)
887 for (i = 0; i < plane->rotation_property->num_values; i++)
888 valid_mask |= (1ULL << plane->rotation_property->values[i]);
890 if (!(*rotation & valid_mask))
895 EXPORT_SYMBOL(drm_client_rotation);
897 static int drm_client_modeset_commit_atomic(struct drm_client_dev *client, bool active)
899 struct drm_device *dev = client->dev;
900 struct drm_plane *plane;
901 struct drm_atomic_state *state;
902 struct drm_modeset_acquire_ctx ctx;
903 struct drm_mode_set *mode_set;
906 drm_modeset_acquire_init(&ctx, 0);
908 state = drm_atomic_state_alloc(dev);
914 state->acquire_ctx = &ctx;
916 drm_for_each_plane(plane, dev) {
917 struct drm_plane_state *plane_state;
919 plane_state = drm_atomic_get_plane_state(state, plane);
920 if (IS_ERR(plane_state)) {
921 ret = PTR_ERR(plane_state);
925 plane_state->rotation = DRM_MODE_ROTATE_0;
927 /* disable non-primary: */
928 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
931 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
936 drm_client_for_each_modeset(mode_set, client) {
937 struct drm_plane *primary = mode_set->crtc->primary;
938 unsigned int rotation;
940 if (drm_client_rotation(mode_set, &rotation)) {
941 struct drm_plane_state *plane_state;
943 /* Cannot fail as we've already gotten the plane state above */
944 plane_state = drm_atomic_get_new_plane_state(state, primary);
945 plane_state->rotation = rotation;
948 ret = __drm_atomic_helper_set_config(mode_set, state);
953 * __drm_atomic_helper_set_config() sets active when a
954 * mode is set, unconditionally clear it if we force DPMS off
957 struct drm_crtc *crtc = mode_set->crtc;
958 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
960 crtc_state->active = false;
964 ret = drm_atomic_commit(state);
970 drm_atomic_state_put(state);
972 drm_modeset_drop_locks(&ctx);
973 drm_modeset_acquire_fini(&ctx);
978 drm_atomic_state_clear(state);
979 drm_modeset_backoff(&ctx);
984 static int drm_client_modeset_commit_legacy(struct drm_client_dev *client)
986 struct drm_device *dev = client->dev;
987 struct drm_mode_set *mode_set;
988 struct drm_plane *plane;
991 drm_modeset_lock_all(dev);
992 drm_for_each_plane(plane, dev) {
993 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
994 drm_plane_force_disable(plane);
996 if (plane->rotation_property)
997 drm_mode_plane_set_obj_prop(plane,
998 plane->rotation_property,
1002 drm_client_for_each_modeset(mode_set, client) {
1003 struct drm_crtc *crtc = mode_set->crtc;
1005 if (crtc->funcs->cursor_set2) {
1006 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
1009 } else if (crtc->funcs->cursor_set) {
1010 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
1015 ret = drm_mode_set_config_internal(mode_set);
1020 drm_modeset_unlock_all(dev);
1026 * drm_client_modeset_commit_force() - Force commit CRTC configuration
1027 * @client: DRM client
1029 * Commit modeset configuration to crtcs without checking if there is a DRM master.
1032 * Zero on success or negative error code on failure.
1034 int drm_client_modeset_commit_force(struct drm_client_dev *client)
1036 struct drm_device *dev = client->dev;
1039 mutex_lock(&client->modeset_mutex);
1040 if (drm_drv_uses_atomic_modeset(dev))
1041 ret = drm_client_modeset_commit_atomic(client, true);
1043 ret = drm_client_modeset_commit_legacy(client);
1044 mutex_unlock(&client->modeset_mutex);
1048 EXPORT_SYMBOL(drm_client_modeset_commit_force);
1051 * drm_client_modeset_commit() - Commit CRTC configuration
1052 * @client: DRM client
1054 * Commit modeset configuration to crtcs.
1057 * Zero on success or negative error code on failure.
1059 int drm_client_modeset_commit(struct drm_client_dev *client)
1061 struct drm_device *dev = client->dev;
1064 if (!drm_master_internal_acquire(dev))
1067 ret = drm_client_modeset_commit_force(client);
1069 drm_master_internal_release(dev);
1073 EXPORT_SYMBOL(drm_client_modeset_commit);
1075 static void drm_client_modeset_dpms_legacy(struct drm_client_dev *client, int dpms_mode)
1077 struct drm_device *dev = client->dev;
1078 struct drm_connector *connector;
1079 struct drm_mode_set *modeset;
1082 drm_modeset_lock_all(dev);
1083 drm_client_for_each_modeset(modeset, client) {
1084 if (!modeset->crtc->enabled)
1087 for (j = 0; j < modeset->num_connectors; j++) {
1088 connector = modeset->connectors[j];
1089 connector->funcs->dpms(connector, dpms_mode);
1090 drm_object_property_set_value(&connector->base,
1091 dev->mode_config.dpms_property, dpms_mode);
1094 drm_modeset_unlock_all(dev);
1098 * drm_client_modeset_dpms() - Set DPMS mode
1099 * @client: DRM client
1102 * Note: For atomic drivers @mode is reduced to on/off.
1105 * Zero on success or negative error code on failure.
1107 int drm_client_modeset_dpms(struct drm_client_dev *client, int mode)
1109 struct drm_device *dev = client->dev;
1112 if (!drm_master_internal_acquire(dev))
1115 mutex_lock(&client->modeset_mutex);
1116 if (drm_drv_uses_atomic_modeset(dev))
1117 ret = drm_client_modeset_commit_atomic(client, mode == DRM_MODE_DPMS_ON);
1119 drm_client_modeset_dpms_legacy(client, mode);
1120 mutex_unlock(&client->modeset_mutex);
1122 drm_master_internal_release(dev);
1126 EXPORT_SYMBOL(drm_client_modeset_dpms);