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