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