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