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