Linux Audio

Check our new training course

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