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