xref: /linux/drivers/gpu/drm/drm_client_modeset.c (revision 15833fea97c1fdb3b34fceefa4b51177dd57e18f)
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);
24514b7897dSJani Nikula 		drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] enabled? %s\n",
24614b7897dSJani Nikula 			    connector->base.id, connector->name,
24714b7897dSJani Nikula 			    connector->display_info.non_desktop ?
24814b7897dSJani Nikula 			    "non desktop" : str_yes_no(enabled[i]));
249cf13909aSNoralf Trønnes 
250cf13909aSNoralf Trønnes 		any_enabled |= enabled[i];
251cf13909aSNoralf Trønnes 	}
252cf13909aSNoralf Trønnes 
253cf13909aSNoralf Trønnes 	if (any_enabled)
254cf13909aSNoralf Trønnes 		return;
255cf13909aSNoralf Trønnes 
256cf13909aSNoralf Trønnes 	for (i = 0; i < connector_count; i++)
257cf13909aSNoralf Trønnes 		enabled[i] = drm_connector_enabled(connectors[i], false);
258cf13909aSNoralf Trønnes }
259cf13909aSNoralf 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)260cf13909aSNoralf Trønnes static bool drm_client_target_cloned(struct drm_device *dev,
261cf13909aSNoralf Trønnes 				     struct drm_connector **connectors,
262cf13909aSNoralf Trønnes 				     unsigned int connector_count,
263cf13909aSNoralf Trønnes 				     struct drm_display_mode **modes,
264cf13909aSNoralf Trønnes 				     struct drm_client_offset *offsets,
265cf13909aSNoralf Trønnes 				     bool *enabled, int width, int height)
266cf13909aSNoralf Trønnes {
267cf13909aSNoralf Trønnes 	int count, i, j;
268cf13909aSNoralf Trønnes 	bool can_clone = false;
269cf13909aSNoralf Trønnes 	struct drm_display_mode *dmt_mode, *mode;
270cf13909aSNoralf Trønnes 
271cf13909aSNoralf Trønnes 	/* only contemplate cloning in the single crtc case */
272cf13909aSNoralf Trønnes 	if (dev->mode_config.num_crtc > 1)
273cf13909aSNoralf Trønnes 		return false;
274cf13909aSNoralf Trønnes 
275cf13909aSNoralf Trønnes 	count = 0;
276cf13909aSNoralf Trønnes 	for (i = 0; i < connector_count; i++) {
277cf13909aSNoralf Trønnes 		if (enabled[i])
278cf13909aSNoralf Trønnes 			count++;
279cf13909aSNoralf Trønnes 	}
280cf13909aSNoralf Trønnes 
281cf13909aSNoralf Trønnes 	/* only contemplate cloning if more than one connector is enabled */
282cf13909aSNoralf Trønnes 	if (count <= 1)
283cf13909aSNoralf Trønnes 		return false;
284cf13909aSNoralf Trønnes 
285cf13909aSNoralf Trønnes 	/* check the command line or if nothing common pick 1024x768 */
286cf13909aSNoralf Trønnes 	can_clone = true;
287cf13909aSNoralf Trønnes 	for (i = 0; i < connector_count; i++) {
288cf13909aSNoralf Trønnes 		if (!enabled[i])
289cf13909aSNoralf Trønnes 			continue;
290cf13909aSNoralf Trønnes 		modes[i] = drm_connector_pick_cmdline_mode(connectors[i]);
291cf13909aSNoralf Trønnes 		if (!modes[i]) {
292cf13909aSNoralf Trønnes 			can_clone = false;
293cf13909aSNoralf Trønnes 			break;
294cf13909aSNoralf Trønnes 		}
295cf13909aSNoralf Trønnes 		for (j = 0; j < i; j++) {
296cf13909aSNoralf Trønnes 			if (!enabled[j])
297cf13909aSNoralf Trønnes 				continue;
298cf13909aSNoralf Trønnes 			if (!drm_mode_match(modes[j], modes[i],
299cf13909aSNoralf Trønnes 					    DRM_MODE_MATCH_TIMINGS |
300cf13909aSNoralf Trønnes 					    DRM_MODE_MATCH_CLOCK |
301cf13909aSNoralf Trønnes 					    DRM_MODE_MATCH_FLAGS |
302cf13909aSNoralf Trønnes 					    DRM_MODE_MATCH_3D_FLAGS))
303cf13909aSNoralf Trønnes 				can_clone = false;
304cf13909aSNoralf Trønnes 		}
305cf13909aSNoralf Trønnes 	}
306cf13909aSNoralf Trønnes 
307cf13909aSNoralf Trønnes 	if (can_clone) {
30814b7897dSJani Nikula 		drm_dbg_kms(dev, "can clone using command line\n");
309cf13909aSNoralf Trønnes 		return true;
310cf13909aSNoralf Trønnes 	}
311cf13909aSNoralf Trønnes 
312cf13909aSNoralf Trønnes 	/* try and find a 1024x768 mode on each connector */
313cf13909aSNoralf Trønnes 	can_clone = true;
314cf13909aSNoralf Trønnes 	dmt_mode = drm_mode_find_dmt(dev, 1024, 768, 60, false);
315cf13909aSNoralf Trønnes 
316c2a88e8bSJocelyn Falempe 	if (!dmt_mode)
317c2a88e8bSJocelyn Falempe 		goto fail;
318c2a88e8bSJocelyn Falempe 
319cf13909aSNoralf Trønnes 	for (i = 0; i < connector_count; i++) {
320cf13909aSNoralf Trønnes 		if (!enabled[i])
321cf13909aSNoralf Trønnes 			continue;
322cf13909aSNoralf Trønnes 
323cf13909aSNoralf Trønnes 		list_for_each_entry(mode, &connectors[i]->modes, head) {
324cf13909aSNoralf Trønnes 			if (drm_mode_match(mode, dmt_mode,
325cf13909aSNoralf Trønnes 					   DRM_MODE_MATCH_TIMINGS |
326cf13909aSNoralf Trønnes 					   DRM_MODE_MATCH_CLOCK |
327cf13909aSNoralf Trønnes 					   DRM_MODE_MATCH_FLAGS |
328cf13909aSNoralf Trønnes 					   DRM_MODE_MATCH_3D_FLAGS))
329cf13909aSNoralf Trønnes 				modes[i] = mode;
330cf13909aSNoralf Trønnes 		}
331cf13909aSNoralf Trønnes 		if (!modes[i])
332cf13909aSNoralf Trønnes 			can_clone = false;
333cf13909aSNoralf Trønnes 	}
334c2a88e8bSJocelyn Falempe 	kfree(dmt_mode);
335cf13909aSNoralf Trønnes 
336cf13909aSNoralf Trønnes 	if (can_clone) {
33714b7897dSJani Nikula 		drm_dbg_kms(dev, "can clone using 1024x768\n");
338cf13909aSNoralf Trønnes 		return true;
339cf13909aSNoralf Trønnes 	}
340c2a88e8bSJocelyn Falempe fail:
34114b7897dSJani Nikula 	drm_info(dev, "kms: can't enable cloning when we probably wanted to.\n");
342cf13909aSNoralf Trønnes 	return false;
343cf13909aSNoralf Trønnes }
344cf13909aSNoralf Trønnes 
drm_client_get_tile_offsets(struct drm_device * dev,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)34514b7897dSJani Nikula static int drm_client_get_tile_offsets(struct drm_device *dev,
34614b7897dSJani Nikula 				       struct drm_connector **connectors,
347cf13909aSNoralf Trønnes 				       unsigned int connector_count,
348cf13909aSNoralf Trønnes 				       struct drm_display_mode **modes,
349cf13909aSNoralf Trønnes 				       struct drm_client_offset *offsets,
350cf13909aSNoralf Trønnes 				       int idx,
351cf13909aSNoralf Trønnes 				       int h_idx, int v_idx)
352cf13909aSNoralf Trønnes {
353cf13909aSNoralf Trønnes 	struct drm_connector *connector;
354cf13909aSNoralf Trønnes 	int i;
355cf13909aSNoralf Trønnes 	int hoffset = 0, voffset = 0;
356cf13909aSNoralf Trønnes 
357cf13909aSNoralf Trønnes 	for (i = 0; i < connector_count; i++) {
358cf13909aSNoralf Trønnes 		connector = connectors[i];
359cf13909aSNoralf Trønnes 		if (!connector->has_tile)
360cf13909aSNoralf Trønnes 			continue;
361cf13909aSNoralf Trønnes 
362cf13909aSNoralf Trønnes 		if (!modes[i] && (h_idx || v_idx)) {
36314b7897dSJani Nikula 			drm_dbg_kms(dev,
36414b7897dSJani Nikula 				    "[CONNECTOR:%d:%s] no modes for connector tiled %d\n",
36514b7897dSJani Nikula 				    connector->base.id, connector->name, i);
366cf13909aSNoralf Trønnes 			continue;
367cf13909aSNoralf Trønnes 		}
368cf13909aSNoralf Trønnes 		if (connector->tile_h_loc < h_idx)
369cf13909aSNoralf Trønnes 			hoffset += modes[i]->hdisplay;
370cf13909aSNoralf Trønnes 
371cf13909aSNoralf Trønnes 		if (connector->tile_v_loc < v_idx)
372cf13909aSNoralf Trønnes 			voffset += modes[i]->vdisplay;
373cf13909aSNoralf Trønnes 	}
374cf13909aSNoralf Trønnes 	offsets[idx].x = hoffset;
375cf13909aSNoralf Trønnes 	offsets[idx].y = voffset;
37614b7897dSJani Nikula 	drm_dbg_kms(dev, "returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
377cf13909aSNoralf Trønnes 	return 0;
378cf13909aSNoralf Trønnes }
379cf13909aSNoralf Trønnes 
drm_client_target_preferred(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)38014b7897dSJani Nikula static bool drm_client_target_preferred(struct drm_device *dev,
38114b7897dSJani Nikula 					struct drm_connector **connectors,
382cf13909aSNoralf Trønnes 					unsigned int connector_count,
383cf13909aSNoralf Trønnes 					struct drm_display_mode **modes,
384cf13909aSNoralf Trønnes 					struct drm_client_offset *offsets,
385cf13909aSNoralf Trønnes 					bool *enabled, int width, int height)
386cf13909aSNoralf Trønnes {
387cf13909aSNoralf Trønnes 	const u64 mask = BIT_ULL(connector_count) - 1;
388cf13909aSNoralf Trønnes 	struct drm_connector *connector;
389cf13909aSNoralf Trønnes 	u64 conn_configured = 0;
390cf13909aSNoralf Trønnes 	int tile_pass = 0;
391cf1d0180SManasi Navare 	int num_tiled_conns = 0;
392cf13909aSNoralf Trønnes 	int i;
393cf13909aSNoralf Trønnes 
394cf1d0180SManasi Navare 	for (i = 0; i < connector_count; i++) {
395cf1d0180SManasi Navare 		if (connectors[i]->has_tile &&
396cf1d0180SManasi Navare 		    connectors[i]->status == connector_status_connected)
397cf1d0180SManasi Navare 			num_tiled_conns++;
398cf1d0180SManasi Navare 	}
399cf1d0180SManasi Navare 
400cf13909aSNoralf Trønnes retry:
401cf13909aSNoralf Trønnes 	for (i = 0; i < connector_count; i++) {
402cf13909aSNoralf Trønnes 		connector = connectors[i];
403cf13909aSNoralf Trønnes 
404cf13909aSNoralf Trønnes 		if (conn_configured & BIT_ULL(i))
405cf13909aSNoralf Trønnes 			continue;
406cf13909aSNoralf Trønnes 
407cf13909aSNoralf Trønnes 		if (enabled[i] == false) {
408cf13909aSNoralf Trønnes 			conn_configured |= BIT_ULL(i);
409cf13909aSNoralf Trønnes 			continue;
410cf13909aSNoralf Trønnes 		}
411cf13909aSNoralf Trønnes 
412cf13909aSNoralf Trønnes 		/* first pass over all the untiled connectors */
413cf13909aSNoralf Trønnes 		if (tile_pass == 0 && connector->has_tile)
414cf13909aSNoralf Trønnes 			continue;
415cf13909aSNoralf Trønnes 
416cf13909aSNoralf Trønnes 		if (tile_pass == 1) {
417cf13909aSNoralf Trønnes 			if (connector->tile_h_loc != 0 ||
418cf13909aSNoralf Trønnes 			    connector->tile_v_loc != 0)
419cf13909aSNoralf Trønnes 				continue;
420cf13909aSNoralf Trønnes 
421cf13909aSNoralf Trønnes 		} else {
422cf13909aSNoralf Trønnes 			if (connector->tile_h_loc != tile_pass - 1 &&
423cf13909aSNoralf Trønnes 			    connector->tile_v_loc != tile_pass - 1)
424cf13909aSNoralf Trønnes 			/* if this tile_pass doesn't cover any of the tiles - keep going */
425cf13909aSNoralf Trønnes 				continue;
426cf13909aSNoralf Trønnes 
427cf13909aSNoralf Trønnes 			/*
428cf13909aSNoralf Trønnes 			 * find the tile offsets for this pass - need to find
429cf13909aSNoralf Trønnes 			 * all tiles left and above
430cf13909aSNoralf Trønnes 			 */
43114b7897dSJani Nikula 			drm_client_get_tile_offsets(dev, connectors, connector_count,
43214b7897dSJani Nikula 						    modes, offsets, i,
433cf13909aSNoralf Trønnes 						    connector->tile_h_loc, connector->tile_v_loc);
434cf13909aSNoralf Trønnes 		}
43514b7897dSJani Nikula 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] looking for cmdline mode\n",
43614b7897dSJani Nikula 			    connector->base.id, connector->name);
437cf13909aSNoralf Trønnes 
438cf13909aSNoralf Trønnes 		/* got for command line mode first */
439cf13909aSNoralf Trønnes 		modes[i] = drm_connector_pick_cmdline_mode(connector);
440cf13909aSNoralf Trønnes 		if (!modes[i]) {
44114b7897dSJani Nikula 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] looking for preferred mode, tile %d\n",
44214b7897dSJani Nikula 				    connector->base.id, connector->name,
44314b7897dSJani Nikula 				    connector->tile_group ? connector->tile_group->id : 0);
444cf13909aSNoralf Trønnes 			modes[i] = drm_connector_has_preferred_mode(connector, width, height);
445cf13909aSNoralf Trønnes 		}
446cf13909aSNoralf Trønnes 		/* No preferred modes, pick one off the list */
447cf13909aSNoralf Trønnes 		if (!modes[i] && !list_empty(&connector->modes)) {
448cf13909aSNoralf Trønnes 			list_for_each_entry(modes[i], &connector->modes, head)
449cf13909aSNoralf Trønnes 				break;
450cf13909aSNoralf Trønnes 		}
451cf1d0180SManasi Navare 		/*
452cf1d0180SManasi Navare 		 * In case of tiled mode if all tiles not present fallback to
453cf1d0180SManasi Navare 		 * first available non tiled mode.
454cf1d0180SManasi Navare 		 * After all tiles are present, try to find the tiled mode
455cf1d0180SManasi Navare 		 * for all and if tiled mode not present due to fbcon size
456cf1d0180SManasi Navare 		 * limitations, use first non tiled mode only for
457cf1d0180SManasi Navare 		 * tile 0,0 and set to no mode for all other tiles.
458cf1d0180SManasi Navare 		 */
459cf1d0180SManasi Navare 		if (connector->has_tile) {
460cf1d0180SManasi Navare 			if (num_tiled_conns <
461cf1d0180SManasi Navare 			    connector->num_h_tile * connector->num_v_tile ||
462cf1d0180SManasi Navare 			    (connector->tile_h_loc == 0 &&
463cf1d0180SManasi Navare 			     connector->tile_v_loc == 0 &&
464cf1d0180SManasi Navare 			     !drm_connector_get_tiled_mode(connector))) {
46514b7897dSJani Nikula 				drm_dbg_kms(dev,
46614b7897dSJani Nikula 					    "[CONNECTOR:%d:%s] Falling back to non-tiled mode\n",
46714b7897dSJani Nikula 					    connector->base.id, connector->name);
468cf1d0180SManasi Navare 				modes[i] = drm_connector_fallback_non_tiled_mode(connector);
469cf1d0180SManasi Navare 			} else {
470cf1d0180SManasi Navare 				modes[i] = drm_connector_get_tiled_mode(connector);
471cf1d0180SManasi Navare 			}
472cf1d0180SManasi Navare 		}
473cf1d0180SManasi Navare 
47414b7897dSJani Nikula 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Found mode %s\n",
47514b7897dSJani Nikula 			    connector->base.id, connector->name,
47614b7897dSJani Nikula 			    modes[i] ? modes[i]->name : "none");
477cf13909aSNoralf Trønnes 		conn_configured |= BIT_ULL(i);
478cf13909aSNoralf Trønnes 	}
479cf13909aSNoralf Trønnes 
480cf13909aSNoralf Trønnes 	if ((conn_configured & mask) != mask) {
481cf13909aSNoralf Trønnes 		tile_pass++;
482cf13909aSNoralf Trønnes 		goto retry;
483cf13909aSNoralf Trønnes 	}
484cf13909aSNoralf Trønnes 	return true;
485cf13909aSNoralf Trønnes }
486cf13909aSNoralf Trønnes 
connector_has_possible_crtc(struct drm_connector * connector,struct drm_crtc * crtc)487cf13909aSNoralf Trønnes static bool connector_has_possible_crtc(struct drm_connector *connector,
488cf13909aSNoralf Trønnes 					struct drm_crtc *crtc)
489cf13909aSNoralf Trønnes {
490cf13909aSNoralf Trønnes 	struct drm_encoder *encoder;
491cf13909aSNoralf Trønnes 
49262afb4adSJosé Roberto de Souza 	drm_connector_for_each_possible_encoder(connector, encoder) {
493cf13909aSNoralf Trønnes 		if (encoder->possible_crtcs & drm_crtc_mask(crtc))
494cf13909aSNoralf Trønnes 			return true;
495cf13909aSNoralf Trønnes 	}
496cf13909aSNoralf Trønnes 
497cf13909aSNoralf Trønnes 	return false;
498cf13909aSNoralf Trønnes }
499cf13909aSNoralf 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)500cf13909aSNoralf Trønnes static int drm_client_pick_crtcs(struct drm_client_dev *client,
501cf13909aSNoralf Trønnes 				 struct drm_connector **connectors,
502cf13909aSNoralf Trønnes 				 unsigned int connector_count,
503cf13909aSNoralf Trønnes 				 struct drm_crtc **best_crtcs,
504cf13909aSNoralf Trønnes 				 struct drm_display_mode **modes,
505cf13909aSNoralf Trønnes 				 int n, int width, int height)
506cf13909aSNoralf Trønnes {
507cf13909aSNoralf Trønnes 	struct drm_device *dev = client->dev;
508cf13909aSNoralf Trønnes 	struct drm_connector *connector;
509cf13909aSNoralf Trønnes 	int my_score, best_score, score;
510cf13909aSNoralf Trønnes 	struct drm_crtc **crtcs, *crtc;
511cf13909aSNoralf Trønnes 	struct drm_mode_set *modeset;
512cf13909aSNoralf Trønnes 	int o;
513cf13909aSNoralf Trønnes 
514cf13909aSNoralf Trønnes 	if (n == connector_count)
515cf13909aSNoralf Trønnes 		return 0;
516cf13909aSNoralf Trønnes 
517cf13909aSNoralf Trønnes 	connector = connectors[n];
518cf13909aSNoralf Trønnes 
519cf13909aSNoralf Trønnes 	best_crtcs[n] = NULL;
520cf13909aSNoralf Trønnes 	best_score = drm_client_pick_crtcs(client, connectors, connector_count,
521cf13909aSNoralf Trønnes 					   best_crtcs, modes, n + 1, width, height);
522cf13909aSNoralf Trønnes 	if (modes[n] == NULL)
523cf13909aSNoralf Trønnes 		return best_score;
524cf13909aSNoralf Trønnes 
525cf13909aSNoralf Trønnes 	crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
526cf13909aSNoralf Trønnes 	if (!crtcs)
527cf13909aSNoralf Trønnes 		return best_score;
528cf13909aSNoralf Trønnes 
529cf13909aSNoralf Trønnes 	my_score = 1;
530cf13909aSNoralf Trønnes 	if (connector->status == connector_status_connected)
531cf13909aSNoralf Trønnes 		my_score++;
532cf13909aSNoralf Trønnes 	if (connector->cmdline_mode.specified)
533cf13909aSNoralf Trønnes 		my_score++;
534cf13909aSNoralf Trønnes 	if (drm_connector_has_preferred_mode(connector, width, height))
535cf13909aSNoralf Trønnes 		my_score++;
536cf13909aSNoralf Trønnes 
537cf13909aSNoralf Trønnes 	/*
538cf13909aSNoralf Trønnes 	 * select a crtc for this connector and then attempt to configure
539cf13909aSNoralf Trønnes 	 * remaining connectors
540cf13909aSNoralf Trønnes 	 */
541cf13909aSNoralf Trønnes 	drm_client_for_each_modeset(modeset, client) {
542cf13909aSNoralf Trønnes 		crtc = modeset->crtc;
543cf13909aSNoralf Trønnes 
544cf13909aSNoralf Trønnes 		if (!connector_has_possible_crtc(connector, crtc))
545cf13909aSNoralf Trønnes 			continue;
546cf13909aSNoralf Trønnes 
547cf13909aSNoralf Trønnes 		for (o = 0; o < n; o++)
548cf13909aSNoralf Trønnes 			if (best_crtcs[o] == crtc)
549cf13909aSNoralf Trønnes 				break;
550cf13909aSNoralf Trønnes 
551cf13909aSNoralf Trønnes 		if (o < n) {
552cf13909aSNoralf Trønnes 			/* ignore cloning unless only a single crtc */
553cf13909aSNoralf Trønnes 			if (dev->mode_config.num_crtc > 1)
554cf13909aSNoralf Trønnes 				continue;
555cf13909aSNoralf Trønnes 
556cf13909aSNoralf Trønnes 			if (!drm_mode_equal(modes[o], modes[n]))
557cf13909aSNoralf Trønnes 				continue;
558cf13909aSNoralf Trønnes 		}
559cf13909aSNoralf Trønnes 
560cf13909aSNoralf Trønnes 		crtcs[n] = crtc;
561cf13909aSNoralf Trønnes 		memcpy(crtcs, best_crtcs, n * sizeof(*crtcs));
562cf13909aSNoralf Trønnes 		score = my_score + drm_client_pick_crtcs(client, connectors, connector_count,
563cf13909aSNoralf Trønnes 							 crtcs, modes, n + 1, width, height);
564cf13909aSNoralf Trønnes 		if (score > best_score) {
565cf13909aSNoralf Trønnes 			best_score = score;
566cf13909aSNoralf Trønnes 			memcpy(best_crtcs, crtcs, connector_count * sizeof(*crtcs));
567cf13909aSNoralf Trønnes 		}
568cf13909aSNoralf Trønnes 	}
569cf13909aSNoralf Trønnes 
570cf13909aSNoralf Trønnes 	kfree(crtcs);
571cf13909aSNoralf Trønnes 	return best_score;
572cf13909aSNoralf Trønnes }
573cf13909aSNoralf Trønnes 
574cf13909aSNoralf 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)575cf13909aSNoralf Trønnes static bool drm_client_firmware_config(struct drm_client_dev *client,
576cf13909aSNoralf Trønnes 				       struct drm_connector **connectors,
577cf13909aSNoralf Trønnes 				       unsigned int connector_count,
578cf13909aSNoralf Trønnes 				       struct drm_crtc **crtcs,
579cf13909aSNoralf Trønnes 				       struct drm_display_mode **modes,
580cf13909aSNoralf Trønnes 				       struct drm_client_offset *offsets,
581cf13909aSNoralf Trønnes 				       bool *enabled, int width, int height)
582cf13909aSNoralf Trønnes {
5832803aa74SChris Wilson 	const int count = min_t(unsigned int, connector_count, BITS_PER_LONG);
584cf13909aSNoralf Trønnes 	unsigned long conn_configured, conn_seq, mask;
585cf13909aSNoralf Trønnes 	struct drm_device *dev = client->dev;
586cf13909aSNoralf Trønnes 	int i, j;
587cf13909aSNoralf Trønnes 	bool *save_enabled;
588cf13909aSNoralf Trønnes 	bool fallback = true, ret = true;
589cf13909aSNoralf Trønnes 	int num_connectors_enabled = 0;
590cf13909aSNoralf Trønnes 	int num_connectors_detected = 0;
591cf1d0180SManasi Navare 	int num_tiled_conns = 0;
592cf13909aSNoralf Trønnes 	struct drm_modeset_acquire_ctx ctx;
593cf13909aSNoralf Trønnes 
594cf13909aSNoralf Trønnes 	if (!drm_drv_uses_atomic_modeset(dev))
595cf13909aSNoralf Trønnes 		return false;
596cf13909aSNoralf Trønnes 
59714b7897dSJani Nikula 	if (drm_WARN_ON(dev, count <= 0))
5982803aa74SChris Wilson 		return false;
5992803aa74SChris Wilson 
600cf13909aSNoralf Trønnes 	save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
601cf13909aSNoralf Trønnes 	if (!save_enabled)
602cf13909aSNoralf Trønnes 		return false;
603cf13909aSNoralf Trønnes 
60461bae132SSean Paul 	drm_modeset_acquire_init(&ctx, 0);
60561bae132SSean Paul 
60661bae132SSean Paul 	while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
60761bae132SSean Paul 		drm_modeset_backoff(&ctx);
608cf13909aSNoralf Trønnes 
609cf13909aSNoralf Trønnes 	memcpy(save_enabled, enabled, count);
610cf13909aSNoralf Trønnes 	mask = GENMASK(count - 1, 0);
611cf13909aSNoralf Trønnes 	conn_configured = 0;
612cf1d0180SManasi Navare 	for (i = 0; i < count; i++) {
613cf1d0180SManasi Navare 		if (connectors[i]->has_tile &&
614cf1d0180SManasi Navare 		    connectors[i]->status == connector_status_connected)
615cf1d0180SManasi Navare 			num_tiled_conns++;
616cf1d0180SManasi Navare 	}
617cf13909aSNoralf Trønnes retry:
618cf13909aSNoralf Trønnes 	conn_seq = conn_configured;
619cf13909aSNoralf Trønnes 	for (i = 0; i < count; i++) {
620cf13909aSNoralf Trønnes 		struct drm_connector *connector;
621cf13909aSNoralf Trønnes 		struct drm_encoder *encoder;
622cf13909aSNoralf Trønnes 		struct drm_crtc *new_crtc;
623cf13909aSNoralf Trønnes 
624cf13909aSNoralf Trønnes 		connector = connectors[i];
625cf13909aSNoralf Trønnes 
626cf13909aSNoralf Trønnes 		if (conn_configured & BIT(i))
627cf13909aSNoralf Trønnes 			continue;
628cf13909aSNoralf Trønnes 
629cf13909aSNoralf Trønnes 		if (conn_seq == 0 && !connector->has_tile)
630cf13909aSNoralf Trønnes 			continue;
631cf13909aSNoralf Trønnes 
632cf13909aSNoralf Trønnes 		if (connector->status == connector_status_connected)
633cf13909aSNoralf Trønnes 			num_connectors_detected++;
634cf13909aSNoralf Trønnes 
635cf13909aSNoralf Trønnes 		if (!enabled[i]) {
63614b7897dSJani Nikula 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] not enabled, skipping\n",
63714b7897dSJani Nikula 				    connector->base.id, connector->name);
638cf13909aSNoralf Trønnes 			conn_configured |= BIT(i);
639cf13909aSNoralf Trønnes 			continue;
640cf13909aSNoralf Trønnes 		}
641cf13909aSNoralf Trønnes 
642cf13909aSNoralf Trønnes 		if (connector->force == DRM_FORCE_OFF) {
64314b7897dSJani Nikula 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] disabled by user, skipping\n",
64414b7897dSJani Nikula 				    connector->base.id, connector->name);
645cf13909aSNoralf Trønnes 			enabled[i] = false;
646cf13909aSNoralf Trønnes 			continue;
647cf13909aSNoralf Trønnes 		}
648cf13909aSNoralf Trønnes 
649cf13909aSNoralf Trønnes 		encoder = connector->state->best_encoder;
65014b7897dSJani Nikula 		if (!encoder || drm_WARN_ON(dev, !connector->state->crtc)) {
651cf13909aSNoralf Trønnes 			if (connector->force > DRM_FORCE_OFF)
652cf13909aSNoralf Trønnes 				goto bail;
653cf13909aSNoralf Trønnes 
65414b7897dSJani Nikula 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] has no encoder or crtc, skipping\n",
65514b7897dSJani Nikula 				    connector->base.id, connector->name);
656cf13909aSNoralf Trønnes 			enabled[i] = false;
657cf13909aSNoralf Trønnes 			conn_configured |= BIT(i);
658cf13909aSNoralf Trønnes 			continue;
659cf13909aSNoralf Trønnes 		}
660cf13909aSNoralf Trønnes 
661cf13909aSNoralf Trønnes 		num_connectors_enabled++;
662cf13909aSNoralf Trønnes 
663cf13909aSNoralf Trønnes 		new_crtc = connector->state->crtc;
664cf13909aSNoralf Trønnes 
665cf13909aSNoralf Trønnes 		/*
666cf13909aSNoralf Trønnes 		 * Make sure we're not trying to drive multiple connectors
667cf13909aSNoralf Trønnes 		 * with a single CRTC, since our cloning support may not
668cf13909aSNoralf Trønnes 		 * match the BIOS.
669cf13909aSNoralf Trønnes 		 */
670cf13909aSNoralf Trønnes 		for (j = 0; j < count; j++) {
671cf13909aSNoralf Trønnes 			if (crtcs[j] == new_crtc) {
67214b7897dSJani Nikula 				drm_dbg_kms(dev, "fallback: cloned configuration\n");
673cf13909aSNoralf Trønnes 				goto bail;
674cf13909aSNoralf Trønnes 			}
675cf13909aSNoralf Trønnes 		}
676cf13909aSNoralf Trønnes 
67714b7897dSJani Nikula 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] looking for cmdline mode\n",
67814b7897dSJani Nikula 			    connector->base.id, connector->name);
679cf13909aSNoralf Trønnes 
680cf13909aSNoralf Trønnes 		/* go for command line mode first */
681cf13909aSNoralf Trønnes 		modes[i] = drm_connector_pick_cmdline_mode(connector);
682cf13909aSNoralf Trønnes 
683cf13909aSNoralf Trønnes 		/* try for preferred next */
684cf13909aSNoralf Trønnes 		if (!modes[i]) {
68514b7897dSJani Nikula 			drm_dbg_kms(dev,
68614b7897dSJani Nikula 				    "[CONNECTOR:%d:%s] looking for preferred mode, has tile: %s\n",
68714b7897dSJani Nikula 				    connector->base.id, connector->name,
68814b7897dSJani Nikula 				    str_yes_no(connector->has_tile));
689cf13909aSNoralf Trønnes 			modes[i] = drm_connector_has_preferred_mode(connector, width, height);
690cf13909aSNoralf Trønnes 		}
691cf13909aSNoralf Trønnes 
692cf13909aSNoralf Trønnes 		/* No preferred mode marked by the EDID? Are there any modes? */
693cf13909aSNoralf Trønnes 		if (!modes[i] && !list_empty(&connector->modes)) {
69414b7897dSJani Nikula 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] using first listed mode\n",
69514b7897dSJani Nikula 				    connector->base.id, connector->name);
696cf13909aSNoralf Trønnes 			modes[i] = list_first_entry(&connector->modes,
697cf13909aSNoralf Trønnes 						    struct drm_display_mode,
698cf13909aSNoralf Trønnes 						    head);
699cf13909aSNoralf Trønnes 		}
700cf13909aSNoralf Trønnes 
701cf13909aSNoralf Trønnes 		/* last resort: use current mode */
702cf13909aSNoralf Trønnes 		if (!modes[i]) {
703cf13909aSNoralf Trønnes 			/*
704cf13909aSNoralf Trønnes 			 * IMPORTANT: We want to use the adjusted mode (i.e.
705cf13909aSNoralf Trønnes 			 * after the panel fitter upscaling) as the initial
706cf13909aSNoralf Trønnes 			 * config, not the input mode, which is what crtc->mode
707cf13909aSNoralf Trønnes 			 * usually contains. But since our current
708cf13909aSNoralf Trønnes 			 * code puts a mode derived from the post-pfit timings
709cf13909aSNoralf Trønnes 			 * into crtc->mode this works out correctly.
710cf13909aSNoralf Trønnes 			 *
711cf13909aSNoralf Trønnes 			 * This is crtc->mode and not crtc->state->mode for the
712cf13909aSNoralf Trønnes 			 * fastboot check to work correctly.
713cf13909aSNoralf Trønnes 			 */
71414b7897dSJani Nikula 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] looking for current mode\n",
71514b7897dSJani Nikula 				    connector->base.id, connector->name);
716cf13909aSNoralf Trønnes 			modes[i] = &connector->state->crtc->mode;
717cf13909aSNoralf Trønnes 		}
718cf1d0180SManasi Navare 		/*
719cf1d0180SManasi Navare 		 * In case of tiled modes, if all tiles are not present
720cf1d0180SManasi Navare 		 * then fallback to a non tiled mode.
721cf1d0180SManasi Navare 		 */
722cf1d0180SManasi Navare 		if (connector->has_tile &&
723cf1d0180SManasi Navare 		    num_tiled_conns < connector->num_h_tile * connector->num_v_tile) {
72414b7897dSJani Nikula 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Falling back to non-tiled mode\n",
72514b7897dSJani Nikula 				    connector->base.id, connector->name);
726cf1d0180SManasi Navare 			modes[i] = drm_connector_fallback_non_tiled_mode(connector);
727cf1d0180SManasi Navare 		}
728cf13909aSNoralf Trønnes 		crtcs[i] = new_crtc;
729cf13909aSNoralf Trønnes 
73014b7897dSJani Nikula 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] on [CRTC:%d:%s]: %dx%d%s\n",
73114b7897dSJani Nikula 			    connector->base.id, connector->name,
732cf13909aSNoralf Trønnes 			    connector->state->crtc->base.id,
733cf13909aSNoralf Trønnes 			    connector->state->crtc->name,
734cf13909aSNoralf Trønnes 			    modes[i]->hdisplay, modes[i]->vdisplay,
735cf13909aSNoralf Trønnes 			    modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "");
736cf13909aSNoralf Trønnes 
737cf13909aSNoralf Trønnes 		fallback = false;
738cf13909aSNoralf Trønnes 		conn_configured |= BIT(i);
739cf13909aSNoralf Trønnes 	}
740cf13909aSNoralf Trønnes 
741cf13909aSNoralf Trønnes 	if ((conn_configured & mask) != mask && conn_configured != conn_seq)
742cf13909aSNoralf Trønnes 		goto retry;
743cf13909aSNoralf Trønnes 
744cf13909aSNoralf Trønnes 	/*
745cf13909aSNoralf Trønnes 	 * If the BIOS didn't enable everything it could, fall back to have the
746cf13909aSNoralf Trønnes 	 * same user experiencing of lighting up as much as possible like the
747cf13909aSNoralf Trønnes 	 * fbdev helper library.
748cf13909aSNoralf Trønnes 	 */
749cf13909aSNoralf Trønnes 	if (num_connectors_enabled != num_connectors_detected &&
750cf13909aSNoralf Trønnes 	    num_connectors_enabled < dev->mode_config.num_crtc) {
75114b7897dSJani Nikula 		drm_dbg_kms(dev, "fallback: Not all outputs enabled\n");
75214b7897dSJani Nikula 		drm_dbg_kms(dev, "Enabled: %i, detected: %i\n",
75314b7897dSJani Nikula 			    num_connectors_enabled, num_connectors_detected);
754cf13909aSNoralf Trønnes 		fallback = true;
755cf13909aSNoralf Trønnes 	}
756cf13909aSNoralf Trønnes 
757cf13909aSNoralf Trønnes 	if (fallback) {
758cf13909aSNoralf Trønnes bail:
75914b7897dSJani Nikula 		drm_dbg_kms(dev, "Not using firmware configuration\n");
760cf13909aSNoralf Trønnes 		memcpy(enabled, save_enabled, count);
761cf13909aSNoralf Trønnes 		ret = false;
762cf13909aSNoralf Trønnes 	}
763cf13909aSNoralf Trønnes 
76461bae132SSean Paul 	drm_modeset_drop_locks(&ctx);
76561bae132SSean Paul 	drm_modeset_acquire_fini(&ctx);
766cf13909aSNoralf Trønnes 
767cf13909aSNoralf Trønnes 	kfree(save_enabled);
768cf13909aSNoralf Trønnes 	return ret;
769cf13909aSNoralf Trønnes }
770cf13909aSNoralf Trønnes 
771cf13909aSNoralf Trønnes /**
772cf13909aSNoralf Trønnes  * drm_client_modeset_probe() - Probe for displays
773cf13909aSNoralf Trønnes  * @client: DRM client
774cf13909aSNoralf Trønnes  * @width: Maximum display mode width (optional)
775cf13909aSNoralf Trønnes  * @height: Maximum display mode height (optional)
776cf13909aSNoralf Trønnes  *
777cf13909aSNoralf Trønnes  * This function sets up display pipelines for enabled connectors and stores the
778cf13909aSNoralf Trønnes  * config in the client's modeset array.
779cf13909aSNoralf Trønnes  *
780cf13909aSNoralf Trønnes  * Returns:
781cf13909aSNoralf Trønnes  * Zero on success or negative error code on failure.
782cf13909aSNoralf Trønnes  */
drm_client_modeset_probe(struct drm_client_dev * client,unsigned int width,unsigned int height)783cf13909aSNoralf Trønnes int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height)
784cf13909aSNoralf Trønnes {
785cf13909aSNoralf Trønnes 	struct drm_connector *connector, **connectors = NULL;
786cf13909aSNoralf Trønnes 	struct drm_connector_list_iter conn_iter;
787cf13909aSNoralf Trønnes 	struct drm_device *dev = client->dev;
788cf13909aSNoralf Trønnes 	unsigned int total_modes_count = 0;
789cf13909aSNoralf Trønnes 	struct drm_client_offset *offsets;
790cf13909aSNoralf Trønnes 	unsigned int connector_count = 0;
7913eadd887SVille Syrjälä 	/* points to modes protected by mode_config.mutex */
792cf13909aSNoralf Trønnes 	struct drm_display_mode **modes;
793cf13909aSNoralf Trønnes 	struct drm_crtc **crtcs;
794cf13909aSNoralf Trønnes 	int i, ret = 0;
795cf13909aSNoralf Trønnes 	bool *enabled;
796cf13909aSNoralf Trønnes 
79714b7897dSJani Nikula 	drm_dbg_kms(dev, "\n");
798cf13909aSNoralf Trønnes 
799cf13909aSNoralf Trønnes 	if (!width)
800cf13909aSNoralf Trønnes 		width = dev->mode_config.max_width;
801cf13909aSNoralf Trønnes 	if (!height)
802cf13909aSNoralf Trønnes 		height = dev->mode_config.max_height;
803cf13909aSNoralf Trønnes 
804cf13909aSNoralf Trønnes 	drm_connector_list_iter_begin(dev, &conn_iter);
805cf13909aSNoralf Trønnes 	drm_client_for_each_connector_iter(connector, &conn_iter) {
806cf13909aSNoralf Trønnes 		struct drm_connector **tmp;
807cf13909aSNoralf Trønnes 
808cf13909aSNoralf Trønnes 		tmp = krealloc(connectors, (connector_count + 1) * sizeof(*connectors), GFP_KERNEL);
809cf13909aSNoralf Trønnes 		if (!tmp) {
810cf13909aSNoralf Trønnes 			ret = -ENOMEM;
811cf13909aSNoralf Trønnes 			goto free_connectors;
812cf13909aSNoralf Trønnes 		}
813cf13909aSNoralf Trønnes 
814cf13909aSNoralf Trønnes 		connectors = tmp;
815cf13909aSNoralf Trønnes 		drm_connector_get(connector);
816cf13909aSNoralf Trønnes 		connectors[connector_count++] = connector;
817cf13909aSNoralf Trønnes 	}
818cf13909aSNoralf Trønnes 	drm_connector_list_iter_end(&conn_iter);
819cf13909aSNoralf Trønnes 
820cf13909aSNoralf Trønnes 	if (!connector_count)
821cf13909aSNoralf Trønnes 		return 0;
822cf13909aSNoralf Trønnes 
823cf13909aSNoralf Trønnes 	crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
824cf13909aSNoralf Trønnes 	modes = kcalloc(connector_count, sizeof(*modes), GFP_KERNEL);
825cf13909aSNoralf Trønnes 	offsets = kcalloc(connector_count, sizeof(*offsets), GFP_KERNEL);
826cf13909aSNoralf Trønnes 	enabled = kcalloc(connector_count, sizeof(bool), GFP_KERNEL);
827cf13909aSNoralf Trønnes 	if (!crtcs || !modes || !enabled || !offsets) {
828cf13909aSNoralf Trønnes 		ret = -ENOMEM;
829cf13909aSNoralf Trønnes 		goto out;
830cf13909aSNoralf Trønnes 	}
831cf13909aSNoralf Trønnes 
832cf13909aSNoralf Trønnes 	mutex_lock(&client->modeset_mutex);
833cf13909aSNoralf Trønnes 
834cf13909aSNoralf Trønnes 	mutex_lock(&dev->mode_config.mutex);
835cf13909aSNoralf Trønnes 	for (i = 0; i < connector_count; i++)
836cf13909aSNoralf Trønnes 		total_modes_count += connectors[i]->funcs->fill_modes(connectors[i], width, height);
837cf13909aSNoralf Trønnes 	if (!total_modes_count)
83814b7897dSJani Nikula 		drm_dbg_kms(dev, "No connectors reported connected with modes\n");
839cf13909aSNoralf Trønnes 	drm_client_connectors_enabled(connectors, connector_count, enabled);
840cf13909aSNoralf Trønnes 
841cf13909aSNoralf Trønnes 	if (!drm_client_firmware_config(client, connectors, connector_count, crtcs,
842cf13909aSNoralf Trønnes 					modes, offsets, enabled, width, height)) {
843cf13909aSNoralf Trønnes 		memset(modes, 0, connector_count * sizeof(*modes));
844cf13909aSNoralf Trønnes 		memset(crtcs, 0, connector_count * sizeof(*crtcs));
845cf13909aSNoralf Trønnes 		memset(offsets, 0, connector_count * sizeof(*offsets));
846cf13909aSNoralf Trønnes 
847cf13909aSNoralf Trønnes 		if (!drm_client_target_cloned(dev, connectors, connector_count, modes,
848cf13909aSNoralf Trønnes 					      offsets, enabled, width, height) &&
84914b7897dSJani Nikula 		    !drm_client_target_preferred(dev, connectors, connector_count, modes,
850cf13909aSNoralf Trønnes 						 offsets, enabled, width, height))
85114b7897dSJani Nikula 			drm_err(dev, "Unable to find initial modes\n");
852cf13909aSNoralf Trønnes 
85314b7897dSJani Nikula 		drm_dbg_kms(dev, "picking CRTCs for %dx%d config\n",
854cf13909aSNoralf Trønnes 			    width, height);
855cf13909aSNoralf Trønnes 
856cf13909aSNoralf Trønnes 		drm_client_pick_crtcs(client, connectors, connector_count,
857cf13909aSNoralf Trønnes 				      crtcs, modes, 0, width, height);
858cf13909aSNoralf Trønnes 	}
859cf13909aSNoralf Trønnes 
860cf13909aSNoralf Trønnes 	drm_client_modeset_release(client);
861cf13909aSNoralf Trønnes 
862cf13909aSNoralf Trønnes 	for (i = 0; i < connector_count; i++) {
863cf13909aSNoralf Trønnes 		struct drm_display_mode *mode = modes[i];
864cf13909aSNoralf Trønnes 		struct drm_crtc *crtc = crtcs[i];
865cf13909aSNoralf Trønnes 		struct drm_client_offset *offset = &offsets[i];
866cf13909aSNoralf Trønnes 
867cf13909aSNoralf Trønnes 		if (mode && crtc) {
868cf13909aSNoralf Trønnes 			struct drm_mode_set *modeset = drm_client_find_modeset(client, crtc);
869cf13909aSNoralf Trønnes 			struct drm_connector *connector = connectors[i];
870cf13909aSNoralf Trønnes 
87114b7897dSJani Nikula 			drm_dbg_kms(dev, "[CRTC:%d:%s] desired mode %s set (%d,%d)\n",
87214b7897dSJani Nikula 				    crtc->base.id, crtc->name,
87314b7897dSJani Nikula 				    mode->name, offset->x, offset->y);
874cf13909aSNoralf Trønnes 
87514b7897dSJani Nikula 			if (drm_WARN_ON_ONCE(dev, modeset->num_connectors == DRM_CLIENT_MAX_CLONED_CONNECTORS ||
876cf13909aSNoralf Trønnes 					     (dev->mode_config.num_crtc > 1 && modeset->num_connectors == 1))) {
877cf13909aSNoralf Trønnes 				ret = -EINVAL;
878cf13909aSNoralf Trønnes 				break;
879cf13909aSNoralf Trønnes 			}
880cf13909aSNoralf Trønnes 
8812329cc7aSJocelyn Falempe 			kfree(modeset->mode);
882cf13909aSNoralf Trønnes 			modeset->mode = drm_mode_duplicate(dev, mode);
883*113fd637SMa Ke 			if (!modeset->mode) {
884*113fd637SMa Ke 				ret = -ENOMEM;
885*113fd637SMa Ke 				break;
886*113fd637SMa Ke 			}
887*113fd637SMa Ke 
888cf13909aSNoralf Trønnes 			drm_connector_get(connector);
889cf13909aSNoralf Trønnes 			modeset->connectors[modeset->num_connectors++] = connector;
890cf13909aSNoralf Trønnes 			modeset->x = offset->x;
891cf13909aSNoralf Trønnes 			modeset->y = offset->y;
892cf13909aSNoralf Trønnes 		}
893cf13909aSNoralf Trønnes 	}
8943eadd887SVille Syrjälä 	mutex_unlock(&dev->mode_config.mutex);
895cf13909aSNoralf Trønnes 
896cf13909aSNoralf Trønnes 	mutex_unlock(&client->modeset_mutex);
897cf13909aSNoralf Trønnes out:
898cf13909aSNoralf Trønnes 	kfree(crtcs);
899cf13909aSNoralf Trønnes 	kfree(modes);
900cf13909aSNoralf Trønnes 	kfree(offsets);
901cf13909aSNoralf Trønnes 	kfree(enabled);
902cf13909aSNoralf Trønnes free_connectors:
903cf13909aSNoralf Trønnes 	for (i = 0; i < connector_count; i++)
904cf13909aSNoralf Trønnes 		drm_connector_put(connectors[i]);
905cf13909aSNoralf Trønnes 	kfree(connectors);
906cf13909aSNoralf Trønnes 
907cf13909aSNoralf Trønnes 	return ret;
908cf13909aSNoralf Trønnes }
909cf13909aSNoralf Trønnes EXPORT_SYMBOL(drm_client_modeset_probe);
910aec3925fSNoralf Trønnes 
911aec3925fSNoralf Trønnes /**
912a99076e8SMaxime Ripard  * drm_client_rotation() - Check the initial rotation value
913aec3925fSNoralf Trønnes  * @modeset: DRM modeset
914aec3925fSNoralf Trønnes  * @rotation: Returned rotation value
915aec3925fSNoralf Trønnes  *
916a99076e8SMaxime Ripard  * This function checks if the primary plane in @modeset can hw rotate
917a99076e8SMaxime Ripard  * to match the rotation needed on its connector.
918aec3925fSNoralf Trønnes  *
919aec3925fSNoralf Trønnes  * Note: Currently only 0 and 180 degrees are supported.
920aec3925fSNoralf Trønnes  *
921aec3925fSNoralf Trønnes  * Return:
922aec3925fSNoralf Trønnes  * True if the plane can do the rotation, false otherwise.
923aec3925fSNoralf Trønnes  */
drm_client_rotation(struct drm_mode_set * modeset,unsigned int * rotation)924a99076e8SMaxime Ripard bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation)
925aec3925fSNoralf Trønnes {
926aec3925fSNoralf Trønnes 	struct drm_connector *connector = modeset->connectors[0];
927aec3925fSNoralf Trønnes 	struct drm_plane *plane = modeset->crtc->primary;
9281bf4e092SMaxime Ripard 	struct drm_cmdline_mode *cmdline;
929aec3925fSNoralf Trønnes 	u64 valid_mask = 0;
930aec3925fSNoralf Trønnes 	unsigned int i;
931aec3925fSNoralf Trønnes 
932aec3925fSNoralf Trønnes 	if (!modeset->num_connectors)
933aec3925fSNoralf Trønnes 		return false;
934aec3925fSNoralf Trønnes 
935aec3925fSNoralf Trønnes 	switch (connector->display_info.panel_orientation) {
936aec3925fSNoralf Trønnes 	case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
937aec3925fSNoralf Trønnes 		*rotation = DRM_MODE_ROTATE_180;
938aec3925fSNoralf Trønnes 		break;
939aec3925fSNoralf Trønnes 	case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
940aec3925fSNoralf Trønnes 		*rotation = DRM_MODE_ROTATE_90;
941aec3925fSNoralf Trønnes 		break;
942aec3925fSNoralf Trønnes 	case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
943aec3925fSNoralf Trønnes 		*rotation = DRM_MODE_ROTATE_270;
944aec3925fSNoralf Trønnes 		break;
945aec3925fSNoralf Trønnes 	default:
946aec3925fSNoralf Trønnes 		*rotation = DRM_MODE_ROTATE_0;
947aec3925fSNoralf Trønnes 	}
948aec3925fSNoralf Trønnes 
9491bf4e092SMaxime Ripard 	/**
9501bf4e092SMaxime Ripard 	 * The panel already defined the default rotation
9511bf4e092SMaxime Ripard 	 * through its orientation. Whatever has been provided
9521bf4e092SMaxime Ripard 	 * on the command line needs to be added to that.
9531bf4e092SMaxime Ripard 	 *
9541bf4e092SMaxime Ripard 	 * Unfortunately, the rotations are at different bit
9551bf4e092SMaxime Ripard 	 * indices, so the math to add them up are not as
9561bf4e092SMaxime Ripard 	 * trivial as they could.
9571bf4e092SMaxime Ripard 	 *
9581bf4e092SMaxime Ripard 	 * Reflections on the other hand are pretty trivial to deal with, a
9591bf4e092SMaxime Ripard 	 * simple XOR between the two handle the addition nicely.
9601bf4e092SMaxime Ripard 	 */
9611bf4e092SMaxime Ripard 	cmdline = &connector->cmdline_mode;
9627aaddd96SDmitry Osipenko 	if (cmdline->specified && cmdline->rotation_reflection) {
9631bf4e092SMaxime Ripard 		unsigned int cmdline_rest, panel_rest;
9641bf4e092SMaxime Ripard 		unsigned int cmdline_rot, panel_rot;
9651bf4e092SMaxime Ripard 		unsigned int sum_rot, sum_rest;
9661bf4e092SMaxime Ripard 
9671bf4e092SMaxime Ripard 		panel_rot = ilog2(*rotation & DRM_MODE_ROTATE_MASK);
9681bf4e092SMaxime Ripard 		cmdline_rot = ilog2(cmdline->rotation_reflection & DRM_MODE_ROTATE_MASK);
9691bf4e092SMaxime Ripard 		sum_rot = (panel_rot + cmdline_rot) % 4;
9701bf4e092SMaxime Ripard 
9711bf4e092SMaxime Ripard 		panel_rest = *rotation & ~DRM_MODE_ROTATE_MASK;
9721bf4e092SMaxime Ripard 		cmdline_rest = cmdline->rotation_reflection & ~DRM_MODE_ROTATE_MASK;
9731bf4e092SMaxime Ripard 		sum_rest = panel_rest ^ cmdline_rest;
9741bf4e092SMaxime Ripard 
9751bf4e092SMaxime Ripard 		*rotation = (1 << sum_rot) | sum_rest;
9761bf4e092SMaxime Ripard 	}
9771bf4e092SMaxime Ripard 
978aec3925fSNoralf Trønnes 	/*
979aec3925fSNoralf Trønnes 	 * TODO: support 90 / 270 degree hardware rotation,
980aec3925fSNoralf Trønnes 	 * depending on the hardware this may require the framebuffer
981aec3925fSNoralf Trønnes 	 * to be in a specific tiling format.
982aec3925fSNoralf Trønnes 	 */
9835c320b6cSStephan Gerhold 	if (((*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0 &&
9845c320b6cSStephan Gerhold 	     (*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_180) ||
985307696d3SMaxime Ripard 	    !plane->rotation_property)
986aec3925fSNoralf Trønnes 		return false;
987aec3925fSNoralf Trønnes 
988aec3925fSNoralf Trønnes 	for (i = 0; i < plane->rotation_property->num_values; i++)
989aec3925fSNoralf Trønnes 		valid_mask |= (1ULL << plane->rotation_property->values[i]);
990aec3925fSNoralf Trønnes 
991aec3925fSNoralf Trønnes 	if (!(*rotation & valid_mask))
992aec3925fSNoralf Trønnes 		return false;
993aec3925fSNoralf Trønnes 
994aec3925fSNoralf Trønnes 	return true;
995aec3925fSNoralf Trønnes }
996a99076e8SMaxime Ripard EXPORT_SYMBOL(drm_client_rotation);
997aec3925fSNoralf Trønnes 
drm_client_modeset_commit_atomic(struct drm_client_dev * client,bool active,bool check)99864593f2aSNoralf Trønnes static int drm_client_modeset_commit_atomic(struct drm_client_dev *client, bool active, bool check)
999aec3925fSNoralf Trønnes {
1000aec3925fSNoralf Trønnes 	struct drm_device *dev = client->dev;
1001aec3925fSNoralf Trønnes 	struct drm_plane *plane;
1002aec3925fSNoralf Trønnes 	struct drm_atomic_state *state;
1003aec3925fSNoralf Trønnes 	struct drm_modeset_acquire_ctx ctx;
1004aec3925fSNoralf Trønnes 	struct drm_mode_set *mode_set;
1005aec3925fSNoralf Trønnes 	int ret;
1006aec3925fSNoralf Trønnes 
1007aec3925fSNoralf Trønnes 	drm_modeset_acquire_init(&ctx, 0);
1008aec3925fSNoralf Trønnes 
1009aec3925fSNoralf Trønnes 	state = drm_atomic_state_alloc(dev);
1010aec3925fSNoralf Trønnes 	if (!state) {
1011aec3925fSNoralf Trønnes 		ret = -ENOMEM;
1012aec3925fSNoralf Trønnes 		goto out_ctx;
1013aec3925fSNoralf Trønnes 	}
1014aec3925fSNoralf Trønnes 
1015aec3925fSNoralf Trønnes 	state->acquire_ctx = &ctx;
1016aec3925fSNoralf Trønnes retry:
1017aec3925fSNoralf Trønnes 	drm_for_each_plane(plane, dev) {
101846cc2d76SMaxime Ripard 		struct drm_plane_state *plane_state;
101946cc2d76SMaxime Ripard 
1020aec3925fSNoralf Trønnes 		plane_state = drm_atomic_get_plane_state(state, plane);
1021aec3925fSNoralf Trønnes 		if (IS_ERR(plane_state)) {
1022aec3925fSNoralf Trønnes 			ret = PTR_ERR(plane_state);
1023aec3925fSNoralf Trønnes 			goto out_state;
1024aec3925fSNoralf Trønnes 		}
1025aec3925fSNoralf Trønnes 
1026aec3925fSNoralf Trønnes 		plane_state->rotation = DRM_MODE_ROTATE_0;
1027aec3925fSNoralf Trønnes 
1028aec3925fSNoralf Trønnes 		/* disable non-primary: */
1029aec3925fSNoralf Trønnes 		if (plane->type == DRM_PLANE_TYPE_PRIMARY)
1030aec3925fSNoralf Trønnes 			continue;
1031aec3925fSNoralf Trønnes 
1032aec3925fSNoralf Trønnes 		ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1033aec3925fSNoralf Trønnes 		if (ret != 0)
1034aec3925fSNoralf Trønnes 			goto out_state;
1035aec3925fSNoralf Trønnes 	}
1036aec3925fSNoralf Trønnes 
1037aec3925fSNoralf Trønnes 	drm_client_for_each_modeset(mode_set, client) {
1038aec3925fSNoralf Trønnes 		struct drm_plane *primary = mode_set->crtc->primary;
1039aec3925fSNoralf Trønnes 		unsigned int rotation;
1040aec3925fSNoralf Trønnes 
1041a99076e8SMaxime Ripard 		if (drm_client_rotation(mode_set, &rotation)) {
104246cc2d76SMaxime Ripard 			struct drm_plane_state *plane_state;
104346cc2d76SMaxime Ripard 
1044aec3925fSNoralf Trønnes 			/* Cannot fail as we've already gotten the plane state above */
1045aec3925fSNoralf Trønnes 			plane_state = drm_atomic_get_new_plane_state(state, primary);
1046aec3925fSNoralf Trønnes 			plane_state->rotation = rotation;
1047aec3925fSNoralf Trønnes 		}
1048aec3925fSNoralf Trønnes 
1049aec3925fSNoralf Trønnes 		ret = __drm_atomic_helper_set_config(mode_set, state);
1050aec3925fSNoralf Trønnes 		if (ret != 0)
1051aec3925fSNoralf Trønnes 			goto out_state;
1052aec3925fSNoralf Trønnes 
1053aec3925fSNoralf Trønnes 		/*
1054aec3925fSNoralf Trønnes 		 * __drm_atomic_helper_set_config() sets active when a
1055aec3925fSNoralf Trønnes 		 * mode is set, unconditionally clear it if we force DPMS off
1056aec3925fSNoralf Trønnes 		 */
1057aec3925fSNoralf Trønnes 		if (!active) {
1058aec3925fSNoralf Trønnes 			struct drm_crtc *crtc = mode_set->crtc;
1059aec3925fSNoralf Trønnes 			struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1060aec3925fSNoralf Trønnes 
1061aec3925fSNoralf Trønnes 			crtc_state->active = false;
1062aec3925fSNoralf Trønnes 		}
1063aec3925fSNoralf Trønnes 	}
1064aec3925fSNoralf Trønnes 
106564593f2aSNoralf Trønnes 	if (check)
106664593f2aSNoralf Trønnes 		ret = drm_atomic_check_only(state);
106764593f2aSNoralf Trønnes 	else
1068aec3925fSNoralf Trønnes 		ret = drm_atomic_commit(state);
1069aec3925fSNoralf Trønnes 
1070aec3925fSNoralf Trønnes out_state:
1071aec3925fSNoralf Trønnes 	if (ret == -EDEADLK)
1072aec3925fSNoralf Trønnes 		goto backoff;
1073aec3925fSNoralf Trønnes 
1074aec3925fSNoralf Trønnes 	drm_atomic_state_put(state);
1075aec3925fSNoralf Trønnes out_ctx:
1076aec3925fSNoralf Trønnes 	drm_modeset_drop_locks(&ctx);
1077aec3925fSNoralf Trønnes 	drm_modeset_acquire_fini(&ctx);
1078aec3925fSNoralf Trønnes 
1079aec3925fSNoralf Trønnes 	return ret;
1080aec3925fSNoralf Trønnes 
1081aec3925fSNoralf Trønnes backoff:
1082aec3925fSNoralf Trønnes 	drm_atomic_state_clear(state);
1083aec3925fSNoralf Trønnes 	drm_modeset_backoff(&ctx);
1084aec3925fSNoralf Trønnes 
1085aec3925fSNoralf Trønnes 	goto retry;
1086aec3925fSNoralf Trønnes }
1087aec3925fSNoralf Trønnes 
drm_client_modeset_commit_legacy(struct drm_client_dev * client)1088aec3925fSNoralf Trønnes static int drm_client_modeset_commit_legacy(struct drm_client_dev *client)
1089aec3925fSNoralf Trønnes {
1090aec3925fSNoralf Trønnes 	struct drm_device *dev = client->dev;
1091aec3925fSNoralf Trønnes 	struct drm_mode_set *mode_set;
1092aec3925fSNoralf Trønnes 	struct drm_plane *plane;
1093aec3925fSNoralf Trønnes 	int ret = 0;
1094aec3925fSNoralf Trønnes 
109576fd2c37SSean Paul 	drm_modeset_lock_all(dev);
1096aec3925fSNoralf Trønnes 	drm_for_each_plane(plane, dev) {
1097aec3925fSNoralf Trønnes 		if (plane->type != DRM_PLANE_TYPE_PRIMARY)
1098aec3925fSNoralf Trønnes 			drm_plane_force_disable(plane);
1099aec3925fSNoralf Trønnes 
1100aec3925fSNoralf Trønnes 		if (plane->rotation_property)
1101aec3925fSNoralf Trønnes 			drm_mode_plane_set_obj_prop(plane,
1102aec3925fSNoralf Trønnes 						    plane->rotation_property,
1103aec3925fSNoralf Trønnes 						    DRM_MODE_ROTATE_0);
1104aec3925fSNoralf Trønnes 	}
1105aec3925fSNoralf Trønnes 
1106aec3925fSNoralf Trønnes 	drm_client_for_each_modeset(mode_set, client) {
1107aec3925fSNoralf Trønnes 		struct drm_crtc *crtc = mode_set->crtc;
1108aec3925fSNoralf Trønnes 
1109aec3925fSNoralf Trønnes 		if (crtc->funcs->cursor_set2) {
1110aec3925fSNoralf Trønnes 			ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
1111aec3925fSNoralf Trønnes 			if (ret)
1112aec3925fSNoralf Trønnes 				goto out;
1113aec3925fSNoralf Trønnes 		} else if (crtc->funcs->cursor_set) {
1114aec3925fSNoralf Trønnes 			ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
1115aec3925fSNoralf Trønnes 			if (ret)
1116aec3925fSNoralf Trønnes 				goto out;
1117aec3925fSNoralf Trønnes 		}
1118aec3925fSNoralf Trønnes 
1119aec3925fSNoralf Trønnes 		ret = drm_mode_set_config_internal(mode_set);
1120aec3925fSNoralf Trønnes 		if (ret)
1121aec3925fSNoralf Trønnes 			goto out;
1122aec3925fSNoralf Trønnes 	}
1123aec3925fSNoralf Trønnes out:
112476fd2c37SSean Paul 	drm_modeset_unlock_all(dev);
1125aec3925fSNoralf Trønnes 
1126aec3925fSNoralf Trønnes 	return ret;
1127aec3925fSNoralf Trønnes }
1128aec3925fSNoralf Trønnes 
1129aec3925fSNoralf Trønnes /**
113064593f2aSNoralf Trønnes  * drm_client_modeset_check() - Check modeset configuration
113164593f2aSNoralf Trønnes  * @client: DRM client
113264593f2aSNoralf Trønnes  *
113364593f2aSNoralf Trønnes  * Check modeset configuration.
113464593f2aSNoralf Trønnes  *
113564593f2aSNoralf Trønnes  * Returns:
113664593f2aSNoralf Trønnes  * Zero on success or negative error code on failure.
113764593f2aSNoralf Trønnes  */
drm_client_modeset_check(struct drm_client_dev * client)113864593f2aSNoralf Trønnes int drm_client_modeset_check(struct drm_client_dev *client)
113964593f2aSNoralf Trønnes {
114064593f2aSNoralf Trønnes 	int ret;
114164593f2aSNoralf Trønnes 
114264593f2aSNoralf Trønnes 	if (!drm_drv_uses_atomic_modeset(client->dev))
114364593f2aSNoralf Trønnes 		return 0;
114464593f2aSNoralf Trønnes 
114564593f2aSNoralf Trønnes 	mutex_lock(&client->modeset_mutex);
114664593f2aSNoralf Trønnes 	ret = drm_client_modeset_commit_atomic(client, true, true);
114764593f2aSNoralf Trønnes 	mutex_unlock(&client->modeset_mutex);
114864593f2aSNoralf Trønnes 
114964593f2aSNoralf Trønnes 	return ret;
115064593f2aSNoralf Trønnes }
115164593f2aSNoralf Trønnes EXPORT_SYMBOL(drm_client_modeset_check);
115264593f2aSNoralf Trønnes 
115364593f2aSNoralf Trønnes /**
1154c368ec19SDaniel Vetter  * drm_client_modeset_commit_locked() - Force commit CRTC configuration
1155aec3925fSNoralf Trønnes  * @client: DRM client
1156aec3925fSNoralf Trønnes  *
1157c368ec19SDaniel Vetter  * Commit modeset configuration to crtcs without checking if there is a DRM
1158c368ec19SDaniel Vetter  * master. The assumption is that the caller already holds an internal DRM
1159c368ec19SDaniel Vetter  * master reference acquired with drm_master_internal_acquire().
1160aec3925fSNoralf Trønnes  *
1161aec3925fSNoralf Trønnes  * Returns:
1162aec3925fSNoralf Trønnes  * Zero on success or negative error code on failure.
1163aec3925fSNoralf Trønnes  */
drm_client_modeset_commit_locked(struct drm_client_dev * client)1164c368ec19SDaniel Vetter int drm_client_modeset_commit_locked(struct drm_client_dev *client)
1165aec3925fSNoralf Trønnes {
1166aec3925fSNoralf Trønnes 	struct drm_device *dev = client->dev;
1167aec3925fSNoralf Trønnes 	int ret;
1168aec3925fSNoralf Trønnes 
1169aec3925fSNoralf Trønnes 	mutex_lock(&client->modeset_mutex);
1170aec3925fSNoralf Trønnes 	if (drm_drv_uses_atomic_modeset(dev))
117164593f2aSNoralf Trønnes 		ret = drm_client_modeset_commit_atomic(client, true, false);
1172aec3925fSNoralf Trønnes 	else
1173aec3925fSNoralf Trønnes 		ret = drm_client_modeset_commit_legacy(client);
1174aec3925fSNoralf Trønnes 	mutex_unlock(&client->modeset_mutex);
1175aec3925fSNoralf Trønnes 
1176aec3925fSNoralf Trønnes 	return ret;
1177aec3925fSNoralf Trønnes }
1178c368ec19SDaniel Vetter EXPORT_SYMBOL(drm_client_modeset_commit_locked);
1179aec3925fSNoralf Trønnes 
1180aec3925fSNoralf Trønnes /**
1181aec3925fSNoralf Trønnes  * drm_client_modeset_commit() - Commit CRTC configuration
1182aec3925fSNoralf Trønnes  * @client: DRM client
1183aec3925fSNoralf Trønnes  *
1184aec3925fSNoralf Trønnes  * Commit modeset configuration to crtcs.
1185aec3925fSNoralf Trønnes  *
1186aec3925fSNoralf Trønnes  * Returns:
1187aec3925fSNoralf Trønnes  * Zero on success or negative error code on failure.
1188aec3925fSNoralf Trønnes  */
drm_client_modeset_commit(struct drm_client_dev * client)1189aec3925fSNoralf Trønnes int drm_client_modeset_commit(struct drm_client_dev *client)
1190aec3925fSNoralf Trønnes {
1191aec3925fSNoralf Trønnes 	struct drm_device *dev = client->dev;
1192aec3925fSNoralf Trønnes 	int ret;
1193aec3925fSNoralf Trønnes 
1194aec3925fSNoralf Trønnes 	if (!drm_master_internal_acquire(dev))
1195aec3925fSNoralf Trønnes 		return -EBUSY;
1196aec3925fSNoralf Trønnes 
1197c368ec19SDaniel Vetter 	ret = drm_client_modeset_commit_locked(client);
1198aec3925fSNoralf Trønnes 
1199aec3925fSNoralf Trønnes 	drm_master_internal_release(dev);
1200aec3925fSNoralf Trønnes 
1201aec3925fSNoralf Trønnes 	return ret;
1202aec3925fSNoralf Trønnes }
1203aec3925fSNoralf Trønnes EXPORT_SYMBOL(drm_client_modeset_commit);
1204aec3925fSNoralf Trønnes 
drm_client_modeset_dpms_legacy(struct drm_client_dev * client,int dpms_mode)1205aec3925fSNoralf Trønnes static void drm_client_modeset_dpms_legacy(struct drm_client_dev *client, int dpms_mode)
1206aec3925fSNoralf Trønnes {
1207aec3925fSNoralf Trønnes 	struct drm_device *dev = client->dev;
1208aec3925fSNoralf Trønnes 	struct drm_connector *connector;
1209aec3925fSNoralf Trønnes 	struct drm_mode_set *modeset;
121057037094SJoseph Schulte 	struct drm_modeset_acquire_ctx ctx;
1211aec3925fSNoralf Trønnes 	int j;
121257037094SJoseph Schulte 	int ret;
1213aec3925fSNoralf Trønnes 
121457037094SJoseph Schulte 	DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
1215aec3925fSNoralf Trønnes 	drm_client_for_each_modeset(modeset, client) {
1216aec3925fSNoralf Trønnes 		if (!modeset->crtc->enabled)
1217aec3925fSNoralf Trønnes 			continue;
1218aec3925fSNoralf Trønnes 
1219aec3925fSNoralf Trønnes 		for (j = 0; j < modeset->num_connectors; j++) {
1220aec3925fSNoralf Trønnes 			connector = modeset->connectors[j];
1221aec3925fSNoralf Trønnes 			connector->funcs->dpms(connector, dpms_mode);
1222aec3925fSNoralf Trønnes 			drm_object_property_set_value(&connector->base,
1223aec3925fSNoralf Trønnes 				dev->mode_config.dpms_property, dpms_mode);
1224aec3925fSNoralf Trønnes 		}
1225aec3925fSNoralf Trønnes 	}
122657037094SJoseph Schulte 	DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
1227aec3925fSNoralf Trønnes }
1228aec3925fSNoralf Trønnes 
1229aec3925fSNoralf Trønnes /**
1230aec3925fSNoralf Trønnes  * drm_client_modeset_dpms() - Set DPMS mode
1231aec3925fSNoralf Trønnes  * @client: DRM client
1232aec3925fSNoralf Trønnes  * @mode: DPMS mode
1233aec3925fSNoralf Trønnes  *
1234aec3925fSNoralf Trønnes  * Note: For atomic drivers @mode is reduced to on/off.
1235aec3925fSNoralf Trønnes  *
1236aec3925fSNoralf Trønnes  * Returns:
1237aec3925fSNoralf Trønnes  * Zero on success or negative error code on failure.
1238aec3925fSNoralf Trønnes  */
drm_client_modeset_dpms(struct drm_client_dev * client,int mode)1239aec3925fSNoralf Trønnes int drm_client_modeset_dpms(struct drm_client_dev *client, int mode)
1240aec3925fSNoralf Trønnes {
1241aec3925fSNoralf Trønnes 	struct drm_device *dev = client->dev;
1242aec3925fSNoralf Trønnes 	int ret = 0;
1243aec3925fSNoralf Trønnes 
1244aec3925fSNoralf Trønnes 	if (!drm_master_internal_acquire(dev))
1245aec3925fSNoralf Trønnes 		return -EBUSY;
1246aec3925fSNoralf Trønnes 
1247aec3925fSNoralf Trønnes 	mutex_lock(&client->modeset_mutex);
1248aec3925fSNoralf Trønnes 	if (drm_drv_uses_atomic_modeset(dev))
124964593f2aSNoralf Trønnes 		ret = drm_client_modeset_commit_atomic(client, mode == DRM_MODE_DPMS_ON, false);
1250aec3925fSNoralf Trønnes 	else
1251aec3925fSNoralf Trønnes 		drm_client_modeset_dpms_legacy(client, mode);
1252aec3925fSNoralf Trønnes 	mutex_unlock(&client->modeset_mutex);
1253aec3925fSNoralf Trønnes 
1254aec3925fSNoralf Trønnes 	drm_master_internal_release(dev);
1255aec3925fSNoralf Trønnes 
1256aec3925fSNoralf Trønnes 	return ret;
1257aec3925fSNoralf Trønnes }
1258aec3925fSNoralf Trønnes EXPORT_SYMBOL(drm_client_modeset_dpms);
12598fc0380fSMaxime Ripard 
12608fc0380fSMaxime Ripard #ifdef CONFIG_DRM_KUNIT_TEST
12618fc0380fSMaxime Ripard #include "tests/drm_client_modeset_test.c"
12628fc0380fSMaxime Ripard #endif
1263