[go: nahoru, domu]

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