[go: nahoru, domu]

drm_crtc.c revision 5848ad409c1817f5b70507e6b4440fcbd002f6c5
1/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission.  The copyright holders make no representations
15 * about the suitability of this software for any purpose.  It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 *      Keith Packard
28 *	Eric Anholt <eric@anholt.net>
29 *      Dave Airlie <airlied@linux.ie>
30 *      Jesse Barnes <jesse.barnes@intel.com>
31 */
32#include <linux/ctype.h>
33#include <linux/list.h>
34#include <linux/slab.h>
35#include <linux/export.h>
36#include <drm/drmP.h>
37#include <drm/drm_crtc.h>
38#include <drm/drm_edid.h>
39#include <drm/drm_fourcc.h>
40
41/**
42 * drm_modeset_lock_all - take all modeset locks
43 * @dev: drm device
44 *
45 * This function takes all modeset locks, suitable where a more fine-grained
46 * scheme isn't (yet) implemented.
47 */
48void drm_modeset_lock_all(struct drm_device *dev)
49{
50	struct drm_crtc *crtc;
51
52	mutex_lock(&dev->mode_config.mutex);
53
54	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
55		mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
56}
57EXPORT_SYMBOL(drm_modeset_lock_all);
58
59/**
60 * drm_modeset_unlock_all - drop all modeset locks
61 * @dev: device
62 */
63void drm_modeset_unlock_all(struct drm_device *dev)
64{
65	struct drm_crtc *crtc;
66
67	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
68		mutex_unlock(&crtc->mutex);
69
70	mutex_unlock(&dev->mode_config.mutex);
71}
72EXPORT_SYMBOL(drm_modeset_unlock_all);
73
74/**
75 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
76 * @dev: device
77 */
78void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
79{
80	struct drm_crtc *crtc;
81
82	/* Locking is currently fubar in the panic handler. */
83	if (oops_in_progress)
84		return;
85
86	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
87		WARN_ON(!mutex_is_locked(&crtc->mutex));
88
89	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
90}
91EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
92
93/* Avoid boilerplate.  I'm tired of typing. */
94#define DRM_ENUM_NAME_FN(fnname, list)				\
95	const char *fnname(int val)				\
96	{							\
97		int i;						\
98		for (i = 0; i < ARRAY_SIZE(list); i++) {	\
99			if (list[i].type == val)		\
100				return list[i].name;		\
101		}						\
102		return "(unknown)";				\
103	}
104
105/*
106 * Global properties
107 */
108static const struct drm_prop_enum_list drm_dpms_enum_list[] =
109{	{ DRM_MODE_DPMS_ON, "On" },
110	{ DRM_MODE_DPMS_STANDBY, "Standby" },
111	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
112	{ DRM_MODE_DPMS_OFF, "Off" }
113};
114
115DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
116
117/*
118 * Optional properties
119 */
120static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
121{
122	{ DRM_MODE_SCALE_NONE, "None" },
123	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
124	{ DRM_MODE_SCALE_CENTER, "Center" },
125	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
126};
127
128/*
129 * Non-global properties, but "required" for certain connectors.
130 */
131static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
132{
133	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
134	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
135	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
136};
137
138DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
139
140static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
141{
142	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
143	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
144	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
145};
146
147DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
148		 drm_dvi_i_subconnector_enum_list)
149
150static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
151{
152	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
153	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
154	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
155	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
156	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
157};
158
159DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
160
161static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
162{
163	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
164	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
165	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
166	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
167	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
168};
169
170DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
171		 drm_tv_subconnector_enum_list)
172
173static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
174	{ DRM_MODE_DIRTY_OFF,      "Off"      },
175	{ DRM_MODE_DIRTY_ON,       "On"       },
176	{ DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
177};
178
179struct drm_conn_prop_enum_list {
180	int type;
181	const char *name;
182	struct ida ida;
183};
184
185/*
186 * Connector and encoder types.
187 */
188static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
189{	{ DRM_MODE_CONNECTOR_Unknown, "Unknown" },
190	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
191	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
192	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
193	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
194	{ DRM_MODE_CONNECTOR_Composite, "Composite" },
195	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
196	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
197	{ DRM_MODE_CONNECTOR_Component, "Component" },
198	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
199	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
200	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
201	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
202	{ DRM_MODE_CONNECTOR_TV, "TV" },
203	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
204	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
205	{ DRM_MODE_CONNECTOR_DSI, "DSI" },
206};
207
208static const struct drm_prop_enum_list drm_encoder_enum_list[] =
209{	{ DRM_MODE_ENCODER_NONE, "None" },
210	{ DRM_MODE_ENCODER_DAC, "DAC" },
211	{ DRM_MODE_ENCODER_TMDS, "TMDS" },
212	{ DRM_MODE_ENCODER_LVDS, "LVDS" },
213	{ DRM_MODE_ENCODER_TVDAC, "TV" },
214	{ DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
215	{ DRM_MODE_ENCODER_DSI, "DSI" },
216};
217
218void drm_connector_ida_init(void)
219{
220	int i;
221
222	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
223		ida_init(&drm_connector_enum_list[i].ida);
224}
225
226void drm_connector_ida_destroy(void)
227{
228	int i;
229
230	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
231		ida_destroy(&drm_connector_enum_list[i].ida);
232}
233
234const char *drm_get_encoder_name(const struct drm_encoder *encoder)
235{
236	static char buf[32];
237
238	snprintf(buf, 32, "%s-%d",
239		 drm_encoder_enum_list[encoder->encoder_type].name,
240		 encoder->base.id);
241	return buf;
242}
243EXPORT_SYMBOL(drm_get_encoder_name);
244
245const char *drm_get_connector_name(const struct drm_connector *connector)
246{
247	static char buf[32];
248
249	snprintf(buf, 32, "%s-%d",
250		 drm_connector_enum_list[connector->connector_type].name,
251		 connector->connector_type_id);
252	return buf;
253}
254EXPORT_SYMBOL(drm_get_connector_name);
255
256const char *drm_get_connector_status_name(enum drm_connector_status status)
257{
258	if (status == connector_status_connected)
259		return "connected";
260	else if (status == connector_status_disconnected)
261		return "disconnected";
262	else
263		return "unknown";
264}
265EXPORT_SYMBOL(drm_get_connector_status_name);
266
267static char printable_char(int c)
268{
269	return isascii(c) && isprint(c) ? c : '?';
270}
271
272const char *drm_get_format_name(uint32_t format)
273{
274	static char buf[32];
275
276	snprintf(buf, sizeof(buf),
277		 "%c%c%c%c %s-endian (0x%08x)",
278		 printable_char(format & 0xff),
279		 printable_char((format >> 8) & 0xff),
280		 printable_char((format >> 16) & 0xff),
281		 printable_char((format >> 24) & 0x7f),
282		 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
283		 format);
284
285	return buf;
286}
287EXPORT_SYMBOL(drm_get_format_name);
288
289/**
290 * drm_mode_object_get - allocate a new modeset identifier
291 * @dev: DRM device
292 * @obj: object pointer, used to generate unique ID
293 * @obj_type: object type
294 *
295 * Create a unique identifier based on @ptr in @dev's identifier space.  Used
296 * for tracking modes, CRTCs and connectors.
297 *
298 * RETURNS:
299 * New unique (relative to other objects in @dev) integer identifier for the
300 * object.
301 */
302static int drm_mode_object_get(struct drm_device *dev,
303			       struct drm_mode_object *obj, uint32_t obj_type)
304{
305	int ret;
306
307	mutex_lock(&dev->mode_config.idr_mutex);
308	ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
309	if (ret >= 0) {
310		/*
311		 * Set up the object linking under the protection of the idr
312		 * lock so that other users can't see inconsistent state.
313		 */
314		obj->id = ret;
315		obj->type = obj_type;
316	}
317	mutex_unlock(&dev->mode_config.idr_mutex);
318
319	return ret < 0 ? ret : 0;
320}
321
322/**
323 * drm_mode_object_put - free a modeset identifer
324 * @dev: DRM device
325 * @object: object to free
326 *
327 * Free @id from @dev's unique identifier pool.
328 */
329static void drm_mode_object_put(struct drm_device *dev,
330				struct drm_mode_object *object)
331{
332	mutex_lock(&dev->mode_config.idr_mutex);
333	idr_remove(&dev->mode_config.crtc_idr, object->id);
334	mutex_unlock(&dev->mode_config.idr_mutex);
335}
336
337/**
338 * drm_mode_object_find - look up a drm object with static lifetime
339 * @dev: drm device
340 * @id: id of the mode object
341 * @type: type of the mode object
342 *
343 * Note that framebuffers cannot be looked up with this functions - since those
344 * are reference counted, they need special treatment.
345 */
346struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
347		uint32_t id, uint32_t type)
348{
349	struct drm_mode_object *obj = NULL;
350
351	/* Framebuffers are reference counted and need their own lookup
352	 * function.*/
353	WARN_ON(type == DRM_MODE_OBJECT_FB);
354
355	mutex_lock(&dev->mode_config.idr_mutex);
356	obj = idr_find(&dev->mode_config.crtc_idr, id);
357	if (!obj || (obj->type != type) || (obj->id != id))
358		obj = NULL;
359	mutex_unlock(&dev->mode_config.idr_mutex);
360
361	return obj;
362}
363EXPORT_SYMBOL(drm_mode_object_find);
364
365/**
366 * drm_framebuffer_init - initialize a framebuffer
367 * @dev: DRM device
368 * @fb: framebuffer to be initialized
369 * @funcs: ... with these functions
370 *
371 * Allocates an ID for the framebuffer's parent mode object, sets its mode
372 * functions & device file and adds it to the master fd list.
373 *
374 * IMPORTANT:
375 * This functions publishes the fb and makes it available for concurrent access
376 * by other users. Which means by this point the fb _must_ be fully set up -
377 * since all the fb attributes are invariant over its lifetime, no further
378 * locking but only correct reference counting is required.
379 *
380 * RETURNS:
381 * Zero on success, error code on failure.
382 */
383int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
384			 const struct drm_framebuffer_funcs *funcs)
385{
386	int ret;
387
388	mutex_lock(&dev->mode_config.fb_lock);
389	kref_init(&fb->refcount);
390	INIT_LIST_HEAD(&fb->filp_head);
391	fb->dev = dev;
392	fb->funcs = funcs;
393
394	ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
395	if (ret)
396		goto out;
397
398	/* Grab the idr reference. */
399	drm_framebuffer_reference(fb);
400
401	dev->mode_config.num_fb++;
402	list_add(&fb->head, &dev->mode_config.fb_list);
403out:
404	mutex_unlock(&dev->mode_config.fb_lock);
405
406	return 0;
407}
408EXPORT_SYMBOL(drm_framebuffer_init);
409
410static void drm_framebuffer_free(struct kref *kref)
411{
412	struct drm_framebuffer *fb =
413			container_of(kref, struct drm_framebuffer, refcount);
414	fb->funcs->destroy(fb);
415}
416
417static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
418							uint32_t id)
419{
420	struct drm_mode_object *obj = NULL;
421	struct drm_framebuffer *fb;
422
423	mutex_lock(&dev->mode_config.idr_mutex);
424	obj = idr_find(&dev->mode_config.crtc_idr, id);
425	if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
426		fb = NULL;
427	else
428		fb = obj_to_fb(obj);
429	mutex_unlock(&dev->mode_config.idr_mutex);
430
431	return fb;
432}
433
434/**
435 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
436 * @dev: drm device
437 * @id: id of the fb object
438 *
439 * If successful, this grabs an additional reference to the framebuffer -
440 * callers need to make sure to eventually unreference the returned framebuffer
441 * again.
442 */
443struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
444					       uint32_t id)
445{
446	struct drm_framebuffer *fb;
447
448	mutex_lock(&dev->mode_config.fb_lock);
449	fb = __drm_framebuffer_lookup(dev, id);
450	if (fb)
451		drm_framebuffer_reference(fb);
452	mutex_unlock(&dev->mode_config.fb_lock);
453
454	return fb;
455}
456EXPORT_SYMBOL(drm_framebuffer_lookup);
457
458/**
459 * drm_framebuffer_unreference - unref a framebuffer
460 * @fb: framebuffer to unref
461 *
462 * This functions decrements the fb's refcount and frees it if it drops to zero.
463 */
464void drm_framebuffer_unreference(struct drm_framebuffer *fb)
465{
466	DRM_DEBUG("FB ID: %d\n", fb->base.id);
467	kref_put(&fb->refcount, drm_framebuffer_free);
468}
469EXPORT_SYMBOL(drm_framebuffer_unreference);
470
471/**
472 * drm_framebuffer_reference - incr the fb refcnt
473 * @fb: framebuffer
474 */
475void drm_framebuffer_reference(struct drm_framebuffer *fb)
476{
477	DRM_DEBUG("FB ID: %d\n", fb->base.id);
478	kref_get(&fb->refcount);
479}
480EXPORT_SYMBOL(drm_framebuffer_reference);
481
482static void drm_framebuffer_free_bug(struct kref *kref)
483{
484	BUG();
485}
486
487static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
488{
489	DRM_DEBUG("FB ID: %d\n", fb->base.id);
490	kref_put(&fb->refcount, drm_framebuffer_free_bug);
491}
492
493/* dev->mode_config.fb_lock must be held! */
494static void __drm_framebuffer_unregister(struct drm_device *dev,
495					 struct drm_framebuffer *fb)
496{
497	mutex_lock(&dev->mode_config.idr_mutex);
498	idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
499	mutex_unlock(&dev->mode_config.idr_mutex);
500
501	fb->base.id = 0;
502
503	__drm_framebuffer_unreference(fb);
504}
505
506/**
507 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
508 * @fb: fb to unregister
509 *
510 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
511 * those used for fbdev. Note that the caller must hold a reference of it's own,
512 * i.e. the object may not be destroyed through this call (since it'll lead to a
513 * locking inversion).
514 */
515void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
516{
517	struct drm_device *dev = fb->dev;
518
519	mutex_lock(&dev->mode_config.fb_lock);
520	/* Mark fb as reaped and drop idr ref. */
521	__drm_framebuffer_unregister(dev, fb);
522	mutex_unlock(&dev->mode_config.fb_lock);
523}
524EXPORT_SYMBOL(drm_framebuffer_unregister_private);
525
526/**
527 * drm_framebuffer_cleanup - remove a framebuffer object
528 * @fb: framebuffer to remove
529 *
530 * Cleanup references to a user-created framebuffer. This function is intended
531 * to be used from the drivers ->destroy callback.
532 *
533 * Note that this function does not remove the fb from active usuage - if it is
534 * still used anywhere, hilarity can ensue since userspace could call getfb on
535 * the id and get back -EINVAL. Obviously no concern at driver unload time.
536 *
537 * Also, the framebuffer will not be removed from the lookup idr - for
538 * user-created framebuffers this will happen in in the rmfb ioctl. For
539 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
540 * drm_framebuffer_unregister_private.
541 */
542void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
543{
544	struct drm_device *dev = fb->dev;
545
546	mutex_lock(&dev->mode_config.fb_lock);
547	list_del(&fb->head);
548	dev->mode_config.num_fb--;
549	mutex_unlock(&dev->mode_config.fb_lock);
550}
551EXPORT_SYMBOL(drm_framebuffer_cleanup);
552
553/**
554 * drm_framebuffer_remove - remove and unreference a framebuffer object
555 * @fb: framebuffer to remove
556 *
557 * Scans all the CRTCs and planes in @dev's mode_config.  If they're
558 * using @fb, removes it, setting it to NULL. Then drops the reference to the
559 * passed-in framebuffer. Might take the modeset locks.
560 *
561 * Note that this function optimizes the cleanup away if the caller holds the
562 * last reference to the framebuffer. It is also guaranteed to not take the
563 * modeset locks in this case.
564 */
565void drm_framebuffer_remove(struct drm_framebuffer *fb)
566{
567	struct drm_device *dev = fb->dev;
568	struct drm_crtc *crtc;
569	struct drm_plane *plane;
570	struct drm_mode_set set;
571	int ret;
572
573	WARN_ON(!list_empty(&fb->filp_head));
574
575	/*
576	 * drm ABI mandates that we remove any deleted framebuffers from active
577	 * useage. But since most sane clients only remove framebuffers they no
578	 * longer need, try to optimize this away.
579	 *
580	 * Since we're holding a reference ourselves, observing a refcount of 1
581	 * means that we're the last holder and can skip it. Also, the refcount
582	 * can never increase from 1 again, so we don't need any barriers or
583	 * locks.
584	 *
585	 * Note that userspace could try to race with use and instate a new
586	 * usage _after_ we've cleared all current ones. End result will be an
587	 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
588	 * in this manner.
589	 */
590	if (atomic_read(&fb->refcount.refcount) > 1) {
591		drm_modeset_lock_all(dev);
592		/* remove from any CRTC */
593		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
594			if (crtc->fb == fb) {
595				/* should turn off the crtc */
596				memset(&set, 0, sizeof(struct drm_mode_set));
597				set.crtc = crtc;
598				set.fb = NULL;
599				ret = drm_mode_set_config_internal(&set);
600				if (ret)
601					DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
602			}
603		}
604
605		list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
606			if (plane->fb == fb)
607				drm_plane_force_disable(plane);
608		}
609		drm_modeset_unlock_all(dev);
610	}
611
612	drm_framebuffer_unreference(fb);
613}
614EXPORT_SYMBOL(drm_framebuffer_remove);
615
616/**
617 * drm_crtc_init - Initialise a new CRTC object
618 * @dev: DRM device
619 * @crtc: CRTC object to init
620 * @funcs: callbacks for the new CRTC
621 *
622 * Inits a new object created as base part of a driver crtc object.
623 *
624 * RETURNS:
625 * Zero on success, error code on failure.
626 */
627int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
628		   const struct drm_crtc_funcs *funcs)
629{
630	int ret;
631
632	crtc->dev = dev;
633	crtc->funcs = funcs;
634	crtc->invert_dimensions = false;
635
636	drm_modeset_lock_all(dev);
637	mutex_init(&crtc->mutex);
638	mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
639
640	ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
641	if (ret)
642		goto out;
643
644	crtc->base.properties = &crtc->properties;
645
646	list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
647	dev->mode_config.num_crtc++;
648
649 out:
650	drm_modeset_unlock_all(dev);
651
652	return ret;
653}
654EXPORT_SYMBOL(drm_crtc_init);
655
656/**
657 * drm_crtc_cleanup - Clean up the core crtc usage
658 * @crtc: CRTC to cleanup
659 *
660 * This function cleans up @crtc and removes it from the DRM mode setting
661 * core. Note that the function does *not* free the crtc structure itself,
662 * this is the responsibility of the caller.
663 */
664void drm_crtc_cleanup(struct drm_crtc *crtc)
665{
666	struct drm_device *dev = crtc->dev;
667
668	kfree(crtc->gamma_store);
669	crtc->gamma_store = NULL;
670
671	drm_mode_object_put(dev, &crtc->base);
672	list_del(&crtc->head);
673	dev->mode_config.num_crtc--;
674}
675EXPORT_SYMBOL(drm_crtc_cleanup);
676
677/**
678 * drm_mode_probed_add - add a mode to a connector's probed mode list
679 * @connector: connector the new mode
680 * @mode: mode data
681 *
682 * Add @mode to @connector's mode list for later use.
683 */
684void drm_mode_probed_add(struct drm_connector *connector,
685			 struct drm_display_mode *mode)
686{
687	list_add_tail(&mode->head, &connector->probed_modes);
688}
689EXPORT_SYMBOL(drm_mode_probed_add);
690
691/*
692 * drm_mode_remove - remove and free a mode
693 * @connector: connector list to modify
694 * @mode: mode to remove
695 *
696 * Remove @mode from @connector's mode list, then free it.
697 */
698static void drm_mode_remove(struct drm_connector *connector,
699			    struct drm_display_mode *mode)
700{
701	list_del(&mode->head);
702	drm_mode_destroy(connector->dev, mode);
703}
704
705/**
706 * drm_connector_init - Init a preallocated connector
707 * @dev: DRM device
708 * @connector: the connector to init
709 * @funcs: callbacks for this connector
710 * @connector_type: user visible type of the connector
711 *
712 * Initialises a preallocated connector. Connectors should be
713 * subclassed as part of driver connector objects.
714 *
715 * RETURNS:
716 * Zero on success, error code on failure.
717 */
718int drm_connector_init(struct drm_device *dev,
719		       struct drm_connector *connector,
720		       const struct drm_connector_funcs *funcs,
721		       int connector_type)
722{
723	int ret;
724	struct ida *connector_ida =
725		&drm_connector_enum_list[connector_type].ida;
726
727	drm_modeset_lock_all(dev);
728
729	ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
730	if (ret)
731		goto out;
732
733	connector->base.properties = &connector->properties;
734	connector->dev = dev;
735	connector->funcs = funcs;
736	connector->connector_type = connector_type;
737	connector->connector_type_id =
738		ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
739	if (connector->connector_type_id < 0) {
740		ret = connector->connector_type_id;
741		drm_mode_object_put(dev, &connector->base);
742		goto out;
743	}
744	INIT_LIST_HEAD(&connector->probed_modes);
745	INIT_LIST_HEAD(&connector->modes);
746	connector->edid_blob_ptr = NULL;
747	connector->status = connector_status_unknown;
748
749	list_add_tail(&connector->head, &dev->mode_config.connector_list);
750	dev->mode_config.num_connector++;
751
752	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
753		drm_object_attach_property(&connector->base,
754					      dev->mode_config.edid_property,
755					      0);
756
757	drm_object_attach_property(&connector->base,
758				      dev->mode_config.dpms_property, 0);
759
760 out:
761	drm_modeset_unlock_all(dev);
762
763	return ret;
764}
765EXPORT_SYMBOL(drm_connector_init);
766
767/**
768 * drm_connector_cleanup - cleans up an initialised connector
769 * @connector: connector to cleanup
770 *
771 * Cleans up the connector but doesn't free the object.
772 */
773void drm_connector_cleanup(struct drm_connector *connector)
774{
775	struct drm_device *dev = connector->dev;
776	struct drm_display_mode *mode, *t;
777
778	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
779		drm_mode_remove(connector, mode);
780
781	list_for_each_entry_safe(mode, t, &connector->modes, head)
782		drm_mode_remove(connector, mode);
783
784	ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
785		   connector->connector_type_id);
786
787	drm_mode_object_put(dev, &connector->base);
788	list_del(&connector->head);
789	dev->mode_config.num_connector--;
790}
791EXPORT_SYMBOL(drm_connector_cleanup);
792
793void drm_connector_unplug_all(struct drm_device *dev)
794{
795	struct drm_connector *connector;
796
797	/* taking the mode config mutex ends up in a clash with sysfs */
798	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
799		drm_sysfs_connector_remove(connector);
800
801}
802EXPORT_SYMBOL(drm_connector_unplug_all);
803
804int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
805		const struct drm_bridge_funcs *funcs)
806{
807	int ret;
808
809	drm_modeset_lock_all(dev);
810
811	ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
812	if (ret)
813		goto out;
814
815	bridge->dev = dev;
816	bridge->funcs = funcs;
817
818	list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
819	dev->mode_config.num_bridge++;
820
821 out:
822	drm_modeset_unlock_all(dev);
823	return ret;
824}
825EXPORT_SYMBOL(drm_bridge_init);
826
827void drm_bridge_cleanup(struct drm_bridge *bridge)
828{
829	struct drm_device *dev = bridge->dev;
830
831	drm_modeset_lock_all(dev);
832	drm_mode_object_put(dev, &bridge->base);
833	list_del(&bridge->head);
834	dev->mode_config.num_bridge--;
835	drm_modeset_unlock_all(dev);
836}
837EXPORT_SYMBOL(drm_bridge_cleanup);
838
839int drm_encoder_init(struct drm_device *dev,
840		      struct drm_encoder *encoder,
841		      const struct drm_encoder_funcs *funcs,
842		      int encoder_type)
843{
844	int ret;
845
846	drm_modeset_lock_all(dev);
847
848	ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
849	if (ret)
850		goto out;
851
852	encoder->dev = dev;
853	encoder->encoder_type = encoder_type;
854	encoder->funcs = funcs;
855
856	list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
857	dev->mode_config.num_encoder++;
858
859 out:
860	drm_modeset_unlock_all(dev);
861
862	return ret;
863}
864EXPORT_SYMBOL(drm_encoder_init);
865
866void drm_encoder_cleanup(struct drm_encoder *encoder)
867{
868	struct drm_device *dev = encoder->dev;
869	drm_modeset_lock_all(dev);
870	drm_mode_object_put(dev, &encoder->base);
871	list_del(&encoder->head);
872	dev->mode_config.num_encoder--;
873	drm_modeset_unlock_all(dev);
874}
875EXPORT_SYMBOL(drm_encoder_cleanup);
876
877/**
878 * drm_plane_init - Initialise a new plane object
879 * @dev: DRM device
880 * @plane: plane object to init
881 * @possible_crtcs: bitmask of possible CRTCs
882 * @funcs: callbacks for the new plane
883 * @formats: array of supported formats (%DRM_FORMAT_*)
884 * @format_count: number of elements in @formats
885 * @priv: plane is private (hidden from userspace)?
886 *
887 * Inits a new object created as base part of a driver plane object.
888 *
889 * RETURNS:
890 * Zero on success, error code on failure.
891 */
892int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
893		   unsigned long possible_crtcs,
894		   const struct drm_plane_funcs *funcs,
895		   const uint32_t *formats, uint32_t format_count,
896		   bool priv)
897{
898	int ret;
899
900	drm_modeset_lock_all(dev);
901
902	ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
903	if (ret)
904		goto out;
905
906	plane->base.properties = &plane->properties;
907	plane->dev = dev;
908	plane->funcs = funcs;
909	plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
910				      GFP_KERNEL);
911	if (!plane->format_types) {
912		DRM_DEBUG_KMS("out of memory when allocating plane\n");
913		drm_mode_object_put(dev, &plane->base);
914		ret = -ENOMEM;
915		goto out;
916	}
917
918	memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
919	plane->format_count = format_count;
920	plane->possible_crtcs = possible_crtcs;
921
922	/* private planes are not exposed to userspace, but depending on
923	 * display hardware, might be convenient to allow sharing programming
924	 * for the scanout engine with the crtc implementation.
925	 */
926	if (!priv) {
927		list_add_tail(&plane->head, &dev->mode_config.plane_list);
928		dev->mode_config.num_plane++;
929	} else {
930		INIT_LIST_HEAD(&plane->head);
931	}
932
933 out:
934	drm_modeset_unlock_all(dev);
935
936	return ret;
937}
938EXPORT_SYMBOL(drm_plane_init);
939
940/**
941 * drm_plane_cleanup - Clean up the core plane usage
942 * @plane: plane to cleanup
943 *
944 * This function cleans up @plane and removes it from the DRM mode setting
945 * core. Note that the function does *not* free the plane structure itself,
946 * this is the responsibility of the caller.
947 */
948void drm_plane_cleanup(struct drm_plane *plane)
949{
950	struct drm_device *dev = plane->dev;
951
952	drm_modeset_lock_all(dev);
953	kfree(plane->format_types);
954	drm_mode_object_put(dev, &plane->base);
955	/* if not added to a list, it must be a private plane */
956	if (!list_empty(&plane->head)) {
957		list_del(&plane->head);
958		dev->mode_config.num_plane--;
959	}
960	drm_modeset_unlock_all(dev);
961}
962EXPORT_SYMBOL(drm_plane_cleanup);
963
964/**
965 * drm_plane_force_disable - Forcibly disable a plane
966 * @plane: plane to disable
967 *
968 * Forces the plane to be disabled.
969 *
970 * Used when the plane's current framebuffer is destroyed,
971 * and when restoring fbdev mode.
972 */
973void drm_plane_force_disable(struct drm_plane *plane)
974{
975	int ret;
976
977	if (!plane->fb)
978		return;
979
980	ret = plane->funcs->disable_plane(plane);
981	if (ret)
982		DRM_ERROR("failed to disable plane with busy fb\n");
983	/* disconnect the plane from the fb and crtc: */
984	__drm_framebuffer_unreference(plane->fb);
985	plane->fb = NULL;
986	plane->crtc = NULL;
987}
988EXPORT_SYMBOL(drm_plane_force_disable);
989
990/**
991 * drm_mode_create - create a new display mode
992 * @dev: DRM device
993 *
994 * Create a new drm_display_mode, give it an ID, and return it.
995 *
996 * RETURNS:
997 * Pointer to new mode on success, NULL on error.
998 */
999struct drm_display_mode *drm_mode_create(struct drm_device *dev)
1000{
1001	struct drm_display_mode *nmode;
1002
1003	nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
1004	if (!nmode)
1005		return NULL;
1006
1007	if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
1008		kfree(nmode);
1009		return NULL;
1010	}
1011
1012	return nmode;
1013}
1014EXPORT_SYMBOL(drm_mode_create);
1015
1016/**
1017 * drm_mode_destroy - remove a mode
1018 * @dev: DRM device
1019 * @mode: mode to remove
1020 *
1021 * Free @mode's unique identifier, then free it.
1022 */
1023void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
1024{
1025	if (!mode)
1026		return;
1027
1028	drm_mode_object_put(dev, &mode->base);
1029
1030	kfree(mode);
1031}
1032EXPORT_SYMBOL(drm_mode_destroy);
1033
1034static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1035{
1036	struct drm_property *edid;
1037	struct drm_property *dpms;
1038
1039	/*
1040	 * Standard properties (apply to all connectors)
1041	 */
1042	edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1043				   DRM_MODE_PROP_IMMUTABLE,
1044				   "EDID", 0);
1045	dev->mode_config.edid_property = edid;
1046
1047	dpms = drm_property_create_enum(dev, 0,
1048				   "DPMS", drm_dpms_enum_list,
1049				   ARRAY_SIZE(drm_dpms_enum_list));
1050	dev->mode_config.dpms_property = dpms;
1051
1052	return 0;
1053}
1054
1055/**
1056 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1057 * @dev: DRM device
1058 *
1059 * Called by a driver the first time a DVI-I connector is made.
1060 */
1061int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1062{
1063	struct drm_property *dvi_i_selector;
1064	struct drm_property *dvi_i_subconnector;
1065
1066	if (dev->mode_config.dvi_i_select_subconnector_property)
1067		return 0;
1068
1069	dvi_i_selector =
1070		drm_property_create_enum(dev, 0,
1071				    "select subconnector",
1072				    drm_dvi_i_select_enum_list,
1073				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
1074	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1075
1076	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1077				    "subconnector",
1078				    drm_dvi_i_subconnector_enum_list,
1079				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1080	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1081
1082	return 0;
1083}
1084EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1085
1086/**
1087 * drm_create_tv_properties - create TV specific connector properties
1088 * @dev: DRM device
1089 * @num_modes: number of different TV formats (modes) supported
1090 * @modes: array of pointers to strings containing name of each format
1091 *
1092 * Called by a driver's TV initialization routine, this function creates
1093 * the TV specific connector properties for a given device.  Caller is
1094 * responsible for allocating a list of format names and passing them to
1095 * this routine.
1096 */
1097int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1098				  char *modes[])
1099{
1100	struct drm_property *tv_selector;
1101	struct drm_property *tv_subconnector;
1102	int i;
1103
1104	if (dev->mode_config.tv_select_subconnector_property)
1105		return 0;
1106
1107	/*
1108	 * Basic connector properties
1109	 */
1110	tv_selector = drm_property_create_enum(dev, 0,
1111					  "select subconnector",
1112					  drm_tv_select_enum_list,
1113					  ARRAY_SIZE(drm_tv_select_enum_list));
1114	dev->mode_config.tv_select_subconnector_property = tv_selector;
1115
1116	tv_subconnector =
1117		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1118				    "subconnector",
1119				    drm_tv_subconnector_enum_list,
1120				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
1121	dev->mode_config.tv_subconnector_property = tv_subconnector;
1122
1123	/*
1124	 * Other, TV specific properties: margins & TV modes.
1125	 */
1126	dev->mode_config.tv_left_margin_property =
1127		drm_property_create_range(dev, 0, "left margin", 0, 100);
1128
1129	dev->mode_config.tv_right_margin_property =
1130		drm_property_create_range(dev, 0, "right margin", 0, 100);
1131
1132	dev->mode_config.tv_top_margin_property =
1133		drm_property_create_range(dev, 0, "top margin", 0, 100);
1134
1135	dev->mode_config.tv_bottom_margin_property =
1136		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1137
1138	dev->mode_config.tv_mode_property =
1139		drm_property_create(dev, DRM_MODE_PROP_ENUM,
1140				    "mode", num_modes);
1141	for (i = 0; i < num_modes; i++)
1142		drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1143				      i, modes[i]);
1144
1145	dev->mode_config.tv_brightness_property =
1146		drm_property_create_range(dev, 0, "brightness", 0, 100);
1147
1148	dev->mode_config.tv_contrast_property =
1149		drm_property_create_range(dev, 0, "contrast", 0, 100);
1150
1151	dev->mode_config.tv_flicker_reduction_property =
1152		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1153
1154	dev->mode_config.tv_overscan_property =
1155		drm_property_create_range(dev, 0, "overscan", 0, 100);
1156
1157	dev->mode_config.tv_saturation_property =
1158		drm_property_create_range(dev, 0, "saturation", 0, 100);
1159
1160	dev->mode_config.tv_hue_property =
1161		drm_property_create_range(dev, 0, "hue", 0, 100);
1162
1163	return 0;
1164}
1165EXPORT_SYMBOL(drm_mode_create_tv_properties);
1166
1167/**
1168 * drm_mode_create_scaling_mode_property - create scaling mode property
1169 * @dev: DRM device
1170 *
1171 * Called by a driver the first time it's needed, must be attached to desired
1172 * connectors.
1173 */
1174int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1175{
1176	struct drm_property *scaling_mode;
1177
1178	if (dev->mode_config.scaling_mode_property)
1179		return 0;
1180
1181	scaling_mode =
1182		drm_property_create_enum(dev, 0, "scaling mode",
1183				drm_scaling_mode_enum_list,
1184				    ARRAY_SIZE(drm_scaling_mode_enum_list));
1185
1186	dev->mode_config.scaling_mode_property = scaling_mode;
1187
1188	return 0;
1189}
1190EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1191
1192/**
1193 * drm_mode_create_dirty_property - create dirty property
1194 * @dev: DRM device
1195 *
1196 * Called by a driver the first time it's needed, must be attached to desired
1197 * connectors.
1198 */
1199int drm_mode_create_dirty_info_property(struct drm_device *dev)
1200{
1201	struct drm_property *dirty_info;
1202
1203	if (dev->mode_config.dirty_info_property)
1204		return 0;
1205
1206	dirty_info =
1207		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1208				    "dirty",
1209				    drm_dirty_info_enum_list,
1210				    ARRAY_SIZE(drm_dirty_info_enum_list));
1211	dev->mode_config.dirty_info_property = dirty_info;
1212
1213	return 0;
1214}
1215EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1216
1217static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1218{
1219	uint32_t total_objects = 0;
1220
1221	total_objects += dev->mode_config.num_crtc;
1222	total_objects += dev->mode_config.num_connector;
1223	total_objects += dev->mode_config.num_encoder;
1224	total_objects += dev->mode_config.num_bridge;
1225
1226	group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1227	if (!group->id_list)
1228		return -ENOMEM;
1229
1230	group->num_crtcs = 0;
1231	group->num_connectors = 0;
1232	group->num_encoders = 0;
1233	group->num_bridges = 0;
1234	return 0;
1235}
1236
1237int drm_mode_group_init_legacy_group(struct drm_device *dev,
1238				     struct drm_mode_group *group)
1239{
1240	struct drm_crtc *crtc;
1241	struct drm_encoder *encoder;
1242	struct drm_connector *connector;
1243	struct drm_bridge *bridge;
1244	int ret;
1245
1246	if ((ret = drm_mode_group_init(dev, group)))
1247		return ret;
1248
1249	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1250		group->id_list[group->num_crtcs++] = crtc->base.id;
1251
1252	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1253		group->id_list[group->num_crtcs + group->num_encoders++] =
1254		encoder->base.id;
1255
1256	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1257		group->id_list[group->num_crtcs + group->num_encoders +
1258			       group->num_connectors++] = connector->base.id;
1259
1260	list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1261		group->id_list[group->num_crtcs + group->num_encoders +
1262			       group->num_connectors + group->num_bridges++] =
1263					bridge->base.id;
1264
1265	return 0;
1266}
1267EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1268
1269/**
1270 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1271 * @out: drm_mode_modeinfo struct to return to the user
1272 * @in: drm_display_mode to use
1273 *
1274 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1275 * the user.
1276 */
1277static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1278				      const struct drm_display_mode *in)
1279{
1280	WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1281	     in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1282	     in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1283	     in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1284	     in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1285	     "timing values too large for mode info\n");
1286
1287	out->clock = in->clock;
1288	out->hdisplay = in->hdisplay;
1289	out->hsync_start = in->hsync_start;
1290	out->hsync_end = in->hsync_end;
1291	out->htotal = in->htotal;
1292	out->hskew = in->hskew;
1293	out->vdisplay = in->vdisplay;
1294	out->vsync_start = in->vsync_start;
1295	out->vsync_end = in->vsync_end;
1296	out->vtotal = in->vtotal;
1297	out->vscan = in->vscan;
1298	out->vrefresh = in->vrefresh;
1299	out->flags = in->flags;
1300	out->type = in->type;
1301	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1302	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1303}
1304
1305/**
1306 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1307 * @out: drm_display_mode to return to the user
1308 * @in: drm_mode_modeinfo to use
1309 *
1310 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1311 * the caller.
1312 *
1313 * RETURNS:
1314 * Zero on success, errno on failure.
1315 */
1316static int drm_crtc_convert_umode(struct drm_display_mode *out,
1317				  const struct drm_mode_modeinfo *in)
1318{
1319	if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1320		return -ERANGE;
1321
1322	if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1323		return -EINVAL;
1324
1325	out->clock = in->clock;
1326	out->hdisplay = in->hdisplay;
1327	out->hsync_start = in->hsync_start;
1328	out->hsync_end = in->hsync_end;
1329	out->htotal = in->htotal;
1330	out->hskew = in->hskew;
1331	out->vdisplay = in->vdisplay;
1332	out->vsync_start = in->vsync_start;
1333	out->vsync_end = in->vsync_end;
1334	out->vtotal = in->vtotal;
1335	out->vscan = in->vscan;
1336	out->vrefresh = in->vrefresh;
1337	out->flags = in->flags;
1338	out->type = in->type;
1339	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1340	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1341
1342	return 0;
1343}
1344
1345/**
1346 * drm_mode_getresources - get graphics configuration
1347 * @dev: drm device for the ioctl
1348 * @data: data pointer for the ioctl
1349 * @file_priv: drm file for the ioctl call
1350 *
1351 * Construct a set of configuration description structures and return
1352 * them to the user, including CRTC, connector and framebuffer configuration.
1353 *
1354 * Called by the user via ioctl.
1355 *
1356 * RETURNS:
1357 * Zero on success, errno on failure.
1358 */
1359int drm_mode_getresources(struct drm_device *dev, void *data,
1360			  struct drm_file *file_priv)
1361{
1362	struct drm_mode_card_res *card_res = data;
1363	struct list_head *lh;
1364	struct drm_framebuffer *fb;
1365	struct drm_connector *connector;
1366	struct drm_crtc *crtc;
1367	struct drm_encoder *encoder;
1368	int ret = 0;
1369	int connector_count = 0;
1370	int crtc_count = 0;
1371	int fb_count = 0;
1372	int encoder_count = 0;
1373	int copied = 0, i;
1374	uint32_t __user *fb_id;
1375	uint32_t __user *crtc_id;
1376	uint32_t __user *connector_id;
1377	uint32_t __user *encoder_id;
1378	struct drm_mode_group *mode_group;
1379
1380	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1381		return -EINVAL;
1382
1383
1384	mutex_lock(&file_priv->fbs_lock);
1385	/*
1386	 * For the non-control nodes we need to limit the list of resources
1387	 * by IDs in the group list for this node
1388	 */
1389	list_for_each(lh, &file_priv->fbs)
1390		fb_count++;
1391
1392	/* handle this in 4 parts */
1393	/* FBs */
1394	if (card_res->count_fbs >= fb_count) {
1395		copied = 0;
1396		fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1397		list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1398			if (put_user(fb->base.id, fb_id + copied)) {
1399				mutex_unlock(&file_priv->fbs_lock);
1400				return -EFAULT;
1401			}
1402			copied++;
1403		}
1404	}
1405	card_res->count_fbs = fb_count;
1406	mutex_unlock(&file_priv->fbs_lock);
1407
1408	drm_modeset_lock_all(dev);
1409	mode_group = &file_priv->master->minor->mode_group;
1410	if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1411
1412		list_for_each(lh, &dev->mode_config.crtc_list)
1413			crtc_count++;
1414
1415		list_for_each(lh, &dev->mode_config.connector_list)
1416			connector_count++;
1417
1418		list_for_each(lh, &dev->mode_config.encoder_list)
1419			encoder_count++;
1420	} else {
1421
1422		crtc_count = mode_group->num_crtcs;
1423		connector_count = mode_group->num_connectors;
1424		encoder_count = mode_group->num_encoders;
1425	}
1426
1427	card_res->max_height = dev->mode_config.max_height;
1428	card_res->min_height = dev->mode_config.min_height;
1429	card_res->max_width = dev->mode_config.max_width;
1430	card_res->min_width = dev->mode_config.min_width;
1431
1432	/* CRTCs */
1433	if (card_res->count_crtcs >= crtc_count) {
1434		copied = 0;
1435		crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1436		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1437			list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1438					    head) {
1439				DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1440				if (put_user(crtc->base.id, crtc_id + copied)) {
1441					ret = -EFAULT;
1442					goto out;
1443				}
1444				copied++;
1445			}
1446		} else {
1447			for (i = 0; i < mode_group->num_crtcs; i++) {
1448				if (put_user(mode_group->id_list[i],
1449					     crtc_id + copied)) {
1450					ret = -EFAULT;
1451					goto out;
1452				}
1453				copied++;
1454			}
1455		}
1456	}
1457	card_res->count_crtcs = crtc_count;
1458
1459	/* Encoders */
1460	if (card_res->count_encoders >= encoder_count) {
1461		copied = 0;
1462		encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1463		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1464			list_for_each_entry(encoder,
1465					    &dev->mode_config.encoder_list,
1466					    head) {
1467				DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1468						drm_get_encoder_name(encoder));
1469				if (put_user(encoder->base.id, encoder_id +
1470					     copied)) {
1471					ret = -EFAULT;
1472					goto out;
1473				}
1474				copied++;
1475			}
1476		} else {
1477			for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1478				if (put_user(mode_group->id_list[i],
1479					     encoder_id + copied)) {
1480					ret = -EFAULT;
1481					goto out;
1482				}
1483				copied++;
1484			}
1485
1486		}
1487	}
1488	card_res->count_encoders = encoder_count;
1489
1490	/* Connectors */
1491	if (card_res->count_connectors >= connector_count) {
1492		copied = 0;
1493		connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1494		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1495			list_for_each_entry(connector,
1496					    &dev->mode_config.connector_list,
1497					    head) {
1498				DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1499					connector->base.id,
1500					drm_get_connector_name(connector));
1501				if (put_user(connector->base.id,
1502					     connector_id + copied)) {
1503					ret = -EFAULT;
1504					goto out;
1505				}
1506				copied++;
1507			}
1508		} else {
1509			int start = mode_group->num_crtcs +
1510				mode_group->num_encoders;
1511			for (i = start; i < start + mode_group->num_connectors; i++) {
1512				if (put_user(mode_group->id_list[i],
1513					     connector_id + copied)) {
1514					ret = -EFAULT;
1515					goto out;
1516				}
1517				copied++;
1518			}
1519		}
1520	}
1521	card_res->count_connectors = connector_count;
1522
1523	DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1524		  card_res->count_connectors, card_res->count_encoders);
1525
1526out:
1527	drm_modeset_unlock_all(dev);
1528	return ret;
1529}
1530
1531/**
1532 * drm_mode_getcrtc - get CRTC configuration
1533 * @dev: drm device for the ioctl
1534 * @data: data pointer for the ioctl
1535 * @file_priv: drm file for the ioctl call
1536 *
1537 * Construct a CRTC configuration structure to return to the user.
1538 *
1539 * Called by the user via ioctl.
1540 *
1541 * RETURNS:
1542 * Zero on success, errno on failure.
1543 */
1544int drm_mode_getcrtc(struct drm_device *dev,
1545		     void *data, struct drm_file *file_priv)
1546{
1547	struct drm_mode_crtc *crtc_resp = data;
1548	struct drm_crtc *crtc;
1549	struct drm_mode_object *obj;
1550	int ret = 0;
1551
1552	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1553		return -EINVAL;
1554
1555	drm_modeset_lock_all(dev);
1556
1557	obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1558				   DRM_MODE_OBJECT_CRTC);
1559	if (!obj) {
1560		ret = -EINVAL;
1561		goto out;
1562	}
1563	crtc = obj_to_crtc(obj);
1564
1565	crtc_resp->x = crtc->x;
1566	crtc_resp->y = crtc->y;
1567	crtc_resp->gamma_size = crtc->gamma_size;
1568	if (crtc->fb)
1569		crtc_resp->fb_id = crtc->fb->base.id;
1570	else
1571		crtc_resp->fb_id = 0;
1572
1573	if (crtc->enabled) {
1574
1575		drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1576		crtc_resp->mode_valid = 1;
1577
1578	} else {
1579		crtc_resp->mode_valid = 0;
1580	}
1581
1582out:
1583	drm_modeset_unlock_all(dev);
1584	return ret;
1585}
1586
1587static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1588					 const struct drm_file *file_priv)
1589{
1590	/*
1591	 * If user-space hasn't configured the driver to expose the stereo 3D
1592	 * modes, don't expose them.
1593	 */
1594	if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1595		return false;
1596
1597	return true;
1598}
1599
1600/**
1601 * drm_mode_getconnector - get connector configuration
1602 * @dev: drm device for the ioctl
1603 * @data: data pointer for the ioctl
1604 * @file_priv: drm file for the ioctl call
1605 *
1606 * Construct a connector configuration structure to return to the user.
1607 *
1608 * Called by the user via ioctl.
1609 *
1610 * RETURNS:
1611 * Zero on success, errno on failure.
1612 */
1613int drm_mode_getconnector(struct drm_device *dev, void *data,
1614			  struct drm_file *file_priv)
1615{
1616	struct drm_mode_get_connector *out_resp = data;
1617	struct drm_mode_object *obj;
1618	struct drm_connector *connector;
1619	struct drm_display_mode *mode;
1620	int mode_count = 0;
1621	int props_count = 0;
1622	int encoders_count = 0;
1623	int ret = 0;
1624	int copied = 0;
1625	int i;
1626	struct drm_mode_modeinfo u_mode;
1627	struct drm_mode_modeinfo __user *mode_ptr;
1628	uint32_t __user *prop_ptr;
1629	uint64_t __user *prop_values;
1630	uint32_t __user *encoder_ptr;
1631
1632	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1633		return -EINVAL;
1634
1635	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1636
1637	DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1638
1639	mutex_lock(&dev->mode_config.mutex);
1640
1641	obj = drm_mode_object_find(dev, out_resp->connector_id,
1642				   DRM_MODE_OBJECT_CONNECTOR);
1643	if (!obj) {
1644		ret = -EINVAL;
1645		goto out;
1646	}
1647	connector = obj_to_connector(obj);
1648
1649	props_count = connector->properties.count;
1650
1651	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1652		if (connector->encoder_ids[i] != 0) {
1653			encoders_count++;
1654		}
1655	}
1656
1657	if (out_resp->count_modes == 0) {
1658		connector->funcs->fill_modes(connector,
1659					     dev->mode_config.max_width,
1660					     dev->mode_config.max_height);
1661	}
1662
1663	/* delayed so we get modes regardless of pre-fill_modes state */
1664	list_for_each_entry(mode, &connector->modes, head)
1665		if (drm_mode_expose_to_userspace(mode, file_priv))
1666			mode_count++;
1667
1668	out_resp->connector_id = connector->base.id;
1669	out_resp->connector_type = connector->connector_type;
1670	out_resp->connector_type_id = connector->connector_type_id;
1671	out_resp->mm_width = connector->display_info.width_mm;
1672	out_resp->mm_height = connector->display_info.height_mm;
1673	out_resp->subpixel = connector->display_info.subpixel_order;
1674	out_resp->connection = connector->status;
1675	if (connector->encoder)
1676		out_resp->encoder_id = connector->encoder->base.id;
1677	else
1678		out_resp->encoder_id = 0;
1679
1680	/*
1681	 * This ioctl is called twice, once to determine how much space is
1682	 * needed, and the 2nd time to fill it.
1683	 */
1684	if ((out_resp->count_modes >= mode_count) && mode_count) {
1685		copied = 0;
1686		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1687		list_for_each_entry(mode, &connector->modes, head) {
1688			if (!drm_mode_expose_to_userspace(mode, file_priv))
1689				continue;
1690
1691			drm_crtc_convert_to_umode(&u_mode, mode);
1692			if (copy_to_user(mode_ptr + copied,
1693					 &u_mode, sizeof(u_mode))) {
1694				ret = -EFAULT;
1695				goto out;
1696			}
1697			copied++;
1698		}
1699	}
1700	out_resp->count_modes = mode_count;
1701
1702	if ((out_resp->count_props >= props_count) && props_count) {
1703		copied = 0;
1704		prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1705		prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1706		for (i = 0; i < connector->properties.count; i++) {
1707			if (put_user(connector->properties.ids[i],
1708				     prop_ptr + copied)) {
1709				ret = -EFAULT;
1710				goto out;
1711			}
1712
1713			if (put_user(connector->properties.values[i],
1714				     prop_values + copied)) {
1715				ret = -EFAULT;
1716				goto out;
1717			}
1718			copied++;
1719		}
1720	}
1721	out_resp->count_props = props_count;
1722
1723	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1724		copied = 0;
1725		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1726		for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1727			if (connector->encoder_ids[i] != 0) {
1728				if (put_user(connector->encoder_ids[i],
1729					     encoder_ptr + copied)) {
1730					ret = -EFAULT;
1731					goto out;
1732				}
1733				copied++;
1734			}
1735		}
1736	}
1737	out_resp->count_encoders = encoders_count;
1738
1739out:
1740	mutex_unlock(&dev->mode_config.mutex);
1741
1742	return ret;
1743}
1744
1745int drm_mode_getencoder(struct drm_device *dev, void *data,
1746			struct drm_file *file_priv)
1747{
1748	struct drm_mode_get_encoder *enc_resp = data;
1749	struct drm_mode_object *obj;
1750	struct drm_encoder *encoder;
1751	int ret = 0;
1752
1753	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1754		return -EINVAL;
1755
1756	drm_modeset_lock_all(dev);
1757	obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1758				   DRM_MODE_OBJECT_ENCODER);
1759	if (!obj) {
1760		ret = -EINVAL;
1761		goto out;
1762	}
1763	encoder = obj_to_encoder(obj);
1764
1765	if (encoder->crtc)
1766		enc_resp->crtc_id = encoder->crtc->base.id;
1767	else
1768		enc_resp->crtc_id = 0;
1769	enc_resp->encoder_type = encoder->encoder_type;
1770	enc_resp->encoder_id = encoder->base.id;
1771	enc_resp->possible_crtcs = encoder->possible_crtcs;
1772	enc_resp->possible_clones = encoder->possible_clones;
1773
1774out:
1775	drm_modeset_unlock_all(dev);
1776	return ret;
1777}
1778
1779/**
1780 * drm_mode_getplane_res - get plane info
1781 * @dev: DRM device
1782 * @data: ioctl data
1783 * @file_priv: DRM file info
1784 *
1785 * Return an plane count and set of IDs.
1786 */
1787int drm_mode_getplane_res(struct drm_device *dev, void *data,
1788			    struct drm_file *file_priv)
1789{
1790	struct drm_mode_get_plane_res *plane_resp = data;
1791	struct drm_mode_config *config;
1792	struct drm_plane *plane;
1793	uint32_t __user *plane_ptr;
1794	int copied = 0, ret = 0;
1795
1796	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1797		return -EINVAL;
1798
1799	drm_modeset_lock_all(dev);
1800	config = &dev->mode_config;
1801
1802	/*
1803	 * This ioctl is called twice, once to determine how much space is
1804	 * needed, and the 2nd time to fill it.
1805	 */
1806	if (config->num_plane &&
1807	    (plane_resp->count_planes >= config->num_plane)) {
1808		plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1809
1810		list_for_each_entry(plane, &config->plane_list, head) {
1811			if (put_user(plane->base.id, plane_ptr + copied)) {
1812				ret = -EFAULT;
1813				goto out;
1814			}
1815			copied++;
1816		}
1817	}
1818	plane_resp->count_planes = config->num_plane;
1819
1820out:
1821	drm_modeset_unlock_all(dev);
1822	return ret;
1823}
1824
1825/**
1826 * drm_mode_getplane - get plane info
1827 * @dev: DRM device
1828 * @data: ioctl data
1829 * @file_priv: DRM file info
1830 *
1831 * Return plane info, including formats supported, gamma size, any
1832 * current fb, etc.
1833 */
1834int drm_mode_getplane(struct drm_device *dev, void *data,
1835			struct drm_file *file_priv)
1836{
1837	struct drm_mode_get_plane *plane_resp = data;
1838	struct drm_mode_object *obj;
1839	struct drm_plane *plane;
1840	uint32_t __user *format_ptr;
1841	int ret = 0;
1842
1843	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1844		return -EINVAL;
1845
1846	drm_modeset_lock_all(dev);
1847	obj = drm_mode_object_find(dev, plane_resp->plane_id,
1848				   DRM_MODE_OBJECT_PLANE);
1849	if (!obj) {
1850		ret = -ENOENT;
1851		goto out;
1852	}
1853	plane = obj_to_plane(obj);
1854
1855	if (plane->crtc)
1856		plane_resp->crtc_id = plane->crtc->base.id;
1857	else
1858		plane_resp->crtc_id = 0;
1859
1860	if (plane->fb)
1861		plane_resp->fb_id = plane->fb->base.id;
1862	else
1863		plane_resp->fb_id = 0;
1864
1865	plane_resp->plane_id = plane->base.id;
1866	plane_resp->possible_crtcs = plane->possible_crtcs;
1867	plane_resp->gamma_size = 0;
1868
1869	/*
1870	 * This ioctl is called twice, once to determine how much space is
1871	 * needed, and the 2nd time to fill it.
1872	 */
1873	if (plane->format_count &&
1874	    (plane_resp->count_format_types >= plane->format_count)) {
1875		format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1876		if (copy_to_user(format_ptr,
1877				 plane->format_types,
1878				 sizeof(uint32_t) * plane->format_count)) {
1879			ret = -EFAULT;
1880			goto out;
1881		}
1882	}
1883	plane_resp->count_format_types = plane->format_count;
1884
1885out:
1886	drm_modeset_unlock_all(dev);
1887	return ret;
1888}
1889
1890/**
1891 * drm_mode_setplane - set up or tear down an plane
1892 * @dev: DRM device
1893 * @data: ioctl data*
1894 * @file_priv: DRM file info
1895 *
1896 * Set plane info, including placement, fb, scaling, and other factors.
1897 * Or pass a NULL fb to disable.
1898 */
1899int drm_mode_setplane(struct drm_device *dev, void *data,
1900			struct drm_file *file_priv)
1901{
1902	struct drm_mode_set_plane *plane_req = data;
1903	struct drm_mode_object *obj;
1904	struct drm_plane *plane;
1905	struct drm_crtc *crtc;
1906	struct drm_framebuffer *fb = NULL, *old_fb = NULL;
1907	int ret = 0;
1908	unsigned int fb_width, fb_height;
1909	int i;
1910
1911	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1912		return -EINVAL;
1913
1914	/*
1915	 * First, find the plane, crtc, and fb objects.  If not available,
1916	 * we don't bother to call the driver.
1917	 */
1918	obj = drm_mode_object_find(dev, plane_req->plane_id,
1919				   DRM_MODE_OBJECT_PLANE);
1920	if (!obj) {
1921		DRM_DEBUG_KMS("Unknown plane ID %d\n",
1922			      plane_req->plane_id);
1923		return -ENOENT;
1924	}
1925	plane = obj_to_plane(obj);
1926
1927	/* No fb means shut it down */
1928	if (!plane_req->fb_id) {
1929		drm_modeset_lock_all(dev);
1930		old_fb = plane->fb;
1931		plane->funcs->disable_plane(plane);
1932		plane->crtc = NULL;
1933		plane->fb = NULL;
1934		drm_modeset_unlock_all(dev);
1935		goto out;
1936	}
1937
1938	obj = drm_mode_object_find(dev, plane_req->crtc_id,
1939				   DRM_MODE_OBJECT_CRTC);
1940	if (!obj) {
1941		DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1942			      plane_req->crtc_id);
1943		ret = -ENOENT;
1944		goto out;
1945	}
1946	crtc = obj_to_crtc(obj);
1947
1948	fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1949	if (!fb) {
1950		DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1951			      plane_req->fb_id);
1952		ret = -ENOENT;
1953		goto out;
1954	}
1955
1956	/* Check whether this plane supports the fb pixel format. */
1957	for (i = 0; i < plane->format_count; i++)
1958		if (fb->pixel_format == plane->format_types[i])
1959			break;
1960	if (i == plane->format_count) {
1961		DRM_DEBUG_KMS("Invalid pixel format %s\n",
1962			      drm_get_format_name(fb->pixel_format));
1963		ret = -EINVAL;
1964		goto out;
1965	}
1966
1967	fb_width = fb->width << 16;
1968	fb_height = fb->height << 16;
1969
1970	/* Make sure source coordinates are inside the fb. */
1971	if (plane_req->src_w > fb_width ||
1972	    plane_req->src_x > fb_width - plane_req->src_w ||
1973	    plane_req->src_h > fb_height ||
1974	    plane_req->src_y > fb_height - plane_req->src_h) {
1975		DRM_DEBUG_KMS("Invalid source coordinates "
1976			      "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1977			      plane_req->src_w >> 16,
1978			      ((plane_req->src_w & 0xffff) * 15625) >> 10,
1979			      plane_req->src_h >> 16,
1980			      ((plane_req->src_h & 0xffff) * 15625) >> 10,
1981			      plane_req->src_x >> 16,
1982			      ((plane_req->src_x & 0xffff) * 15625) >> 10,
1983			      plane_req->src_y >> 16,
1984			      ((plane_req->src_y & 0xffff) * 15625) >> 10);
1985		ret = -ENOSPC;
1986		goto out;
1987	}
1988
1989	/* Give drivers some help against integer overflows */
1990	if (plane_req->crtc_w > INT_MAX ||
1991	    plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1992	    plane_req->crtc_h > INT_MAX ||
1993	    plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1994		DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1995			      plane_req->crtc_w, plane_req->crtc_h,
1996			      plane_req->crtc_x, plane_req->crtc_y);
1997		ret = -ERANGE;
1998		goto out;
1999	}
2000
2001	drm_modeset_lock_all(dev);
2002	ret = plane->funcs->update_plane(plane, crtc, fb,
2003					 plane_req->crtc_x, plane_req->crtc_y,
2004					 plane_req->crtc_w, plane_req->crtc_h,
2005					 plane_req->src_x, plane_req->src_y,
2006					 plane_req->src_w, plane_req->src_h);
2007	if (!ret) {
2008		old_fb = plane->fb;
2009		plane->crtc = crtc;
2010		plane->fb = fb;
2011		fb = NULL;
2012	}
2013	drm_modeset_unlock_all(dev);
2014
2015out:
2016	if (fb)
2017		drm_framebuffer_unreference(fb);
2018	if (old_fb)
2019		drm_framebuffer_unreference(old_fb);
2020
2021	return ret;
2022}
2023
2024/**
2025 * drm_mode_set_config_internal - helper to call ->set_config
2026 * @set: modeset config to set
2027 *
2028 * This is a little helper to wrap internal calls to the ->set_config driver
2029 * interface. The only thing it adds is correct refcounting dance.
2030 */
2031int drm_mode_set_config_internal(struct drm_mode_set *set)
2032{
2033	struct drm_crtc *crtc = set->crtc;
2034	struct drm_framebuffer *fb;
2035	struct drm_crtc *tmp;
2036	int ret;
2037
2038	/*
2039	 * NOTE: ->set_config can also disable other crtcs (if we steal all
2040	 * connectors from it), hence we need to refcount the fbs across all
2041	 * crtcs. Atomic modeset will have saner semantics ...
2042	 */
2043	list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2044		tmp->old_fb = tmp->fb;
2045
2046	fb = set->fb;
2047
2048	ret = crtc->funcs->set_config(set);
2049	if (ret == 0) {
2050		/* crtc->fb must be updated by ->set_config, enforces this. */
2051		WARN_ON(fb != crtc->fb);
2052	}
2053
2054	list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2055		if (tmp->fb)
2056			drm_framebuffer_reference(tmp->fb);
2057		if (tmp->old_fb)
2058			drm_framebuffer_unreference(tmp->old_fb);
2059	}
2060
2061	return ret;
2062}
2063EXPORT_SYMBOL(drm_mode_set_config_internal);
2064
2065/*
2066 * Checks that the framebuffer is big enough for the CRTC viewport
2067 * (x, y, hdisplay, vdisplay)
2068 */
2069static int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2070				   int x, int y,
2071				   const struct drm_display_mode *mode,
2072				   const struct drm_framebuffer *fb)
2073
2074{
2075	int hdisplay, vdisplay;
2076
2077	hdisplay = mode->hdisplay;
2078	vdisplay = mode->vdisplay;
2079
2080	if (drm_mode_is_stereo(mode)) {
2081		struct drm_display_mode adjusted = *mode;
2082
2083		drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2084		hdisplay = adjusted.crtc_hdisplay;
2085		vdisplay = adjusted.crtc_vdisplay;
2086	}
2087
2088	if (crtc->invert_dimensions)
2089		swap(hdisplay, vdisplay);
2090
2091	if (hdisplay > fb->width ||
2092	    vdisplay > fb->height ||
2093	    x > fb->width - hdisplay ||
2094	    y > fb->height - vdisplay) {
2095		DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2096			      fb->width, fb->height, hdisplay, vdisplay, x, y,
2097			      crtc->invert_dimensions ? " (inverted)" : "");
2098		return -ENOSPC;
2099	}
2100
2101	return 0;
2102}
2103
2104/**
2105 * drm_mode_setcrtc - set CRTC configuration
2106 * @dev: drm device for the ioctl
2107 * @data: data pointer for the ioctl
2108 * @file_priv: drm file for the ioctl call
2109 *
2110 * Build a new CRTC configuration based on user request.
2111 *
2112 * Called by the user via ioctl.
2113 *
2114 * RETURNS:
2115 * Zero on success, errno on failure.
2116 */
2117int drm_mode_setcrtc(struct drm_device *dev, void *data,
2118		     struct drm_file *file_priv)
2119{
2120	struct drm_mode_config *config = &dev->mode_config;
2121	struct drm_mode_crtc *crtc_req = data;
2122	struct drm_mode_object *obj;
2123	struct drm_crtc *crtc;
2124	struct drm_connector **connector_set = NULL, *connector;
2125	struct drm_framebuffer *fb = NULL;
2126	struct drm_display_mode *mode = NULL;
2127	struct drm_mode_set set;
2128	uint32_t __user *set_connectors_ptr;
2129	int ret;
2130	int i;
2131
2132	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2133		return -EINVAL;
2134
2135	/* For some reason crtc x/y offsets are signed internally. */
2136	if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2137		return -ERANGE;
2138
2139	drm_modeset_lock_all(dev);
2140	obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2141				   DRM_MODE_OBJECT_CRTC);
2142	if (!obj) {
2143		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2144		ret = -EINVAL;
2145		goto out;
2146	}
2147	crtc = obj_to_crtc(obj);
2148	DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2149
2150	if (crtc_req->mode_valid) {
2151		/* If we have a mode we need a framebuffer. */
2152		/* If we pass -1, set the mode with the currently bound fb */
2153		if (crtc_req->fb_id == -1) {
2154			if (!crtc->fb) {
2155				DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2156				ret = -EINVAL;
2157				goto out;
2158			}
2159			fb = crtc->fb;
2160			/* Make refcounting symmetric with the lookup path. */
2161			drm_framebuffer_reference(fb);
2162		} else {
2163			fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2164			if (!fb) {
2165				DRM_DEBUG_KMS("Unknown FB ID%d\n",
2166						crtc_req->fb_id);
2167				ret = -EINVAL;
2168				goto out;
2169			}
2170		}
2171
2172		mode = drm_mode_create(dev);
2173		if (!mode) {
2174			ret = -ENOMEM;
2175			goto out;
2176		}
2177
2178		ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2179		if (ret) {
2180			DRM_DEBUG_KMS("Invalid mode\n");
2181			goto out;
2182		}
2183
2184		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2185
2186		ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2187					      mode, fb);
2188		if (ret)
2189			goto out;
2190
2191	}
2192
2193	if (crtc_req->count_connectors == 0 && mode) {
2194		DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2195		ret = -EINVAL;
2196		goto out;
2197	}
2198
2199	if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2200		DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2201			  crtc_req->count_connectors);
2202		ret = -EINVAL;
2203		goto out;
2204	}
2205
2206	if (crtc_req->count_connectors > 0) {
2207		u32 out_id;
2208
2209		/* Avoid unbounded kernel memory allocation */
2210		if (crtc_req->count_connectors > config->num_connector) {
2211			ret = -EINVAL;
2212			goto out;
2213		}
2214
2215		connector_set = kmalloc(crtc_req->count_connectors *
2216					sizeof(struct drm_connector *),
2217					GFP_KERNEL);
2218		if (!connector_set) {
2219			ret = -ENOMEM;
2220			goto out;
2221		}
2222
2223		for (i = 0; i < crtc_req->count_connectors; i++) {
2224			set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2225			if (get_user(out_id, &set_connectors_ptr[i])) {
2226				ret = -EFAULT;
2227				goto out;
2228			}
2229
2230			obj = drm_mode_object_find(dev, out_id,
2231						   DRM_MODE_OBJECT_CONNECTOR);
2232			if (!obj) {
2233				DRM_DEBUG_KMS("Connector id %d unknown\n",
2234						out_id);
2235				ret = -EINVAL;
2236				goto out;
2237			}
2238			connector = obj_to_connector(obj);
2239			DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2240					connector->base.id,
2241					drm_get_connector_name(connector));
2242
2243			connector_set[i] = connector;
2244		}
2245	}
2246
2247	set.crtc = crtc;
2248	set.x = crtc_req->x;
2249	set.y = crtc_req->y;
2250	set.mode = mode;
2251	set.connectors = connector_set;
2252	set.num_connectors = crtc_req->count_connectors;
2253	set.fb = fb;
2254	ret = drm_mode_set_config_internal(&set);
2255
2256out:
2257	if (fb)
2258		drm_framebuffer_unreference(fb);
2259
2260	kfree(connector_set);
2261	drm_mode_destroy(dev, mode);
2262	drm_modeset_unlock_all(dev);
2263	return ret;
2264}
2265
2266static int drm_mode_cursor_common(struct drm_device *dev,
2267				  struct drm_mode_cursor2 *req,
2268				  struct drm_file *file_priv)
2269{
2270	struct drm_mode_object *obj;
2271	struct drm_crtc *crtc;
2272	int ret = 0;
2273
2274	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2275		return -EINVAL;
2276
2277	if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2278		return -EINVAL;
2279
2280	obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2281	if (!obj) {
2282		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2283		return -EINVAL;
2284	}
2285	crtc = obj_to_crtc(obj);
2286
2287	mutex_lock(&crtc->mutex);
2288	if (req->flags & DRM_MODE_CURSOR_BO) {
2289		if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2290			ret = -ENXIO;
2291			goto out;
2292		}
2293		/* Turns off the cursor if handle is 0 */
2294		if (crtc->funcs->cursor_set2)
2295			ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2296						      req->width, req->height, req->hot_x, req->hot_y);
2297		else
2298			ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2299						      req->width, req->height);
2300	}
2301
2302	if (req->flags & DRM_MODE_CURSOR_MOVE) {
2303		if (crtc->funcs->cursor_move) {
2304			ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2305		} else {
2306			ret = -EFAULT;
2307			goto out;
2308		}
2309	}
2310out:
2311	mutex_unlock(&crtc->mutex);
2312
2313	return ret;
2314
2315}
2316int drm_mode_cursor_ioctl(struct drm_device *dev,
2317			void *data, struct drm_file *file_priv)
2318{
2319	struct drm_mode_cursor *req = data;
2320	struct drm_mode_cursor2 new_req;
2321
2322	memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2323	new_req.hot_x = new_req.hot_y = 0;
2324
2325	return drm_mode_cursor_common(dev, &new_req, file_priv);
2326}
2327
2328int drm_mode_cursor2_ioctl(struct drm_device *dev,
2329			   void *data, struct drm_file *file_priv)
2330{
2331	struct drm_mode_cursor2 *req = data;
2332	return drm_mode_cursor_common(dev, req, file_priv);
2333}
2334
2335/* Original addfb only supported RGB formats, so figure out which one */
2336uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2337{
2338	uint32_t fmt;
2339
2340	switch (bpp) {
2341	case 8:
2342		fmt = DRM_FORMAT_C8;
2343		break;
2344	case 16:
2345		if (depth == 15)
2346			fmt = DRM_FORMAT_XRGB1555;
2347		else
2348			fmt = DRM_FORMAT_RGB565;
2349		break;
2350	case 24:
2351		fmt = DRM_FORMAT_RGB888;
2352		break;
2353	case 32:
2354		if (depth == 24)
2355			fmt = DRM_FORMAT_XRGB8888;
2356		else if (depth == 30)
2357			fmt = DRM_FORMAT_XRGB2101010;
2358		else
2359			fmt = DRM_FORMAT_ARGB8888;
2360		break;
2361	default:
2362		DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2363		fmt = DRM_FORMAT_XRGB8888;
2364		break;
2365	}
2366
2367	return fmt;
2368}
2369EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2370
2371/**
2372 * drm_mode_addfb - add an FB to the graphics configuration
2373 * @dev: drm device for the ioctl
2374 * @data: data pointer for the ioctl
2375 * @file_priv: drm file for the ioctl call
2376 *
2377 * Add a new FB to the specified CRTC, given a user request.
2378 *
2379 * Called by the user via ioctl.
2380 *
2381 * RETURNS:
2382 * Zero on success, errno on failure.
2383 */
2384int drm_mode_addfb(struct drm_device *dev,
2385		   void *data, struct drm_file *file_priv)
2386{
2387	struct drm_mode_fb_cmd *or = data;
2388	struct drm_mode_fb_cmd2 r = {};
2389	struct drm_mode_config *config = &dev->mode_config;
2390	struct drm_framebuffer *fb;
2391	int ret = 0;
2392
2393	/* Use new struct with format internally */
2394	r.fb_id = or->fb_id;
2395	r.width = or->width;
2396	r.height = or->height;
2397	r.pitches[0] = or->pitch;
2398	r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2399	r.handles[0] = or->handle;
2400
2401	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2402		return -EINVAL;
2403
2404	if ((config->min_width > r.width) || (r.width > config->max_width))
2405		return -EINVAL;
2406
2407	if ((config->min_height > r.height) || (r.height > config->max_height))
2408		return -EINVAL;
2409
2410	fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2411	if (IS_ERR(fb)) {
2412		DRM_DEBUG_KMS("could not create framebuffer\n");
2413		return PTR_ERR(fb);
2414	}
2415
2416	mutex_lock(&file_priv->fbs_lock);
2417	or->fb_id = fb->base.id;
2418	list_add(&fb->filp_head, &file_priv->fbs);
2419	DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2420	mutex_unlock(&file_priv->fbs_lock);
2421
2422	return ret;
2423}
2424
2425static int format_check(const struct drm_mode_fb_cmd2 *r)
2426{
2427	uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2428
2429	switch (format) {
2430	case DRM_FORMAT_C8:
2431	case DRM_FORMAT_RGB332:
2432	case DRM_FORMAT_BGR233:
2433	case DRM_FORMAT_XRGB4444:
2434	case DRM_FORMAT_XBGR4444:
2435	case DRM_FORMAT_RGBX4444:
2436	case DRM_FORMAT_BGRX4444:
2437	case DRM_FORMAT_ARGB4444:
2438	case DRM_FORMAT_ABGR4444:
2439	case DRM_FORMAT_RGBA4444:
2440	case DRM_FORMAT_BGRA4444:
2441	case DRM_FORMAT_XRGB1555:
2442	case DRM_FORMAT_XBGR1555:
2443	case DRM_FORMAT_RGBX5551:
2444	case DRM_FORMAT_BGRX5551:
2445	case DRM_FORMAT_ARGB1555:
2446	case DRM_FORMAT_ABGR1555:
2447	case DRM_FORMAT_RGBA5551:
2448	case DRM_FORMAT_BGRA5551:
2449	case DRM_FORMAT_RGB565:
2450	case DRM_FORMAT_BGR565:
2451	case DRM_FORMAT_RGB888:
2452	case DRM_FORMAT_BGR888:
2453	case DRM_FORMAT_XRGB8888:
2454	case DRM_FORMAT_XBGR8888:
2455	case DRM_FORMAT_RGBX8888:
2456	case DRM_FORMAT_BGRX8888:
2457	case DRM_FORMAT_ARGB8888:
2458	case DRM_FORMAT_ABGR8888:
2459	case DRM_FORMAT_RGBA8888:
2460	case DRM_FORMAT_BGRA8888:
2461	case DRM_FORMAT_XRGB2101010:
2462	case DRM_FORMAT_XBGR2101010:
2463	case DRM_FORMAT_RGBX1010102:
2464	case DRM_FORMAT_BGRX1010102:
2465	case DRM_FORMAT_ARGB2101010:
2466	case DRM_FORMAT_ABGR2101010:
2467	case DRM_FORMAT_RGBA1010102:
2468	case DRM_FORMAT_BGRA1010102:
2469	case DRM_FORMAT_YUYV:
2470	case DRM_FORMAT_YVYU:
2471	case DRM_FORMAT_UYVY:
2472	case DRM_FORMAT_VYUY:
2473	case DRM_FORMAT_AYUV:
2474	case DRM_FORMAT_NV12:
2475	case DRM_FORMAT_NV21:
2476	case DRM_FORMAT_NV16:
2477	case DRM_FORMAT_NV61:
2478	case DRM_FORMAT_NV24:
2479	case DRM_FORMAT_NV42:
2480	case DRM_FORMAT_YUV410:
2481	case DRM_FORMAT_YVU410:
2482	case DRM_FORMAT_YUV411:
2483	case DRM_FORMAT_YVU411:
2484	case DRM_FORMAT_YUV420:
2485	case DRM_FORMAT_YVU420:
2486	case DRM_FORMAT_YUV422:
2487	case DRM_FORMAT_YVU422:
2488	case DRM_FORMAT_YUV444:
2489	case DRM_FORMAT_YVU444:
2490		return 0;
2491	default:
2492		return -EINVAL;
2493	}
2494}
2495
2496static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2497{
2498	int ret, hsub, vsub, num_planes, i;
2499
2500	ret = format_check(r);
2501	if (ret) {
2502		DRM_DEBUG_KMS("bad framebuffer format %s\n",
2503			      drm_get_format_name(r->pixel_format));
2504		return ret;
2505	}
2506
2507	hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2508	vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2509	num_planes = drm_format_num_planes(r->pixel_format);
2510
2511	if (r->width == 0 || r->width % hsub) {
2512		DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2513		return -EINVAL;
2514	}
2515
2516	if (r->height == 0 || r->height % vsub) {
2517		DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2518		return -EINVAL;
2519	}
2520
2521	for (i = 0; i < num_planes; i++) {
2522		unsigned int width = r->width / (i != 0 ? hsub : 1);
2523		unsigned int height = r->height / (i != 0 ? vsub : 1);
2524		unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2525
2526		if (!r->handles[i]) {
2527			DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2528			return -EINVAL;
2529		}
2530
2531		if ((uint64_t) width * cpp > UINT_MAX)
2532			return -ERANGE;
2533
2534		if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2535			return -ERANGE;
2536
2537		if (r->pitches[i] < width * cpp) {
2538			DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2539			return -EINVAL;
2540		}
2541	}
2542
2543	return 0;
2544}
2545
2546/**
2547 * drm_mode_addfb2 - add an FB to the graphics configuration
2548 * @dev: drm device for the ioctl
2549 * @data: data pointer for the ioctl
2550 * @file_priv: drm file for the ioctl call
2551 *
2552 * Add a new FB to the specified CRTC, given a user request with format.
2553 *
2554 * Called by the user via ioctl.
2555 *
2556 * RETURNS:
2557 * Zero on success, errno on failure.
2558 */
2559int drm_mode_addfb2(struct drm_device *dev,
2560		    void *data, struct drm_file *file_priv)
2561{
2562	struct drm_mode_fb_cmd2 *r = data;
2563	struct drm_mode_config *config = &dev->mode_config;
2564	struct drm_framebuffer *fb;
2565	int ret;
2566
2567	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2568		return -EINVAL;
2569
2570	if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2571		DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2572		return -EINVAL;
2573	}
2574
2575	if ((config->min_width > r->width) || (r->width > config->max_width)) {
2576		DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2577			  r->width, config->min_width, config->max_width);
2578		return -EINVAL;
2579	}
2580	if ((config->min_height > r->height) || (r->height > config->max_height)) {
2581		DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2582			  r->height, config->min_height, config->max_height);
2583		return -EINVAL;
2584	}
2585
2586	ret = framebuffer_check(r);
2587	if (ret)
2588		return ret;
2589
2590	fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2591	if (IS_ERR(fb)) {
2592		DRM_DEBUG_KMS("could not create framebuffer\n");
2593		return PTR_ERR(fb);
2594	}
2595
2596	mutex_lock(&file_priv->fbs_lock);
2597	r->fb_id = fb->base.id;
2598	list_add(&fb->filp_head, &file_priv->fbs);
2599	DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2600	mutex_unlock(&file_priv->fbs_lock);
2601
2602
2603	return ret;
2604}
2605
2606/**
2607 * drm_mode_rmfb - remove an FB from the configuration
2608 * @dev: drm device for the ioctl
2609 * @data: data pointer for the ioctl
2610 * @file_priv: drm file for the ioctl call
2611 *
2612 * Remove the FB specified by the user.
2613 *
2614 * Called by the user via ioctl.
2615 *
2616 * RETURNS:
2617 * Zero on success, errno on failure.
2618 */
2619int drm_mode_rmfb(struct drm_device *dev,
2620		   void *data, struct drm_file *file_priv)
2621{
2622	struct drm_framebuffer *fb = NULL;
2623	struct drm_framebuffer *fbl = NULL;
2624	uint32_t *id = data;
2625	int found = 0;
2626
2627	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2628		return -EINVAL;
2629
2630	mutex_lock(&file_priv->fbs_lock);
2631	mutex_lock(&dev->mode_config.fb_lock);
2632	fb = __drm_framebuffer_lookup(dev, *id);
2633	if (!fb)
2634		goto fail_lookup;
2635
2636	list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2637		if (fb == fbl)
2638			found = 1;
2639	if (!found)
2640		goto fail_lookup;
2641
2642	/* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2643	__drm_framebuffer_unregister(dev, fb);
2644
2645	list_del_init(&fb->filp_head);
2646	mutex_unlock(&dev->mode_config.fb_lock);
2647	mutex_unlock(&file_priv->fbs_lock);
2648
2649	drm_framebuffer_remove(fb);
2650
2651	return 0;
2652
2653fail_lookup:
2654	mutex_unlock(&dev->mode_config.fb_lock);
2655	mutex_unlock(&file_priv->fbs_lock);
2656
2657	return -EINVAL;
2658}
2659
2660/**
2661 * drm_mode_getfb - get FB info
2662 * @dev: drm device for the ioctl
2663 * @data: data pointer for the ioctl
2664 * @file_priv: drm file for the ioctl call
2665 *
2666 * Lookup the FB given its ID and return info about it.
2667 *
2668 * Called by the user via ioctl.
2669 *
2670 * RETURNS:
2671 * Zero on success, errno on failure.
2672 */
2673int drm_mode_getfb(struct drm_device *dev,
2674		   void *data, struct drm_file *file_priv)
2675{
2676	struct drm_mode_fb_cmd *r = data;
2677	struct drm_framebuffer *fb;
2678	int ret;
2679
2680	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2681		return -EINVAL;
2682
2683	fb = drm_framebuffer_lookup(dev, r->fb_id);
2684	if (!fb)
2685		return -EINVAL;
2686
2687	r->height = fb->height;
2688	r->width = fb->width;
2689	r->depth = fb->depth;
2690	r->bpp = fb->bits_per_pixel;
2691	r->pitch = fb->pitches[0];
2692	if (fb->funcs->create_handle) {
2693		if (file_priv->is_master || capable(CAP_SYS_ADMIN)) {
2694			ret = fb->funcs->create_handle(fb, file_priv,
2695						       &r->handle);
2696		} else {
2697			/* GET_FB() is an unprivileged ioctl so we must not
2698			 * return a buffer-handle to non-master processes! For
2699			 * backwards-compatibility reasons, we cannot make
2700			 * GET_FB() privileged, so just return an invalid handle
2701			 * for non-masters. */
2702			r->handle = 0;
2703			ret = 0;
2704		}
2705	} else {
2706		ret = -ENODEV;
2707	}
2708
2709	drm_framebuffer_unreference(fb);
2710
2711	return ret;
2712}
2713
2714int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2715			   void *data, struct drm_file *file_priv)
2716{
2717	struct drm_clip_rect __user *clips_ptr;
2718	struct drm_clip_rect *clips = NULL;
2719	struct drm_mode_fb_dirty_cmd *r = data;
2720	struct drm_framebuffer *fb;
2721	unsigned flags;
2722	int num_clips;
2723	int ret;
2724
2725	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2726		return -EINVAL;
2727
2728	fb = drm_framebuffer_lookup(dev, r->fb_id);
2729	if (!fb)
2730		return -EINVAL;
2731
2732	num_clips = r->num_clips;
2733	clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2734
2735	if (!num_clips != !clips_ptr) {
2736		ret = -EINVAL;
2737		goto out_err1;
2738	}
2739
2740	flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2741
2742	/* If userspace annotates copy, clips must come in pairs */
2743	if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2744		ret = -EINVAL;
2745		goto out_err1;
2746	}
2747
2748	if (num_clips && clips_ptr) {
2749		if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2750			ret = -EINVAL;
2751			goto out_err1;
2752		}
2753		clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2754		if (!clips) {
2755			ret = -ENOMEM;
2756			goto out_err1;
2757		}
2758
2759		ret = copy_from_user(clips, clips_ptr,
2760				     num_clips * sizeof(*clips));
2761		if (ret) {
2762			ret = -EFAULT;
2763			goto out_err2;
2764		}
2765	}
2766
2767	if (fb->funcs->dirty) {
2768		drm_modeset_lock_all(dev);
2769		ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2770				       clips, num_clips);
2771		drm_modeset_unlock_all(dev);
2772	} else {
2773		ret = -ENOSYS;
2774	}
2775
2776out_err2:
2777	kfree(clips);
2778out_err1:
2779	drm_framebuffer_unreference(fb);
2780
2781	return ret;
2782}
2783
2784
2785/**
2786 * drm_fb_release - remove and free the FBs on this file
2787 * @priv: drm file for the ioctl
2788 *
2789 * Destroy all the FBs associated with @filp.
2790 *
2791 * Called by the user via ioctl.
2792 *
2793 * RETURNS:
2794 * Zero on success, errno on failure.
2795 */
2796void drm_fb_release(struct drm_file *priv)
2797{
2798	struct drm_device *dev = priv->minor->dev;
2799	struct drm_framebuffer *fb, *tfb;
2800
2801	mutex_lock(&priv->fbs_lock);
2802	list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2803
2804		mutex_lock(&dev->mode_config.fb_lock);
2805		/* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2806		__drm_framebuffer_unregister(dev, fb);
2807		mutex_unlock(&dev->mode_config.fb_lock);
2808
2809		list_del_init(&fb->filp_head);
2810
2811		/* This will also drop the fpriv->fbs reference. */
2812		drm_framebuffer_remove(fb);
2813	}
2814	mutex_unlock(&priv->fbs_lock);
2815}
2816
2817struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2818					 const char *name, int num_values)
2819{
2820	struct drm_property *property = NULL;
2821	int ret;
2822
2823	property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2824	if (!property)
2825		return NULL;
2826
2827	if (num_values) {
2828		property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2829		if (!property->values)
2830			goto fail;
2831	}
2832
2833	ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2834	if (ret)
2835		goto fail;
2836
2837	property->flags = flags;
2838	property->num_values = num_values;
2839	INIT_LIST_HEAD(&property->enum_blob_list);
2840
2841	if (name) {
2842		strncpy(property->name, name, DRM_PROP_NAME_LEN);
2843		property->name[DRM_PROP_NAME_LEN-1] = '\0';
2844	}
2845
2846	list_add_tail(&property->head, &dev->mode_config.property_list);
2847	return property;
2848fail:
2849	kfree(property->values);
2850	kfree(property);
2851	return NULL;
2852}
2853EXPORT_SYMBOL(drm_property_create);
2854
2855struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2856					 const char *name,
2857					 const struct drm_prop_enum_list *props,
2858					 int num_values)
2859{
2860	struct drm_property *property;
2861	int i, ret;
2862
2863	flags |= DRM_MODE_PROP_ENUM;
2864
2865	property = drm_property_create(dev, flags, name, num_values);
2866	if (!property)
2867		return NULL;
2868
2869	for (i = 0; i < num_values; i++) {
2870		ret = drm_property_add_enum(property, i,
2871				      props[i].type,
2872				      props[i].name);
2873		if (ret) {
2874			drm_property_destroy(dev, property);
2875			return NULL;
2876		}
2877	}
2878
2879	return property;
2880}
2881EXPORT_SYMBOL(drm_property_create_enum);
2882
2883struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2884					 int flags, const char *name,
2885					 const struct drm_prop_enum_list *props,
2886					 int num_values)
2887{
2888	struct drm_property *property;
2889	int i, ret;
2890
2891	flags |= DRM_MODE_PROP_BITMASK;
2892
2893	property = drm_property_create(dev, flags, name, num_values);
2894	if (!property)
2895		return NULL;
2896
2897	for (i = 0; i < num_values; i++) {
2898		ret = drm_property_add_enum(property, i,
2899				      props[i].type,
2900				      props[i].name);
2901		if (ret) {
2902			drm_property_destroy(dev, property);
2903			return NULL;
2904		}
2905	}
2906
2907	return property;
2908}
2909EXPORT_SYMBOL(drm_property_create_bitmask);
2910
2911struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2912					 const char *name,
2913					 uint64_t min, uint64_t max)
2914{
2915	struct drm_property *property;
2916
2917	flags |= DRM_MODE_PROP_RANGE;
2918
2919	property = drm_property_create(dev, flags, name, 2);
2920	if (!property)
2921		return NULL;
2922
2923	property->values[0] = min;
2924	property->values[1] = max;
2925
2926	return property;
2927}
2928EXPORT_SYMBOL(drm_property_create_range);
2929
2930int drm_property_add_enum(struct drm_property *property, int index,
2931			  uint64_t value, const char *name)
2932{
2933	struct drm_property_enum *prop_enum;
2934
2935	if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2936		return -EINVAL;
2937
2938	/*
2939	 * Bitmask enum properties have the additional constraint of values
2940	 * from 0 to 63
2941	 */
2942	if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2943		return -EINVAL;
2944
2945	if (!list_empty(&property->enum_blob_list)) {
2946		list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2947			if (prop_enum->value == value) {
2948				strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2949				prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2950				return 0;
2951			}
2952		}
2953	}
2954
2955	prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2956	if (!prop_enum)
2957		return -ENOMEM;
2958
2959	strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2960	prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2961	prop_enum->value = value;
2962
2963	property->values[index] = value;
2964	list_add_tail(&prop_enum->head, &property->enum_blob_list);
2965	return 0;
2966}
2967EXPORT_SYMBOL(drm_property_add_enum);
2968
2969void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2970{
2971	struct drm_property_enum *prop_enum, *pt;
2972
2973	list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2974		list_del(&prop_enum->head);
2975		kfree(prop_enum);
2976	}
2977
2978	if (property->num_values)
2979		kfree(property->values);
2980	drm_mode_object_put(dev, &property->base);
2981	list_del(&property->head);
2982	kfree(property);
2983}
2984EXPORT_SYMBOL(drm_property_destroy);
2985
2986void drm_object_attach_property(struct drm_mode_object *obj,
2987				struct drm_property *property,
2988				uint64_t init_val)
2989{
2990	int count = obj->properties->count;
2991
2992	if (count == DRM_OBJECT_MAX_PROPERTY) {
2993		WARN(1, "Failed to attach object property (type: 0x%x). Please "
2994			"increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2995			"you see this message on the same object type.\n",
2996			obj->type);
2997		return;
2998	}
2999
3000	obj->properties->ids[count] = property->base.id;
3001	obj->properties->values[count] = init_val;
3002	obj->properties->count++;
3003}
3004EXPORT_SYMBOL(drm_object_attach_property);
3005
3006int drm_object_property_set_value(struct drm_mode_object *obj,
3007				  struct drm_property *property, uint64_t val)
3008{
3009	int i;
3010
3011	for (i = 0; i < obj->properties->count; i++) {
3012		if (obj->properties->ids[i] == property->base.id) {
3013			obj->properties->values[i] = val;
3014			return 0;
3015		}
3016	}
3017
3018	return -EINVAL;
3019}
3020EXPORT_SYMBOL(drm_object_property_set_value);
3021
3022int drm_object_property_get_value(struct drm_mode_object *obj,
3023				  struct drm_property *property, uint64_t *val)
3024{
3025	int i;
3026
3027	for (i = 0; i < obj->properties->count; i++) {
3028		if (obj->properties->ids[i] == property->base.id) {
3029			*val = obj->properties->values[i];
3030			return 0;
3031		}
3032	}
3033
3034	return -EINVAL;
3035}
3036EXPORT_SYMBOL(drm_object_property_get_value);
3037
3038int drm_mode_getproperty_ioctl(struct drm_device *dev,
3039			       void *data, struct drm_file *file_priv)
3040{
3041	struct drm_mode_object *obj;
3042	struct drm_mode_get_property *out_resp = data;
3043	struct drm_property *property;
3044	int enum_count = 0;
3045	int blob_count = 0;
3046	int value_count = 0;
3047	int ret = 0, i;
3048	int copied;
3049	struct drm_property_enum *prop_enum;
3050	struct drm_mode_property_enum __user *enum_ptr;
3051	struct drm_property_blob *prop_blob;
3052	uint32_t __user *blob_id_ptr;
3053	uint64_t __user *values_ptr;
3054	uint32_t __user *blob_length_ptr;
3055
3056	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3057		return -EINVAL;
3058
3059	drm_modeset_lock_all(dev);
3060	obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3061	if (!obj) {
3062		ret = -EINVAL;
3063		goto done;
3064	}
3065	property = obj_to_property(obj);
3066
3067	if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3068		list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3069			enum_count++;
3070	} else if (property->flags & DRM_MODE_PROP_BLOB) {
3071		list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3072			blob_count++;
3073	}
3074
3075	value_count = property->num_values;
3076
3077	strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3078	out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3079	out_resp->flags = property->flags;
3080
3081	if ((out_resp->count_values >= value_count) && value_count) {
3082		values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3083		for (i = 0; i < value_count; i++) {
3084			if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3085				ret = -EFAULT;
3086				goto done;
3087			}
3088		}
3089	}
3090	out_resp->count_values = value_count;
3091
3092	if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3093		if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3094			copied = 0;
3095			enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3096			list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3097
3098				if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3099					ret = -EFAULT;
3100					goto done;
3101				}
3102
3103				if (copy_to_user(&enum_ptr[copied].name,
3104						 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3105					ret = -EFAULT;
3106					goto done;
3107				}
3108				copied++;
3109			}
3110		}
3111		out_resp->count_enum_blobs = enum_count;
3112	}
3113
3114	if (property->flags & DRM_MODE_PROP_BLOB) {
3115		if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3116			copied = 0;
3117			blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3118			blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3119
3120			list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3121				if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3122					ret = -EFAULT;
3123					goto done;
3124				}
3125
3126				if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3127					ret = -EFAULT;
3128					goto done;
3129				}
3130
3131				copied++;
3132			}
3133		}
3134		out_resp->count_enum_blobs = blob_count;
3135	}
3136done:
3137	drm_modeset_unlock_all(dev);
3138	return ret;
3139}
3140
3141static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3142							  void *data)
3143{
3144	struct drm_property_blob *blob;
3145	int ret;
3146
3147	if (!length || !data)
3148		return NULL;
3149
3150	blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3151	if (!blob)
3152		return NULL;
3153
3154	ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3155	if (ret) {
3156		kfree(blob);
3157		return NULL;
3158	}
3159
3160	blob->length = length;
3161
3162	memcpy(blob->data, data, length);
3163
3164	list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3165	return blob;
3166}
3167
3168static void drm_property_destroy_blob(struct drm_device *dev,
3169			       struct drm_property_blob *blob)
3170{
3171	drm_mode_object_put(dev, &blob->base);
3172	list_del(&blob->head);
3173	kfree(blob);
3174}
3175
3176int drm_mode_getblob_ioctl(struct drm_device *dev,
3177			   void *data, struct drm_file *file_priv)
3178{
3179	struct drm_mode_object *obj;
3180	struct drm_mode_get_blob *out_resp = data;
3181	struct drm_property_blob *blob;
3182	int ret = 0;
3183	void __user *blob_ptr;
3184
3185	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3186		return -EINVAL;
3187
3188	drm_modeset_lock_all(dev);
3189	obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3190	if (!obj) {
3191		ret = -EINVAL;
3192		goto done;
3193	}
3194	blob = obj_to_blob(obj);
3195
3196	if (out_resp->length == blob->length) {
3197		blob_ptr = (void __user *)(unsigned long)out_resp->data;
3198		if (copy_to_user(blob_ptr, blob->data, blob->length)){
3199			ret = -EFAULT;
3200			goto done;
3201		}
3202	}
3203	out_resp->length = blob->length;
3204
3205done:
3206	drm_modeset_unlock_all(dev);
3207	return ret;
3208}
3209
3210int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3211					    struct edid *edid)
3212{
3213	struct drm_device *dev = connector->dev;
3214	int ret, size;
3215
3216	if (connector->edid_blob_ptr)
3217		drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3218
3219	/* Delete edid, when there is none. */
3220	if (!edid) {
3221		connector->edid_blob_ptr = NULL;
3222		ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3223		return ret;
3224	}
3225
3226	size = EDID_LENGTH * (1 + edid->extensions);
3227	connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3228							    size, edid);
3229	if (!connector->edid_blob_ptr)
3230		return -EINVAL;
3231
3232	ret = drm_object_property_set_value(&connector->base,
3233					       dev->mode_config.edid_property,
3234					       connector->edid_blob_ptr->base.id);
3235
3236	return ret;
3237}
3238EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3239
3240static bool drm_property_change_is_valid(struct drm_property *property,
3241					 uint64_t value)
3242{
3243	if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3244		return false;
3245	if (property->flags & DRM_MODE_PROP_RANGE) {
3246		if (value < property->values[0] || value > property->values[1])
3247			return false;
3248		return true;
3249	} else if (property->flags & DRM_MODE_PROP_BITMASK) {
3250		int i;
3251		uint64_t valid_mask = 0;
3252		for (i = 0; i < property->num_values; i++)
3253			valid_mask |= (1ULL << property->values[i]);
3254		return !(value & ~valid_mask);
3255	} else if (property->flags & DRM_MODE_PROP_BLOB) {
3256		/* Only the driver knows */
3257		return true;
3258	} else {
3259		int i;
3260		for (i = 0; i < property->num_values; i++)
3261			if (property->values[i] == value)
3262				return true;
3263		return false;
3264	}
3265}
3266
3267int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3268				       void *data, struct drm_file *file_priv)
3269{
3270	struct drm_mode_connector_set_property *conn_set_prop = data;
3271	struct drm_mode_obj_set_property obj_set_prop = {
3272		.value = conn_set_prop->value,
3273		.prop_id = conn_set_prop->prop_id,
3274		.obj_id = conn_set_prop->connector_id,
3275		.obj_type = DRM_MODE_OBJECT_CONNECTOR
3276	};
3277
3278	/* It does all the locking and checking we need */
3279	return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3280}
3281
3282static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3283					   struct drm_property *property,
3284					   uint64_t value)
3285{
3286	int ret = -EINVAL;
3287	struct drm_connector *connector = obj_to_connector(obj);
3288
3289	/* Do DPMS ourselves */
3290	if (property == connector->dev->mode_config.dpms_property) {
3291		if (connector->funcs->dpms)
3292			(*connector->funcs->dpms)(connector, (int)value);
3293		ret = 0;
3294	} else if (connector->funcs->set_property)
3295		ret = connector->funcs->set_property(connector, property, value);
3296
3297	/* store the property value if successful */
3298	if (!ret)
3299		drm_object_property_set_value(&connector->base, property, value);
3300	return ret;
3301}
3302
3303static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3304				      struct drm_property *property,
3305				      uint64_t value)
3306{
3307	int ret = -EINVAL;
3308	struct drm_crtc *crtc = obj_to_crtc(obj);
3309
3310	if (crtc->funcs->set_property)
3311		ret = crtc->funcs->set_property(crtc, property, value);
3312	if (!ret)
3313		drm_object_property_set_value(obj, property, value);
3314
3315	return ret;
3316}
3317
3318static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3319				      struct drm_property *property,
3320				      uint64_t value)
3321{
3322	int ret = -EINVAL;
3323	struct drm_plane *plane = obj_to_plane(obj);
3324
3325	if (plane->funcs->set_property)
3326		ret = plane->funcs->set_property(plane, property, value);
3327	if (!ret)
3328		drm_object_property_set_value(obj, property, value);
3329
3330	return ret;
3331}
3332
3333int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3334				      struct drm_file *file_priv)
3335{
3336	struct drm_mode_obj_get_properties *arg = data;
3337	struct drm_mode_object *obj;
3338	int ret = 0;
3339	int i;
3340	int copied = 0;
3341	int props_count = 0;
3342	uint32_t __user *props_ptr;
3343	uint64_t __user *prop_values_ptr;
3344
3345	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3346		return -EINVAL;
3347
3348	drm_modeset_lock_all(dev);
3349
3350	obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3351	if (!obj) {
3352		ret = -EINVAL;
3353		goto out;
3354	}
3355	if (!obj->properties) {
3356		ret = -EINVAL;
3357		goto out;
3358	}
3359
3360	props_count = obj->properties->count;
3361
3362	/* This ioctl is called twice, once to determine how much space is
3363	 * needed, and the 2nd time to fill it. */
3364	if ((arg->count_props >= props_count) && props_count) {
3365		copied = 0;
3366		props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3367		prop_values_ptr = (uint64_t __user *)(unsigned long)
3368				  (arg->prop_values_ptr);
3369		for (i = 0; i < props_count; i++) {
3370			if (put_user(obj->properties->ids[i],
3371				     props_ptr + copied)) {
3372				ret = -EFAULT;
3373				goto out;
3374			}
3375			if (put_user(obj->properties->values[i],
3376				     prop_values_ptr + copied)) {
3377				ret = -EFAULT;
3378				goto out;
3379			}
3380			copied++;
3381		}
3382	}
3383	arg->count_props = props_count;
3384out:
3385	drm_modeset_unlock_all(dev);
3386	return ret;
3387}
3388
3389int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3390				    struct drm_file *file_priv)
3391{
3392	struct drm_mode_obj_set_property *arg = data;
3393	struct drm_mode_object *arg_obj;
3394	struct drm_mode_object *prop_obj;
3395	struct drm_property *property;
3396	int ret = -EINVAL;
3397	int i;
3398
3399	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3400		return -EINVAL;
3401
3402	drm_modeset_lock_all(dev);
3403
3404	arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3405	if (!arg_obj)
3406		goto out;
3407	if (!arg_obj->properties)
3408		goto out;
3409
3410	for (i = 0; i < arg_obj->properties->count; i++)
3411		if (arg_obj->properties->ids[i] == arg->prop_id)
3412			break;
3413
3414	if (i == arg_obj->properties->count)
3415		goto out;
3416
3417	prop_obj = drm_mode_object_find(dev, arg->prop_id,
3418					DRM_MODE_OBJECT_PROPERTY);
3419	if (!prop_obj)
3420		goto out;
3421	property = obj_to_property(prop_obj);
3422
3423	if (!drm_property_change_is_valid(property, arg->value))
3424		goto out;
3425
3426	switch (arg_obj->type) {
3427	case DRM_MODE_OBJECT_CONNECTOR:
3428		ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3429						      arg->value);
3430		break;
3431	case DRM_MODE_OBJECT_CRTC:
3432		ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3433		break;
3434	case DRM_MODE_OBJECT_PLANE:
3435		ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3436		break;
3437	}
3438
3439out:
3440	drm_modeset_unlock_all(dev);
3441	return ret;
3442}
3443
3444int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3445				      struct drm_encoder *encoder)
3446{
3447	int i;
3448
3449	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3450		if (connector->encoder_ids[i] == 0) {
3451			connector->encoder_ids[i] = encoder->base.id;
3452			return 0;
3453		}
3454	}
3455	return -ENOMEM;
3456}
3457EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3458
3459void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3460				    struct drm_encoder *encoder)
3461{
3462	int i;
3463	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3464		if (connector->encoder_ids[i] == encoder->base.id) {
3465			connector->encoder_ids[i] = 0;
3466			if (connector->encoder == encoder)
3467				connector->encoder = NULL;
3468			break;
3469		}
3470	}
3471}
3472EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3473
3474int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3475				  int gamma_size)
3476{
3477	crtc->gamma_size = gamma_size;
3478
3479	crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3480	if (!crtc->gamma_store) {
3481		crtc->gamma_size = 0;
3482		return -ENOMEM;
3483	}
3484
3485	return 0;
3486}
3487EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3488
3489int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3490			     void *data, struct drm_file *file_priv)
3491{
3492	struct drm_mode_crtc_lut *crtc_lut = data;
3493	struct drm_mode_object *obj;
3494	struct drm_crtc *crtc;
3495	void *r_base, *g_base, *b_base;
3496	int size;
3497	int ret = 0;
3498
3499	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3500		return -EINVAL;
3501
3502	drm_modeset_lock_all(dev);
3503	obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3504	if (!obj) {
3505		ret = -EINVAL;
3506		goto out;
3507	}
3508	crtc = obj_to_crtc(obj);
3509
3510	if (crtc->funcs->gamma_set == NULL) {
3511		ret = -ENOSYS;
3512		goto out;
3513	}
3514
3515	/* memcpy into gamma store */
3516	if (crtc_lut->gamma_size != crtc->gamma_size) {
3517		ret = -EINVAL;
3518		goto out;
3519	}
3520
3521	size = crtc_lut->gamma_size * (sizeof(uint16_t));
3522	r_base = crtc->gamma_store;
3523	if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3524		ret = -EFAULT;
3525		goto out;
3526	}
3527
3528	g_base = r_base + size;
3529	if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3530		ret = -EFAULT;
3531		goto out;
3532	}
3533
3534	b_base = g_base + size;
3535	if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3536		ret = -EFAULT;
3537		goto out;
3538	}
3539
3540	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3541
3542out:
3543	drm_modeset_unlock_all(dev);
3544	return ret;
3545
3546}
3547
3548int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3549			     void *data, struct drm_file *file_priv)
3550{
3551	struct drm_mode_crtc_lut *crtc_lut = data;
3552	struct drm_mode_object *obj;
3553	struct drm_crtc *crtc;
3554	void *r_base, *g_base, *b_base;
3555	int size;
3556	int ret = 0;
3557
3558	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3559		return -EINVAL;
3560
3561	drm_modeset_lock_all(dev);
3562	obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3563	if (!obj) {
3564		ret = -EINVAL;
3565		goto out;
3566	}
3567	crtc = obj_to_crtc(obj);
3568
3569	/* memcpy into gamma store */
3570	if (crtc_lut->gamma_size != crtc->gamma_size) {
3571		ret = -EINVAL;
3572		goto out;
3573	}
3574
3575	size = crtc_lut->gamma_size * (sizeof(uint16_t));
3576	r_base = crtc->gamma_store;
3577	if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3578		ret = -EFAULT;
3579		goto out;
3580	}
3581
3582	g_base = r_base + size;
3583	if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3584		ret = -EFAULT;
3585		goto out;
3586	}
3587
3588	b_base = g_base + size;
3589	if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3590		ret = -EFAULT;
3591		goto out;
3592	}
3593out:
3594	drm_modeset_unlock_all(dev);
3595	return ret;
3596}
3597
3598int drm_mode_page_flip_ioctl(struct drm_device *dev,
3599			     void *data, struct drm_file *file_priv)
3600{
3601	struct drm_mode_crtc_page_flip *page_flip = data;
3602	struct drm_mode_object *obj;
3603	struct drm_crtc *crtc;
3604	struct drm_framebuffer *fb = NULL, *old_fb = NULL;
3605	struct drm_pending_vblank_event *e = NULL;
3606	unsigned long flags;
3607	int ret = -EINVAL;
3608
3609	if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3610	    page_flip->reserved != 0)
3611		return -EINVAL;
3612
3613	if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
3614		return -EINVAL;
3615
3616	obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3617	if (!obj)
3618		return -EINVAL;
3619	crtc = obj_to_crtc(obj);
3620
3621	mutex_lock(&crtc->mutex);
3622	if (crtc->fb == NULL) {
3623		/* The framebuffer is currently unbound, presumably
3624		 * due to a hotplug event, that userspace has not
3625		 * yet discovered.
3626		 */
3627		ret = -EBUSY;
3628		goto out;
3629	}
3630
3631	if (crtc->funcs->page_flip == NULL)
3632		goto out;
3633
3634	fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3635	if (!fb)
3636		goto out;
3637
3638	ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
3639	if (ret)
3640		goto out;
3641
3642	if (crtc->fb->pixel_format != fb->pixel_format) {
3643		DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3644		ret = -EINVAL;
3645		goto out;
3646	}
3647
3648	if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3649		ret = -ENOMEM;
3650		spin_lock_irqsave(&dev->event_lock, flags);
3651		if (file_priv->event_space < sizeof e->event) {
3652			spin_unlock_irqrestore(&dev->event_lock, flags);
3653			goto out;
3654		}
3655		file_priv->event_space -= sizeof e->event;
3656		spin_unlock_irqrestore(&dev->event_lock, flags);
3657
3658		e = kzalloc(sizeof *e, GFP_KERNEL);
3659		if (e == NULL) {
3660			spin_lock_irqsave(&dev->event_lock, flags);
3661			file_priv->event_space += sizeof e->event;
3662			spin_unlock_irqrestore(&dev->event_lock, flags);
3663			goto out;
3664		}
3665
3666		e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3667		e->event.base.length = sizeof e->event;
3668		e->event.user_data = page_flip->user_data;
3669		e->base.event = &e->event.base;
3670		e->base.file_priv = file_priv;
3671		e->base.destroy =
3672			(void (*) (struct drm_pending_event *)) kfree;
3673	}
3674
3675	old_fb = crtc->fb;
3676	ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
3677	if (ret) {
3678		if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3679			spin_lock_irqsave(&dev->event_lock, flags);
3680			file_priv->event_space += sizeof e->event;
3681			spin_unlock_irqrestore(&dev->event_lock, flags);
3682			kfree(e);
3683		}
3684		/* Keep the old fb, don't unref it. */
3685		old_fb = NULL;
3686	} else {
3687		/*
3688		 * Warn if the driver hasn't properly updated the crtc->fb
3689		 * field to reflect that the new framebuffer is now used.
3690		 * Failing to do so will screw with the reference counting
3691		 * on framebuffers.
3692		 */
3693		WARN_ON(crtc->fb != fb);
3694		/* Unref only the old framebuffer. */
3695		fb = NULL;
3696	}
3697
3698out:
3699	if (fb)
3700		drm_framebuffer_unreference(fb);
3701	if (old_fb)
3702		drm_framebuffer_unreference(old_fb);
3703	mutex_unlock(&crtc->mutex);
3704
3705	return ret;
3706}
3707
3708void drm_mode_config_reset(struct drm_device *dev)
3709{
3710	struct drm_crtc *crtc;
3711	struct drm_encoder *encoder;
3712	struct drm_connector *connector;
3713
3714	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3715		if (crtc->funcs->reset)
3716			crtc->funcs->reset(crtc);
3717
3718	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3719		if (encoder->funcs->reset)
3720			encoder->funcs->reset(encoder);
3721
3722	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3723		connector->status = connector_status_unknown;
3724
3725		if (connector->funcs->reset)
3726			connector->funcs->reset(connector);
3727	}
3728}
3729EXPORT_SYMBOL(drm_mode_config_reset);
3730
3731int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3732			       void *data, struct drm_file *file_priv)
3733{
3734	struct drm_mode_create_dumb *args = data;
3735
3736	if (!dev->driver->dumb_create)
3737		return -ENOSYS;
3738	return dev->driver->dumb_create(file_priv, dev, args);
3739}
3740
3741int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3742			     void *data, struct drm_file *file_priv)
3743{
3744	struct drm_mode_map_dumb *args = data;
3745
3746	/* call driver ioctl to get mmap offset */
3747	if (!dev->driver->dumb_map_offset)
3748		return -ENOSYS;
3749
3750	return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3751}
3752
3753int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3754				void *data, struct drm_file *file_priv)
3755{
3756	struct drm_mode_destroy_dumb *args = data;
3757
3758	if (!dev->driver->dumb_destroy)
3759		return -ENOSYS;
3760
3761	return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3762}
3763
3764/*
3765 * Just need to support RGB formats here for compat with code that doesn't
3766 * use pixel formats directly yet.
3767 */
3768void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3769			  int *bpp)
3770{
3771	switch (format) {
3772	case DRM_FORMAT_C8:
3773	case DRM_FORMAT_RGB332:
3774	case DRM_FORMAT_BGR233:
3775		*depth = 8;
3776		*bpp = 8;
3777		break;
3778	case DRM_FORMAT_XRGB1555:
3779	case DRM_FORMAT_XBGR1555:
3780	case DRM_FORMAT_RGBX5551:
3781	case DRM_FORMAT_BGRX5551:
3782	case DRM_FORMAT_ARGB1555:
3783	case DRM_FORMAT_ABGR1555:
3784	case DRM_FORMAT_RGBA5551:
3785	case DRM_FORMAT_BGRA5551:
3786		*depth = 15;
3787		*bpp = 16;
3788		break;
3789	case DRM_FORMAT_RGB565:
3790	case DRM_FORMAT_BGR565:
3791		*depth = 16;
3792		*bpp = 16;
3793		break;
3794	case DRM_FORMAT_RGB888:
3795	case DRM_FORMAT_BGR888:
3796		*depth = 24;
3797		*bpp = 24;
3798		break;
3799	case DRM_FORMAT_XRGB8888:
3800	case DRM_FORMAT_XBGR8888:
3801	case DRM_FORMAT_RGBX8888:
3802	case DRM_FORMAT_BGRX8888:
3803		*depth = 24;
3804		*bpp = 32;
3805		break;
3806	case DRM_FORMAT_XRGB2101010:
3807	case DRM_FORMAT_XBGR2101010:
3808	case DRM_FORMAT_RGBX1010102:
3809	case DRM_FORMAT_BGRX1010102:
3810	case DRM_FORMAT_ARGB2101010:
3811	case DRM_FORMAT_ABGR2101010:
3812	case DRM_FORMAT_RGBA1010102:
3813	case DRM_FORMAT_BGRA1010102:
3814		*depth = 30;
3815		*bpp = 32;
3816		break;
3817	case DRM_FORMAT_ARGB8888:
3818	case DRM_FORMAT_ABGR8888:
3819	case DRM_FORMAT_RGBA8888:
3820	case DRM_FORMAT_BGRA8888:
3821		*depth = 32;
3822		*bpp = 32;
3823		break;
3824	default:
3825		DRM_DEBUG_KMS("unsupported pixel format\n");
3826		*depth = 0;
3827		*bpp = 0;
3828		break;
3829	}
3830}
3831EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3832
3833/**
3834 * drm_format_num_planes - get the number of planes for format
3835 * @format: pixel format (DRM_FORMAT_*)
3836 *
3837 * RETURNS:
3838 * The number of planes used by the specified pixel format.
3839 */
3840int drm_format_num_planes(uint32_t format)
3841{
3842	switch (format) {
3843	case DRM_FORMAT_YUV410:
3844	case DRM_FORMAT_YVU410:
3845	case DRM_FORMAT_YUV411:
3846	case DRM_FORMAT_YVU411:
3847	case DRM_FORMAT_YUV420:
3848	case DRM_FORMAT_YVU420:
3849	case DRM_FORMAT_YUV422:
3850	case DRM_FORMAT_YVU422:
3851	case DRM_FORMAT_YUV444:
3852	case DRM_FORMAT_YVU444:
3853		return 3;
3854	case DRM_FORMAT_NV12:
3855	case DRM_FORMAT_NV21:
3856	case DRM_FORMAT_NV16:
3857	case DRM_FORMAT_NV61:
3858	case DRM_FORMAT_NV24:
3859	case DRM_FORMAT_NV42:
3860		return 2;
3861	default:
3862		return 1;
3863	}
3864}
3865EXPORT_SYMBOL(drm_format_num_planes);
3866
3867/**
3868 * drm_format_plane_cpp - determine the bytes per pixel value
3869 * @format: pixel format (DRM_FORMAT_*)
3870 * @plane: plane index
3871 *
3872 * RETURNS:
3873 * The bytes per pixel value for the specified plane.
3874 */
3875int drm_format_plane_cpp(uint32_t format, int plane)
3876{
3877	unsigned int depth;
3878	int bpp;
3879
3880	if (plane >= drm_format_num_planes(format))
3881		return 0;
3882
3883	switch (format) {
3884	case DRM_FORMAT_YUYV:
3885	case DRM_FORMAT_YVYU:
3886	case DRM_FORMAT_UYVY:
3887	case DRM_FORMAT_VYUY:
3888		return 2;
3889	case DRM_FORMAT_NV12:
3890	case DRM_FORMAT_NV21:
3891	case DRM_FORMAT_NV16:
3892	case DRM_FORMAT_NV61:
3893	case DRM_FORMAT_NV24:
3894	case DRM_FORMAT_NV42:
3895		return plane ? 2 : 1;
3896	case DRM_FORMAT_YUV410:
3897	case DRM_FORMAT_YVU410:
3898	case DRM_FORMAT_YUV411:
3899	case DRM_FORMAT_YVU411:
3900	case DRM_FORMAT_YUV420:
3901	case DRM_FORMAT_YVU420:
3902	case DRM_FORMAT_YUV422:
3903	case DRM_FORMAT_YVU422:
3904	case DRM_FORMAT_YUV444:
3905	case DRM_FORMAT_YVU444:
3906		return 1;
3907	default:
3908		drm_fb_get_bpp_depth(format, &depth, &bpp);
3909		return bpp >> 3;
3910	}
3911}
3912EXPORT_SYMBOL(drm_format_plane_cpp);
3913
3914/**
3915 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3916 * @format: pixel format (DRM_FORMAT_*)
3917 *
3918 * RETURNS:
3919 * The horizontal chroma subsampling factor for the
3920 * specified pixel format.
3921 */
3922int drm_format_horz_chroma_subsampling(uint32_t format)
3923{
3924	switch (format) {
3925	case DRM_FORMAT_YUV411:
3926	case DRM_FORMAT_YVU411:
3927	case DRM_FORMAT_YUV410:
3928	case DRM_FORMAT_YVU410:
3929		return 4;
3930	case DRM_FORMAT_YUYV:
3931	case DRM_FORMAT_YVYU:
3932	case DRM_FORMAT_UYVY:
3933	case DRM_FORMAT_VYUY:
3934	case DRM_FORMAT_NV12:
3935	case DRM_FORMAT_NV21:
3936	case DRM_FORMAT_NV16:
3937	case DRM_FORMAT_NV61:
3938	case DRM_FORMAT_YUV422:
3939	case DRM_FORMAT_YVU422:
3940	case DRM_FORMAT_YUV420:
3941	case DRM_FORMAT_YVU420:
3942		return 2;
3943	default:
3944		return 1;
3945	}
3946}
3947EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3948
3949/**
3950 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3951 * @format: pixel format (DRM_FORMAT_*)
3952 *
3953 * RETURNS:
3954 * The vertical chroma subsampling factor for the
3955 * specified pixel format.
3956 */
3957int drm_format_vert_chroma_subsampling(uint32_t format)
3958{
3959	switch (format) {
3960	case DRM_FORMAT_YUV410:
3961	case DRM_FORMAT_YVU410:
3962		return 4;
3963	case DRM_FORMAT_YUV420:
3964	case DRM_FORMAT_YVU420:
3965	case DRM_FORMAT_NV12:
3966	case DRM_FORMAT_NV21:
3967		return 2;
3968	default:
3969		return 1;
3970	}
3971}
3972EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
3973
3974/**
3975 * drm_mode_config_init - initialize DRM mode_configuration structure
3976 * @dev: DRM device
3977 *
3978 * Initialize @dev's mode_config structure, used for tracking the graphics
3979 * configuration of @dev.
3980 *
3981 * Since this initializes the modeset locks, no locking is possible. Which is no
3982 * problem, since this should happen single threaded at init time. It is the
3983 * driver's problem to ensure this guarantee.
3984 *
3985 */
3986void drm_mode_config_init(struct drm_device *dev)
3987{
3988	mutex_init(&dev->mode_config.mutex);
3989	mutex_init(&dev->mode_config.idr_mutex);
3990	mutex_init(&dev->mode_config.fb_lock);
3991	INIT_LIST_HEAD(&dev->mode_config.fb_list);
3992	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3993	INIT_LIST_HEAD(&dev->mode_config.connector_list);
3994	INIT_LIST_HEAD(&dev->mode_config.bridge_list);
3995	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3996	INIT_LIST_HEAD(&dev->mode_config.property_list);
3997	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3998	INIT_LIST_HEAD(&dev->mode_config.plane_list);
3999	idr_init(&dev->mode_config.crtc_idr);
4000
4001	drm_modeset_lock_all(dev);
4002	drm_mode_create_standard_connector_properties(dev);
4003	drm_modeset_unlock_all(dev);
4004
4005	/* Just to be sure */
4006	dev->mode_config.num_fb = 0;
4007	dev->mode_config.num_connector = 0;
4008	dev->mode_config.num_crtc = 0;
4009	dev->mode_config.num_encoder = 0;
4010}
4011EXPORT_SYMBOL(drm_mode_config_init);
4012
4013/**
4014 * drm_mode_config_cleanup - free up DRM mode_config info
4015 * @dev: DRM device
4016 *
4017 * Free up all the connectors and CRTCs associated with this DRM device, then
4018 * free up the framebuffers and associated buffer objects.
4019 *
4020 * Note that since this /should/ happen single-threaded at driver/device
4021 * teardown time, no locking is required. It's the driver's job to ensure that
4022 * this guarantee actually holds true.
4023 *
4024 * FIXME: cleanup any dangling user buffer objects too
4025 */
4026void drm_mode_config_cleanup(struct drm_device *dev)
4027{
4028	struct drm_connector *connector, *ot;
4029	struct drm_crtc *crtc, *ct;
4030	struct drm_encoder *encoder, *enct;
4031	struct drm_bridge *bridge, *brt;
4032	struct drm_framebuffer *fb, *fbt;
4033	struct drm_property *property, *pt;
4034	struct drm_property_blob *blob, *bt;
4035	struct drm_plane *plane, *plt;
4036
4037	list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4038				 head) {
4039		encoder->funcs->destroy(encoder);
4040	}
4041
4042	list_for_each_entry_safe(bridge, brt,
4043				 &dev->mode_config.bridge_list, head) {
4044		bridge->funcs->destroy(bridge);
4045	}
4046
4047	list_for_each_entry_safe(connector, ot,
4048				 &dev->mode_config.connector_list, head) {
4049		connector->funcs->destroy(connector);
4050	}
4051
4052	list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4053				 head) {
4054		drm_property_destroy(dev, property);
4055	}
4056
4057	list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4058				 head) {
4059		drm_property_destroy_blob(dev, blob);
4060	}
4061
4062	/*
4063	 * Single-threaded teardown context, so it's not required to grab the
4064	 * fb_lock to protect against concurrent fb_list access. Contrary, it
4065	 * would actually deadlock with the drm_framebuffer_cleanup function.
4066	 *
4067	 * Also, if there are any framebuffers left, that's a driver leak now,
4068	 * so politely WARN about this.
4069	 */
4070	WARN_ON(!list_empty(&dev->mode_config.fb_list));
4071	list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4072		drm_framebuffer_remove(fb);
4073	}
4074
4075	list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4076				 head) {
4077		plane->funcs->destroy(plane);
4078	}
4079
4080	list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4081		crtc->funcs->destroy(crtc);
4082	}
4083
4084	idr_destroy(&dev->mode_config.crtc_idr);
4085}
4086EXPORT_SYMBOL(drm_mode_config_cleanup);
4087