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