Loading...
1// SPDX-License-Identifier: MIT
2/*
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>
8 */
9
10#include <linux/module.h>
11#include <linux/mutex.h>
12#include <linux/slab.h>
13
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>
22
23#include "drm_crtc_internal.h"
24#include "drm_internal.h"
25
26#define DRM_CLIENT_MAX_CLONED_CONNECTORS 8
27
28struct drm_client_offset {
29 int x, y;
30};
31
32int drm_client_modeset_create(struct drm_client_dev *client)
33{
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;
39 unsigned int i = 0;
40
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)
44 return -ENOMEM;
45
46 mutex_init(&client->modeset_mutex);
47
48 drm_for_each_crtc(crtc, dev)
49 client->modesets[i++].crtc = crtc;
50
51 /* Cloning is only supported in the single crtc case. */
52 if (num_crtc == 1)
53 max_connector_count = DRM_CLIENT_MAX_CLONED_CONNECTORS;
54
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)
59 goto err_free;
60 }
61
62 return 0;
63
64err_free:
65 drm_client_modeset_free(client);
66
67 return -ENOMEM;
68}
69
70static void drm_client_modeset_release(struct drm_client_dev *client)
71{
72 struct drm_mode_set *modeset;
73 unsigned int i;
74
75 drm_client_for_each_modeset(modeset, client) {
76 drm_mode_destroy(client->dev, modeset->mode);
77 modeset->mode = NULL;
78 modeset->fb = NULL;
79
80 for (i = 0; i < modeset->num_connectors; i++) {
81 drm_connector_put(modeset->connectors[i]);
82 modeset->connectors[i] = NULL;
83 }
84 modeset->num_connectors = 0;
85 }
86}
87
88void drm_client_modeset_free(struct drm_client_dev *client)
89{
90 struct drm_mode_set *modeset;
91
92 mutex_lock(&client->modeset_mutex);
93
94 drm_client_modeset_release(client);
95
96 drm_client_for_each_modeset(modeset, client)
97 kfree(modeset->connectors);
98
99 mutex_unlock(&client->modeset_mutex);
100
101 mutex_destroy(&client->modeset_mutex);
102 kfree(client->modesets);
103}
104
105static struct drm_mode_set *
106drm_client_find_modeset(struct drm_client_dev *client, struct drm_crtc *crtc)
107{
108 struct drm_mode_set *modeset;
109
110 drm_client_for_each_modeset(modeset, client)
111 if (modeset->crtc == crtc)
112 return modeset;
113
114 return NULL;
115}
116
117static struct drm_display_mode *
118drm_connector_has_preferred_mode(struct drm_connector *connector, int width, int height)
119{
120 struct drm_display_mode *mode;
121
122 list_for_each_entry(mode, &connector->modes, head) {
123 if (mode->hdisplay > width ||
124 mode->vdisplay > height)
125 continue;
126 if (mode->type & DRM_MODE_TYPE_PREFERRED)
127 return mode;
128 }
129 return NULL;
130}
131
132static struct drm_display_mode *
133drm_connector_pick_cmdline_mode(struct drm_connector *connector)
134{
135 struct drm_cmdline_mode *cmdline_mode;
136 struct drm_display_mode *mode;
137 bool prefer_non_interlace;
138
139 cmdline_mode = &connector->cmdline_mode;
140 if (cmdline_mode->specified == false)
141 return NULL;
142
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
145 */
146 if (cmdline_mode->rb || cmdline_mode->margins)
147 goto create_mode;
148
149 prefer_non_interlace = !cmdline_mode->interlace;
150again:
151 list_for_each_entry(mode, &connector->modes, head) {
152 /* Check (optional) mode name first */
153 if (!strcmp(mode->name, cmdline_mode->name))
154 return mode;
155
156 /* check width/height */
157 if (mode->hdisplay != cmdline_mode->xres ||
158 mode->vdisplay != cmdline_mode->yres)
159 continue;
160
161 if (cmdline_mode->refresh_specified) {
162 if (mode->vrefresh != cmdline_mode->refresh)
163 continue;
164 }
165
166 if (cmdline_mode->interlace) {
167 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
168 continue;
169 } else if (prefer_non_interlace) {
170 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
171 continue;
172 }
173 return mode;
174 }
175
176 if (prefer_non_interlace) {
177 prefer_non_interlace = false;
178 goto again;
179 }
180
181create_mode:
182 mode = drm_mode_create_from_cmdline_mode(connector->dev, cmdline_mode);
183 if (mode)
184 list_add(&mode->head, &connector->modes);
185
186 return mode;
187}
188
189static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
190{
191 bool enable;
192
193 if (connector->display_info.non_desktop)
194 return false;
195
196 if (strict)
197 enable = connector->status == connector_status_connected;
198 else
199 enable = connector->status != connector_status_disconnected;
200
201 return enable;
202}
203
204static void drm_client_connectors_enabled(struct drm_connector **connectors,
205 unsigned int connector_count,
206 bool *enabled)
207{
208 bool any_enabled = false;
209 struct drm_connector *connector;
210 int i = 0;
211
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");
217
218 any_enabled |= enabled[i];
219 }
220
221 if (any_enabled)
222 return;
223
224 for (i = 0; i < connector_count; i++)
225 enabled[i] = drm_connector_enabled(connectors[i], false);
226}
227
228static 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)
234{
235 int count, i, j;
236 bool can_clone = false;
237 struct drm_display_mode *dmt_mode, *mode;
238
239 /* only contemplate cloning in the single crtc case */
240 if (dev->mode_config.num_crtc > 1)
241 return false;
242
243 count = 0;
244 for (i = 0; i < connector_count; i++) {
245 if (enabled[i])
246 count++;
247 }
248
249 /* only contemplate cloning if more than one connector is enabled */
250 if (count <= 1)
251 return false;
252
253 /* check the command line or if nothing common pick 1024x768 */
254 can_clone = true;
255 for (i = 0; i < connector_count; i++) {
256 if (!enabled[i])
257 continue;
258 modes[i] = drm_connector_pick_cmdline_mode(connectors[i]);
259 if (!modes[i]) {
260 can_clone = false;
261 break;
262 }
263 for (j = 0; j < i; j++) {
264 if (!enabled[j])
265 continue;
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))
271 can_clone = false;
272 }
273 }
274
275 if (can_clone) {
276 DRM_DEBUG_KMS("can clone using command line\n");
277 return true;
278 }
279
280 /* try and find a 1024x768 mode on each connector */
281 can_clone = true;
282 dmt_mode = drm_mode_find_dmt(dev, 1024, 768, 60, false);
283
284 for (i = 0; i < connector_count; i++) {
285 if (!enabled[i])
286 continue;
287
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))
294 modes[i] = mode;
295 }
296 if (!modes[i])
297 can_clone = false;
298 }
299
300 if (can_clone) {
301 DRM_DEBUG_KMS("can clone using 1024x768\n");
302 return true;
303 }
304 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
305 return false;
306}
307
308static 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,
312 int idx,
313 int h_idx, int v_idx)
314{
315 struct drm_connector *connector;
316 int i;
317 int hoffset = 0, voffset = 0;
318
319 for (i = 0; i < connector_count; i++) {
320 connector = connectors[i];
321 if (!connector->has_tile)
322 continue;
323
324 if (!modes[i] && (h_idx || v_idx)) {
325 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
326 connector->base.id);
327 continue;
328 }
329 if (connector->tile_h_loc < h_idx)
330 hoffset += modes[i]->hdisplay;
331
332 if (connector->tile_v_loc < v_idx)
333 voffset += modes[i]->vdisplay;
334 }
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);
338 return 0;
339}
340
341static 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)
346{
347 const u64 mask = BIT_ULL(connector_count) - 1;
348 struct drm_connector *connector;
349 u64 conn_configured = 0;
350 int tile_pass = 0;
351 int i;
352
353retry:
354 for (i = 0; i < connector_count; i++) {
355 connector = connectors[i];
356
357 if (conn_configured & BIT_ULL(i))
358 continue;
359
360 if (enabled[i] == false) {
361 conn_configured |= BIT_ULL(i);
362 continue;
363 }
364
365 /* first pass over all the untiled connectors */
366 if (tile_pass == 0 && connector->has_tile)
367 continue;
368
369 if (tile_pass == 1) {
370 if (connector->tile_h_loc != 0 ||
371 connector->tile_v_loc != 0)
372 continue;
373
374 } else {
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 */
378 continue;
379
380 /*
381 * find the tile offsets for this pass - need to find
382 * all tiles left and above
383 */
384 drm_client_get_tile_offsets(connectors, connector_count, modes, offsets, i,
385 connector->tile_h_loc, connector->tile_v_loc);
386 }
387 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
388 connector->base.id);
389
390 /* got for command line mode first */
391 modes[i] = drm_connector_pick_cmdline_mode(connector);
392 if (!modes[i]) {
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);
396 }
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)
400 break;
401 }
402 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
403 "none");
404 conn_configured |= BIT_ULL(i);
405 }
406
407 if ((conn_configured & mask) != mask) {
408 tile_pass++;
409 goto retry;
410 }
411 return true;
412}
413
414static bool connector_has_possible_crtc(struct drm_connector *connector,
415 struct drm_crtc *crtc)
416{
417 struct drm_encoder *encoder;
418 int i;
419
420 drm_connector_for_each_possible_encoder(connector, encoder, i) {
421 if (encoder->possible_crtcs & drm_crtc_mask(crtc))
422 return true;
423 }
424
425 return false;
426}
427
428static 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)
434{
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;
440 int o;
441
442 if (n == connector_count)
443 return 0;
444
445 connector = connectors[n];
446
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)
451 return best_score;
452
453 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
454 if (!crtcs)
455 return best_score;
456
457 my_score = 1;
458 if (connector->status == connector_status_connected)
459 my_score++;
460 if (connector->cmdline_mode.specified)
461 my_score++;
462 if (drm_connector_has_preferred_mode(connector, width, height))
463 my_score++;
464
465 /*
466 * select a crtc for this connector and then attempt to configure
467 * remaining connectors
468 */
469 drm_client_for_each_modeset(modeset, client) {
470 crtc = modeset->crtc;
471
472 if (!connector_has_possible_crtc(connector, crtc))
473 continue;
474
475 for (o = 0; o < n; o++)
476 if (best_crtcs[o] == crtc)
477 break;
478
479 if (o < n) {
480 /* ignore cloning unless only a single crtc */
481 if (dev->mode_config.num_crtc > 1)
482 continue;
483
484 if (!drm_mode_equal(modes[o], modes[n]))
485 continue;
486 }
487
488 crtcs[n] = crtc;
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) {
493 best_score = score;
494 memcpy(best_crtcs, crtcs, connector_count * sizeof(*crtcs));
495 }
496 }
497
498 kfree(crtcs);
499 return best_score;
500}
501
502/* Try to read the BIOS display configuration and use it for the initial config */
503static 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)
510{
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;
514 int i, j;
515 bool *save_enabled;
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;
520
521 if (!drm_drv_uses_atomic_modeset(dev))
522 return false;
523
524 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
525 if (!save_enabled)
526 return false;
527
528 drm_modeset_acquire_init(&ctx, 0);
529
530 while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
531 drm_modeset_backoff(&ctx);
532
533 memcpy(save_enabled, enabled, count);
534 mask = GENMASK(count - 1, 0);
535 conn_configured = 0;
536retry:
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;
542
543 connector = connectors[i];
544
545 if (conn_configured & BIT(i))
546 continue;
547
548 if (conn_seq == 0 && !connector->has_tile)
549 continue;
550
551 if (connector->status == connector_status_connected)
552 num_connectors_detected++;
553
554 if (!enabled[i]) {
555 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
556 connector->name);
557 conn_configured |= BIT(i);
558 continue;
559 }
560
561 if (connector->force == DRM_FORCE_OFF) {
562 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
563 connector->name);
564 enabled[i] = false;
565 continue;
566 }
567
568 encoder = connector->state->best_encoder;
569 if (!encoder || WARN_ON(!connector->state->crtc)) {
570 if (connector->force > DRM_FORCE_OFF)
571 goto bail;
572
573 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
574 connector->name);
575 enabled[i] = false;
576 conn_configured |= BIT(i);
577 continue;
578 }
579
580 num_connectors_enabled++;
581
582 new_crtc = connector->state->crtc;
583
584 /*
585 * Make sure we're not trying to drive multiple connectors
586 * with a single CRTC, since our cloning support may not
587 * match the BIOS.
588 */
589 for (j = 0; j < count; j++) {
590 if (crtcs[j] == new_crtc) {
591 DRM_DEBUG_KMS("fallback: cloned configuration\n");
592 goto bail;
593 }
594 }
595
596 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
597 connector->name);
598
599 /* go for command line mode first */
600 modes[i] = drm_connector_pick_cmdline_mode(connector);
601
602 /* try for preferred next */
603 if (!modes[i]) {
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);
607 }
608
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",
612 connector->name);
613 modes[i] = list_first_entry(&connector->modes,
614 struct drm_display_mode,
615 head);
616 }
617
618 /* last resort: use current mode */
619 if (!modes[i]) {
620 /*
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.
627 *
628 * This is crtc->mode and not crtc->state->mode for the
629 * fastboot check to work correctly.
630 */
631 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
632 connector->name);
633 modes[i] = &connector->state->crtc->mode;
634 }
635 crtcs[i] = new_crtc;
636
637 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
638 connector->name,
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" : "");
643
644 fallback = false;
645 conn_configured |= BIT(i);
646 }
647
648 if ((conn_configured & mask) != mask && conn_configured != conn_seq)
649 goto retry;
650
651 /*
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.
655 */
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);
661 fallback = true;
662 }
663
664 if (fallback) {
665bail:
666 DRM_DEBUG_KMS("Not using firmware configuration\n");
667 memcpy(enabled, save_enabled, count);
668 ret = false;
669 }
670
671 drm_modeset_drop_locks(&ctx);
672 drm_modeset_acquire_fini(&ctx);
673
674 kfree(save_enabled);
675 return ret;
676}
677
678/**
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)
683 *
684 * This function sets up display pipelines for enabled connectors and stores the
685 * config in the client's modeset array.
686 *
687 * Returns:
688 * Zero on success or negative error code on failure.
689 */
690int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height)
691{
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;
700 int i, ret = 0;
701 bool *enabled;
702
703 DRM_DEBUG_KMS("\n");
704
705 if (!width)
706 width = dev->mode_config.max_width;
707 if (!height)
708 height = dev->mode_config.max_height;
709
710 drm_connector_list_iter_begin(dev, &conn_iter);
711 drm_client_for_each_connector_iter(connector, &conn_iter) {
712 struct drm_connector **tmp;
713
714 tmp = krealloc(connectors, (connector_count + 1) * sizeof(*connectors), GFP_KERNEL);
715 if (!tmp) {
716 ret = -ENOMEM;
717 goto free_connectors;
718 }
719
720 connectors = tmp;
721 drm_connector_get(connector);
722 connectors[connector_count++] = connector;
723 }
724 drm_connector_list_iter_end(&conn_iter);
725
726 if (!connector_count)
727 return 0;
728
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");
735 ret = -ENOMEM;
736 goto out;
737 }
738
739 mutex_lock(&client->modeset_mutex);
740
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);
747
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));
753
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");
759
760 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
761 width, height);
762
763 drm_client_pick_crtcs(client, connectors, connector_count,
764 crtcs, modes, 0, width, height);
765 }
766 mutex_unlock(&dev->mode_config.mutex);
767
768 drm_client_modeset_release(client);
769
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];
774
775 if (mode && crtc) {
776 struct drm_mode_set *modeset = drm_client_find_modeset(client, crtc);
777 struct drm_connector *connector = connectors[i];
778
779 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
780 mode->name, crtc->base.id, offset->x, offset->y);
781
782 if (WARN_ON_ONCE(modeset->num_connectors == DRM_CLIENT_MAX_CLONED_CONNECTORS ||
783 (dev->mode_config.num_crtc > 1 && modeset->num_connectors == 1))) {
784 ret = -EINVAL;
785 break;
786 }
787
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;
793 }
794 }
795
796 mutex_unlock(&client->modeset_mutex);
797out:
798 kfree(crtcs);
799 kfree(modes);
800 kfree(offsets);
801 kfree(enabled);
802free_connectors:
803 for (i = 0; i < connector_count; i++)
804 drm_connector_put(connectors[i]);
805 kfree(connectors);
806
807 return ret;
808}
809EXPORT_SYMBOL(drm_client_modeset_probe);
810
811/**
812 * drm_client_rotation() - Check the initial rotation value
813 * @modeset: DRM modeset
814 * @rotation: Returned rotation value
815 *
816 * This function checks if the primary plane in @modeset can hw rotate
817 * to match the rotation needed on its connector.
818 *
819 * Note: Currently only 0 and 180 degrees are supported.
820 *
821 * Return:
822 * True if the plane can do the rotation, false otherwise.
823 */
824bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation)
825{
826 struct drm_connector *connector = modeset->connectors[0];
827 struct drm_plane *plane = modeset->crtc->primary;
828 struct drm_cmdline_mode *cmdline;
829 u64 valid_mask = 0;
830 unsigned int i;
831
832 if (!modeset->num_connectors)
833 return false;
834
835 switch (connector->display_info.panel_orientation) {
836 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
837 *rotation = DRM_MODE_ROTATE_180;
838 break;
839 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
840 *rotation = DRM_MODE_ROTATE_90;
841 break;
842 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
843 *rotation = DRM_MODE_ROTATE_270;
844 break;
845 default:
846 *rotation = DRM_MODE_ROTATE_0;
847 }
848
849 /**
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.
853 *
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.
857 *
858 * Reflections on the other hand are pretty trivial to deal with, a
859 * simple XOR between the two handle the addition nicely.
860 */
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;
866
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;
870
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;
874
875 *rotation = (1 << sum_rot) | sum_rest;
876 }
877
878 /*
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.
882 */
883 if ((*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_180 ||
884 !plane->rotation_property)
885 return false;
886
887 for (i = 0; i < plane->rotation_property->num_values; i++)
888 valid_mask |= (1ULL << plane->rotation_property->values[i]);
889
890 if (!(*rotation & valid_mask))
891 return false;
892
893 return true;
894}
895EXPORT_SYMBOL(drm_client_rotation);
896
897static int drm_client_modeset_commit_atomic(struct drm_client_dev *client, bool active)
898{
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;
904 int ret;
905
906 drm_modeset_acquire_init(&ctx, 0);
907
908 state = drm_atomic_state_alloc(dev);
909 if (!state) {
910 ret = -ENOMEM;
911 goto out_ctx;
912 }
913
914 state->acquire_ctx = &ctx;
915retry:
916 drm_for_each_plane(plane, dev) {
917 struct drm_plane_state *plane_state;
918
919 plane_state = drm_atomic_get_plane_state(state, plane);
920 if (IS_ERR(plane_state)) {
921 ret = PTR_ERR(plane_state);
922 goto out_state;
923 }
924
925 plane_state->rotation = DRM_MODE_ROTATE_0;
926
927 /* disable non-primary: */
928 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
929 continue;
930
931 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
932 if (ret != 0)
933 goto out_state;
934 }
935
936 drm_client_for_each_modeset(mode_set, client) {
937 struct drm_plane *primary = mode_set->crtc->primary;
938 unsigned int rotation;
939
940 if (drm_client_rotation(mode_set, &rotation)) {
941 struct drm_plane_state *plane_state;
942
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;
946 }
947
948 ret = __drm_atomic_helper_set_config(mode_set, state);
949 if (ret != 0)
950 goto out_state;
951
952 /*
953 * __drm_atomic_helper_set_config() sets active when a
954 * mode is set, unconditionally clear it if we force DPMS off
955 */
956 if (!active) {
957 struct drm_crtc *crtc = mode_set->crtc;
958 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
959
960 crtc_state->active = false;
961 }
962 }
963
964 ret = drm_atomic_commit(state);
965
966out_state:
967 if (ret == -EDEADLK)
968 goto backoff;
969
970 drm_atomic_state_put(state);
971out_ctx:
972 drm_modeset_drop_locks(&ctx);
973 drm_modeset_acquire_fini(&ctx);
974
975 return ret;
976
977backoff:
978 drm_atomic_state_clear(state);
979 drm_modeset_backoff(&ctx);
980
981 goto retry;
982}
983
984static int drm_client_modeset_commit_legacy(struct drm_client_dev *client)
985{
986 struct drm_device *dev = client->dev;
987 struct drm_mode_set *mode_set;
988 struct drm_plane *plane;
989 int ret = 0;
990
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);
995
996 if (plane->rotation_property)
997 drm_mode_plane_set_obj_prop(plane,
998 plane->rotation_property,
999 DRM_MODE_ROTATE_0);
1000 }
1001
1002 drm_client_for_each_modeset(mode_set, client) {
1003 struct drm_crtc *crtc = mode_set->crtc;
1004
1005 if (crtc->funcs->cursor_set2) {
1006 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
1007 if (ret)
1008 goto out;
1009 } else if (crtc->funcs->cursor_set) {
1010 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
1011 if (ret)
1012 goto out;
1013 }
1014
1015 ret = drm_mode_set_config_internal(mode_set);
1016 if (ret)
1017 goto out;
1018 }
1019out:
1020 drm_modeset_unlock_all(dev);
1021
1022 return ret;
1023}
1024
1025/**
1026 * drm_client_modeset_commit_force() - Force commit CRTC configuration
1027 * @client: DRM client
1028 *
1029 * Commit modeset configuration to crtcs without checking if there is a DRM master.
1030 *
1031 * Returns:
1032 * Zero on success or negative error code on failure.
1033 */
1034int drm_client_modeset_commit_force(struct drm_client_dev *client)
1035{
1036 struct drm_device *dev = client->dev;
1037 int ret;
1038
1039 mutex_lock(&client->modeset_mutex);
1040 if (drm_drv_uses_atomic_modeset(dev))
1041 ret = drm_client_modeset_commit_atomic(client, true);
1042 else
1043 ret = drm_client_modeset_commit_legacy(client);
1044 mutex_unlock(&client->modeset_mutex);
1045
1046 return ret;
1047}
1048EXPORT_SYMBOL(drm_client_modeset_commit_force);
1049
1050/**
1051 * drm_client_modeset_commit() - Commit CRTC configuration
1052 * @client: DRM client
1053 *
1054 * Commit modeset configuration to crtcs.
1055 *
1056 * Returns:
1057 * Zero on success or negative error code on failure.
1058 */
1059int drm_client_modeset_commit(struct drm_client_dev *client)
1060{
1061 struct drm_device *dev = client->dev;
1062 int ret;
1063
1064 if (!drm_master_internal_acquire(dev))
1065 return -EBUSY;
1066
1067 ret = drm_client_modeset_commit_force(client);
1068
1069 drm_master_internal_release(dev);
1070
1071 return ret;
1072}
1073EXPORT_SYMBOL(drm_client_modeset_commit);
1074
1075static void drm_client_modeset_dpms_legacy(struct drm_client_dev *client, int dpms_mode)
1076{
1077 struct drm_device *dev = client->dev;
1078 struct drm_connector *connector;
1079 struct drm_mode_set *modeset;
1080 int j;
1081
1082 drm_modeset_lock_all(dev);
1083 drm_client_for_each_modeset(modeset, client) {
1084 if (!modeset->crtc->enabled)
1085 continue;
1086
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);
1092 }
1093 }
1094 drm_modeset_unlock_all(dev);
1095}
1096
1097/**
1098 * drm_client_modeset_dpms() - Set DPMS mode
1099 * @client: DRM client
1100 * @mode: DPMS mode
1101 *
1102 * Note: For atomic drivers @mode is reduced to on/off.
1103 *
1104 * Returns:
1105 * Zero on success or negative error code on failure.
1106 */
1107int drm_client_modeset_dpms(struct drm_client_dev *client, int mode)
1108{
1109 struct drm_device *dev = client->dev;
1110 int ret = 0;
1111
1112 if (!drm_master_internal_acquire(dev))
1113 return -EBUSY;
1114
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);
1118 else
1119 drm_client_modeset_dpms_legacy(client, mode);
1120 mutex_unlock(&client->modeset_mutex);
1121
1122 drm_master_internal_release(dev);
1123
1124 return ret;
1125}
1126EXPORT_SYMBOL(drm_client_modeset_dpms);
1// SPDX-License-Identifier: MIT
2/*
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>
8 */
9
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>
15
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>
25
26#include "drm_crtc_internal.h"
27#include "drm_internal.h"
28
29#define DRM_CLIENT_MAX_CLONED_CONNECTORS 8
30
31struct drm_client_offset {
32 int x, y;
33};
34
35int drm_client_modeset_create(struct drm_client_dev *client)
36{
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;
42 unsigned int i = 0;
43
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)
47 return -ENOMEM;
48
49 mutex_init(&client->modeset_mutex);
50
51 drm_for_each_crtc(crtc, dev)
52 client->modesets[i++].crtc = crtc;
53
54 /* Cloning is only supported in the single crtc case. */
55 if (num_crtc == 1)
56 max_connector_count = DRM_CLIENT_MAX_CLONED_CONNECTORS;
57
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)
62 goto err_free;
63 }
64
65 return 0;
66
67err_free:
68 drm_client_modeset_free(client);
69
70 return -ENOMEM;
71}
72
73static void drm_client_modeset_release(struct drm_client_dev *client)
74{
75 struct drm_mode_set *modeset;
76 unsigned int i;
77
78 drm_client_for_each_modeset(modeset, client) {
79 drm_mode_destroy(client->dev, modeset->mode);
80 modeset->mode = NULL;
81 modeset->fb = NULL;
82
83 for (i = 0; i < modeset->num_connectors; i++) {
84 drm_connector_put(modeset->connectors[i]);
85 modeset->connectors[i] = NULL;
86 }
87 modeset->num_connectors = 0;
88 }
89}
90
91void drm_client_modeset_free(struct drm_client_dev *client)
92{
93 struct drm_mode_set *modeset;
94
95 mutex_lock(&client->modeset_mutex);
96
97 drm_client_modeset_release(client);
98
99 drm_client_for_each_modeset(modeset, client)
100 kfree(modeset->connectors);
101
102 mutex_unlock(&client->modeset_mutex);
103
104 mutex_destroy(&client->modeset_mutex);
105 kfree(client->modesets);
106}
107
108static struct drm_mode_set *
109drm_client_find_modeset(struct drm_client_dev *client, struct drm_crtc *crtc)
110{
111 struct drm_mode_set *modeset;
112
113 drm_client_for_each_modeset(modeset, client)
114 if (modeset->crtc == crtc)
115 return modeset;
116
117 return NULL;
118}
119
120static struct drm_display_mode *
121drm_connector_get_tiled_mode(struct drm_connector *connector)
122{
123 struct drm_display_mode *mode;
124
125 list_for_each_entry(mode, &connector->modes, head) {
126 if (mode->hdisplay == connector->tile_h_size &&
127 mode->vdisplay == connector->tile_v_size)
128 return mode;
129 }
130 return NULL;
131}
132
133static struct drm_display_mode *
134drm_connector_fallback_non_tiled_mode(struct drm_connector *connector)
135{
136 struct drm_display_mode *mode;
137
138 list_for_each_entry(mode, &connector->modes, head) {
139 if (mode->hdisplay == connector->tile_h_size &&
140 mode->vdisplay == connector->tile_v_size)
141 continue;
142 return mode;
143 }
144 return NULL;
145}
146
147static struct drm_display_mode *
148drm_connector_has_preferred_mode(struct drm_connector *connector, int width, int height)
149{
150 struct drm_display_mode *mode;
151
152 list_for_each_entry(mode, &connector->modes, head) {
153 if (mode->hdisplay > width ||
154 mode->vdisplay > height)
155 continue;
156 if (mode->type & DRM_MODE_TYPE_PREFERRED)
157 return mode;
158 }
159 return NULL;
160}
161
162static struct drm_display_mode *drm_connector_pick_cmdline_mode(struct drm_connector *connector)
163{
164 struct drm_cmdline_mode *cmdline_mode;
165 struct drm_display_mode *mode;
166 bool prefer_non_interlace;
167
168 /*
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
171 * list.
172 */
173
174 list_for_each_entry(mode, &connector->modes, head) {
175 if (mode->type & DRM_MODE_TYPE_USERDEF)
176 return mode;
177 }
178
179 cmdline_mode = &connector->cmdline_mode;
180 if (cmdline_mode->specified == false)
181 return NULL;
182
183 /*
184 * Attempt to find a matching mode in the list of modes we
185 * have gotten so far.
186 */
187
188 prefer_non_interlace = !cmdline_mode->interlace;
189again:
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)
194 continue;
195
196 if (cmdline_mode->refresh_specified) {
197 if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
198 continue;
199 }
200
201 if (cmdline_mode->interlace) {
202 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
203 continue;
204 } else if (prefer_non_interlace) {
205 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
206 continue;
207 }
208 return mode;
209 }
210
211 if (prefer_non_interlace) {
212 prefer_non_interlace = false;
213 goto again;
214 }
215
216 return NULL;
217}
218
219static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
220{
221 bool enable;
222
223 if (connector->display_info.non_desktop)
224 return false;
225
226 if (strict)
227 enable = connector->status == connector_status_connected;
228 else
229 enable = connector->status != connector_status_disconnected;
230
231 return enable;
232}
233
234static void drm_client_connectors_enabled(struct drm_connector **connectors,
235 unsigned int connector_count,
236 bool *enabled)
237{
238 bool any_enabled = false;
239 struct drm_connector *connector;
240 int i = 0;
241
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]));
247
248 any_enabled |= enabled[i];
249 }
250
251 if (any_enabled)
252 return;
253
254 for (i = 0; i < connector_count; i++)
255 enabled[i] = drm_connector_enabled(connectors[i], false);
256}
257
258static 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)
264{
265 int count, i, j;
266 bool can_clone = false;
267 struct drm_display_mode *dmt_mode, *mode;
268
269 /* only contemplate cloning in the single crtc case */
270 if (dev->mode_config.num_crtc > 1)
271 return false;
272
273 count = 0;
274 for (i = 0; i < connector_count; i++) {
275 if (enabled[i])
276 count++;
277 }
278
279 /* only contemplate cloning if more than one connector is enabled */
280 if (count <= 1)
281 return false;
282
283 /* check the command line or if nothing common pick 1024x768 */
284 can_clone = true;
285 for (i = 0; i < connector_count; i++) {
286 if (!enabled[i])
287 continue;
288 modes[i] = drm_connector_pick_cmdline_mode(connectors[i]);
289 if (!modes[i]) {
290 can_clone = false;
291 break;
292 }
293 for (j = 0; j < i; j++) {
294 if (!enabled[j])
295 continue;
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))
301 can_clone = false;
302 }
303 }
304
305 if (can_clone) {
306 DRM_DEBUG_KMS("can clone using command line\n");
307 return true;
308 }
309
310 /* try and find a 1024x768 mode on each connector */
311 can_clone = true;
312 dmt_mode = drm_mode_find_dmt(dev, 1024, 768, 60, false);
313
314 if (!dmt_mode)
315 goto fail;
316
317 for (i = 0; i < connector_count; i++) {
318 if (!enabled[i])
319 continue;
320
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))
327 modes[i] = mode;
328 }
329 if (!modes[i])
330 can_clone = false;
331 }
332 kfree(dmt_mode);
333
334 if (can_clone) {
335 DRM_DEBUG_KMS("can clone using 1024x768\n");
336 return true;
337 }
338fail:
339 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
340 return false;
341}
342
343static 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,
347 int idx,
348 int h_idx, int v_idx)
349{
350 struct drm_connector *connector;
351 int i;
352 int hoffset = 0, voffset = 0;
353
354 for (i = 0; i < connector_count; i++) {
355 connector = connectors[i];
356 if (!connector->has_tile)
357 continue;
358
359 if (!modes[i] && (h_idx || v_idx)) {
360 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
361 connector->base.id);
362 continue;
363 }
364 if (connector->tile_h_loc < h_idx)
365 hoffset += modes[i]->hdisplay;
366
367 if (connector->tile_v_loc < v_idx)
368 voffset += modes[i]->vdisplay;
369 }
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);
373 return 0;
374}
375
376static 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)
381{
382 const u64 mask = BIT_ULL(connector_count) - 1;
383 struct drm_connector *connector;
384 u64 conn_configured = 0;
385 int tile_pass = 0;
386 int num_tiled_conns = 0;
387 int i;
388
389 for (i = 0; i < connector_count; i++) {
390 if (connectors[i]->has_tile &&
391 connectors[i]->status == connector_status_connected)
392 num_tiled_conns++;
393 }
394
395retry:
396 for (i = 0; i < connector_count; i++) {
397 connector = connectors[i];
398
399 if (conn_configured & BIT_ULL(i))
400 continue;
401
402 if (enabled[i] == false) {
403 conn_configured |= BIT_ULL(i);
404 continue;
405 }
406
407 /* first pass over all the untiled connectors */
408 if (tile_pass == 0 && connector->has_tile)
409 continue;
410
411 if (tile_pass == 1) {
412 if (connector->tile_h_loc != 0 ||
413 connector->tile_v_loc != 0)
414 continue;
415
416 } else {
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 */
420 continue;
421
422 /*
423 * find the tile offsets for this pass - need to find
424 * all tiles left and above
425 */
426 drm_client_get_tile_offsets(connectors, connector_count, modes, offsets, i,
427 connector->tile_h_loc, connector->tile_v_loc);
428 }
429 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
430 connector->base.id);
431
432 /* got for command line mode first */
433 modes[i] = drm_connector_pick_cmdline_mode(connector);
434 if (!modes[i]) {
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);
438 }
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)
442 break;
443 }
444 /*
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.
451 */
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",
459 connector->base.id);
460 modes[i] = drm_connector_fallback_non_tiled_mode(connector);
461 } else {
462 modes[i] = drm_connector_get_tiled_mode(connector);
463 }
464 }
465
466 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
467 "none");
468 conn_configured |= BIT_ULL(i);
469 }
470
471 if ((conn_configured & mask) != mask) {
472 tile_pass++;
473 goto retry;
474 }
475 return true;
476}
477
478static bool connector_has_possible_crtc(struct drm_connector *connector,
479 struct drm_crtc *crtc)
480{
481 struct drm_encoder *encoder;
482
483 drm_connector_for_each_possible_encoder(connector, encoder) {
484 if (encoder->possible_crtcs & drm_crtc_mask(crtc))
485 return true;
486 }
487
488 return false;
489}
490
491static 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)
497{
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;
503 int o;
504
505 if (n == connector_count)
506 return 0;
507
508 connector = connectors[n];
509
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)
514 return best_score;
515
516 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
517 if (!crtcs)
518 return best_score;
519
520 my_score = 1;
521 if (connector->status == connector_status_connected)
522 my_score++;
523 if (connector->cmdline_mode.specified)
524 my_score++;
525 if (drm_connector_has_preferred_mode(connector, width, height))
526 my_score++;
527
528 /*
529 * select a crtc for this connector and then attempt to configure
530 * remaining connectors
531 */
532 drm_client_for_each_modeset(modeset, client) {
533 crtc = modeset->crtc;
534
535 if (!connector_has_possible_crtc(connector, crtc))
536 continue;
537
538 for (o = 0; o < n; o++)
539 if (best_crtcs[o] == crtc)
540 break;
541
542 if (o < n) {
543 /* ignore cloning unless only a single crtc */
544 if (dev->mode_config.num_crtc > 1)
545 continue;
546
547 if (!drm_mode_equal(modes[o], modes[n]))
548 continue;
549 }
550
551 crtcs[n] = crtc;
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) {
556 best_score = score;
557 memcpy(best_crtcs, crtcs, connector_count * sizeof(*crtcs));
558 }
559 }
560
561 kfree(crtcs);
562 return best_score;
563}
564
565/* Try to read the BIOS display configuration and use it for the initial config */
566static 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)
573{
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;
577 int i, j;
578 bool *save_enabled;
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;
584
585 if (!drm_drv_uses_atomic_modeset(dev))
586 return false;
587
588 if (WARN_ON(count <= 0))
589 return false;
590
591 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
592 if (!save_enabled)
593 return false;
594
595 drm_modeset_acquire_init(&ctx, 0);
596
597 while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
598 drm_modeset_backoff(&ctx);
599
600 memcpy(save_enabled, enabled, count);
601 mask = GENMASK(count - 1, 0);
602 conn_configured = 0;
603 for (i = 0; i < count; i++) {
604 if (connectors[i]->has_tile &&
605 connectors[i]->status == connector_status_connected)
606 num_tiled_conns++;
607 }
608retry:
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;
614
615 connector = connectors[i];
616
617 if (conn_configured & BIT(i))
618 continue;
619
620 if (conn_seq == 0 && !connector->has_tile)
621 continue;
622
623 if (connector->status == connector_status_connected)
624 num_connectors_detected++;
625
626 if (!enabled[i]) {
627 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
628 connector->name);
629 conn_configured |= BIT(i);
630 continue;
631 }
632
633 if (connector->force == DRM_FORCE_OFF) {
634 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
635 connector->name);
636 enabled[i] = false;
637 continue;
638 }
639
640 encoder = connector->state->best_encoder;
641 if (!encoder || WARN_ON(!connector->state->crtc)) {
642 if (connector->force > DRM_FORCE_OFF)
643 goto bail;
644
645 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
646 connector->name);
647 enabled[i] = false;
648 conn_configured |= BIT(i);
649 continue;
650 }
651
652 num_connectors_enabled++;
653
654 new_crtc = connector->state->crtc;
655
656 /*
657 * Make sure we're not trying to drive multiple connectors
658 * with a single CRTC, since our cloning support may not
659 * match the BIOS.
660 */
661 for (j = 0; j < count; j++) {
662 if (crtcs[j] == new_crtc) {
663 DRM_DEBUG_KMS("fallback: cloned configuration\n");
664 goto bail;
665 }
666 }
667
668 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
669 connector->name);
670
671 /* go for command line mode first */
672 modes[i] = drm_connector_pick_cmdline_mode(connector);
673
674 /* try for preferred next */
675 if (!modes[i]) {
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);
679 }
680
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",
684 connector->name);
685 modes[i] = list_first_entry(&connector->modes,
686 struct drm_display_mode,
687 head);
688 }
689
690 /* last resort: use current mode */
691 if (!modes[i]) {
692 /*
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.
699 *
700 * This is crtc->mode and not crtc->state->mode for the
701 * fastboot check to work correctly.
702 */
703 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
704 connector->name);
705 modes[i] = &connector->state->crtc->mode;
706 }
707 /*
708 * In case of tiled modes, if all tiles are not present
709 * then fallback to a non tiled mode.
710 */
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",
714 connector->base.id);
715 modes[i] = drm_connector_fallback_non_tiled_mode(connector);
716 }
717 crtcs[i] = new_crtc;
718
719 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
720 connector->name,
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" : "");
725
726 fallback = false;
727 conn_configured |= BIT(i);
728 }
729
730 if ((conn_configured & mask) != mask && conn_configured != conn_seq)
731 goto retry;
732
733 /*
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.
737 */
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);
743 fallback = true;
744 }
745
746 if (fallback) {
747bail:
748 DRM_DEBUG_KMS("Not using firmware configuration\n");
749 memcpy(enabled, save_enabled, count);
750 ret = false;
751 }
752
753 drm_modeset_drop_locks(&ctx);
754 drm_modeset_acquire_fini(&ctx);
755
756 kfree(save_enabled);
757 return ret;
758}
759
760/**
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)
765 *
766 * This function sets up display pipelines for enabled connectors and stores the
767 * config in the client's modeset array.
768 *
769 * Returns:
770 * Zero on success or negative error code on failure.
771 */
772int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height)
773{
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 /* points to modes protected by mode_config.mutex */
781 struct drm_display_mode **modes;
782 struct drm_crtc **crtcs;
783 int i, ret = 0;
784 bool *enabled;
785
786 DRM_DEBUG_KMS("\n");
787
788 if (!width)
789 width = dev->mode_config.max_width;
790 if (!height)
791 height = dev->mode_config.max_height;
792
793 drm_connector_list_iter_begin(dev, &conn_iter);
794 drm_client_for_each_connector_iter(connector, &conn_iter) {
795 struct drm_connector **tmp;
796
797 tmp = krealloc(connectors, (connector_count + 1) * sizeof(*connectors), GFP_KERNEL);
798 if (!tmp) {
799 ret = -ENOMEM;
800 goto free_connectors;
801 }
802
803 connectors = tmp;
804 drm_connector_get(connector);
805 connectors[connector_count++] = connector;
806 }
807 drm_connector_list_iter_end(&conn_iter);
808
809 if (!connector_count)
810 return 0;
811
812 crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
813 modes = kcalloc(connector_count, sizeof(*modes), GFP_KERNEL);
814 offsets = kcalloc(connector_count, sizeof(*offsets), GFP_KERNEL);
815 enabled = kcalloc(connector_count, sizeof(bool), GFP_KERNEL);
816 if (!crtcs || !modes || !enabled || !offsets) {
817 DRM_ERROR("Memory allocation failed\n");
818 ret = -ENOMEM;
819 goto out;
820 }
821
822 mutex_lock(&client->modeset_mutex);
823
824 mutex_lock(&dev->mode_config.mutex);
825 for (i = 0; i < connector_count; i++)
826 total_modes_count += connectors[i]->funcs->fill_modes(connectors[i], width, height);
827 if (!total_modes_count)
828 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
829 drm_client_connectors_enabled(connectors, connector_count, enabled);
830
831 if (!drm_client_firmware_config(client, connectors, connector_count, crtcs,
832 modes, offsets, enabled, width, height)) {
833 memset(modes, 0, connector_count * sizeof(*modes));
834 memset(crtcs, 0, connector_count * sizeof(*crtcs));
835 memset(offsets, 0, connector_count * sizeof(*offsets));
836
837 if (!drm_client_target_cloned(dev, connectors, connector_count, modes,
838 offsets, enabled, width, height) &&
839 !drm_client_target_preferred(connectors, connector_count, modes,
840 offsets, enabled, width, height))
841 DRM_ERROR("Unable to find initial modes\n");
842
843 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
844 width, height);
845
846 drm_client_pick_crtcs(client, connectors, connector_count,
847 crtcs, modes, 0, width, height);
848 }
849
850 drm_client_modeset_release(client);
851
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];
856
857 if (mode && crtc) {
858 struct drm_mode_set *modeset = drm_client_find_modeset(client, crtc);
859 struct drm_connector *connector = connectors[i];
860
861 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
862 mode->name, crtc->base.id, offset->x, offset->y);
863
864 if (WARN_ON_ONCE(modeset->num_connectors == DRM_CLIENT_MAX_CLONED_CONNECTORS ||
865 (dev->mode_config.num_crtc > 1 && modeset->num_connectors == 1))) {
866 ret = -EINVAL;
867 break;
868 }
869
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;
876 }
877 }
878 mutex_unlock(&dev->mode_config.mutex);
879
880 mutex_unlock(&client->modeset_mutex);
881out:
882 kfree(crtcs);
883 kfree(modes);
884 kfree(offsets);
885 kfree(enabled);
886free_connectors:
887 for (i = 0; i < connector_count; i++)
888 drm_connector_put(connectors[i]);
889 kfree(connectors);
890
891 return ret;
892}
893EXPORT_SYMBOL(drm_client_modeset_probe);
894
895/**
896 * drm_client_rotation() - Check the initial rotation value
897 * @modeset: DRM modeset
898 * @rotation: Returned rotation value
899 *
900 * This function checks if the primary plane in @modeset can hw rotate
901 * to match the rotation needed on its connector.
902 *
903 * Note: Currently only 0 and 180 degrees are supported.
904 *
905 * Return:
906 * True if the plane can do the rotation, false otherwise.
907 */
908bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation)
909{
910 struct drm_connector *connector = modeset->connectors[0];
911 struct drm_plane *plane = modeset->crtc->primary;
912 struct drm_cmdline_mode *cmdline;
913 u64 valid_mask = 0;
914 unsigned int i;
915
916 if (!modeset->num_connectors)
917 return false;
918
919 switch (connector->display_info.panel_orientation) {
920 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
921 *rotation = DRM_MODE_ROTATE_180;
922 break;
923 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
924 *rotation = DRM_MODE_ROTATE_90;
925 break;
926 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
927 *rotation = DRM_MODE_ROTATE_270;
928 break;
929 default:
930 *rotation = DRM_MODE_ROTATE_0;
931 }
932
933 /**
934 * The panel already defined the default rotation
935 * through its orientation. Whatever has been provided
936 * on the command line needs to be added to that.
937 *
938 * Unfortunately, the rotations are at different bit
939 * indices, so the math to add them up are not as
940 * trivial as they could.
941 *
942 * Reflections on the other hand are pretty trivial to deal with, a
943 * simple XOR between the two handle the addition nicely.
944 */
945 cmdline = &connector->cmdline_mode;
946 if (cmdline->specified && cmdline->rotation_reflection) {
947 unsigned int cmdline_rest, panel_rest;
948 unsigned int cmdline_rot, panel_rot;
949 unsigned int sum_rot, sum_rest;
950
951 panel_rot = ilog2(*rotation & DRM_MODE_ROTATE_MASK);
952 cmdline_rot = ilog2(cmdline->rotation_reflection & DRM_MODE_ROTATE_MASK);
953 sum_rot = (panel_rot + cmdline_rot) % 4;
954
955 panel_rest = *rotation & ~DRM_MODE_ROTATE_MASK;
956 cmdline_rest = cmdline->rotation_reflection & ~DRM_MODE_ROTATE_MASK;
957 sum_rest = panel_rest ^ cmdline_rest;
958
959 *rotation = (1 << sum_rot) | sum_rest;
960 }
961
962 /*
963 * TODO: support 90 / 270 degree hardware rotation,
964 * depending on the hardware this may require the framebuffer
965 * to be in a specific tiling format.
966 */
967 if (((*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0 &&
968 (*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_180) ||
969 !plane->rotation_property)
970 return false;
971
972 for (i = 0; i < plane->rotation_property->num_values; i++)
973 valid_mask |= (1ULL << plane->rotation_property->values[i]);
974
975 if (!(*rotation & valid_mask))
976 return false;
977
978 return true;
979}
980EXPORT_SYMBOL(drm_client_rotation);
981
982static int drm_client_modeset_commit_atomic(struct drm_client_dev *client, bool active, bool check)
983{
984 struct drm_device *dev = client->dev;
985 struct drm_plane *plane;
986 struct drm_atomic_state *state;
987 struct drm_modeset_acquire_ctx ctx;
988 struct drm_mode_set *mode_set;
989 int ret;
990
991 drm_modeset_acquire_init(&ctx, 0);
992
993 state = drm_atomic_state_alloc(dev);
994 if (!state) {
995 ret = -ENOMEM;
996 goto out_ctx;
997 }
998
999 state->acquire_ctx = &ctx;
1000retry:
1001 drm_for_each_plane(plane, dev) {
1002 struct drm_plane_state *plane_state;
1003
1004 plane_state = drm_atomic_get_plane_state(state, plane);
1005 if (IS_ERR(plane_state)) {
1006 ret = PTR_ERR(plane_state);
1007 goto out_state;
1008 }
1009
1010 plane_state->rotation = DRM_MODE_ROTATE_0;
1011
1012 /* disable non-primary: */
1013 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
1014 continue;
1015
1016 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1017 if (ret != 0)
1018 goto out_state;
1019 }
1020
1021 drm_client_for_each_modeset(mode_set, client) {
1022 struct drm_plane *primary = mode_set->crtc->primary;
1023 unsigned int rotation;
1024
1025 if (drm_client_rotation(mode_set, &rotation)) {
1026 struct drm_plane_state *plane_state;
1027
1028 /* Cannot fail as we've already gotten the plane state above */
1029 plane_state = drm_atomic_get_new_plane_state(state, primary);
1030 plane_state->rotation = rotation;
1031 }
1032
1033 ret = __drm_atomic_helper_set_config(mode_set, state);
1034 if (ret != 0)
1035 goto out_state;
1036
1037 /*
1038 * __drm_atomic_helper_set_config() sets active when a
1039 * mode is set, unconditionally clear it if we force DPMS off
1040 */
1041 if (!active) {
1042 struct drm_crtc *crtc = mode_set->crtc;
1043 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1044
1045 crtc_state->active = false;
1046 }
1047 }
1048
1049 if (check)
1050 ret = drm_atomic_check_only(state);
1051 else
1052 ret = drm_atomic_commit(state);
1053
1054out_state:
1055 if (ret == -EDEADLK)
1056 goto backoff;
1057
1058 drm_atomic_state_put(state);
1059out_ctx:
1060 drm_modeset_drop_locks(&ctx);
1061 drm_modeset_acquire_fini(&ctx);
1062
1063 return ret;
1064
1065backoff:
1066 drm_atomic_state_clear(state);
1067 drm_modeset_backoff(&ctx);
1068
1069 goto retry;
1070}
1071
1072static int drm_client_modeset_commit_legacy(struct drm_client_dev *client)
1073{
1074 struct drm_device *dev = client->dev;
1075 struct drm_mode_set *mode_set;
1076 struct drm_plane *plane;
1077 int ret = 0;
1078
1079 drm_modeset_lock_all(dev);
1080 drm_for_each_plane(plane, dev) {
1081 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
1082 drm_plane_force_disable(plane);
1083
1084 if (plane->rotation_property)
1085 drm_mode_plane_set_obj_prop(plane,
1086 plane->rotation_property,
1087 DRM_MODE_ROTATE_0);
1088 }
1089
1090 drm_client_for_each_modeset(mode_set, client) {
1091 struct drm_crtc *crtc = mode_set->crtc;
1092
1093 if (crtc->funcs->cursor_set2) {
1094 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
1095 if (ret)
1096 goto out;
1097 } else if (crtc->funcs->cursor_set) {
1098 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
1099 if (ret)
1100 goto out;
1101 }
1102
1103 ret = drm_mode_set_config_internal(mode_set);
1104 if (ret)
1105 goto out;
1106 }
1107out:
1108 drm_modeset_unlock_all(dev);
1109
1110 return ret;
1111}
1112
1113/**
1114 * drm_client_modeset_check() - Check modeset configuration
1115 * @client: DRM client
1116 *
1117 * Check modeset configuration.
1118 *
1119 * Returns:
1120 * Zero on success or negative error code on failure.
1121 */
1122int drm_client_modeset_check(struct drm_client_dev *client)
1123{
1124 int ret;
1125
1126 if (!drm_drv_uses_atomic_modeset(client->dev))
1127 return 0;
1128
1129 mutex_lock(&client->modeset_mutex);
1130 ret = drm_client_modeset_commit_atomic(client, true, true);
1131 mutex_unlock(&client->modeset_mutex);
1132
1133 return ret;
1134}
1135EXPORT_SYMBOL(drm_client_modeset_check);
1136
1137/**
1138 * drm_client_modeset_commit_locked() - Force commit CRTC configuration
1139 * @client: DRM client
1140 *
1141 * Commit modeset configuration to crtcs without checking if there is a DRM
1142 * master. The assumption is that the caller already holds an internal DRM
1143 * master reference acquired with drm_master_internal_acquire().
1144 *
1145 * Returns:
1146 * Zero on success or negative error code on failure.
1147 */
1148int drm_client_modeset_commit_locked(struct drm_client_dev *client)
1149{
1150 struct drm_device *dev = client->dev;
1151 int ret;
1152
1153 mutex_lock(&client->modeset_mutex);
1154 if (drm_drv_uses_atomic_modeset(dev))
1155 ret = drm_client_modeset_commit_atomic(client, true, false);
1156 else
1157 ret = drm_client_modeset_commit_legacy(client);
1158 mutex_unlock(&client->modeset_mutex);
1159
1160 return ret;
1161}
1162EXPORT_SYMBOL(drm_client_modeset_commit_locked);
1163
1164/**
1165 * drm_client_modeset_commit() - Commit CRTC configuration
1166 * @client: DRM client
1167 *
1168 * Commit modeset configuration to crtcs.
1169 *
1170 * Returns:
1171 * Zero on success or negative error code on failure.
1172 */
1173int drm_client_modeset_commit(struct drm_client_dev *client)
1174{
1175 struct drm_device *dev = client->dev;
1176 int ret;
1177
1178 if (!drm_master_internal_acquire(dev))
1179 return -EBUSY;
1180
1181 ret = drm_client_modeset_commit_locked(client);
1182
1183 drm_master_internal_release(dev);
1184
1185 return ret;
1186}
1187EXPORT_SYMBOL(drm_client_modeset_commit);
1188
1189static void drm_client_modeset_dpms_legacy(struct drm_client_dev *client, int dpms_mode)
1190{
1191 struct drm_device *dev = client->dev;
1192 struct drm_connector *connector;
1193 struct drm_mode_set *modeset;
1194 struct drm_modeset_acquire_ctx ctx;
1195 int j;
1196 int ret;
1197
1198 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
1199 drm_client_for_each_modeset(modeset, client) {
1200 if (!modeset->crtc->enabled)
1201 continue;
1202
1203 for (j = 0; j < modeset->num_connectors; j++) {
1204 connector = modeset->connectors[j];
1205 connector->funcs->dpms(connector, dpms_mode);
1206 drm_object_property_set_value(&connector->base,
1207 dev->mode_config.dpms_property, dpms_mode);
1208 }
1209 }
1210 DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
1211}
1212
1213/**
1214 * drm_client_modeset_dpms() - Set DPMS mode
1215 * @client: DRM client
1216 * @mode: DPMS mode
1217 *
1218 * Note: For atomic drivers @mode is reduced to on/off.
1219 *
1220 * Returns:
1221 * Zero on success or negative error code on failure.
1222 */
1223int drm_client_modeset_dpms(struct drm_client_dev *client, int mode)
1224{
1225 struct drm_device *dev = client->dev;
1226 int ret = 0;
1227
1228 if (!drm_master_internal_acquire(dev))
1229 return -EBUSY;
1230
1231 mutex_lock(&client->modeset_mutex);
1232 if (drm_drv_uses_atomic_modeset(dev))
1233 ret = drm_client_modeset_commit_atomic(client, mode == DRM_MODE_DPMS_ON, false);
1234 else
1235 drm_client_modeset_dpms_legacy(client, mode);
1236 mutex_unlock(&client->modeset_mutex);
1237
1238 drm_master_internal_release(dev);
1239
1240 return ret;
1241}
1242EXPORT_SYMBOL(drm_client_modeset_dpms);
1243
1244#ifdef CONFIG_DRM_KUNIT_TEST
1245#include "tests/drm_client_modeset_test.c"
1246#endif