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