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