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