xref: /linux/drivers/gpu/drm/drm_client_modeset.c (revision 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd)
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 						   GFP_KERNEL);
64 		if (!modeset->connectors)
65 			goto err_free;
66 	}
67 
68 	return 0;
69 
70 err_free:
71 	drm_client_modeset_free(client);
72 
73 	return -ENOMEM;
74 }
75 
76 static void drm_client_modeset_release(struct drm_client_dev *client)
77 {
78 	struct drm_mode_set *modeset;
79 
80 	drm_client_for_each_modeset(modeset, client) {
81 		int i;
82 
83 		drm_mode_destroy(client->dev, modeset->mode);
84 		modeset->mode = NULL;
85 		modeset->fb = NULL;
86 
87 		for (i = 0; i < modeset->num_connectors; i++) {
88 			drm_connector_put(modeset->connectors[i]);
89 			modeset->connectors[i] = NULL;
90 		}
91 		modeset->num_connectors = 0;
92 	}
93 }
94 
95 void drm_client_modeset_free(struct drm_client_dev *client)
96 {
97 	struct drm_mode_set *modeset;
98 
99 	mutex_lock(&client->modeset_mutex);
100 
101 	drm_client_modeset_release(client);
102 
103 	drm_client_for_each_modeset(modeset, client)
104 		kfree(modeset->connectors);
105 
106 	mutex_unlock(&client->modeset_mutex);
107 
108 	mutex_destroy(&client->modeset_mutex);
109 	kfree(client->modesets);
110 }
111 
112 static struct drm_mode_set *
113 drm_client_find_modeset(struct drm_client_dev *client, struct drm_crtc *crtc)
114 {
115 	struct drm_mode_set *modeset;
116 
117 	drm_client_for_each_modeset(modeset, client)
118 		if (modeset->crtc == crtc)
119 			return modeset;
120 
121 	return NULL;
122 }
123 
124 static const struct drm_display_mode *
125 drm_connector_get_tiled_mode(struct drm_connector *connector)
126 {
127 	const struct drm_display_mode *mode;
128 
129 	list_for_each_entry(mode, &connector->modes, head) {
130 		if (mode->hdisplay == connector->tile_h_size &&
131 		    mode->vdisplay == connector->tile_v_size)
132 			return mode;
133 	}
134 	return NULL;
135 }
136 
137 static const struct drm_display_mode *
138 drm_connector_fallback_non_tiled_mode(struct drm_connector *connector)
139 {
140 	const struct drm_display_mode *mode;
141 
142 	list_for_each_entry(mode, &connector->modes, head) {
143 		if (mode->hdisplay == connector->tile_h_size &&
144 		    mode->vdisplay == connector->tile_v_size)
145 			continue;
146 		return mode;
147 	}
148 	return NULL;
149 }
150 
151 static const struct drm_display_mode *
152 drm_connector_preferred_mode(struct drm_connector *connector, int width, int height)
153 {
154 	const struct drm_display_mode *mode;
155 
156 	list_for_each_entry(mode, &connector->modes, head) {
157 		if (mode->hdisplay > width ||
158 		    mode->vdisplay > height)
159 			continue;
160 		if (mode->type & DRM_MODE_TYPE_PREFERRED)
161 			return mode;
162 	}
163 	return NULL;
164 }
165 
166 static const struct drm_display_mode *
167 drm_connector_first_mode(struct drm_connector *connector)
168 {
169 	return list_first_entry_or_null(&connector->modes,
170 					struct drm_display_mode, head);
171 }
172 
173 static const struct drm_display_mode *
174 drm_connector_pick_cmdline_mode(struct drm_connector *connector)
175 {
176 	const struct drm_cmdline_mode *cmdline_mode;
177 	const struct drm_display_mode *mode;
178 	bool prefer_non_interlace;
179 
180 	/*
181 	 * Find a user-defined mode. If the user gave us a valid
182 	 * mode on the kernel command line, it will show up in this
183 	 * list.
184 	 */
185 
186 	list_for_each_entry(mode, &connector->modes, head) {
187 		if (mode->type & DRM_MODE_TYPE_USERDEF)
188 			return mode;
189 	}
190 
191 	cmdline_mode = &connector->cmdline_mode;
192 	if (cmdline_mode->specified == false)
193 		return NULL;
194 
195 	/*
196 	 * Attempt to find a matching mode in the list of modes we
197 	 * have gotten so far.
198 	 */
199 
200 	prefer_non_interlace = !cmdline_mode->interlace;
201 again:
202 	list_for_each_entry(mode, &connector->modes, head) {
203 		/* check width/height */
204 		if (mode->hdisplay != cmdline_mode->xres ||
205 		    mode->vdisplay != cmdline_mode->yres)
206 			continue;
207 
208 		if (cmdline_mode->refresh_specified) {
209 			if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
210 				continue;
211 		}
212 
213 		if (cmdline_mode->interlace) {
214 			if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
215 				continue;
216 		} else if (prefer_non_interlace) {
217 			if (mode->flags & DRM_MODE_FLAG_INTERLACE)
218 				continue;
219 		}
220 		return mode;
221 	}
222 
223 	if (prefer_non_interlace) {
224 		prefer_non_interlace = false;
225 		goto again;
226 	}
227 
228 	return NULL;
229 }
230 
231 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
232 {
233 	bool enable;
234 
235 	if (connector->display_info.non_desktop)
236 		return false;
237 
238 	if (strict)
239 		enable = connector->status == connector_status_connected;
240 	else
241 		enable = connector->status != connector_status_disconnected;
242 
243 	return enable;
244 }
245 
246 static void drm_client_connectors_enabled(struct drm_connector *connectors[],
247 					  unsigned int connector_count,
248 					  bool enabled[])
249 {
250 	bool any_enabled = false;
251 	struct drm_connector *connector;
252 	int i = 0;
253 
254 	for (i = 0; i < connector_count; i++) {
255 		connector = connectors[i];
256 		enabled[i] = drm_connector_enabled(connector, true);
257 		drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] enabled? %s\n",
258 			    connector->base.id, connector->name,
259 			    connector->display_info.non_desktop ?
260 			    "non desktop" : str_yes_no(enabled[i]));
261 
262 		any_enabled |= enabled[i];
263 	}
264 
265 	if (any_enabled)
266 		return;
267 
268 	for (i = 0; i < connector_count; i++)
269 		enabled[i] = drm_connector_enabled(connectors[i], false);
270 }
271 
272 static void mode_replace(struct drm_device *dev,
273 			 const struct drm_display_mode **dst,
274 			 const struct drm_display_mode *src)
275 {
276 	drm_mode_destroy(dev, (struct drm_display_mode *)*dst);
277 
278 	*dst = src ? drm_mode_duplicate(dev, src) : NULL;
279 }
280 
281 static void modes_destroy(struct drm_device *dev,
282 			  const struct drm_display_mode *modes[],
283 			  int count)
284 {
285 	int i;
286 
287 	for (i = 0; i < count; i++)
288 		mode_replace(dev, &modes[i], NULL);
289 }
290 
291 static bool drm_client_target_cloned(struct drm_device *dev,
292 				     struct drm_connector *connectors[],
293 				     unsigned int connector_count,
294 				     const struct drm_display_mode *modes[],
295 				     struct drm_client_offset offsets[],
296 				     bool enabled[], int width, int height)
297 {
298 	int count, i;
299 	bool can_clone = false;
300 	struct drm_display_mode *dmt_mode;
301 
302 	/* only contemplate cloning in the single crtc case */
303 	if (dev->mode_config.num_crtc > 1)
304 		return false;
305 
306 	count = 0;
307 	for (i = 0; i < connector_count; i++) {
308 		if (enabled[i])
309 			count++;
310 	}
311 
312 	/* only contemplate cloning if more than one connector is enabled */
313 	if (count <= 1)
314 		return false;
315 
316 	/* check the command line or if nothing common pick 1024x768 */
317 	can_clone = true;
318 	for (i = 0; i < connector_count; i++) {
319 		int j;
320 
321 		if (!enabled[i])
322 			continue;
323 
324 		mode_replace(dev, &modes[i],
325 			     drm_connector_pick_cmdline_mode(connectors[i]));
326 		if (!modes[i]) {
327 			can_clone = false;
328 			break;
329 		}
330 		for (j = 0; j < i; j++) {
331 			if (!enabled[j])
332 				continue;
333 			if (!drm_mode_match(modes[j], modes[i],
334 					    DRM_MODE_MATCH_TIMINGS |
335 					    DRM_MODE_MATCH_CLOCK |
336 					    DRM_MODE_MATCH_FLAGS |
337 					    DRM_MODE_MATCH_3D_FLAGS))
338 				can_clone = false;
339 		}
340 	}
341 
342 	if (can_clone) {
343 		drm_dbg_kms(dev, "can clone using command line\n");
344 		return true;
345 	}
346 
347 	/* try and find a 1024x768 mode on each connector */
348 	can_clone = true;
349 	dmt_mode = drm_mode_find_dmt(dev, 1024, 768, 60, false);
350 
351 	if (!dmt_mode)
352 		goto fail;
353 
354 	for (i = 0; i < connector_count; i++) {
355 		const struct drm_display_mode *mode;
356 
357 		if (!enabled[i])
358 			continue;
359 
360 		list_for_each_entry(mode, &connectors[i]->modes, head) {
361 			if (drm_mode_match(mode, dmt_mode,
362 					   DRM_MODE_MATCH_TIMINGS |
363 					   DRM_MODE_MATCH_CLOCK |
364 					   DRM_MODE_MATCH_FLAGS |
365 					   DRM_MODE_MATCH_3D_FLAGS))
366 				mode_replace(dev, &modes[i], mode);
367 		}
368 		if (!modes[i])
369 			can_clone = false;
370 	}
371 	drm_mode_destroy(dev, dmt_mode);
372 
373 	if (can_clone) {
374 		drm_dbg_kms(dev, "can clone using 1024x768\n");
375 		return true;
376 	}
377 fail:
378 	drm_info(dev, "kms: can't enable cloning when we probably wanted to.\n");
379 	return false;
380 }
381 
382 static int drm_client_get_tile_offsets(struct drm_device *dev,
383 				       struct drm_connector *connectors[],
384 				       unsigned int connector_count,
385 				       const struct drm_display_mode *modes[],
386 				       struct drm_client_offset offsets[],
387 				       int idx,
388 				       int h_idx, int v_idx)
389 {
390 	int i;
391 	int hoffset = 0, voffset = 0;
392 
393 	for (i = 0; i < connector_count; i++) {
394 		struct drm_connector *connector = connectors[i];
395 
396 		if (!connector->has_tile)
397 			continue;
398 
399 		if (!modes[i] && (h_idx || v_idx)) {
400 			drm_dbg_kms(dev,
401 				    "[CONNECTOR:%d:%s] no modes for connector tiled %d\n",
402 				    connector->base.id, connector->name, i);
403 			continue;
404 		}
405 		if (connector->tile_h_loc < h_idx)
406 			hoffset += modes[i]->hdisplay;
407 
408 		if (connector->tile_v_loc < v_idx)
409 			voffset += modes[i]->vdisplay;
410 	}
411 	offsets[idx].x = hoffset;
412 	offsets[idx].y = voffset;
413 	drm_dbg_kms(dev, "returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
414 	return 0;
415 }
416 
417 static bool drm_client_target_preferred(struct drm_device *dev,
418 					struct drm_connector *connectors[],
419 					unsigned int connector_count,
420 					const struct drm_display_mode *modes[],
421 					struct drm_client_offset offsets[],
422 					bool enabled[], int width, int height)
423 {
424 	const u64 mask = BIT_ULL(connector_count) - 1;
425 	u64 conn_configured = 0;
426 	int tile_pass = 0;
427 	int num_tiled_conns = 0;
428 	int i;
429 
430 	for (i = 0; i < connector_count; i++) {
431 		if (connectors[i]->has_tile &&
432 		    connectors[i]->status == connector_status_connected)
433 			num_tiled_conns++;
434 	}
435 
436 retry:
437 	for (i = 0; i < connector_count; i++) {
438 		struct drm_connector *connector = connectors[i];
439 		const char *mode_type;
440 
441 
442 		if (conn_configured & BIT_ULL(i))
443 			continue;
444 
445 		if (enabled[i] == false) {
446 			conn_configured |= BIT_ULL(i);
447 			continue;
448 		}
449 
450 		/* first pass over all the untiled connectors */
451 		if (tile_pass == 0 && connector->has_tile)
452 			continue;
453 
454 		if (tile_pass == 1) {
455 			if (connector->tile_h_loc != 0 ||
456 			    connector->tile_v_loc != 0)
457 				continue;
458 
459 		} else {
460 			if (connector->tile_h_loc != tile_pass - 1 &&
461 			    connector->tile_v_loc != tile_pass - 1)
462 			/* if this tile_pass doesn't cover any of the tiles - keep going */
463 				continue;
464 
465 			/*
466 			 * find the tile offsets for this pass - need to find
467 			 * all tiles left and above
468 			 */
469 			drm_client_get_tile_offsets(dev, connectors, connector_count,
470 						    modes, offsets, i,
471 						    connector->tile_h_loc, connector->tile_v_loc);
472 		}
473 
474 		mode_type = "cmdline";
475 		mode_replace(dev, &modes[i],
476 			     drm_connector_pick_cmdline_mode(connector));
477 
478 		if (!modes[i]) {
479 			mode_type = "preferred";
480 			mode_replace(dev, &modes[i],
481 				     drm_connector_preferred_mode(connector, width, height));
482 		}
483 
484 		if (!modes[i]) {
485 			mode_type = "first";
486 			mode_replace(dev, &modes[i],
487 				     drm_connector_first_mode(connector));
488 		}
489 
490 		/*
491 		 * In case of tiled mode if all tiles not present fallback to
492 		 * first available non tiled mode.
493 		 * After all tiles are present, try to find the tiled mode
494 		 * for all and if tiled mode not present due to fbcon size
495 		 * limitations, use first non tiled mode only for
496 		 * tile 0,0 and set to no mode for all other tiles.
497 		 */
498 		if (connector->has_tile) {
499 			if (num_tiled_conns <
500 			    connector->num_h_tile * connector->num_v_tile ||
501 			    (connector->tile_h_loc == 0 &&
502 			     connector->tile_v_loc == 0 &&
503 			     !drm_connector_get_tiled_mode(connector))) {
504 				mode_type = "non tiled";
505 				mode_replace(dev, &modes[i],
506 					     drm_connector_fallback_non_tiled_mode(connector));
507 			} else {
508 				mode_type = "tiled";
509 				mode_replace(dev, &modes[i],
510 					     drm_connector_get_tiled_mode(connector));
511 			}
512 		}
513 
514 		if (modes[i])
515 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] found %s mode: %s\n",
516 				    connector->base.id, connector->name,
517 				    mode_type, modes[i]->name);
518 		else
519 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] no mode found\n",
520 				    connector->base.id, connector->name);
521 
522 		conn_configured |= BIT_ULL(i);
523 	}
524 
525 	if ((conn_configured & mask) != mask) {
526 		tile_pass++;
527 		goto retry;
528 	}
529 	return true;
530 }
531 
532 static bool connector_has_possible_crtc(struct drm_connector *connector,
533 					struct drm_crtc *crtc)
534 {
535 	struct drm_encoder *encoder;
536 
537 	drm_connector_for_each_possible_encoder(connector, encoder) {
538 		if (encoder->possible_crtcs & drm_crtc_mask(crtc))
539 			return true;
540 	}
541 
542 	return false;
543 }
544 
545 static int drm_client_pick_crtcs(struct drm_client_dev *client,
546 				 struct drm_connector *connectors[],
547 				 unsigned int connector_count,
548 				 struct drm_crtc *best_crtcs[],
549 				 const struct drm_display_mode *modes[],
550 				 int n, int width, int height)
551 {
552 	struct drm_device *dev = client->dev;
553 	struct drm_connector *connector;
554 	int my_score, best_score, score;
555 	struct drm_crtc **crtcs;
556 	struct drm_mode_set *modeset;
557 
558 	if (n == connector_count)
559 		return 0;
560 
561 	connector = connectors[n];
562 
563 	best_crtcs[n] = NULL;
564 	best_score = drm_client_pick_crtcs(client, connectors, connector_count,
565 					   best_crtcs, modes, n + 1, width, height);
566 	if (modes[n] == NULL)
567 		return best_score;
568 
569 	crtcs = kzalloc_objs(*crtcs, connector_count);
570 	if (!crtcs)
571 		return best_score;
572 
573 	my_score = 1;
574 	if (connector->status == connector_status_connected)
575 		my_score++;
576 	if (connector->cmdline_mode.specified)
577 		my_score++;
578 	if (drm_connector_preferred_mode(connector, width, height))
579 		my_score++;
580 
581 	/*
582 	 * select a crtc for this connector and then attempt to configure
583 	 * remaining connectors
584 	 */
585 	drm_client_for_each_modeset(modeset, client) {
586 		struct drm_crtc *crtc = modeset->crtc;
587 		int o;
588 
589 		if (!connector_has_possible_crtc(connector, crtc))
590 			continue;
591 
592 		for (o = 0; o < n; o++)
593 			if (best_crtcs[o] == crtc)
594 				break;
595 
596 		if (o < n) {
597 			/* ignore cloning unless only a single crtc */
598 			if (dev->mode_config.num_crtc > 1)
599 				continue;
600 
601 			if (!drm_mode_equal(modes[o], modes[n]))
602 				continue;
603 		}
604 
605 		crtcs[n] = crtc;
606 		memcpy(crtcs, best_crtcs, n * sizeof(*crtcs));
607 		score = my_score + drm_client_pick_crtcs(client, connectors, connector_count,
608 							 crtcs, modes, n + 1, width, height);
609 		if (score > best_score) {
610 			best_score = score;
611 			memcpy(best_crtcs, crtcs, connector_count * sizeof(*crtcs));
612 		}
613 	}
614 
615 	kfree(crtcs);
616 	return best_score;
617 }
618 
619 /* Try to read the BIOS display configuration and use it for the initial config */
620 static bool drm_client_firmware_config(struct drm_client_dev *client,
621 				       struct drm_connector *connectors[],
622 				       unsigned int connector_count,
623 				       struct drm_crtc *crtcs[],
624 				       const struct drm_display_mode *modes[],
625 				       struct drm_client_offset offsets[],
626 				       bool enabled[], int width, int height)
627 {
628 	const int count = min_t(unsigned int, connector_count, BITS_PER_LONG);
629 	unsigned long conn_configured, conn_seq, mask;
630 	struct drm_device *dev = client->dev;
631 	int i;
632 	bool *save_enabled;
633 	bool fallback = true, ret = true;
634 	int num_connectors_enabled = 0;
635 	int num_connectors_detected = 0;
636 	int num_tiled_conns = 0;
637 	struct drm_modeset_acquire_ctx ctx;
638 
639 	if (!drm_drv_uses_atomic_modeset(dev))
640 		return false;
641 
642 	if (drm_WARN_ON(dev, count <= 0))
643 		return false;
644 
645 	save_enabled = kzalloc_objs(bool, count);
646 	if (!save_enabled)
647 		return false;
648 
649 	drm_modeset_acquire_init(&ctx, 0);
650 
651 	while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
652 		drm_modeset_backoff(&ctx);
653 
654 	memcpy(save_enabled, enabled, count);
655 	mask = GENMASK(count - 1, 0);
656 	conn_configured = 0;
657 	for (i = 0; i < count; i++) {
658 		if (connectors[i]->has_tile &&
659 		    connectors[i]->status == connector_status_connected)
660 			num_tiled_conns++;
661 	}
662 retry:
663 	conn_seq = conn_configured;
664 	for (i = 0; i < count; i++) {
665 		struct drm_connector *connector = connectors[i];
666 		struct drm_encoder *encoder;
667 		struct drm_crtc *crtc;
668 		const char *mode_type;
669 		int j;
670 
671 		if (conn_configured & BIT(i))
672 			continue;
673 
674 		if (conn_seq == 0 && !connector->has_tile)
675 			continue;
676 
677 		if (connector->status == connector_status_connected)
678 			num_connectors_detected++;
679 
680 		if (!enabled[i]) {
681 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] not enabled, skipping\n",
682 				    connector->base.id, connector->name);
683 			conn_configured |= BIT(i);
684 			continue;
685 		}
686 
687 		if (connector->force == DRM_FORCE_OFF) {
688 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] disabled by user, skipping\n",
689 				    connector->base.id, connector->name);
690 			enabled[i] = false;
691 			continue;
692 		}
693 
694 		encoder = connector->state->best_encoder;
695 		if (!encoder || drm_WARN_ON(dev, !connector->state->crtc)) {
696 			if (connector->force > DRM_FORCE_OFF)
697 				goto bail;
698 
699 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] has no encoder or crtc, skipping\n",
700 				    connector->base.id, connector->name);
701 			enabled[i] = false;
702 			conn_configured |= BIT(i);
703 			continue;
704 		}
705 
706 		num_connectors_enabled++;
707 
708 		crtc = connector->state->crtc;
709 
710 		/*
711 		 * Make sure we're not trying to drive multiple connectors
712 		 * with a single CRTC, since our cloning support may not
713 		 * match the BIOS.
714 		 */
715 		for (j = 0; j < count; j++) {
716 			if (crtcs[j] == crtc) {
717 				drm_dbg_kms(dev, "[CONNECTOR:%d:%s] fallback: cloned configuration\n",
718 					    connector->base.id, connector->name);
719 				goto bail;
720 			}
721 		}
722 
723 		mode_type = "cmdline";
724 		mode_replace(dev, &modes[i],
725 			     drm_connector_pick_cmdline_mode(connector));
726 
727 		if (!modes[i]) {
728 			mode_type = "preferred";
729 			mode_replace(dev, &modes[i],
730 				     drm_connector_preferred_mode(connector, width, height));
731 		}
732 
733 		if (!modes[i]) {
734 			mode_type = "first";
735 			mode_replace(dev, &modes[i],
736 				     drm_connector_first_mode(connector));
737 		}
738 
739 		/* last resort: use current mode */
740 		if (!modes[i]) {
741 			mode_type = "current";
742 			mode_replace(dev, &modes[i],
743 				     &crtc->state->mode);
744 		}
745 
746 		/*
747 		 * In case of tiled modes, if all tiles are not present
748 		 * then fallback to a non tiled mode.
749 		 */
750 		if (connector->has_tile &&
751 		    num_tiled_conns < connector->num_h_tile * connector->num_v_tile) {
752 			mode_type = "non tiled";
753 			mode_replace(dev, &modes[i],
754 				     drm_connector_fallback_non_tiled_mode(connector));
755 		}
756 		crtcs[i] = crtc;
757 
758 		drm_dbg_kms(dev, "[CONNECTOR::%d:%s] on [CRTC:%d:%s] using %s mode: %s\n",
759 			    connector->base.id, connector->name,
760 			    crtc->base.id, crtc->name,
761 			    mode_type, modes[i]->name);
762 
763 		fallback = false;
764 		conn_configured |= BIT(i);
765 	}
766 
767 	if ((conn_configured & mask) != mask && conn_configured != conn_seq)
768 		goto retry;
769 
770 	for (i = 0; i < count; i++) {
771 		struct drm_connector *connector = connectors[i];
772 
773 		if (connector->has_tile)
774 			drm_client_get_tile_offsets(dev, connectors, connector_count,
775 						    modes, offsets, i,
776 						    connector->tile_h_loc, connector->tile_v_loc);
777 	}
778 
779 	/*
780 	 * If the BIOS didn't enable everything it could, fall back to have the
781 	 * same user experiencing of lighting up as much as possible like the
782 	 * fbdev helper library.
783 	 */
784 	if (num_connectors_enabled != num_connectors_detected &&
785 	    num_connectors_enabled < dev->mode_config.num_crtc) {
786 		drm_dbg_kms(dev, "fallback: Not all outputs enabled\n");
787 		drm_dbg_kms(dev, "Enabled: %i, detected: %i\n",
788 			    num_connectors_enabled, num_connectors_detected);
789 		fallback = true;
790 	}
791 
792 	if (fallback) {
793 bail:
794 		drm_dbg_kms(dev, "Not using firmware configuration\n");
795 		memcpy(enabled, save_enabled, count);
796 		ret = false;
797 	}
798 
799 	drm_modeset_drop_locks(&ctx);
800 	drm_modeset_acquire_fini(&ctx);
801 
802 	kfree(save_enabled);
803 	return ret;
804 }
805 
806 /**
807  * drm_client_modeset_probe() - Probe for displays
808  * @client: DRM client
809  * @width: Maximum display mode width (optional)
810  * @height: Maximum display mode height (optional)
811  *
812  * This function sets up display pipelines for enabled connectors and stores the
813  * config in the client's modeset array.
814  *
815  * Returns:
816  * Zero on success or negative error code on failure.
817  */
818 int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height)
819 {
820 	struct drm_connector *connector, **connectors = NULL;
821 	struct drm_connector_list_iter conn_iter;
822 	struct drm_device *dev = client->dev;
823 	unsigned int total_modes_count = 0;
824 	struct drm_client_offset *offsets;
825 	unsigned int connector_count = 0;
826 	const struct drm_display_mode **modes;
827 	struct drm_crtc **crtcs;
828 	int i, ret = 0;
829 	bool *enabled;
830 
831 	drm_dbg_kms(dev, "\n");
832 
833 	if (!width)
834 		width = dev->mode_config.max_width;
835 	if (!height)
836 		height = dev->mode_config.max_height;
837 
838 	drm_connector_list_iter_begin(dev, &conn_iter);
839 	drm_client_for_each_connector_iter(connector, &conn_iter) {
840 		struct drm_connector **tmp;
841 
842 		tmp = krealloc(connectors, (connector_count + 1) * sizeof(*connectors), GFP_KERNEL);
843 		if (!tmp) {
844 			ret = -ENOMEM;
845 			goto free_connectors;
846 		}
847 
848 		connectors = tmp;
849 		drm_connector_get(connector);
850 		connectors[connector_count++] = connector;
851 	}
852 	drm_connector_list_iter_end(&conn_iter);
853 
854 	if (!connector_count)
855 		return 0;
856 
857 	crtcs = kzalloc_objs(*crtcs, connector_count);
858 	modes = kzalloc_objs(*modes, connector_count);
859 	offsets = kzalloc_objs(*offsets, connector_count);
860 	enabled = kzalloc_objs(bool, connector_count);
861 	if (!crtcs || !modes || !enabled || !offsets) {
862 		ret = -ENOMEM;
863 		goto out;
864 	}
865 
866 	mutex_lock(&client->modeset_mutex);
867 
868 	mutex_lock(&dev->mode_config.mutex);
869 	for (i = 0; i < connector_count; i++)
870 		total_modes_count += connectors[i]->funcs->fill_modes(connectors[i], width, height);
871 	if (!total_modes_count)
872 		drm_dbg_kms(dev, "No connectors reported connected with modes\n");
873 	drm_client_connectors_enabled(connectors, connector_count, enabled);
874 
875 	if (!drm_client_firmware_config(client, connectors, connector_count, crtcs,
876 					modes, offsets, enabled, width, height)) {
877 		modes_destroy(dev, modes, connector_count);
878 		memset(crtcs, 0, connector_count * sizeof(*crtcs));
879 		memset(offsets, 0, connector_count * sizeof(*offsets));
880 
881 		if (!drm_client_target_cloned(dev, connectors, connector_count, modes,
882 					      offsets, enabled, width, height) &&
883 		    !drm_client_target_preferred(dev, connectors, connector_count, modes,
884 						 offsets, enabled, width, height))
885 			drm_err(dev, "Unable to find initial modes\n");
886 
887 		drm_dbg_kms(dev, "picking CRTCs for %dx%d config\n",
888 			    width, height);
889 
890 		drm_client_pick_crtcs(client, connectors, connector_count,
891 				      crtcs, modes, 0, width, height);
892 	}
893 
894 	mutex_unlock(&dev->mode_config.mutex);
895 
896 	drm_client_modeset_release(client);
897 
898 	for (i = 0; i < connector_count; i++) {
899 		const struct drm_display_mode *mode = modes[i];
900 		struct drm_crtc *crtc = crtcs[i];
901 		struct drm_client_offset *offset = &offsets[i];
902 
903 		if (mode && crtc) {
904 			struct drm_mode_set *modeset = drm_client_find_modeset(client, crtc);
905 			struct drm_connector *connector = connectors[i];
906 
907 			drm_dbg_kms(dev, "[CRTC:%d:%s] desired mode %s set (%d,%d)\n",
908 				    crtc->base.id, crtc->name,
909 				    mode->name, offset->x, offset->y);
910 
911 			if (drm_WARN_ON_ONCE(dev, modeset->num_connectors == DRM_CLIENT_MAX_CLONED_CONNECTORS ||
912 					     (dev->mode_config.num_crtc > 1 && modeset->num_connectors == 1))) {
913 				ret = -EINVAL;
914 				break;
915 			}
916 
917 			drm_mode_destroy(dev, modeset->mode);
918 			modeset->mode = drm_mode_duplicate(dev, mode);
919 			if (!modeset->mode) {
920 				ret = -ENOMEM;
921 				break;
922 			}
923 
924 			drm_connector_get(connector);
925 			modeset->connectors[modeset->num_connectors++] = connector;
926 			modeset->x = offset->x;
927 			modeset->y = offset->y;
928 		}
929 	}
930 
931 	mutex_unlock(&client->modeset_mutex);
932 out:
933 	kfree(crtcs);
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