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