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