xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/drm_crtc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2006-2008 Intel Corporation
3*4882a593Smuzhiyun  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4*4882a593Smuzhiyun  * Copyright (c) 2008 Red Hat Inc.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * DRM core CRTC related functions
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Permission to use, copy, modify, distribute, and sell this software and its
9*4882a593Smuzhiyun  * documentation for any purpose is hereby granted without fee, provided that
10*4882a593Smuzhiyun  * the above copyright notice appear in all copies and that both that copyright
11*4882a593Smuzhiyun  * notice and this permission notice appear in supporting documentation, and
12*4882a593Smuzhiyun  * that the name of the copyright holders not be used in advertising or
13*4882a593Smuzhiyun  * publicity pertaining to distribution of the software without specific,
14*4882a593Smuzhiyun  * written prior permission.  The copyright holders make no representations
15*4882a593Smuzhiyun  * about the suitability of this software for any purpose.  It is provided "as
16*4882a593Smuzhiyun  * is" without express or implied warranty.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19*4882a593Smuzhiyun  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20*4882a593Smuzhiyun  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21*4882a593Smuzhiyun  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22*4882a593Smuzhiyun  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23*4882a593Smuzhiyun  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24*4882a593Smuzhiyun  * OF THIS SOFTWARE.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * Authors:
27*4882a593Smuzhiyun  *      Keith Packard
28*4882a593Smuzhiyun  *	Eric Anholt <eric@anholt.net>
29*4882a593Smuzhiyun  *      Dave Airlie <airlied@linux.ie>
30*4882a593Smuzhiyun  *      Jesse Barnes <jesse.barnes@intel.com>
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun #include <linux/ctype.h>
33*4882a593Smuzhiyun #include <linux/list.h>
34*4882a593Smuzhiyun #include <linux/slab.h>
35*4882a593Smuzhiyun #include <linux/export.h>
36*4882a593Smuzhiyun #include <linux/dma-fence.h>
37*4882a593Smuzhiyun #include <linux/uaccess.h>
38*4882a593Smuzhiyun #include <drm/drm_crtc.h>
39*4882a593Smuzhiyun #include <drm/drm_edid.h>
40*4882a593Smuzhiyun #include <drm/drm_fourcc.h>
41*4882a593Smuzhiyun #include <drm/drm_modeset_lock.h>
42*4882a593Smuzhiyun #include <drm/drm_atomic.h>
43*4882a593Smuzhiyun #include <drm/drm_auth.h>
44*4882a593Smuzhiyun #include <drm/drm_debugfs_crc.h>
45*4882a593Smuzhiyun #include <drm/drm_drv.h>
46*4882a593Smuzhiyun #include <drm/drm_print.h>
47*4882a593Smuzhiyun #include <drm/drm_file.h>
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #include "drm_crtc_internal.h"
50*4882a593Smuzhiyun #include "drm_internal.h"
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /**
53*4882a593Smuzhiyun  * DOC: overview
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * A CRTC represents the overall display pipeline. It receives pixel data from
56*4882a593Smuzhiyun  * &drm_plane and blends them together. The &drm_display_mode is also attached
57*4882a593Smuzhiyun  * to the CRTC, specifying display timings. On the output side the data is fed
58*4882a593Smuzhiyun  * to one or more &drm_encoder, which are then each connected to one
59*4882a593Smuzhiyun  * &drm_connector.
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * To create a CRTC, a KMS drivers allocates and zeroes an instances of
62*4882a593Smuzhiyun  * &struct drm_crtc (possibly as part of a larger structure) and registers it
63*4882a593Smuzhiyun  * with a call to drm_crtc_init_with_planes().
64*4882a593Smuzhiyun  *
65*4882a593Smuzhiyun  * The CRTC is also the entry point for legacy modeset operations, see
66*4882a593Smuzhiyun  * &drm_crtc_funcs.set_config, legacy plane operations, see
67*4882a593Smuzhiyun  * &drm_crtc_funcs.page_flip and &drm_crtc_funcs.cursor_set2, and other legacy
68*4882a593Smuzhiyun  * operations like &drm_crtc_funcs.gamma_set. For atomic drivers all these
69*4882a593Smuzhiyun  * features are controlled through &drm_property and
70*4882a593Smuzhiyun  * &drm_mode_config_funcs.atomic_check and &drm_mode_config_funcs.atomic_check.
71*4882a593Smuzhiyun  */
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /**
74*4882a593Smuzhiyun  * drm_crtc_from_index - find the registered CRTC at an index
75*4882a593Smuzhiyun  * @dev: DRM device
76*4882a593Smuzhiyun  * @idx: index of registered CRTC to find for
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * Given a CRTC index, return the registered CRTC from DRM device's
79*4882a593Smuzhiyun  * list of CRTCs with matching index. This is the inverse of drm_crtc_index().
80*4882a593Smuzhiyun  * It's useful in the vblank callbacks (like &drm_driver.enable_vblank or
81*4882a593Smuzhiyun  * &drm_driver.disable_vblank), since that still deals with indices instead
82*4882a593Smuzhiyun  * of pointers to &struct drm_crtc."
83*4882a593Smuzhiyun  */
drm_crtc_from_index(struct drm_device * dev,int idx)84*4882a593Smuzhiyun struct drm_crtc *drm_crtc_from_index(struct drm_device *dev, int idx)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	struct drm_crtc *crtc;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	drm_for_each_crtc(crtc, dev)
89*4882a593Smuzhiyun 		if (idx == crtc->index)
90*4882a593Smuzhiyun 			return crtc;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	return NULL;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun EXPORT_SYMBOL(drm_crtc_from_index);
95*4882a593Smuzhiyun 
drm_crtc_force_disable(struct drm_crtc * crtc)96*4882a593Smuzhiyun int drm_crtc_force_disable(struct drm_crtc *crtc)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	struct drm_mode_set set = {
99*4882a593Smuzhiyun 		.crtc = crtc,
100*4882a593Smuzhiyun 	};
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	return drm_mode_set_config_internal(&set);
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
drm_num_crtcs(struct drm_device * dev)107*4882a593Smuzhiyun static unsigned int drm_num_crtcs(struct drm_device *dev)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	unsigned int num = 0;
110*4882a593Smuzhiyun 	struct drm_crtc *tmp;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	drm_for_each_crtc(tmp, dev) {
113*4882a593Smuzhiyun 		num++;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	return num;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
drm_crtc_register_all(struct drm_device * dev)119*4882a593Smuzhiyun int drm_crtc_register_all(struct drm_device *dev)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	struct drm_crtc *crtc;
122*4882a593Smuzhiyun 	int ret = 0;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	drm_for_each_crtc(crtc, dev) {
125*4882a593Smuzhiyun 		drm_debugfs_crtc_add(crtc);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 		if (crtc->funcs->late_register)
128*4882a593Smuzhiyun 			ret = crtc->funcs->late_register(crtc);
129*4882a593Smuzhiyun 		if (ret)
130*4882a593Smuzhiyun 			return ret;
131*4882a593Smuzhiyun 	}
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	return 0;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
drm_crtc_unregister_all(struct drm_device * dev)136*4882a593Smuzhiyun void drm_crtc_unregister_all(struct drm_device *dev)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	struct drm_crtc *crtc;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	drm_for_each_crtc(crtc, dev) {
141*4882a593Smuzhiyun 		if (crtc->funcs->early_unregister)
142*4882a593Smuzhiyun 			crtc->funcs->early_unregister(crtc);
143*4882a593Smuzhiyun 		drm_debugfs_crtc_remove(crtc);
144*4882a593Smuzhiyun 	}
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
drm_crtc_crc_init(struct drm_crtc * crtc)147*4882a593Smuzhiyun static int drm_crtc_crc_init(struct drm_crtc *crtc)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
150*4882a593Smuzhiyun 	spin_lock_init(&crtc->crc.lock);
151*4882a593Smuzhiyun 	init_waitqueue_head(&crtc->crc.wq);
152*4882a593Smuzhiyun 	crtc->crc.source = kstrdup("auto", GFP_KERNEL);
153*4882a593Smuzhiyun 	if (!crtc->crc.source)
154*4882a593Smuzhiyun 		return -ENOMEM;
155*4882a593Smuzhiyun #endif
156*4882a593Smuzhiyun 	return 0;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun 
drm_crtc_crc_fini(struct drm_crtc * crtc)159*4882a593Smuzhiyun static void drm_crtc_crc_fini(struct drm_crtc *crtc)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
162*4882a593Smuzhiyun 	kfree(crtc->crc.source);
163*4882a593Smuzhiyun #endif
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun static const struct dma_fence_ops drm_crtc_fence_ops;
167*4882a593Smuzhiyun 
fence_to_crtc(struct dma_fence * fence)168*4882a593Smuzhiyun static struct drm_crtc *fence_to_crtc(struct dma_fence *fence)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun 	BUG_ON(fence->ops != &drm_crtc_fence_ops);
171*4882a593Smuzhiyun 	return container_of(fence->lock, struct drm_crtc, fence_lock);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
drm_crtc_fence_get_driver_name(struct dma_fence * fence)174*4882a593Smuzhiyun static const char *drm_crtc_fence_get_driver_name(struct dma_fence *fence)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	struct drm_crtc *crtc = fence_to_crtc(fence);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	return crtc->dev->driver->name;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
drm_crtc_fence_get_timeline_name(struct dma_fence * fence)181*4882a593Smuzhiyun static const char *drm_crtc_fence_get_timeline_name(struct dma_fence *fence)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	struct drm_crtc *crtc = fence_to_crtc(fence);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	return crtc->timeline_name;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun static const struct dma_fence_ops drm_crtc_fence_ops = {
189*4882a593Smuzhiyun 	.get_driver_name = drm_crtc_fence_get_driver_name,
190*4882a593Smuzhiyun 	.get_timeline_name = drm_crtc_fence_get_timeline_name,
191*4882a593Smuzhiyun };
192*4882a593Smuzhiyun 
drm_crtc_create_fence(struct drm_crtc * crtc)193*4882a593Smuzhiyun struct dma_fence *drm_crtc_create_fence(struct drm_crtc *crtc)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	struct dma_fence *fence;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
198*4882a593Smuzhiyun 	if (!fence)
199*4882a593Smuzhiyun 		return NULL;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	dma_fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock,
202*4882a593Smuzhiyun 		       crtc->fence_context, ++crtc->fence_seqno);
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	return fence;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun /**
208*4882a593Smuzhiyun  * DOC: standard CRTC properties
209*4882a593Smuzhiyun  *
210*4882a593Smuzhiyun  * DRM CRTCs have a few standardized properties:
211*4882a593Smuzhiyun  *
212*4882a593Smuzhiyun  * ACTIVE:
213*4882a593Smuzhiyun  * 	Atomic property for setting the power state of the CRTC. When set to 1
214*4882a593Smuzhiyun  * 	the CRTC will actively display content. When set to 0 the CRTC will be
215*4882a593Smuzhiyun  * 	powered off. There is no expectation that user-space will reset CRTC
216*4882a593Smuzhiyun  * 	resources like the mode and planes when setting ACTIVE to 0.
217*4882a593Smuzhiyun  *
218*4882a593Smuzhiyun  * 	User-space can rely on an ACTIVE change to 1 to never fail an atomic
219*4882a593Smuzhiyun  * 	test as long as no other property has changed. If a change to ACTIVE
220*4882a593Smuzhiyun  * 	fails an atomic test, this is a driver bug. For this reason setting
221*4882a593Smuzhiyun  * 	ACTIVE to 0 must not release internal resources (like reserved memory
222*4882a593Smuzhiyun  * 	bandwidth or clock generators).
223*4882a593Smuzhiyun  *
224*4882a593Smuzhiyun  * 	Note that the legacy DPMS property on connectors is internally routed
225*4882a593Smuzhiyun  * 	to control this property for atomic drivers.
226*4882a593Smuzhiyun  * MODE_ID:
227*4882a593Smuzhiyun  * 	Atomic property for setting the CRTC display timings. The value is the
228*4882a593Smuzhiyun  * 	ID of a blob containing the DRM mode info. To disable the CRTC,
229*4882a593Smuzhiyun  * 	user-space must set this property to 0.
230*4882a593Smuzhiyun  *
231*4882a593Smuzhiyun  * 	Setting MODE_ID to 0 will release reserved resources for the CRTC.
232*4882a593Smuzhiyun  */
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /**
235*4882a593Smuzhiyun  * drm_crtc_init_with_planes - Initialise a new CRTC object with
236*4882a593Smuzhiyun  *    specified primary and cursor planes.
237*4882a593Smuzhiyun  * @dev: DRM device
238*4882a593Smuzhiyun  * @crtc: CRTC object to init
239*4882a593Smuzhiyun  * @primary: Primary plane for CRTC
240*4882a593Smuzhiyun  * @cursor: Cursor plane for CRTC
241*4882a593Smuzhiyun  * @funcs: callbacks for the new CRTC
242*4882a593Smuzhiyun  * @name: printf style format string for the CRTC name, or NULL for default name
243*4882a593Smuzhiyun  *
244*4882a593Smuzhiyun  * Inits a new object created as base part of a driver crtc object. Drivers
245*4882a593Smuzhiyun  * should use this function instead of drm_crtc_init(), which is only provided
246*4882a593Smuzhiyun  * for backwards compatibility with drivers which do not yet support universal
247*4882a593Smuzhiyun  * planes). For really simple hardware which has only 1 plane look at
248*4882a593Smuzhiyun  * drm_simple_display_pipe_init() instead.
249*4882a593Smuzhiyun  *
250*4882a593Smuzhiyun  * Returns:
251*4882a593Smuzhiyun  * Zero on success, error code on failure.
252*4882a593Smuzhiyun  */
drm_crtc_init_with_planes(struct drm_device * dev,struct drm_crtc * crtc,struct drm_plane * primary,struct drm_plane * cursor,const struct drm_crtc_funcs * funcs,const char * name,...)253*4882a593Smuzhiyun int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
254*4882a593Smuzhiyun 			      struct drm_plane *primary,
255*4882a593Smuzhiyun 			      struct drm_plane *cursor,
256*4882a593Smuzhiyun 			      const struct drm_crtc_funcs *funcs,
257*4882a593Smuzhiyun 			      const char *name, ...)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun 	struct drm_mode_config *config = &dev->mode_config;
260*4882a593Smuzhiyun 	int ret;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
263*4882a593Smuzhiyun 	WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	/* crtc index is used with 32bit bitmasks */
266*4882a593Smuzhiyun 	if (WARN_ON(config->num_crtc >= 32))
267*4882a593Smuzhiyun 		return -EINVAL;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
270*4882a593Smuzhiyun 		(!funcs->atomic_destroy_state ||
271*4882a593Smuzhiyun 		 !funcs->atomic_duplicate_state));
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	crtc->dev = dev;
274*4882a593Smuzhiyun 	crtc->funcs = funcs;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	INIT_LIST_HEAD(&crtc->commit_list);
277*4882a593Smuzhiyun 	spin_lock_init(&crtc->commit_lock);
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	drm_modeset_lock_init(&crtc->mutex);
280*4882a593Smuzhiyun 	ret = drm_mode_object_add(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
281*4882a593Smuzhiyun 	if (ret)
282*4882a593Smuzhiyun 		return ret;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	if (name) {
285*4882a593Smuzhiyun 		va_list ap;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 		va_start(ap, name);
288*4882a593Smuzhiyun 		crtc->name = kvasprintf(GFP_KERNEL, name, ap);
289*4882a593Smuzhiyun 		va_end(ap);
290*4882a593Smuzhiyun 	} else {
291*4882a593Smuzhiyun 		crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
292*4882a593Smuzhiyun 				       drm_num_crtcs(dev));
293*4882a593Smuzhiyun 	}
294*4882a593Smuzhiyun 	if (!crtc->name) {
295*4882a593Smuzhiyun 		drm_mode_object_unregister(dev, &crtc->base);
296*4882a593Smuzhiyun 		return -ENOMEM;
297*4882a593Smuzhiyun 	}
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	crtc->fence_context = dma_fence_context_alloc(1);
300*4882a593Smuzhiyun 	spin_lock_init(&crtc->fence_lock);
301*4882a593Smuzhiyun 	snprintf(crtc->timeline_name, sizeof(crtc->timeline_name),
302*4882a593Smuzhiyun 		 "CRTC:%d-%s", crtc->base.id, crtc->name);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	crtc->base.properties = &crtc->properties;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	list_add_tail(&crtc->head, &config->crtc_list);
307*4882a593Smuzhiyun 	crtc->index = config->num_crtc++;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	crtc->primary = primary;
310*4882a593Smuzhiyun 	crtc->cursor = cursor;
311*4882a593Smuzhiyun 	if (primary && !primary->possible_crtcs)
312*4882a593Smuzhiyun 		primary->possible_crtcs = drm_crtc_mask(crtc);
313*4882a593Smuzhiyun 	if (cursor && !cursor->possible_crtcs)
314*4882a593Smuzhiyun 		cursor->possible_crtcs = drm_crtc_mask(crtc);
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	ret = drm_crtc_crc_init(crtc);
317*4882a593Smuzhiyun 	if (ret) {
318*4882a593Smuzhiyun 		drm_mode_object_unregister(dev, &crtc->base);
319*4882a593Smuzhiyun 		return ret;
320*4882a593Smuzhiyun 	}
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
323*4882a593Smuzhiyun 		drm_object_attach_property(&crtc->base, config->prop_active, 0);
324*4882a593Smuzhiyun 		drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
325*4882a593Smuzhiyun 		drm_object_attach_property(&crtc->base,
326*4882a593Smuzhiyun 					   config->prop_out_fence_ptr, 0);
327*4882a593Smuzhiyun 		drm_object_attach_property(&crtc->base,
328*4882a593Smuzhiyun 					   config->prop_vrr_enabled, 0);
329*4882a593Smuzhiyun 	}
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	return 0;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun EXPORT_SYMBOL(drm_crtc_init_with_planes);
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun /**
336*4882a593Smuzhiyun  * drm_crtc_cleanup - Clean up the core crtc usage
337*4882a593Smuzhiyun  * @crtc: CRTC to cleanup
338*4882a593Smuzhiyun  *
339*4882a593Smuzhiyun  * This function cleans up @crtc and removes it from the DRM mode setting
340*4882a593Smuzhiyun  * core. Note that the function does *not* free the crtc structure itself,
341*4882a593Smuzhiyun  * this is the responsibility of the caller.
342*4882a593Smuzhiyun  */
drm_crtc_cleanup(struct drm_crtc * crtc)343*4882a593Smuzhiyun void drm_crtc_cleanup(struct drm_crtc *crtc)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	struct drm_device *dev = crtc->dev;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	/* Note that the crtc_list is considered to be static; should we
348*4882a593Smuzhiyun 	 * remove the drm_crtc at runtime we would have to decrement all
349*4882a593Smuzhiyun 	 * the indices on the drm_crtc after us in the crtc_list.
350*4882a593Smuzhiyun 	 */
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	drm_crtc_crc_fini(crtc);
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	kfree(crtc->gamma_store);
355*4882a593Smuzhiyun 	crtc->gamma_store = NULL;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	drm_modeset_lock_fini(&crtc->mutex);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	drm_mode_object_unregister(dev, &crtc->base);
360*4882a593Smuzhiyun 	list_del(&crtc->head);
361*4882a593Smuzhiyun 	dev->mode_config.num_crtc--;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
364*4882a593Smuzhiyun 	if (crtc->state && crtc->funcs->atomic_destroy_state)
365*4882a593Smuzhiyun 		crtc->funcs->atomic_destroy_state(crtc, crtc->state);
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	kfree(crtc->name);
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	memset(crtc, 0, sizeof(*crtc));
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun EXPORT_SYMBOL(drm_crtc_cleanup);
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun /**
374*4882a593Smuzhiyun  * drm_mode_getcrtc - get CRTC configuration
375*4882a593Smuzhiyun  * @dev: drm device for the ioctl
376*4882a593Smuzhiyun  * @data: data pointer for the ioctl
377*4882a593Smuzhiyun  * @file_priv: drm file for the ioctl call
378*4882a593Smuzhiyun  *
379*4882a593Smuzhiyun  * Construct a CRTC configuration structure to return to the user.
380*4882a593Smuzhiyun  *
381*4882a593Smuzhiyun  * Called by the user via ioctl.
382*4882a593Smuzhiyun  *
383*4882a593Smuzhiyun  * Returns:
384*4882a593Smuzhiyun  * Zero on success, negative errno on failure.
385*4882a593Smuzhiyun  */
drm_mode_getcrtc(struct drm_device * dev,void * data,struct drm_file * file_priv)386*4882a593Smuzhiyun int drm_mode_getcrtc(struct drm_device *dev,
387*4882a593Smuzhiyun 		     void *data, struct drm_file *file_priv)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun 	struct drm_mode_crtc *crtc_resp = data;
390*4882a593Smuzhiyun 	struct drm_crtc *crtc;
391*4882a593Smuzhiyun 	struct drm_plane *plane;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
394*4882a593Smuzhiyun 		return -EOPNOTSUPP;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	crtc = drm_crtc_find(dev, file_priv, crtc_resp->crtc_id);
397*4882a593Smuzhiyun 	if (!crtc)
398*4882a593Smuzhiyun 		return -ENOENT;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	plane = crtc->primary;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	crtc_resp->gamma_size = crtc->gamma_size;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	drm_modeset_lock(&plane->mutex, NULL);
405*4882a593Smuzhiyun 	if (plane->state && plane->state->fb)
406*4882a593Smuzhiyun 		crtc_resp->fb_id = plane->state->fb->base.id;
407*4882a593Smuzhiyun 	else if (!plane->state && plane->fb)
408*4882a593Smuzhiyun 		crtc_resp->fb_id = plane->fb->base.id;
409*4882a593Smuzhiyun 	else
410*4882a593Smuzhiyun 		crtc_resp->fb_id = 0;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	if (plane->state) {
413*4882a593Smuzhiyun 		crtc_resp->x = plane->state->src_x >> 16;
414*4882a593Smuzhiyun 		crtc_resp->y = plane->state->src_y >> 16;
415*4882a593Smuzhiyun 	}
416*4882a593Smuzhiyun 	drm_modeset_unlock(&plane->mutex);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	drm_modeset_lock(&crtc->mutex, NULL);
419*4882a593Smuzhiyun 	if (crtc->state) {
420*4882a593Smuzhiyun 		if (crtc->state->enable) {
421*4882a593Smuzhiyun 			drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
422*4882a593Smuzhiyun 			crtc_resp->mode_valid = 1;
423*4882a593Smuzhiyun 		} else {
424*4882a593Smuzhiyun 			crtc_resp->mode_valid = 0;
425*4882a593Smuzhiyun 		}
426*4882a593Smuzhiyun 	} else {
427*4882a593Smuzhiyun 		crtc_resp->x = crtc->x;
428*4882a593Smuzhiyun 		crtc_resp->y = crtc->y;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 		if (crtc->enabled) {
431*4882a593Smuzhiyun 			drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
432*4882a593Smuzhiyun 			crtc_resp->mode_valid = 1;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 		} else {
435*4882a593Smuzhiyun 			crtc_resp->mode_valid = 0;
436*4882a593Smuzhiyun 		}
437*4882a593Smuzhiyun 	}
438*4882a593Smuzhiyun 	if (!file_priv->aspect_ratio_allowed)
439*4882a593Smuzhiyun 		crtc_resp->mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
440*4882a593Smuzhiyun 	drm_modeset_unlock(&crtc->mutex);
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	return 0;
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun 
__drm_mode_set_config_internal(struct drm_mode_set * set,struct drm_modeset_acquire_ctx * ctx)445*4882a593Smuzhiyun static int __drm_mode_set_config_internal(struct drm_mode_set *set,
446*4882a593Smuzhiyun 					  struct drm_modeset_acquire_ctx *ctx)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun 	struct drm_crtc *crtc = set->crtc;
449*4882a593Smuzhiyun 	struct drm_framebuffer *fb;
450*4882a593Smuzhiyun 	struct drm_crtc *tmp;
451*4882a593Smuzhiyun 	int ret;
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	/*
456*4882a593Smuzhiyun 	 * NOTE: ->set_config can also disable other crtcs (if we steal all
457*4882a593Smuzhiyun 	 * connectors from it), hence we need to refcount the fbs across all
458*4882a593Smuzhiyun 	 * crtcs. Atomic modeset will have saner semantics ...
459*4882a593Smuzhiyun 	 */
460*4882a593Smuzhiyun 	drm_for_each_crtc(tmp, crtc->dev) {
461*4882a593Smuzhiyun 		struct drm_plane *plane = tmp->primary;
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 		plane->old_fb = plane->fb;
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	fb = set->fb;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	ret = crtc->funcs->set_config(set, ctx);
469*4882a593Smuzhiyun 	if (ret == 0) {
470*4882a593Smuzhiyun 		struct drm_plane *plane = crtc->primary;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 		plane->crtc = fb ? crtc : NULL;
473*4882a593Smuzhiyun 		plane->fb = fb;
474*4882a593Smuzhiyun 	}
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	drm_for_each_crtc(tmp, crtc->dev) {
477*4882a593Smuzhiyun 		struct drm_plane *plane = tmp->primary;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 		if (plane->fb)
480*4882a593Smuzhiyun 			drm_framebuffer_get(plane->fb);
481*4882a593Smuzhiyun 		if (plane->old_fb)
482*4882a593Smuzhiyun 			drm_framebuffer_put(plane->old_fb);
483*4882a593Smuzhiyun 		plane->old_fb = NULL;
484*4882a593Smuzhiyun 	}
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	return ret;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun /**
490*4882a593Smuzhiyun  * drm_mode_set_config_internal - helper to call &drm_mode_config_funcs.set_config
491*4882a593Smuzhiyun  * @set: modeset config to set
492*4882a593Smuzhiyun  *
493*4882a593Smuzhiyun  * This is a little helper to wrap internal calls to the
494*4882a593Smuzhiyun  * &drm_mode_config_funcs.set_config driver interface. The only thing it adds is
495*4882a593Smuzhiyun  * correct refcounting dance.
496*4882a593Smuzhiyun  *
497*4882a593Smuzhiyun  * This should only be used by non-atomic legacy drivers.
498*4882a593Smuzhiyun  *
499*4882a593Smuzhiyun  * Returns:
500*4882a593Smuzhiyun  * Zero on success, negative errno on failure.
501*4882a593Smuzhiyun  */
drm_mode_set_config_internal(struct drm_mode_set * set)502*4882a593Smuzhiyun int drm_mode_set_config_internal(struct drm_mode_set *set)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	WARN_ON(drm_drv_uses_atomic_modeset(set->crtc->dev));
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	return __drm_mode_set_config_internal(set, NULL);
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun EXPORT_SYMBOL(drm_mode_set_config_internal);
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun /**
511*4882a593Smuzhiyun  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
512*4882a593Smuzhiyun  *     CRTC viewport
513*4882a593Smuzhiyun  * @crtc: CRTC that framebuffer will be displayed on
514*4882a593Smuzhiyun  * @x: x panning
515*4882a593Smuzhiyun  * @y: y panning
516*4882a593Smuzhiyun  * @mode: mode that framebuffer will be displayed under
517*4882a593Smuzhiyun  * @fb: framebuffer to check size of
518*4882a593Smuzhiyun  */
drm_crtc_check_viewport(const struct drm_crtc * crtc,int x,int y,const struct drm_display_mode * mode,const struct drm_framebuffer * fb)519*4882a593Smuzhiyun int drm_crtc_check_viewport(const struct drm_crtc *crtc,
520*4882a593Smuzhiyun 			    int x, int y,
521*4882a593Smuzhiyun 			    const struct drm_display_mode *mode,
522*4882a593Smuzhiyun 			    const struct drm_framebuffer *fb)
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun 	int hdisplay, vdisplay;
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	if (crtc->state &&
530*4882a593Smuzhiyun 	    drm_rotation_90_or_270(crtc->primary->state->rotation))
531*4882a593Smuzhiyun 		swap(hdisplay, vdisplay);
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	return drm_framebuffer_check_src_coords(x << 16, y << 16,
534*4882a593Smuzhiyun 						hdisplay << 16, vdisplay << 16,
535*4882a593Smuzhiyun 						fb);
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun EXPORT_SYMBOL(drm_crtc_check_viewport);
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun /**
540*4882a593Smuzhiyun  * drm_mode_setcrtc - set CRTC configuration
541*4882a593Smuzhiyun  * @dev: drm device for the ioctl
542*4882a593Smuzhiyun  * @data: data pointer for the ioctl
543*4882a593Smuzhiyun  * @file_priv: drm file for the ioctl call
544*4882a593Smuzhiyun  *
545*4882a593Smuzhiyun  * Build a new CRTC configuration based on user request.
546*4882a593Smuzhiyun  *
547*4882a593Smuzhiyun  * Called by the user via ioctl.
548*4882a593Smuzhiyun  *
549*4882a593Smuzhiyun  * Returns:
550*4882a593Smuzhiyun  * Zero on success, negative errno on failure.
551*4882a593Smuzhiyun  */
drm_mode_setcrtc(struct drm_device * dev,void * data,struct drm_file * file_priv)552*4882a593Smuzhiyun int drm_mode_setcrtc(struct drm_device *dev, void *data,
553*4882a593Smuzhiyun 		     struct drm_file *file_priv)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun 	struct drm_mode_config *config = &dev->mode_config;
556*4882a593Smuzhiyun 	struct drm_mode_crtc *crtc_req = data;
557*4882a593Smuzhiyun 	struct drm_crtc *crtc;
558*4882a593Smuzhiyun 	struct drm_plane *plane;
559*4882a593Smuzhiyun 	struct drm_connector **connector_set = NULL, *connector;
560*4882a593Smuzhiyun 	struct drm_framebuffer *fb = NULL;
561*4882a593Smuzhiyun 	struct drm_display_mode *mode = NULL;
562*4882a593Smuzhiyun 	struct drm_mode_set set;
563*4882a593Smuzhiyun 	uint32_t __user *set_connectors_ptr;
564*4882a593Smuzhiyun 	struct drm_modeset_acquire_ctx ctx;
565*4882a593Smuzhiyun 	int ret;
566*4882a593Smuzhiyun 	int i;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
569*4882a593Smuzhiyun 		return -EOPNOTSUPP;
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	/*
572*4882a593Smuzhiyun 	 * Universal plane src offsets are only 16.16, prevent havoc for
573*4882a593Smuzhiyun 	 * drivers using universal plane code internally.
574*4882a593Smuzhiyun 	 */
575*4882a593Smuzhiyun 	if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
576*4882a593Smuzhiyun 		return -ERANGE;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	crtc = drm_crtc_find(dev, file_priv, crtc_req->crtc_id);
579*4882a593Smuzhiyun 	if (!crtc) {
580*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
581*4882a593Smuzhiyun 		return -ENOENT;
582*4882a593Smuzhiyun 	}
583*4882a593Smuzhiyun 	DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	plane = crtc->primary;
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	/* allow disabling with the primary plane leased */
588*4882a593Smuzhiyun 	if (crtc_req->mode_valid && !drm_lease_held(file_priv, plane->base.id))
589*4882a593Smuzhiyun 		return -EACCES;
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx,
592*4882a593Smuzhiyun 				   DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	if (crtc_req->mode_valid) {
595*4882a593Smuzhiyun 		/* If we have a mode we need a framebuffer. */
596*4882a593Smuzhiyun 		/* If we pass -1, set the mode with the currently bound fb */
597*4882a593Smuzhiyun 		if (crtc_req->fb_id == -1) {
598*4882a593Smuzhiyun 			struct drm_framebuffer *old_fb;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 			if (plane->state)
601*4882a593Smuzhiyun 				old_fb = plane->state->fb;
602*4882a593Smuzhiyun 			else
603*4882a593Smuzhiyun 				old_fb = plane->fb;
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 			if (!old_fb) {
606*4882a593Smuzhiyun 				DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
607*4882a593Smuzhiyun 				ret = -EINVAL;
608*4882a593Smuzhiyun 				goto out;
609*4882a593Smuzhiyun 			}
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 			fb = old_fb;
612*4882a593Smuzhiyun 			/* Make refcounting symmetric with the lookup path. */
613*4882a593Smuzhiyun 			drm_framebuffer_get(fb);
614*4882a593Smuzhiyun 		} else {
615*4882a593Smuzhiyun 			fb = drm_framebuffer_lookup(dev, file_priv, crtc_req->fb_id);
616*4882a593Smuzhiyun 			if (!fb) {
617*4882a593Smuzhiyun 				DRM_DEBUG_KMS("Unknown FB ID%d\n",
618*4882a593Smuzhiyun 						crtc_req->fb_id);
619*4882a593Smuzhiyun 				ret = -ENOENT;
620*4882a593Smuzhiyun 				goto out;
621*4882a593Smuzhiyun 			}
622*4882a593Smuzhiyun 		}
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 		mode = drm_mode_create(dev);
625*4882a593Smuzhiyun 		if (!mode) {
626*4882a593Smuzhiyun 			ret = -ENOMEM;
627*4882a593Smuzhiyun 			goto out;
628*4882a593Smuzhiyun 		}
629*4882a593Smuzhiyun 		if (!file_priv->aspect_ratio_allowed &&
630*4882a593Smuzhiyun 		    (crtc_req->mode.flags & DRM_MODE_FLAG_PIC_AR_MASK) != DRM_MODE_FLAG_PIC_AR_NONE) {
631*4882a593Smuzhiyun 			DRM_DEBUG_KMS("Unexpected aspect-ratio flag bits\n");
632*4882a593Smuzhiyun 			ret = -EINVAL;
633*4882a593Smuzhiyun 			goto out;
634*4882a593Smuzhiyun 		}
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 		ret = drm_mode_convert_umode(dev, mode, &crtc_req->mode);
638*4882a593Smuzhiyun 		if (ret) {
639*4882a593Smuzhiyun 			DRM_DEBUG_KMS("Invalid mode (ret=%d, status=%s)\n",
640*4882a593Smuzhiyun 				      ret, drm_get_mode_status_name(mode->status));
641*4882a593Smuzhiyun 			drm_mode_debug_printmodeline(mode);
642*4882a593Smuzhiyun 			goto out;
643*4882a593Smuzhiyun 		}
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 		/*
646*4882a593Smuzhiyun 		 * Check whether the primary plane supports the fb pixel format.
647*4882a593Smuzhiyun 		 * Drivers not implementing the universal planes API use a
648*4882a593Smuzhiyun 		 * default formats list provided by the DRM core which doesn't
649*4882a593Smuzhiyun 		 * match real hardware capabilities. Skip the check in that
650*4882a593Smuzhiyun 		 * case.
651*4882a593Smuzhiyun 		 */
652*4882a593Smuzhiyun 		if (!plane->format_default) {
653*4882a593Smuzhiyun 			ret = drm_plane_check_pixel_format(plane,
654*4882a593Smuzhiyun 							   fb->format->format,
655*4882a593Smuzhiyun 							   fb->modifier);
656*4882a593Smuzhiyun 			if (ret) {
657*4882a593Smuzhiyun 				struct drm_format_name_buf format_name;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 				DRM_DEBUG_KMS("Invalid pixel format %s, modifier 0x%llx\n",
660*4882a593Smuzhiyun 					      drm_get_format_name(fb->format->format,
661*4882a593Smuzhiyun 								  &format_name),
662*4882a593Smuzhiyun 					      fb->modifier);
663*4882a593Smuzhiyun 				goto out;
664*4882a593Smuzhiyun 			}
665*4882a593Smuzhiyun 		}
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 		ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
668*4882a593Smuzhiyun 					      mode, fb);
669*4882a593Smuzhiyun 		if (ret)
670*4882a593Smuzhiyun 			goto out;
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	}
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	if (crtc_req->count_connectors == 0 && mode) {
675*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
676*4882a593Smuzhiyun 		ret = -EINVAL;
677*4882a593Smuzhiyun 		goto out;
678*4882a593Smuzhiyun 	}
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
681*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
682*4882a593Smuzhiyun 			  crtc_req->count_connectors);
683*4882a593Smuzhiyun 		ret = -EINVAL;
684*4882a593Smuzhiyun 		goto out;
685*4882a593Smuzhiyun 	}
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	if (crtc_req->count_connectors > 0) {
688*4882a593Smuzhiyun 		u32 out_id;
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 		/* Avoid unbounded kernel memory allocation */
691*4882a593Smuzhiyun 		if (crtc_req->count_connectors > config->num_connector) {
692*4882a593Smuzhiyun 			ret = -EINVAL;
693*4882a593Smuzhiyun 			goto out;
694*4882a593Smuzhiyun 		}
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 		connector_set = kmalloc_array(crtc_req->count_connectors,
697*4882a593Smuzhiyun 					      sizeof(struct drm_connector *),
698*4882a593Smuzhiyun 					      GFP_KERNEL);
699*4882a593Smuzhiyun 		if (!connector_set) {
700*4882a593Smuzhiyun 			ret = -ENOMEM;
701*4882a593Smuzhiyun 			goto out;
702*4882a593Smuzhiyun 		}
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 		for (i = 0; i < crtc_req->count_connectors; i++) {
705*4882a593Smuzhiyun 			connector_set[i] = NULL;
706*4882a593Smuzhiyun 			set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
707*4882a593Smuzhiyun 			if (get_user(out_id, &set_connectors_ptr[i])) {
708*4882a593Smuzhiyun 				ret = -EFAULT;
709*4882a593Smuzhiyun 				goto out;
710*4882a593Smuzhiyun 			}
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 			connector = drm_connector_lookup(dev, file_priv, out_id);
713*4882a593Smuzhiyun 			if (!connector) {
714*4882a593Smuzhiyun 				DRM_DEBUG_KMS("Connector id %d unknown\n",
715*4882a593Smuzhiyun 						out_id);
716*4882a593Smuzhiyun 				ret = -ENOENT;
717*4882a593Smuzhiyun 				goto out;
718*4882a593Smuzhiyun 			}
719*4882a593Smuzhiyun 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
720*4882a593Smuzhiyun 					connector->base.id,
721*4882a593Smuzhiyun 					connector->name);
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 			connector_set[i] = connector;
724*4882a593Smuzhiyun 		}
725*4882a593Smuzhiyun 	}
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	set.crtc = crtc;
728*4882a593Smuzhiyun 	set.x = crtc_req->x;
729*4882a593Smuzhiyun 	set.y = crtc_req->y;
730*4882a593Smuzhiyun 	set.mode = mode;
731*4882a593Smuzhiyun 	set.connectors = connector_set;
732*4882a593Smuzhiyun 	set.num_connectors = crtc_req->count_connectors;
733*4882a593Smuzhiyun 	set.fb = fb;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	if (drm_drv_uses_atomic_modeset(dev))
736*4882a593Smuzhiyun 		ret = crtc->funcs->set_config(&set, &ctx);
737*4882a593Smuzhiyun 	else
738*4882a593Smuzhiyun 		ret = __drm_mode_set_config_internal(&set, &ctx);
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun out:
741*4882a593Smuzhiyun 	if (fb)
742*4882a593Smuzhiyun 		drm_framebuffer_put(fb);
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	if (connector_set) {
745*4882a593Smuzhiyun 		for (i = 0; i < crtc_req->count_connectors; i++) {
746*4882a593Smuzhiyun 			if (connector_set[i])
747*4882a593Smuzhiyun 				drm_connector_put(connector_set[i]);
748*4882a593Smuzhiyun 		}
749*4882a593Smuzhiyun 	}
750*4882a593Smuzhiyun 	kfree(connector_set);
751*4882a593Smuzhiyun 	drm_mode_destroy(dev, mode);
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	/* In case we need to retry... */
754*4882a593Smuzhiyun 	connector_set = NULL;
755*4882a593Smuzhiyun 	fb = NULL;
756*4882a593Smuzhiyun 	mode = NULL;
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	return ret;
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun 
drm_mode_crtc_set_obj_prop(struct drm_mode_object * obj,struct drm_property * property,uint64_t value)763*4882a593Smuzhiyun int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
764*4882a593Smuzhiyun 			       struct drm_property *property,
765*4882a593Smuzhiyun 			       uint64_t value)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun 	int ret = -EINVAL;
768*4882a593Smuzhiyun 	struct drm_crtc *crtc = obj_to_crtc(obj);
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	if (crtc->funcs->set_property)
771*4882a593Smuzhiyun 		ret = crtc->funcs->set_property(crtc, property, value);
772*4882a593Smuzhiyun 	if (!ret)
773*4882a593Smuzhiyun 		drm_object_property_set_value(obj, property, value);
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	return ret;
776*4882a593Smuzhiyun }
777