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