Home | History | Annotate | Line # | Download | only in drm
drm_crtc.c revision 1.11.6.1
      1  1.11.6.1        ad /*	$NetBSD: drm_crtc.c,v 1.11.6.1 2020/02/29 20:20:13 ad Exp $	*/
      2       1.5  riastrad 
      3       1.1  riastrad /*
      4       1.1  riastrad  * Copyright (c) 2006-2008 Intel Corporation
      5       1.1  riastrad  * Copyright (c) 2007 Dave Airlie <airlied (at) linux.ie>
      6       1.1  riastrad  * Copyright (c) 2008 Red Hat Inc.
      7       1.1  riastrad  *
      8       1.1  riastrad  * DRM core CRTC related functions
      9       1.1  riastrad  *
     10       1.1  riastrad  * Permission to use, copy, modify, distribute, and sell this software and its
     11       1.1  riastrad  * documentation for any purpose is hereby granted without fee, provided that
     12       1.1  riastrad  * the above copyright notice appear in all copies and that both that copyright
     13       1.1  riastrad  * notice and this permission notice appear in supporting documentation, and
     14       1.1  riastrad  * that the name of the copyright holders not be used in advertising or
     15       1.1  riastrad  * publicity pertaining to distribution of the software without specific,
     16       1.1  riastrad  * written prior permission.  The copyright holders make no representations
     17       1.1  riastrad  * about the suitability of this software for any purpose.  It is provided "as
     18       1.1  riastrad  * is" without express or implied warranty.
     19       1.1  riastrad  *
     20       1.1  riastrad  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     21       1.1  riastrad  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
     22       1.1  riastrad  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
     23       1.1  riastrad  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
     24       1.1  riastrad  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
     25       1.1  riastrad  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
     26       1.1  riastrad  * OF THIS SOFTWARE.
     27       1.1  riastrad  *
     28       1.1  riastrad  * Authors:
     29       1.1  riastrad  *      Keith Packard
     30       1.1  riastrad  *	Eric Anholt <eric (at) anholt.net>
     31       1.1  riastrad  *      Dave Airlie <airlied (at) linux.ie>
     32       1.1  riastrad  *      Jesse Barnes <jesse.barnes (at) intel.com>
     33       1.1  riastrad  */
     34       1.5  riastrad #include <sys/cdefs.h>
     35  1.11.6.1        ad __KERNEL_RCSID(0, "$NetBSD: drm_crtc.c,v 1.11.6.1 2020/02/29 20:20:13 ad Exp $");
     36       1.5  riastrad 
     37       1.3  riastrad #include <linux/ctype.h>
     38       1.1  riastrad #include <linux/list.h>
     39       1.1  riastrad #include <linux/slab.h>
     40       1.1  riastrad #include <linux/export.h>
     41       1.1  riastrad #include <drm/drmP.h>
     42       1.1  riastrad #include <drm/drm_crtc.h>
     43       1.1  riastrad #include <drm/drm_edid.h>
     44       1.1  riastrad #include <drm/drm_fourcc.h>
     45       1.5  riastrad #include <drm/drm_modeset_lock.h>
     46       1.5  riastrad #include <drm/drm_atomic.h>
     47       1.1  riastrad 
     48       1.3  riastrad #include "drm_crtc_internal.h"
     49       1.5  riastrad #include "drm_internal.h"
     50       1.3  riastrad 
     51  1.11.6.1        ad #include <linux/nbsd-namespace.h>
     52  1.11.6.1        ad 
     53       1.5  riastrad static struct drm_framebuffer *
     54       1.5  riastrad internal_framebuffer_create(struct drm_device *dev,
     55       1.5  riastrad 			    struct drm_mode_fb_cmd2 *r,
     56       1.5  riastrad 			    struct drm_file *file_priv);
     57       1.3  riastrad 
     58       1.1  riastrad /* Avoid boilerplate.  I'm tired of typing. */
     59       1.1  riastrad #define DRM_ENUM_NAME_FN(fnname, list)				\
     60       1.2  riastrad 	const char *fnname(int val)				\
     61       1.1  riastrad 	{							\
     62       1.1  riastrad 		int i;						\
     63       1.1  riastrad 		for (i = 0; i < ARRAY_SIZE(list); i++) {	\
     64       1.1  riastrad 			if (list[i].type == val)		\
     65       1.1  riastrad 				return list[i].name;		\
     66       1.1  riastrad 		}						\
     67       1.1  riastrad 		return "(unknown)";				\
     68       1.1  riastrad 	}
     69       1.1  riastrad 
     70       1.1  riastrad /*
     71       1.1  riastrad  * Global properties
     72       1.1  riastrad  */
     73       1.5  riastrad static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
     74       1.5  riastrad 	{ DRM_MODE_DPMS_ON, "On" },
     75       1.1  riastrad 	{ DRM_MODE_DPMS_STANDBY, "Standby" },
     76       1.1  riastrad 	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
     77       1.1  riastrad 	{ DRM_MODE_DPMS_OFF, "Off" }
     78       1.1  riastrad };
     79       1.1  riastrad 
     80       1.1  riastrad DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
     81       1.1  riastrad 
     82       1.5  riastrad static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
     83       1.3  riastrad 	{ DRM_PLANE_TYPE_OVERLAY, "Overlay" },
     84       1.3  riastrad 	{ DRM_PLANE_TYPE_PRIMARY, "Primary" },
     85       1.3  riastrad 	{ DRM_PLANE_TYPE_CURSOR, "Cursor" },
     86       1.3  riastrad };
     87       1.3  riastrad 
     88       1.1  riastrad /*
     89       1.1  riastrad  * Optional properties
     90       1.1  riastrad  */
     91       1.5  riastrad static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
     92       1.1  riastrad 	{ DRM_MODE_SCALE_NONE, "None" },
     93       1.1  riastrad 	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
     94       1.1  riastrad 	{ DRM_MODE_SCALE_CENTER, "Center" },
     95       1.1  riastrad 	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
     96       1.1  riastrad };
     97       1.1  riastrad 
     98       1.5  riastrad static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
     99       1.5  riastrad 	{ DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
    100       1.5  riastrad 	{ DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
    101       1.5  riastrad 	{ DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
    102       1.5  riastrad };
    103       1.5  riastrad 
    104       1.1  riastrad /*
    105       1.1  riastrad  * Non-global properties, but "required" for certain connectors.
    106       1.1  riastrad  */
    107       1.5  riastrad static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
    108       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
    109       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
    110       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
    111       1.1  riastrad };
    112       1.1  riastrad 
    113       1.1  riastrad DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
    114       1.1  riastrad 
    115       1.5  riastrad static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
    116       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
    117       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
    118       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
    119       1.1  riastrad };
    120       1.1  riastrad 
    121       1.1  riastrad DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
    122       1.1  riastrad 		 drm_dvi_i_subconnector_enum_list)
    123       1.1  riastrad 
    124       1.5  riastrad static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
    125       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
    126       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
    127       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
    128       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
    129       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
    130       1.1  riastrad };
    131       1.1  riastrad 
    132       1.1  riastrad DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
    133       1.1  riastrad 
    134       1.5  riastrad static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
    135       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
    136       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
    137       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
    138       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
    139       1.1  riastrad 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
    140       1.1  riastrad };
    141       1.1  riastrad 
    142       1.1  riastrad DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
    143       1.1  riastrad 		 drm_tv_subconnector_enum_list)
    144       1.1  riastrad 
    145       1.3  riastrad static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
    146       1.1  riastrad 	{ DRM_MODE_DIRTY_OFF,      "Off"      },
    147       1.1  riastrad 	{ DRM_MODE_DIRTY_ON,       "On"       },
    148       1.1  riastrad 	{ DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
    149       1.1  riastrad };
    150       1.1  riastrad 
    151       1.1  riastrad struct drm_conn_prop_enum_list {
    152       1.1  riastrad 	int type;
    153       1.2  riastrad 	const char *name;
    154       1.3  riastrad 	struct ida ida;
    155       1.1  riastrad };
    156       1.1  riastrad 
    157       1.1  riastrad /*
    158       1.1  riastrad  * Connector and encoder types.
    159       1.1  riastrad  */
    160       1.5  riastrad static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
    161       1.5  riastrad 	{ DRM_MODE_CONNECTOR_Unknown, "Unknown" },
    162       1.3  riastrad 	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
    163       1.3  riastrad 	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
    164       1.3  riastrad 	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
    165       1.3  riastrad 	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
    166       1.3  riastrad 	{ DRM_MODE_CONNECTOR_Composite, "Composite" },
    167       1.3  riastrad 	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
    168       1.3  riastrad 	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
    169       1.3  riastrad 	{ DRM_MODE_CONNECTOR_Component, "Component" },
    170       1.3  riastrad 	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
    171       1.3  riastrad 	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
    172       1.3  riastrad 	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
    173       1.3  riastrad 	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
    174       1.3  riastrad 	{ DRM_MODE_CONNECTOR_TV, "TV" },
    175       1.3  riastrad 	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
    176       1.3  riastrad 	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
    177       1.3  riastrad 	{ DRM_MODE_CONNECTOR_DSI, "DSI" },
    178       1.1  riastrad };
    179       1.1  riastrad 
    180       1.5  riastrad static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
    181       1.5  riastrad 	{ DRM_MODE_ENCODER_NONE, "None" },
    182       1.1  riastrad 	{ DRM_MODE_ENCODER_DAC, "DAC" },
    183       1.1  riastrad 	{ DRM_MODE_ENCODER_TMDS, "TMDS" },
    184       1.1  riastrad 	{ DRM_MODE_ENCODER_LVDS, "LVDS" },
    185       1.1  riastrad 	{ DRM_MODE_ENCODER_TVDAC, "TV" },
    186       1.1  riastrad 	{ DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
    187       1.3  riastrad 	{ DRM_MODE_ENCODER_DSI, "DSI" },
    188       1.5  riastrad 	{ DRM_MODE_ENCODER_DPMST, "DP MST" },
    189       1.3  riastrad };
    190       1.3  riastrad 
    191       1.5  riastrad static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
    192       1.3  riastrad 	{ SubPixelUnknown, "Unknown" },
    193       1.3  riastrad 	{ SubPixelHorizontalRGB, "Horizontal RGB" },
    194       1.3  riastrad 	{ SubPixelHorizontalBGR, "Horizontal BGR" },
    195       1.3  riastrad 	{ SubPixelVerticalRGB, "Vertical RGB" },
    196       1.3  riastrad 	{ SubPixelVerticalBGR, "Vertical BGR" },
    197       1.3  riastrad 	{ SubPixelNone, "None" },
    198       1.1  riastrad };
    199       1.1  riastrad 
    200       1.3  riastrad void drm_connector_ida_init(void)
    201       1.3  riastrad {
    202       1.3  riastrad 	int i;
    203       1.3  riastrad 
    204       1.3  riastrad 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
    205       1.3  riastrad 		ida_init(&drm_connector_enum_list[i].ida);
    206       1.3  riastrad }
    207       1.3  riastrad 
    208       1.3  riastrad void drm_connector_ida_destroy(void)
    209       1.3  riastrad {
    210       1.3  riastrad 	int i;
    211       1.3  riastrad 
    212       1.3  riastrad 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
    213       1.3  riastrad 		ida_destroy(&drm_connector_enum_list[i].ida);
    214       1.3  riastrad }
    215       1.3  riastrad 
    216       1.3  riastrad /**
    217       1.3  riastrad  * drm_get_connector_status_name - return a string for connector status
    218       1.3  riastrad  * @status: connector status to compute name of
    219       1.3  riastrad  *
    220       1.3  riastrad  * In contrast to the other drm_get_*_name functions this one here returns a
    221       1.3  riastrad  * const pointer and hence is threadsafe.
    222       1.3  riastrad  */
    223       1.3  riastrad const char *drm_get_connector_status_name(enum drm_connector_status status)
    224       1.1  riastrad {
    225       1.1  riastrad 	if (status == connector_status_connected)
    226       1.1  riastrad 		return "connected";
    227       1.1  riastrad 	else if (status == connector_status_disconnected)
    228       1.1  riastrad 		return "disconnected";
    229       1.1  riastrad 	else
    230       1.1  riastrad 		return "unknown";
    231       1.1  riastrad }
    232       1.3  riastrad EXPORT_SYMBOL(drm_get_connector_status_name);
    233       1.3  riastrad 
    234       1.3  riastrad /**
    235       1.3  riastrad  * drm_get_subpixel_order_name - return a string for a given subpixel enum
    236       1.3  riastrad  * @order: enum of subpixel_order
    237       1.3  riastrad  *
    238       1.3  riastrad  * Note you could abuse this and return something out of bounds, but that
    239       1.3  riastrad  * would be a caller error.  No unscrubbed user data should make it here.
    240       1.3  riastrad  */
    241       1.3  riastrad const char *drm_get_subpixel_order_name(enum subpixel_order order)
    242       1.3  riastrad {
    243       1.3  riastrad 	return drm_subpixel_enum_list[order].name;
    244       1.3  riastrad }
    245       1.3  riastrad EXPORT_SYMBOL(drm_get_subpixel_order_name);
    246       1.3  riastrad 
    247       1.3  riastrad static char printable_char(int c)
    248       1.3  riastrad {
    249       1.3  riastrad 	return isascii(c) && isprint(c) ? c : '?';
    250       1.3  riastrad }
    251       1.3  riastrad 
    252       1.3  riastrad /**
    253       1.3  riastrad  * drm_get_format_name - return a string for drm fourcc format
    254       1.3  riastrad  * @format: format to compute name of
    255       1.3  riastrad  *
    256       1.3  riastrad  * Note that the buffer used by this function is globally shared and owned by
    257       1.3  riastrad  * the function itself.
    258       1.3  riastrad  *
    259       1.3  riastrad  * FIXME: This isn't really multithreading safe.
    260       1.3  riastrad  */
    261       1.3  riastrad const char *drm_get_format_name(uint32_t format)
    262       1.3  riastrad {
    263       1.3  riastrad 	static char buf[32];
    264       1.3  riastrad 
    265       1.3  riastrad 	snprintf(buf, sizeof(buf),
    266       1.3  riastrad 		 "%c%c%c%c %s-endian (0x%08x)",
    267       1.3  riastrad 		 printable_char(format & 0xff),
    268       1.3  riastrad 		 printable_char((format >> 8) & 0xff),
    269       1.3  riastrad 		 printable_char((format >> 16) & 0xff),
    270       1.3  riastrad 		 printable_char((format >> 24) & 0x7f),
    271       1.3  riastrad 		 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
    272       1.3  riastrad 		 format);
    273       1.3  riastrad 
    274       1.3  riastrad 	return buf;
    275       1.3  riastrad }
    276       1.3  riastrad EXPORT_SYMBOL(drm_get_format_name);
    277       1.1  riastrad 
    278       1.5  riastrad /*
    279       1.5  riastrad  * Internal function to assign a slot in the object idr and optionally
    280       1.5  riastrad  * register the object into the idr.
    281       1.5  riastrad  */
    282       1.5  riastrad static int drm_mode_object_get_reg(struct drm_device *dev,
    283       1.5  riastrad 				   struct drm_mode_object *obj,
    284       1.5  riastrad 				   uint32_t obj_type,
    285       1.5  riastrad 				   bool register_obj)
    286       1.5  riastrad {
    287       1.5  riastrad 	int ret;
    288       1.5  riastrad 
    289       1.5  riastrad 	idr_preload(GFP_KERNEL);
    290       1.5  riastrad 	mutex_lock(&dev->mode_config.idr_mutex);
    291       1.5  riastrad 	ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
    292       1.5  riastrad 	if (ret >= 0) {
    293       1.5  riastrad 		/*
    294       1.5  riastrad 		 * Set up the object linking under the protection of the idr
    295       1.5  riastrad 		 * lock so that other users can't see inconsistent state.
    296       1.5  riastrad 		 */
    297       1.5  riastrad 		obj->id = ret;
    298       1.5  riastrad 		obj->type = obj_type;
    299       1.5  riastrad 	}
    300       1.5  riastrad 	mutex_unlock(&dev->mode_config.idr_mutex);
    301       1.5  riastrad 	idr_preload_end();
    302       1.5  riastrad 
    303       1.5  riastrad 	return ret < 0 ? ret : 0;
    304       1.5  riastrad }
    305       1.5  riastrad 
    306       1.1  riastrad /**
    307       1.3  riastrad  * drm_mode_object_get - allocate a new modeset identifier
    308       1.1  riastrad  * @dev: DRM device
    309       1.3  riastrad  * @obj: object pointer, used to generate unique ID
    310       1.3  riastrad  * @obj_type: object type
    311       1.1  riastrad  *
    312       1.1  riastrad  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
    313       1.3  riastrad  * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
    314       1.3  riastrad  * modeset identifiers are _not_ reference counted. Hence don't use this for
    315       1.3  riastrad  * reference counted modeset objects like framebuffers.
    316       1.1  riastrad  *
    317       1.3  riastrad  * Returns:
    318       1.5  riastrad  * Zero on success, error code on failure.
    319       1.1  riastrad  */
    320       1.3  riastrad int drm_mode_object_get(struct drm_device *dev,
    321       1.3  riastrad 			struct drm_mode_object *obj, uint32_t obj_type)
    322       1.1  riastrad {
    323       1.5  riastrad 	return drm_mode_object_get_reg(dev, obj, obj_type, true);
    324       1.5  riastrad }
    325       1.1  riastrad 
    326       1.5  riastrad static void drm_mode_object_register(struct drm_device *dev,
    327       1.5  riastrad 				     struct drm_mode_object *obj)
    328       1.5  riastrad {
    329       1.3  riastrad 	mutex_lock(&dev->mode_config.idr_mutex);
    330       1.5  riastrad 	idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
    331       1.1  riastrad 	mutex_unlock(&dev->mode_config.idr_mutex);
    332       1.1  riastrad }
    333       1.1  riastrad 
    334       1.1  riastrad /**
    335       1.3  riastrad  * drm_mode_object_put - free a modeset identifer
    336       1.1  riastrad  * @dev: DRM device
    337       1.3  riastrad  * @object: object to free
    338       1.1  riastrad  *
    339       1.3  riastrad  * Free @id from @dev's unique identifier pool. Note that despite the _get
    340       1.3  riastrad  * postfix modeset identifiers are _not_ reference counted. Hence don't use this
    341       1.3  riastrad  * for reference counted modeset objects like framebuffers.
    342       1.1  riastrad  */
    343       1.3  riastrad void drm_mode_object_put(struct drm_device *dev,
    344       1.3  riastrad 			 struct drm_mode_object *object)
    345       1.1  riastrad {
    346       1.1  riastrad 	mutex_lock(&dev->mode_config.idr_mutex);
    347       1.1  riastrad 	idr_remove(&dev->mode_config.crtc_idr, object->id);
    348       1.1  riastrad 	mutex_unlock(&dev->mode_config.idr_mutex);
    349       1.1  riastrad }
    350       1.1  riastrad 
    351       1.5  riastrad static struct drm_mode_object *_object_find(struct drm_device *dev,
    352       1.5  riastrad 		uint32_t id, uint32_t type)
    353       1.5  riastrad {
    354       1.5  riastrad 	struct drm_mode_object *obj = NULL;
    355       1.5  riastrad 
    356       1.5  riastrad 	mutex_lock(&dev->mode_config.idr_mutex);
    357       1.5  riastrad 	obj = idr_find(&dev->mode_config.crtc_idr, id);
    358       1.5  riastrad 	if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
    359       1.5  riastrad 		obj = NULL;
    360       1.5  riastrad 	if (obj && obj->id != id)
    361       1.5  riastrad 		obj = NULL;
    362       1.5  riastrad 	/* don't leak out unref'd fb's */
    363       1.5  riastrad 	if (obj &&
    364       1.5  riastrad 	    (obj->type == DRM_MODE_OBJECT_FB ||
    365       1.5  riastrad 	     obj->type == DRM_MODE_OBJECT_BLOB))
    366       1.5  riastrad 		obj = NULL;
    367       1.5  riastrad 	mutex_unlock(&dev->mode_config.idr_mutex);
    368       1.5  riastrad 
    369       1.5  riastrad 	return obj;
    370       1.5  riastrad }
    371       1.5  riastrad 
    372       1.3  riastrad /**
    373       1.3  riastrad  * drm_mode_object_find - look up a drm object with static lifetime
    374       1.3  riastrad  * @dev: drm device
    375       1.3  riastrad  * @id: id of the mode object
    376       1.3  riastrad  * @type: type of the mode object
    377       1.3  riastrad  *
    378       1.3  riastrad  * Note that framebuffers cannot be looked up with this functions - since those
    379       1.5  riastrad  * are reference counted, they need special treatment.  Even with
    380       1.5  riastrad  * DRM_MODE_OBJECT_ANY (although that will simply return NULL
    381       1.5  riastrad  * rather than WARN_ON()).
    382       1.3  riastrad  */
    383       1.1  riastrad struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
    384       1.1  riastrad 		uint32_t id, uint32_t type)
    385       1.1  riastrad {
    386       1.1  riastrad 	struct drm_mode_object *obj = NULL;
    387       1.1  riastrad 
    388       1.3  riastrad 	/* Framebuffers are reference counted and need their own lookup
    389       1.3  riastrad 	 * function.*/
    390       1.5  riastrad 	WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
    391       1.5  riastrad 	obj = _object_find(dev, id, type);
    392       1.1  riastrad 	return obj;
    393       1.1  riastrad }
    394       1.1  riastrad EXPORT_SYMBOL(drm_mode_object_find);
    395       1.1  riastrad 
    396       1.1  riastrad /**
    397       1.1  riastrad  * drm_framebuffer_init - initialize a framebuffer
    398       1.1  riastrad  * @dev: DRM device
    399       1.3  riastrad  * @fb: framebuffer to be initialized
    400       1.3  riastrad  * @funcs: ... with these functions
    401       1.1  riastrad  *
    402       1.1  riastrad  * Allocates an ID for the framebuffer's parent mode object, sets its mode
    403       1.1  riastrad  * functions & device file and adds it to the master fd list.
    404       1.1  riastrad  *
    405       1.3  riastrad  * IMPORTANT:
    406       1.3  riastrad  * This functions publishes the fb and makes it available for concurrent access
    407       1.3  riastrad  * by other users. Which means by this point the fb _must_ be fully set up -
    408       1.3  riastrad  * since all the fb attributes are invariant over its lifetime, no further
    409       1.3  riastrad  * locking but only correct reference counting is required.
    410       1.3  riastrad  *
    411       1.3  riastrad  * Returns:
    412       1.1  riastrad  * Zero on success, error code on failure.
    413       1.1  riastrad  */
    414       1.1  riastrad int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
    415       1.1  riastrad 			 const struct drm_framebuffer_funcs *funcs)
    416       1.1  riastrad {
    417       1.1  riastrad 	int ret;
    418       1.1  riastrad 
    419       1.3  riastrad 	mutex_lock(&dev->mode_config.fb_lock);
    420       1.1  riastrad 	kref_init(&fb->refcount);
    421       1.3  riastrad 	INIT_LIST_HEAD(&fb->filp_head);
    422       1.3  riastrad 	fb->dev = dev;
    423       1.3  riastrad 	fb->funcs = funcs;
    424       1.1  riastrad 
    425       1.1  riastrad 	ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
    426       1.1  riastrad 	if (ret)
    427       1.3  riastrad 		goto out;
    428       1.3  riastrad 
    429       1.1  riastrad 	dev->mode_config.num_fb++;
    430       1.1  riastrad 	list_add(&fb->head, &dev->mode_config.fb_list);
    431       1.3  riastrad out:
    432       1.3  riastrad 	mutex_unlock(&dev->mode_config.fb_lock);
    433       1.1  riastrad 
    434       1.5  riastrad 	return ret;
    435       1.1  riastrad }
    436       1.1  riastrad EXPORT_SYMBOL(drm_framebuffer_init);
    437       1.1  riastrad 
    438       1.5  riastrad /* dev->mode_config.fb_lock must be held! */
    439       1.5  riastrad static void __drm_framebuffer_unregister(struct drm_device *dev,
    440       1.5  riastrad 					 struct drm_framebuffer *fb)
    441       1.5  riastrad {
    442       1.5  riastrad 	mutex_lock(&dev->mode_config.idr_mutex);
    443       1.5  riastrad 	idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
    444       1.5  riastrad 	mutex_unlock(&dev->mode_config.idr_mutex);
    445       1.5  riastrad 
    446       1.5  riastrad 	fb->base.id = 0;
    447       1.5  riastrad }
    448       1.5  riastrad 
    449       1.1  riastrad static void drm_framebuffer_free(struct kref *kref)
    450       1.1  riastrad {
    451       1.1  riastrad 	struct drm_framebuffer *fb =
    452       1.1  riastrad 			container_of(kref, struct drm_framebuffer, refcount);
    453       1.5  riastrad 	struct drm_device *dev = fb->dev;
    454       1.5  riastrad 
    455       1.5  riastrad 	/*
    456       1.5  riastrad 	 * The lookup idr holds a weak reference, which has not necessarily been
    457       1.5  riastrad 	 * removed at this point. Check for that.
    458       1.5  riastrad 	 */
    459       1.5  riastrad 	mutex_lock(&dev->mode_config.fb_lock);
    460       1.5  riastrad 	if (fb->base.id) {
    461       1.5  riastrad 		/* Mark fb as reaped and drop idr ref. */
    462       1.5  riastrad 		__drm_framebuffer_unregister(dev, fb);
    463       1.5  riastrad 	}
    464       1.5  riastrad 	mutex_unlock(&dev->mode_config.fb_lock);
    465       1.5  riastrad 
    466       1.1  riastrad 	fb->funcs->destroy(fb);
    467       1.1  riastrad }
    468       1.1  riastrad 
    469       1.3  riastrad static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
    470       1.3  riastrad 							uint32_t id)
    471       1.3  riastrad {
    472       1.3  riastrad 	struct drm_mode_object *obj = NULL;
    473       1.3  riastrad 	struct drm_framebuffer *fb;
    474       1.3  riastrad 
    475       1.3  riastrad 	mutex_lock(&dev->mode_config.idr_mutex);
    476       1.3  riastrad 	obj = idr_find(&dev->mode_config.crtc_idr, id);
    477       1.3  riastrad 	if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
    478       1.3  riastrad 		fb = NULL;
    479       1.3  riastrad 	else
    480       1.3  riastrad 		fb = obj_to_fb(obj);
    481       1.3  riastrad 	mutex_unlock(&dev->mode_config.idr_mutex);
    482       1.3  riastrad 
    483       1.3  riastrad 	return fb;
    484       1.3  riastrad }
    485       1.3  riastrad 
    486       1.3  riastrad /**
    487       1.3  riastrad  * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
    488       1.3  riastrad  * @dev: drm device
    489       1.3  riastrad  * @id: id of the fb object
    490       1.3  riastrad  *
    491       1.3  riastrad  * If successful, this grabs an additional reference to the framebuffer -
    492       1.3  riastrad  * callers need to make sure to eventually unreference the returned framebuffer
    493       1.3  riastrad  * again, using @drm_framebuffer_unreference.
    494       1.3  riastrad  */
    495       1.3  riastrad struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
    496       1.3  riastrad 					       uint32_t id)
    497       1.3  riastrad {
    498       1.3  riastrad 	struct drm_framebuffer *fb;
    499       1.3  riastrad 
    500       1.3  riastrad 	mutex_lock(&dev->mode_config.fb_lock);
    501       1.3  riastrad 	fb = __drm_framebuffer_lookup(dev, id);
    502       1.5  riastrad 	if (fb) {
    503       1.5  riastrad 		if (!kref_get_unless_zero(&fb->refcount))
    504       1.5  riastrad 			fb = NULL;
    505       1.5  riastrad 	}
    506       1.3  riastrad 	mutex_unlock(&dev->mode_config.fb_lock);
    507       1.3  riastrad 
    508       1.3  riastrad 	return fb;
    509       1.3  riastrad }
    510       1.3  riastrad EXPORT_SYMBOL(drm_framebuffer_lookup);
    511       1.3  riastrad 
    512       1.1  riastrad /**
    513       1.1  riastrad  * drm_framebuffer_unreference - unref a framebuffer
    514       1.3  riastrad  * @fb: framebuffer to unref
    515       1.1  riastrad  *
    516       1.3  riastrad  * This functions decrements the fb's refcount and frees it if it drops to zero.
    517       1.1  riastrad  */
    518       1.1  riastrad void drm_framebuffer_unreference(struct drm_framebuffer *fb)
    519       1.1  riastrad {
    520       1.7  riastrad 	DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, fb->refcount.kr_count);
    521       1.1  riastrad 	kref_put(&fb->refcount, drm_framebuffer_free);
    522       1.1  riastrad }
    523       1.1  riastrad EXPORT_SYMBOL(drm_framebuffer_unreference);
    524       1.1  riastrad 
    525       1.1  riastrad /**
    526       1.1  riastrad  * drm_framebuffer_reference - incr the fb refcnt
    527       1.3  riastrad  * @fb: framebuffer
    528       1.3  riastrad  *
    529       1.3  riastrad  * This functions increments the fb's refcount.
    530       1.1  riastrad  */
    531       1.1  riastrad void drm_framebuffer_reference(struct drm_framebuffer *fb)
    532       1.1  riastrad {
    533       1.7  riastrad 	DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, fb->refcount.kr_count);
    534       1.1  riastrad 	kref_get(&fb->refcount);
    535       1.1  riastrad }
    536       1.1  riastrad EXPORT_SYMBOL(drm_framebuffer_reference);
    537       1.1  riastrad 
    538       1.3  riastrad /**
    539       1.3  riastrad  * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
    540       1.3  riastrad  * @fb: fb to unregister
    541       1.3  riastrad  *
    542       1.3  riastrad  * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
    543       1.3  riastrad  * those used for fbdev. Note that the caller must hold a reference of it's own,
    544       1.3  riastrad  * i.e. the object may not be destroyed through this call (since it'll lead to a
    545       1.3  riastrad  * locking inversion).
    546       1.3  riastrad  */
    547       1.3  riastrad void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
    548       1.3  riastrad {
    549       1.5  riastrad 	struct drm_device *dev;
    550       1.5  riastrad 
    551       1.5  riastrad 	if (!fb)
    552       1.5  riastrad 		return;
    553       1.5  riastrad 
    554       1.5  riastrad 	dev = fb->dev;
    555       1.3  riastrad 
    556       1.3  riastrad 	mutex_lock(&dev->mode_config.fb_lock);
    557       1.3  riastrad 	/* Mark fb as reaped and drop idr ref. */
    558       1.3  riastrad 	__drm_framebuffer_unregister(dev, fb);
    559       1.3  riastrad 	mutex_unlock(&dev->mode_config.fb_lock);
    560       1.3  riastrad }
    561       1.3  riastrad EXPORT_SYMBOL(drm_framebuffer_unregister_private);
    562       1.3  riastrad 
    563       1.1  riastrad /**
    564       1.1  riastrad  * drm_framebuffer_cleanup - remove a framebuffer object
    565       1.1  riastrad  * @fb: framebuffer to remove
    566       1.1  riastrad  *
    567       1.3  riastrad  * Cleanup framebuffer. This function is intended to be used from the drivers
    568       1.3  riastrad  * ->destroy callback. It can also be used to clean up driver private
    569       1.3  riastrad  *  framebuffers embedded into a larger structure.
    570       1.3  riastrad  *
    571       1.3  riastrad  * Note that this function does not remove the fb from active usuage - if it is
    572       1.3  riastrad  * still used anywhere, hilarity can ensue since userspace could call getfb on
    573       1.3  riastrad  * the id and get back -EINVAL. Obviously no concern at driver unload time.
    574       1.3  riastrad  *
    575       1.3  riastrad  * Also, the framebuffer will not be removed from the lookup idr - for
    576       1.3  riastrad  * user-created framebuffers this will happen in in the rmfb ioctl. For
    577       1.3  riastrad  * driver-private objects (e.g. for fbdev) drivers need to explicitly call
    578       1.3  riastrad  * drm_framebuffer_unregister_private.
    579       1.1  riastrad  */
    580       1.1  riastrad void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
    581       1.1  riastrad {
    582       1.1  riastrad 	struct drm_device *dev = fb->dev;
    583       1.3  riastrad 
    584       1.3  riastrad 	mutex_lock(&dev->mode_config.fb_lock);
    585       1.1  riastrad 	list_del(&fb->head);
    586       1.1  riastrad 	dev->mode_config.num_fb--;
    587       1.3  riastrad 	mutex_unlock(&dev->mode_config.fb_lock);
    588       1.1  riastrad }
    589       1.1  riastrad EXPORT_SYMBOL(drm_framebuffer_cleanup);
    590       1.1  riastrad 
    591       1.1  riastrad /**
    592       1.1  riastrad  * drm_framebuffer_remove - remove and unreference a framebuffer object
    593       1.1  riastrad  * @fb: framebuffer to remove
    594       1.1  riastrad  *
    595       1.3  riastrad  * Scans all the CRTCs and planes in @dev's mode_config.  If they're
    596       1.3  riastrad  * using @fb, removes it, setting it to NULL. Then drops the reference to the
    597       1.3  riastrad  * passed-in framebuffer. Might take the modeset locks.
    598       1.1  riastrad  *
    599       1.3  riastrad  * Note that this function optimizes the cleanup away if the caller holds the
    600       1.3  riastrad  * last reference to the framebuffer. It is also guaranteed to not take the
    601       1.3  riastrad  * modeset locks in this case.
    602       1.1  riastrad  */
    603       1.1  riastrad void drm_framebuffer_remove(struct drm_framebuffer *fb)
    604       1.1  riastrad {
    605       1.5  riastrad 	struct drm_device *dev;
    606       1.1  riastrad 	struct drm_crtc *crtc;
    607       1.1  riastrad 	struct drm_plane *plane;
    608       1.1  riastrad 	struct drm_mode_set set;
    609       1.1  riastrad 	int ret;
    610       1.1  riastrad 
    611       1.5  riastrad 	if (!fb)
    612       1.5  riastrad 		return;
    613       1.5  riastrad 
    614       1.5  riastrad 	dev = fb->dev;
    615       1.5  riastrad 
    616       1.3  riastrad 	WARN_ON(!list_empty(&fb->filp_head));
    617       1.3  riastrad 
    618       1.3  riastrad 	/*
    619       1.3  riastrad 	 * drm ABI mandates that we remove any deleted framebuffers from active
    620       1.3  riastrad 	 * useage. But since most sane clients only remove framebuffers they no
    621       1.3  riastrad 	 * longer need, try to optimize this away.
    622       1.3  riastrad 	 *
    623       1.3  riastrad 	 * Since we're holding a reference ourselves, observing a refcount of 1
    624       1.3  riastrad 	 * means that we're the last holder and can skip it. Also, the refcount
    625       1.3  riastrad 	 * can never increase from 1 again, so we don't need any barriers or
    626       1.3  riastrad 	 * locks.
    627       1.3  riastrad 	 *
    628       1.3  riastrad 	 * Note that userspace could try to race with use and instate a new
    629       1.3  riastrad 	 * usage _after_ we've cleared all current ones. End result will be an
    630       1.3  riastrad 	 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
    631       1.3  riastrad 	 * in this manner.
    632       1.3  riastrad 	 */
    633       1.4  riastrad 	if (!kref_exclusive_p(&fb->refcount)) {
    634       1.3  riastrad 		drm_modeset_lock_all(dev);
    635       1.3  riastrad 		/* remove from any CRTC */
    636       1.5  riastrad 		drm_for_each_crtc(crtc, dev) {
    637       1.3  riastrad 			if (crtc->primary->fb == fb) {
    638       1.3  riastrad 				/* should turn off the crtc */
    639       1.3  riastrad 				memset(&set, 0, sizeof(struct drm_mode_set));
    640       1.3  riastrad 				set.crtc = crtc;
    641       1.3  riastrad 				set.fb = NULL;
    642       1.3  riastrad 				ret = drm_mode_set_config_internal(&set);
    643       1.3  riastrad 				if (ret)
    644       1.3  riastrad 					DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
    645       1.3  riastrad 			}
    646       1.1  riastrad 		}
    647       1.1  riastrad 
    648       1.5  riastrad 		drm_for_each_plane(plane, dev) {
    649       1.3  riastrad 			if (plane->fb == fb)
    650       1.3  riastrad 				drm_plane_force_disable(plane);
    651       1.1  riastrad 		}
    652       1.3  riastrad 		drm_modeset_unlock_all(dev);
    653       1.1  riastrad 	}
    654       1.1  riastrad 
    655       1.1  riastrad 	drm_framebuffer_unreference(fb);
    656       1.1  riastrad }
    657       1.1  riastrad EXPORT_SYMBOL(drm_framebuffer_remove);
    658       1.1  riastrad 
    659       1.5  riastrad DEFINE_WW_CLASS(crtc_ww_class);
    660       1.5  riastrad 
    661       1.1  riastrad /**
    662       1.3  riastrad  * drm_crtc_init_with_planes - Initialise a new CRTC object with
    663       1.3  riastrad  *    specified primary and cursor planes.
    664       1.1  riastrad  * @dev: DRM device
    665       1.1  riastrad  * @crtc: CRTC object to init
    666       1.3  riastrad  * @primary: Primary plane for CRTC
    667       1.3  riastrad  * @cursor: Cursor plane for CRTC
    668       1.1  riastrad  * @funcs: callbacks for the new CRTC
    669       1.1  riastrad  *
    670       1.3  riastrad  * Inits a new object created as base part of a driver crtc object.
    671       1.1  riastrad  *
    672       1.3  riastrad  * Returns:
    673       1.1  riastrad  * Zero on success, error code on failure.
    674       1.1  riastrad  */
    675       1.3  riastrad int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
    676       1.3  riastrad 			      struct drm_plane *primary,
    677       1.5  riastrad 			      struct drm_plane *cursor,
    678       1.3  riastrad 			      const struct drm_crtc_funcs *funcs)
    679       1.1  riastrad {
    680       1.5  riastrad 	struct drm_mode_config *config = &dev->mode_config;
    681       1.1  riastrad 	int ret;
    682       1.1  riastrad 
    683       1.5  riastrad 	WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
    684       1.5  riastrad 	WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
    685       1.5  riastrad 
    686       1.1  riastrad 	crtc->dev = dev;
    687       1.1  riastrad 	crtc->funcs = funcs;
    688       1.1  riastrad 
    689       1.5  riastrad 	drm_modeset_lock_init(&crtc->mutex);
    690       1.1  riastrad 	ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
    691       1.1  riastrad 	if (ret)
    692       1.5  riastrad 		return ret;
    693       1.1  riastrad 
    694       1.1  riastrad 	crtc->base.properties = &crtc->properties;
    695       1.1  riastrad 
    696       1.5  riastrad 	list_add_tail(&crtc->head, &config->crtc_list);
    697       1.5  riastrad 	config->num_crtc++;
    698       1.1  riastrad 
    699       1.3  riastrad 	crtc->primary = primary;
    700       1.5  riastrad 	crtc->cursor = cursor;
    701       1.3  riastrad 	if (primary)
    702       1.3  riastrad 		primary->possible_crtcs = 1 << drm_crtc_index(crtc);
    703       1.5  riastrad 	if (cursor)
    704       1.5  riastrad 		cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
    705       1.3  riastrad 
    706       1.5  riastrad 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
    707       1.5  riastrad 		drm_object_attach_property(&crtc->base, config->prop_active, 0);
    708       1.5  riastrad 		drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
    709       1.5  riastrad 	}
    710       1.1  riastrad 
    711       1.5  riastrad 	return 0;
    712       1.1  riastrad }
    713       1.3  riastrad EXPORT_SYMBOL(drm_crtc_init_with_planes);
    714       1.1  riastrad 
    715       1.1  riastrad /**
    716       1.3  riastrad  * drm_crtc_cleanup - Clean up the core crtc usage
    717       1.1  riastrad  * @crtc: CRTC to cleanup
    718       1.1  riastrad  *
    719       1.3  riastrad  * This function cleans up @crtc and removes it from the DRM mode setting
    720       1.3  riastrad  * core. Note that the function does *not* free the crtc structure itself,
    721       1.3  riastrad  * this is the responsibility of the caller.
    722       1.1  riastrad  */
    723       1.1  riastrad void drm_crtc_cleanup(struct drm_crtc *crtc)
    724       1.1  riastrad {
    725       1.1  riastrad 	struct drm_device *dev = crtc->dev;
    726       1.1  riastrad 
    727       1.1  riastrad 	kfree(crtc->gamma_store);
    728       1.1  riastrad 	crtc->gamma_store = NULL;
    729       1.1  riastrad 
    730       1.5  riastrad 	drm_modeset_lock_fini(&crtc->mutex);
    731       1.5  riastrad 
    732       1.1  riastrad 	drm_mode_object_put(dev, &crtc->base);
    733       1.1  riastrad 	list_del(&crtc->head);
    734       1.1  riastrad 	dev->mode_config.num_crtc--;
    735       1.4  riastrad 
    736       1.5  riastrad 	WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
    737       1.5  riastrad 	if (crtc->state && crtc->funcs->atomic_destroy_state)
    738       1.5  riastrad 		crtc->funcs->atomic_destroy_state(crtc, crtc->state);
    739       1.5  riastrad 
    740       1.5  riastrad 	memset(crtc, 0, sizeof(*crtc));
    741       1.1  riastrad }
    742       1.1  riastrad EXPORT_SYMBOL(drm_crtc_cleanup);
    743       1.1  riastrad 
    744       1.1  riastrad /**
    745       1.3  riastrad  * drm_crtc_index - find the index of a registered CRTC
    746       1.3  riastrad  * @crtc: CRTC to find index for
    747       1.1  riastrad  *
    748       1.3  riastrad  * Given a registered CRTC, return the index of that CRTC within a DRM
    749       1.3  riastrad  * device's list of CRTCs.
    750       1.1  riastrad  */
    751       1.3  riastrad unsigned int drm_crtc_index(struct drm_crtc *crtc)
    752       1.1  riastrad {
    753       1.3  riastrad 	unsigned int index = 0;
    754       1.3  riastrad 	struct drm_crtc *tmp;
    755       1.3  riastrad 
    756       1.5  riastrad 	drm_for_each_crtc(tmp, crtc->dev) {
    757       1.3  riastrad 		if (tmp == crtc)
    758       1.3  riastrad 			return index;
    759       1.3  riastrad 
    760       1.3  riastrad 		index++;
    761       1.3  riastrad 	}
    762       1.3  riastrad 
    763       1.3  riastrad 	BUG();
    764       1.1  riastrad }
    765       1.3  riastrad EXPORT_SYMBOL(drm_crtc_index);
    766       1.1  riastrad 
    767       1.3  riastrad /*
    768       1.1  riastrad  * drm_mode_remove - remove and free a mode
    769       1.1  riastrad  * @connector: connector list to modify
    770       1.1  riastrad  * @mode: mode to remove
    771       1.1  riastrad  *
    772       1.1  riastrad  * Remove @mode from @connector's mode list, then free it.
    773       1.1  riastrad  */
    774       1.3  riastrad static void drm_mode_remove(struct drm_connector *connector,
    775       1.3  riastrad 			    struct drm_display_mode *mode)
    776       1.1  riastrad {
    777       1.1  riastrad 	list_del(&mode->head);
    778       1.1  riastrad 	drm_mode_destroy(connector->dev, mode);
    779       1.1  riastrad }
    780       1.1  riastrad 
    781       1.1  riastrad /**
    782       1.5  riastrad  * drm_display_info_set_bus_formats - set the supported bus formats
    783       1.5  riastrad  * @info: display info to store bus formats in
    784       1.5  riastrad  * @formats: array containing the supported bus formats
    785       1.5  riastrad  * @num_formats: the number of entries in the fmts array
    786       1.5  riastrad  *
    787       1.5  riastrad  * Store the supported bus formats in display info structure.
    788       1.5  riastrad  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
    789       1.5  riastrad  * a full list of available formats.
    790       1.5  riastrad  */
    791       1.5  riastrad int drm_display_info_set_bus_formats(struct drm_display_info *info,
    792       1.5  riastrad 				     const u32 *formats,
    793       1.5  riastrad 				     unsigned int num_formats)
    794       1.5  riastrad {
    795       1.5  riastrad 	u32 *fmts = NULL;
    796       1.5  riastrad 
    797       1.5  riastrad 	if (!formats && num_formats)
    798       1.5  riastrad 		return -EINVAL;
    799       1.5  riastrad 
    800       1.5  riastrad 	if (formats && num_formats) {
    801       1.5  riastrad 		fmts = kmemdup(formats, sizeof(*formats) * num_formats,
    802       1.5  riastrad 			       GFP_KERNEL);
    803       1.5  riastrad 		if (!fmts)
    804       1.5  riastrad 			return -ENOMEM;
    805       1.5  riastrad 	}
    806       1.5  riastrad 
    807       1.5  riastrad 	kfree(info->bus_formats);
    808       1.5  riastrad 	info->bus_formats = fmts;
    809       1.5  riastrad 	info->num_bus_formats = num_formats;
    810       1.5  riastrad 
    811       1.5  riastrad 	return 0;
    812       1.5  riastrad }
    813       1.5  riastrad EXPORT_SYMBOL(drm_display_info_set_bus_formats);
    814       1.5  riastrad 
    815       1.5  riastrad /**
    816       1.5  riastrad  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
    817       1.5  riastrad  * @connector: connector to quwery
    818       1.5  riastrad  *
    819       1.5  riastrad  * The kernel supports per-connector configration of its consoles through
    820       1.5  riastrad  * use of the video= parameter. This function parses that option and
    821       1.5  riastrad  * extracts the user's specified mode (or enable/disable status) for a
    822       1.5  riastrad  * particular connector. This is typically only used during the early fbdev
    823       1.5  riastrad  * setup.
    824       1.5  riastrad  */
    825       1.5  riastrad static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
    826       1.5  riastrad {
    827       1.5  riastrad 	struct drm_cmdline_mode *mode = &connector->cmdline_mode;
    828       1.5  riastrad 	char *option = NULL;
    829       1.5  riastrad 
    830       1.5  riastrad #ifdef __NetBSD__
    831       1.5  riastrad 	prop_dictionary_t prop = device_properties(connector->dev->dev);
    832       1.5  riastrad 	if (!prop_dictionary_get_cstring(prop, connector->name, &option))
    833       1.5  riastrad 		return;
    834       1.5  riastrad #else
    835       1.5  riastrad 	if (fb_get_options(connector->name, &option))
    836       1.5  riastrad 		return;
    837       1.5  riastrad #endif
    838       1.5  riastrad 
    839       1.5  riastrad 	if (!drm_mode_parse_command_line_for_connector(option,
    840       1.5  riastrad 						       connector,
    841       1.5  riastrad 						       mode))
    842       1.5  riastrad 		return;
    843       1.5  riastrad 
    844       1.5  riastrad 	if (mode->force) {
    845       1.5  riastrad 		const char *s;
    846       1.5  riastrad 
    847       1.5  riastrad 		switch (mode->force) {
    848       1.5  riastrad 		case DRM_FORCE_OFF:
    849       1.5  riastrad 			s = "OFF";
    850       1.5  riastrad 			break;
    851       1.5  riastrad 		case DRM_FORCE_ON_DIGITAL:
    852       1.5  riastrad 			s = "ON - dig";
    853       1.5  riastrad 			break;
    854       1.5  riastrad 		default:
    855       1.5  riastrad 		case DRM_FORCE_ON:
    856       1.5  riastrad 			s = "ON";
    857       1.5  riastrad 			break;
    858       1.5  riastrad 		}
    859       1.5  riastrad 
    860       1.5  riastrad 		DRM_INFO("forcing %s connector %s\n", connector->name, s);
    861       1.5  riastrad 		connector->force = mode->force;
    862       1.5  riastrad 	}
    863       1.5  riastrad 
    864       1.5  riastrad 	DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
    865       1.5  riastrad 		      connector->name,
    866       1.5  riastrad 		      mode->xres, mode->yres,
    867       1.5  riastrad 		      mode->refresh_specified ? mode->refresh : 60,
    868       1.5  riastrad 		      mode->rb ? " reduced blanking" : "",
    869       1.5  riastrad 		      mode->margins ? " with margins" : "",
    870       1.5  riastrad 		      mode->interlace ?  " interlaced" : "");
    871       1.5  riastrad }
    872       1.5  riastrad 
    873       1.5  riastrad /**
    874       1.1  riastrad  * drm_connector_init - Init a preallocated connector
    875       1.1  riastrad  * @dev: DRM device
    876       1.1  riastrad  * @connector: the connector to init
    877       1.1  riastrad  * @funcs: callbacks for this connector
    878       1.3  riastrad  * @connector_type: user visible type of the connector
    879       1.1  riastrad  *
    880       1.1  riastrad  * Initialises a preallocated connector. Connectors should be
    881       1.1  riastrad  * subclassed as part of driver connector objects.
    882       1.1  riastrad  *
    883       1.3  riastrad  * Returns:
    884       1.1  riastrad  * Zero on success, error code on failure.
    885       1.1  riastrad  */
    886       1.1  riastrad int drm_connector_init(struct drm_device *dev,
    887       1.1  riastrad 		       struct drm_connector *connector,
    888       1.1  riastrad 		       const struct drm_connector_funcs *funcs,
    889       1.1  riastrad 		       int connector_type)
    890       1.1  riastrad {
    891       1.5  riastrad 	struct drm_mode_config *config = &dev->mode_config;
    892       1.1  riastrad 	int ret;
    893       1.3  riastrad 	struct ida *connector_ida =
    894       1.3  riastrad 		&drm_connector_enum_list[connector_type].ida;
    895       1.1  riastrad 
    896       1.3  riastrad 	drm_modeset_lock_all(dev);
    897       1.1  riastrad 
    898       1.5  riastrad 	ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
    899       1.1  riastrad 	if (ret)
    900       1.5  riastrad 		goto out_unlock;
    901       1.1  riastrad 
    902       1.1  riastrad 	connector->base.properties = &connector->properties;
    903       1.1  riastrad 	connector->dev = dev;
    904      1.10  riastrad 	connector->kdev = dev->dev;
    905       1.1  riastrad 	connector->funcs = funcs;
    906       1.1  riastrad 	connector->connector_type = connector_type;
    907       1.1  riastrad 	connector->connector_type_id =
    908       1.3  riastrad 		ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
    909       1.3  riastrad 	if (connector->connector_type_id < 0) {
    910       1.3  riastrad 		ret = connector->connector_type_id;
    911       1.5  riastrad 		goto out_put;
    912       1.5  riastrad 	}
    913       1.5  riastrad 	connector->name =
    914       1.5  riastrad 		kasprintf(GFP_KERNEL, "%s-%d",
    915       1.5  riastrad 			  drm_connector_enum_list[connector_type].name,
    916       1.5  riastrad 			  connector->connector_type_id);
    917       1.5  riastrad 	if (!connector->name) {
    918       1.5  riastrad 		ret = -ENOMEM;
    919       1.5  riastrad 		goto out_put;
    920       1.3  riastrad 	}
    921       1.5  riastrad 
    922       1.1  riastrad 	INIT_LIST_HEAD(&connector->probed_modes);
    923       1.1  riastrad 	INIT_LIST_HEAD(&connector->modes);
    924       1.1  riastrad 	connector->edid_blob_ptr = NULL;
    925       1.1  riastrad 	connector->status = connector_status_unknown;
    926       1.1  riastrad 
    927       1.5  riastrad 	drm_connector_get_cmdline_mode(connector);
    928       1.5  riastrad 
    929       1.5  riastrad 	/* We should add connectors at the end to avoid upsetting the connector
    930       1.5  riastrad 	 * index too much. */
    931       1.5  riastrad 	list_add_tail(&connector->head, &config->connector_list);
    932       1.5  riastrad 	config->num_connector++;
    933       1.1  riastrad 
    934       1.1  riastrad 	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
    935       1.1  riastrad 		drm_object_attach_property(&connector->base,
    936       1.5  riastrad 					      config->edid_property,
    937       1.1  riastrad 					      0);
    938       1.1  riastrad 
    939       1.1  riastrad 	drm_object_attach_property(&connector->base,
    940       1.5  riastrad 				      config->dpms_property, 0);
    941       1.5  riastrad 
    942       1.5  riastrad 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
    943       1.5  riastrad 		drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
    944       1.5  riastrad 	}
    945       1.5  riastrad 
    946       1.5  riastrad 	connector->debugfs_entry = NULL;
    947       1.5  riastrad 
    948       1.5  riastrad out_put:
    949       1.5  riastrad 	if (ret)
    950       1.5  riastrad 		drm_mode_object_put(dev, &connector->base);
    951       1.1  riastrad 
    952       1.5  riastrad out_unlock:
    953       1.3  riastrad 	drm_modeset_unlock_all(dev);
    954       1.1  riastrad 
    955       1.1  riastrad 	return ret;
    956       1.1  riastrad }
    957       1.1  riastrad EXPORT_SYMBOL(drm_connector_init);
    958       1.1  riastrad 
    959       1.1  riastrad /**
    960       1.1  riastrad  * drm_connector_cleanup - cleans up an initialised connector
    961       1.1  riastrad  * @connector: connector to cleanup
    962       1.1  riastrad  *
    963       1.1  riastrad  * Cleans up the connector but doesn't free the object.
    964       1.1  riastrad  */
    965       1.1  riastrad void drm_connector_cleanup(struct drm_connector *connector)
    966       1.1  riastrad {
    967       1.1  riastrad 	struct drm_device *dev = connector->dev;
    968       1.1  riastrad 	struct drm_display_mode *mode, *t;
    969       1.1  riastrad 
    970       1.5  riastrad 	if (connector->tile_group) {
    971       1.5  riastrad 		drm_mode_put_tile_group(dev, connector->tile_group);
    972       1.5  riastrad 		connector->tile_group = NULL;
    973       1.5  riastrad 	}
    974       1.5  riastrad 
    975       1.1  riastrad 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
    976       1.1  riastrad 		drm_mode_remove(connector, mode);
    977       1.1  riastrad 
    978       1.1  riastrad 	list_for_each_entry_safe(mode, t, &connector->modes, head)
    979       1.1  riastrad 		drm_mode_remove(connector, mode);
    980       1.1  riastrad 
    981       1.3  riastrad 	ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
    982       1.3  riastrad 		   connector->connector_type_id);
    983       1.1  riastrad 
    984       1.5  riastrad 	kfree(connector->display_info.bus_formats);
    985       1.1  riastrad 	drm_mode_object_put(dev, &connector->base);
    986       1.5  riastrad 	kfree(connector->name);
    987       1.5  riastrad 	connector->name = NULL;
    988       1.1  riastrad 	list_del(&connector->head);
    989       1.1  riastrad 	dev->mode_config.num_connector--;
    990       1.5  riastrad 
    991       1.5  riastrad 	WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
    992       1.5  riastrad 	if (connector->state && connector->funcs->atomic_destroy_state)
    993       1.5  riastrad 		connector->funcs->atomic_destroy_state(connector,
    994       1.5  riastrad 						       connector->state);
    995       1.5  riastrad 
    996       1.5  riastrad 	memset(connector, 0, sizeof(*connector));
    997       1.1  riastrad }
    998       1.1  riastrad EXPORT_SYMBOL(drm_connector_cleanup);
    999       1.1  riastrad 
   1000       1.3  riastrad /**
   1001       1.5  riastrad  * drm_connector_index - find the index of a registered connector
   1002       1.5  riastrad  * @connector: connector to find index for
   1003       1.3  riastrad  *
   1004       1.5  riastrad  * Given a registered connector, return the index of that connector within a DRM
   1005       1.5  riastrad  * device's list of connectors.
   1006       1.3  riastrad  */
   1007       1.5  riastrad unsigned int drm_connector_index(struct drm_connector *connector)
   1008       1.1  riastrad {
   1009       1.5  riastrad 	unsigned int index = 0;
   1010       1.5  riastrad 	struct drm_connector *tmp;
   1011       1.5  riastrad 	struct drm_mode_config *config = &connector->dev->mode_config;
   1012       1.5  riastrad 
   1013       1.5  riastrad 	WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
   1014       1.5  riastrad 
   1015       1.5  riastrad 	drm_for_each_connector(tmp, connector->dev) {
   1016       1.5  riastrad 		if (tmp == connector)
   1017       1.5  riastrad 			return index;
   1018       1.1  riastrad 
   1019       1.5  riastrad 		index++;
   1020       1.5  riastrad 	}
   1021       1.1  riastrad 
   1022       1.5  riastrad 	BUG();
   1023       1.1  riastrad }
   1024       1.5  riastrad EXPORT_SYMBOL(drm_connector_index);
   1025       1.1  riastrad 
   1026       1.3  riastrad /**
   1027       1.5  riastrad  * drm_connector_register - register a connector
   1028       1.5  riastrad  * @connector: the connector to register
   1029       1.3  riastrad  *
   1030       1.5  riastrad  * Register userspace interfaces for a connector
   1031       1.3  riastrad  *
   1032       1.3  riastrad  * Returns:
   1033       1.3  riastrad  * Zero on success, error code on failure.
   1034       1.3  riastrad  */
   1035       1.5  riastrad int drm_connector_register(struct drm_connector *connector)
   1036       1.3  riastrad {
   1037       1.8  riastrad #ifndef __NetBSD__		/* XXX sysfs, debugfs */
   1038       1.3  riastrad 	int ret;
   1039       1.8  riastrad #endif
   1040       1.3  riastrad 
   1041       1.5  riastrad 	drm_mode_object_register(connector->dev, &connector->base);
   1042       1.3  riastrad 
   1043       1.5  riastrad #ifndef __NetBSD__		/* XXX sysfs, debugfs */
   1044       1.5  riastrad 	ret = drm_sysfs_connector_add(connector);
   1045       1.3  riastrad 	if (ret)
   1046       1.5  riastrad 		return ret;
   1047       1.3  riastrad 
   1048       1.5  riastrad 	ret = drm_debugfs_connector_add(connector);
   1049       1.5  riastrad 	if (ret) {
   1050       1.5  riastrad 		drm_sysfs_connector_remove(connector);
   1051       1.5  riastrad 		return ret;
   1052       1.5  riastrad 	}
   1053       1.5  riastrad #endif
   1054       1.3  riastrad 
   1055       1.5  riastrad 	return 0;
   1056       1.5  riastrad }
   1057       1.5  riastrad EXPORT_SYMBOL(drm_connector_register);
   1058       1.3  riastrad 
   1059       1.5  riastrad /**
   1060       1.5  riastrad  * drm_connector_unregister - unregister a connector
   1061       1.5  riastrad  * @connector: the connector to unregister
   1062       1.5  riastrad  *
   1063       1.5  riastrad  * Unregister userspace interfaces for a connector
   1064       1.5  riastrad  */
   1065       1.5  riastrad void drm_connector_unregister(struct drm_connector *connector)
   1066       1.5  riastrad {
   1067       1.5  riastrad #ifndef __NetBSD__		/* XXX sysfs, debugfs */
   1068       1.5  riastrad 	drm_sysfs_connector_remove(connector);
   1069       1.5  riastrad 	drm_debugfs_connector_remove(connector);
   1070       1.5  riastrad #endif
   1071       1.3  riastrad }
   1072       1.5  riastrad EXPORT_SYMBOL(drm_connector_unregister);
   1073       1.5  riastrad 
   1074       1.3  riastrad 
   1075       1.3  riastrad /**
   1076       1.5  riastrad  * drm_connector_unplug_all - unregister connector userspace interfaces
   1077       1.5  riastrad  * @dev: drm device
   1078       1.3  riastrad  *
   1079       1.5  riastrad  * This function unregisters all connector userspace interfaces in sysfs. Should
   1080       1.5  riastrad  * be call when the device is disconnected, e.g. from an usb driver's
   1081       1.5  riastrad  * ->disconnect callback.
   1082       1.3  riastrad  */
   1083       1.5  riastrad void drm_connector_unplug_all(struct drm_device *dev)
   1084       1.3  riastrad {
   1085       1.5  riastrad 	struct drm_connector *connector;
   1086       1.5  riastrad 
   1087       1.5  riastrad 	/* FIXME: taking the mode config mutex ends up in a clash with sysfs */
   1088       1.5  riastrad 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
   1089       1.5  riastrad 		drm_connector_unregister(connector);
   1090       1.3  riastrad 
   1091       1.3  riastrad }
   1092       1.5  riastrad EXPORT_SYMBOL(drm_connector_unplug_all);
   1093       1.3  riastrad 
   1094       1.3  riastrad /**
   1095       1.3  riastrad  * drm_encoder_init - Init a preallocated encoder
   1096       1.3  riastrad  * @dev: drm device
   1097       1.3  riastrad  * @encoder: the encoder to init
   1098       1.3  riastrad  * @funcs: callbacks for this encoder
   1099       1.3  riastrad  * @encoder_type: user visible type of the encoder
   1100       1.3  riastrad  *
   1101       1.3  riastrad  * Initialises a preallocated encoder. Encoder should be
   1102       1.3  riastrad  * subclassed as part of driver encoder objects.
   1103       1.3  riastrad  *
   1104       1.3  riastrad  * Returns:
   1105       1.3  riastrad  * Zero on success, error code on failure.
   1106       1.3  riastrad  */
   1107       1.1  riastrad int drm_encoder_init(struct drm_device *dev,
   1108       1.1  riastrad 		      struct drm_encoder *encoder,
   1109       1.1  riastrad 		      const struct drm_encoder_funcs *funcs,
   1110       1.1  riastrad 		      int encoder_type)
   1111       1.1  riastrad {
   1112       1.1  riastrad 	int ret;
   1113       1.1  riastrad 
   1114       1.3  riastrad 	drm_modeset_lock_all(dev);
   1115       1.1  riastrad 
   1116       1.1  riastrad 	ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
   1117       1.1  riastrad 	if (ret)
   1118       1.5  riastrad 		goto out_unlock;
   1119       1.1  riastrad 
   1120       1.1  riastrad 	encoder->dev = dev;
   1121       1.1  riastrad 	encoder->encoder_type = encoder_type;
   1122       1.1  riastrad 	encoder->funcs = funcs;
   1123       1.5  riastrad 	encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
   1124       1.5  riastrad 				  drm_encoder_enum_list[encoder_type].name,
   1125       1.5  riastrad 				  encoder->base.id);
   1126       1.5  riastrad 	if (!encoder->name) {
   1127       1.5  riastrad 		ret = -ENOMEM;
   1128       1.5  riastrad 		goto out_put;
   1129       1.5  riastrad 	}
   1130       1.1  riastrad 
   1131       1.1  riastrad 	list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
   1132       1.1  riastrad 	dev->mode_config.num_encoder++;
   1133       1.1  riastrad 
   1134       1.5  riastrad out_put:
   1135       1.5  riastrad 	if (ret)
   1136       1.5  riastrad 		drm_mode_object_put(dev, &encoder->base);
   1137       1.5  riastrad 
   1138       1.5  riastrad out_unlock:
   1139       1.3  riastrad 	drm_modeset_unlock_all(dev);
   1140       1.1  riastrad 
   1141       1.1  riastrad 	return ret;
   1142       1.1  riastrad }
   1143       1.1  riastrad EXPORT_SYMBOL(drm_encoder_init);
   1144       1.1  riastrad 
   1145       1.3  riastrad /**
   1146       1.3  riastrad  * drm_encoder_cleanup - cleans up an initialised encoder
   1147       1.3  riastrad  * @encoder: encoder to cleanup
   1148       1.3  riastrad  *
   1149       1.3  riastrad  * Cleans up the encoder but doesn't free the object.
   1150       1.3  riastrad  */
   1151       1.1  riastrad void drm_encoder_cleanup(struct drm_encoder *encoder)
   1152       1.1  riastrad {
   1153       1.1  riastrad 	struct drm_device *dev = encoder->dev;
   1154       1.5  riastrad 
   1155       1.3  riastrad 	drm_modeset_lock_all(dev);
   1156       1.1  riastrad 	drm_mode_object_put(dev, &encoder->base);
   1157       1.5  riastrad 	kfree(encoder->name);
   1158       1.1  riastrad 	list_del(&encoder->head);
   1159       1.1  riastrad 	dev->mode_config.num_encoder--;
   1160       1.3  riastrad 	drm_modeset_unlock_all(dev);
   1161       1.5  riastrad 
   1162       1.5  riastrad 	memset(encoder, 0, sizeof(*encoder));
   1163       1.1  riastrad }
   1164       1.1  riastrad EXPORT_SYMBOL(drm_encoder_cleanup);
   1165       1.1  riastrad 
   1166       1.3  riastrad /**
   1167       1.3  riastrad  * drm_universal_plane_init - Initialize a new universal plane object
   1168       1.3  riastrad  * @dev: DRM device
   1169       1.3  riastrad  * @plane: plane object to init
   1170       1.3  riastrad  * @possible_crtcs: bitmask of possible CRTCs
   1171       1.3  riastrad  * @funcs: callbacks for the new plane
   1172       1.3  riastrad  * @formats: array of supported formats (%DRM_FORMAT_*)
   1173       1.3  riastrad  * @format_count: number of elements in @formats
   1174       1.3  riastrad  * @type: type of plane (overlay, primary, cursor)
   1175       1.3  riastrad  *
   1176       1.3  riastrad  * Initializes a plane object of type @type.
   1177       1.3  riastrad  *
   1178       1.3  riastrad  * Returns:
   1179       1.3  riastrad  * Zero on success, error code on failure.
   1180       1.3  riastrad  */
   1181       1.3  riastrad int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
   1182       1.3  riastrad 			     unsigned long possible_crtcs,
   1183       1.3  riastrad 			     const struct drm_plane_funcs *funcs,
   1184       1.5  riastrad 			     const uint32_t *formats, unsigned int format_count,
   1185       1.3  riastrad 			     enum drm_plane_type type)
   1186       1.1  riastrad {
   1187       1.5  riastrad 	struct drm_mode_config *config = &dev->mode_config;
   1188       1.1  riastrad 	int ret;
   1189       1.1  riastrad 
   1190       1.1  riastrad 	ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
   1191       1.1  riastrad 	if (ret)
   1192       1.5  riastrad 		return ret;
   1193       1.5  riastrad 
   1194       1.5  riastrad 	drm_modeset_lock_init(&plane->mutex);
   1195       1.1  riastrad 
   1196       1.1  riastrad 	plane->base.properties = &plane->properties;
   1197       1.1  riastrad 	plane->dev = dev;
   1198       1.1  riastrad 	plane->funcs = funcs;
   1199       1.5  riastrad 	plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
   1200       1.5  riastrad 					    GFP_KERNEL);
   1201       1.1  riastrad 	if (!plane->format_types) {
   1202       1.1  riastrad 		DRM_DEBUG_KMS("out of memory when allocating plane\n");
   1203       1.1  riastrad 		drm_mode_object_put(dev, &plane->base);
   1204       1.5  riastrad 		return -ENOMEM;
   1205       1.1  riastrad 	}
   1206       1.1  riastrad 
   1207       1.1  riastrad 	memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
   1208       1.1  riastrad 	plane->format_count = format_count;
   1209       1.1  riastrad 	plane->possible_crtcs = possible_crtcs;
   1210       1.3  riastrad 	plane->type = type;
   1211       1.1  riastrad 
   1212       1.5  riastrad 	list_add_tail(&plane->head, &config->plane_list);
   1213       1.5  riastrad 	config->num_total_plane++;
   1214       1.3  riastrad 	if (plane->type == DRM_PLANE_TYPE_OVERLAY)
   1215       1.5  riastrad 		config->num_overlay_plane++;
   1216       1.3  riastrad 
   1217       1.3  riastrad 	drm_object_attach_property(&plane->base,
   1218       1.5  riastrad 				   config->plane_type_property,
   1219       1.3  riastrad 				   plane->type);
   1220       1.1  riastrad 
   1221       1.5  riastrad 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
   1222       1.5  riastrad 		drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
   1223       1.5  riastrad 		drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
   1224       1.5  riastrad 		drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
   1225       1.5  riastrad 		drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
   1226       1.5  riastrad 		drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
   1227       1.5  riastrad 		drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
   1228       1.5  riastrad 		drm_object_attach_property(&plane->base, config->prop_src_x, 0);
   1229       1.5  riastrad 		drm_object_attach_property(&plane->base, config->prop_src_y, 0);
   1230       1.5  riastrad 		drm_object_attach_property(&plane->base, config->prop_src_w, 0);
   1231       1.5  riastrad 		drm_object_attach_property(&plane->base, config->prop_src_h, 0);
   1232       1.5  riastrad 	}
   1233       1.1  riastrad 
   1234       1.5  riastrad 	return 0;
   1235       1.1  riastrad }
   1236       1.3  riastrad EXPORT_SYMBOL(drm_universal_plane_init);
   1237       1.1  riastrad 
   1238       1.3  riastrad /**
   1239       1.3  riastrad  * drm_plane_init - Initialize a legacy plane
   1240       1.3  riastrad  * @dev: DRM device
   1241       1.3  riastrad  * @plane: plane object to init
   1242       1.3  riastrad  * @possible_crtcs: bitmask of possible CRTCs
   1243       1.3  riastrad  * @funcs: callbacks for the new plane
   1244       1.3  riastrad  * @formats: array of supported formats (%DRM_FORMAT_*)
   1245       1.3  riastrad  * @format_count: number of elements in @formats
   1246       1.3  riastrad  * @is_primary: plane type (primary vs overlay)
   1247       1.3  riastrad  *
   1248       1.3  riastrad  * Legacy API to initialize a DRM plane.
   1249       1.3  riastrad  *
   1250       1.3  riastrad  * New drivers should call drm_universal_plane_init() instead.
   1251       1.3  riastrad  *
   1252       1.3  riastrad  * Returns:
   1253       1.3  riastrad  * Zero on success, error code on failure.
   1254       1.3  riastrad  */
   1255       1.3  riastrad int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
   1256       1.3  riastrad 		   unsigned long possible_crtcs,
   1257       1.3  riastrad 		   const struct drm_plane_funcs *funcs,
   1258       1.5  riastrad 		   const uint32_t *formats, unsigned int format_count,
   1259       1.3  riastrad 		   bool is_primary)
   1260       1.1  riastrad {
   1261       1.3  riastrad 	enum drm_plane_type type;
   1262       1.1  riastrad 
   1263       1.3  riastrad 	type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
   1264       1.3  riastrad 	return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
   1265       1.3  riastrad 					formats, format_count, type);
   1266       1.1  riastrad }
   1267       1.3  riastrad EXPORT_SYMBOL(drm_plane_init);
   1268       1.1  riastrad 
   1269       1.1  riastrad /**
   1270       1.3  riastrad  * drm_plane_cleanup - Clean up the core plane usage
   1271       1.3  riastrad  * @plane: plane to cleanup
   1272       1.1  riastrad  *
   1273       1.3  riastrad  * This function cleans up @plane and removes it from the DRM mode setting
   1274       1.3  riastrad  * core. Note that the function does *not* free the plane structure itself,
   1275       1.3  riastrad  * this is the responsibility of the caller.
   1276       1.1  riastrad  */
   1277       1.3  riastrad void drm_plane_cleanup(struct drm_plane *plane)
   1278       1.1  riastrad {
   1279       1.3  riastrad 	struct drm_device *dev = plane->dev;
   1280       1.1  riastrad 
   1281       1.3  riastrad 	drm_modeset_lock_all(dev);
   1282       1.3  riastrad 	kfree(plane->format_types);
   1283       1.3  riastrad 	drm_mode_object_put(dev, &plane->base);
   1284       1.1  riastrad 
   1285       1.3  riastrad 	BUG_ON(list_empty(&plane->head));
   1286       1.1  riastrad 
   1287       1.3  riastrad 	list_del(&plane->head);
   1288       1.3  riastrad 	dev->mode_config.num_total_plane--;
   1289       1.3  riastrad 	if (plane->type == DRM_PLANE_TYPE_OVERLAY)
   1290       1.3  riastrad 		dev->mode_config.num_overlay_plane--;
   1291       1.3  riastrad 	drm_modeset_unlock_all(dev);
   1292       1.5  riastrad 
   1293       1.5  riastrad 	WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
   1294       1.5  riastrad 	if (plane->state && plane->funcs->atomic_destroy_state)
   1295       1.5  riastrad 		plane->funcs->atomic_destroy_state(plane, plane->state);
   1296      1.11  riastrad 	drm_modeset_lock_fini(&plane->mutex);
   1297       1.5  riastrad 
   1298       1.5  riastrad 	memset(plane, 0, sizeof(*plane));
   1299       1.1  riastrad }
   1300       1.3  riastrad EXPORT_SYMBOL(drm_plane_cleanup);
   1301       1.1  riastrad 
   1302       1.1  riastrad /**
   1303       1.5  riastrad  * drm_plane_index - find the index of a registered plane
   1304       1.5  riastrad  * @plane: plane to find index for
   1305       1.5  riastrad  *
   1306       1.5  riastrad  * Given a registered plane, return the index of that CRTC within a DRM
   1307       1.5  riastrad  * device's list of planes.
   1308       1.5  riastrad  */
   1309       1.5  riastrad unsigned int drm_plane_index(struct drm_plane *plane)
   1310       1.5  riastrad {
   1311       1.5  riastrad 	unsigned int index = 0;
   1312       1.5  riastrad 	struct drm_plane *tmp;
   1313       1.5  riastrad 
   1314       1.5  riastrad 	drm_for_each_plane(tmp, plane->dev) {
   1315       1.5  riastrad 		if (tmp == plane)
   1316       1.5  riastrad 			return index;
   1317       1.5  riastrad 
   1318       1.5  riastrad 		index++;
   1319       1.5  riastrad 	}
   1320       1.5  riastrad 
   1321       1.5  riastrad 	BUG();
   1322       1.5  riastrad }
   1323       1.5  riastrad EXPORT_SYMBOL(drm_plane_index);
   1324       1.5  riastrad 
   1325       1.5  riastrad /**
   1326       1.5  riastrad  * drm_plane_from_index - find the registered plane at an index
   1327       1.5  riastrad  * @dev: DRM device
   1328       1.5  riastrad  * @idx: index of registered plane to find for
   1329       1.5  riastrad  *
   1330       1.5  riastrad  * Given a plane index, return the registered plane from DRM device's
   1331       1.5  riastrad  * list of planes with matching index.
   1332       1.5  riastrad  */
   1333       1.5  riastrad struct drm_plane *
   1334       1.5  riastrad drm_plane_from_index(struct drm_device *dev, int idx)
   1335       1.5  riastrad {
   1336       1.5  riastrad 	struct drm_plane *plane;
   1337       1.5  riastrad 	unsigned int i = 0;
   1338       1.5  riastrad 
   1339       1.5  riastrad 	drm_for_each_plane(plane, dev) {
   1340       1.5  riastrad 		if (i == idx)
   1341       1.5  riastrad 			return plane;
   1342       1.5  riastrad 		i++;
   1343       1.5  riastrad 	}
   1344       1.5  riastrad 	return NULL;
   1345       1.5  riastrad }
   1346       1.5  riastrad EXPORT_SYMBOL(drm_plane_from_index);
   1347       1.5  riastrad 
   1348       1.5  riastrad /**
   1349       1.3  riastrad  * drm_plane_force_disable - Forcibly disable a plane
   1350       1.3  riastrad  * @plane: plane to disable
   1351       1.1  riastrad  *
   1352       1.3  riastrad  * Forces the plane to be disabled.
   1353       1.1  riastrad  *
   1354       1.3  riastrad  * Used when the plane's current framebuffer is destroyed,
   1355       1.3  riastrad  * and when restoring fbdev mode.
   1356       1.1  riastrad  */
   1357       1.3  riastrad void drm_plane_force_disable(struct drm_plane *plane)
   1358       1.1  riastrad {
   1359       1.3  riastrad 	int ret;
   1360       1.3  riastrad 
   1361       1.3  riastrad 	if (!plane->fb)
   1362       1.1  riastrad 		return;
   1363       1.1  riastrad 
   1364       1.5  riastrad 	plane->old_fb = plane->fb;
   1365       1.3  riastrad 	ret = plane->funcs->disable_plane(plane);
   1366       1.5  riastrad 	if (ret) {
   1367       1.3  riastrad 		DRM_ERROR("failed to disable plane with busy fb\n");
   1368       1.5  riastrad 		plane->old_fb = NULL;
   1369       1.5  riastrad 		return;
   1370       1.5  riastrad 	}
   1371       1.3  riastrad 	/* disconnect the plane from the fb and crtc: */
   1372       1.5  riastrad 	drm_framebuffer_unreference(plane->old_fb);
   1373       1.5  riastrad 	plane->old_fb = NULL;
   1374       1.3  riastrad 	plane->fb = NULL;
   1375       1.3  riastrad 	plane->crtc = NULL;
   1376       1.1  riastrad }
   1377       1.3  riastrad EXPORT_SYMBOL(drm_plane_force_disable);
   1378       1.1  riastrad 
   1379       1.5  riastrad static int drm_mode_create_standard_properties(struct drm_device *dev)
   1380       1.1  riastrad {
   1381       1.5  riastrad 	struct drm_property *prop;
   1382       1.1  riastrad 
   1383       1.1  riastrad 	/*
   1384       1.1  riastrad 	 * Standard properties (apply to all connectors)
   1385       1.1  riastrad 	 */
   1386       1.5  riastrad 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
   1387       1.1  riastrad 				   DRM_MODE_PROP_IMMUTABLE,
   1388       1.1  riastrad 				   "EDID", 0);
   1389       1.5  riastrad 	if (!prop)
   1390       1.5  riastrad 		return -ENOMEM;
   1391       1.5  riastrad 	dev->mode_config.edid_property = prop;
   1392       1.1  riastrad 
   1393       1.5  riastrad 	prop = drm_property_create_enum(dev, 0,
   1394       1.1  riastrad 				   "DPMS", drm_dpms_enum_list,
   1395       1.1  riastrad 				   ARRAY_SIZE(drm_dpms_enum_list));
   1396       1.5  riastrad 	if (!prop)
   1397       1.5  riastrad 		return -ENOMEM;
   1398       1.5  riastrad 	dev->mode_config.dpms_property = prop;
   1399       1.1  riastrad 
   1400       1.5  riastrad 	prop = drm_property_create(dev,
   1401       1.5  riastrad 				   DRM_MODE_PROP_BLOB |
   1402       1.5  riastrad 				   DRM_MODE_PROP_IMMUTABLE,
   1403       1.5  riastrad 				   "PATH", 0);
   1404       1.5  riastrad 	if (!prop)
   1405       1.5  riastrad 		return -ENOMEM;
   1406       1.5  riastrad 	dev->mode_config.path_property = prop;
   1407       1.1  riastrad 
   1408       1.5  riastrad 	prop = drm_property_create(dev,
   1409       1.5  riastrad 				   DRM_MODE_PROP_BLOB |
   1410       1.5  riastrad 				   DRM_MODE_PROP_IMMUTABLE,
   1411       1.5  riastrad 				   "TILE", 0);
   1412       1.5  riastrad 	if (!prop)
   1413       1.5  riastrad 		return -ENOMEM;
   1414       1.5  riastrad 	dev->mode_config.tile_property = prop;
   1415       1.3  riastrad 
   1416       1.5  riastrad 	prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
   1417       1.3  riastrad 					"type", drm_plane_type_enum_list,
   1418       1.3  riastrad 					ARRAY_SIZE(drm_plane_type_enum_list));
   1419       1.5  riastrad 	if (!prop)
   1420       1.5  riastrad 		return -ENOMEM;
   1421       1.5  riastrad 	dev->mode_config.plane_type_property = prop;
   1422       1.5  riastrad 
   1423       1.5  riastrad 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
   1424       1.5  riastrad 			"SRC_X", 0, UINT_MAX);
   1425       1.5  riastrad 	if (!prop)
   1426       1.5  riastrad 		return -ENOMEM;
   1427       1.5  riastrad 	dev->mode_config.prop_src_x = prop;
   1428       1.5  riastrad 
   1429       1.5  riastrad 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
   1430       1.5  riastrad 			"SRC_Y", 0, UINT_MAX);
   1431       1.5  riastrad 	if (!prop)
   1432       1.5  riastrad 		return -ENOMEM;
   1433       1.5  riastrad 	dev->mode_config.prop_src_y = prop;
   1434       1.5  riastrad 
   1435       1.5  riastrad 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
   1436       1.5  riastrad 			"SRC_W", 0, UINT_MAX);
   1437       1.5  riastrad 	if (!prop)
   1438       1.5  riastrad 		return -ENOMEM;
   1439       1.5  riastrad 	dev->mode_config.prop_src_w = prop;
   1440       1.5  riastrad 
   1441       1.5  riastrad 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
   1442       1.5  riastrad 			"SRC_H", 0, UINT_MAX);
   1443       1.5  riastrad 	if (!prop)
   1444       1.5  riastrad 		return -ENOMEM;
   1445       1.5  riastrad 	dev->mode_config.prop_src_h = prop;
   1446       1.5  riastrad 
   1447       1.5  riastrad 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
   1448       1.5  riastrad 			"CRTC_X", INT_MIN, INT_MAX);
   1449       1.5  riastrad 	if (!prop)
   1450       1.5  riastrad 		return -ENOMEM;
   1451       1.5  riastrad 	dev->mode_config.prop_crtc_x = prop;
   1452       1.5  riastrad 
   1453       1.5  riastrad 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
   1454       1.5  riastrad 			"CRTC_Y", INT_MIN, INT_MAX);
   1455       1.5  riastrad 	if (!prop)
   1456       1.5  riastrad 		return -ENOMEM;
   1457       1.5  riastrad 	dev->mode_config.prop_crtc_y = prop;
   1458       1.5  riastrad 
   1459       1.5  riastrad 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
   1460       1.5  riastrad 			"CRTC_W", 0, INT_MAX);
   1461       1.5  riastrad 	if (!prop)
   1462       1.5  riastrad 		return -ENOMEM;
   1463       1.5  riastrad 	dev->mode_config.prop_crtc_w = prop;
   1464       1.5  riastrad 
   1465       1.5  riastrad 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
   1466       1.5  riastrad 			"CRTC_H", 0, INT_MAX);
   1467       1.5  riastrad 	if (!prop)
   1468       1.5  riastrad 		return -ENOMEM;
   1469       1.5  riastrad 	dev->mode_config.prop_crtc_h = prop;
   1470       1.5  riastrad 
   1471       1.5  riastrad 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
   1472       1.5  riastrad 			"FB_ID", DRM_MODE_OBJECT_FB);
   1473       1.5  riastrad 	if (!prop)
   1474       1.5  riastrad 		return -ENOMEM;
   1475       1.5  riastrad 	dev->mode_config.prop_fb_id = prop;
   1476       1.5  riastrad 
   1477       1.5  riastrad 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
   1478       1.5  riastrad 			"CRTC_ID", DRM_MODE_OBJECT_CRTC);
   1479       1.5  riastrad 	if (!prop)
   1480       1.5  riastrad 		return -ENOMEM;
   1481       1.5  riastrad 	dev->mode_config.prop_crtc_id = prop;
   1482       1.5  riastrad 
   1483       1.5  riastrad 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
   1484       1.5  riastrad 			"ACTIVE");
   1485       1.5  riastrad 	if (!prop)
   1486       1.5  riastrad 		return -ENOMEM;
   1487       1.5  riastrad 	dev->mode_config.prop_active = prop;
   1488       1.5  riastrad 
   1489       1.5  riastrad 	prop = drm_property_create(dev,
   1490       1.5  riastrad 			DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
   1491       1.5  riastrad 			"MODE_ID", 0);
   1492       1.5  riastrad 	if (!prop)
   1493       1.5  riastrad 		return -ENOMEM;
   1494       1.5  riastrad 	dev->mode_config.prop_mode_id = prop;
   1495       1.3  riastrad 
   1496       1.3  riastrad 	return 0;
   1497       1.3  riastrad }
   1498       1.3  riastrad 
   1499       1.1  riastrad /**
   1500       1.1  riastrad  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
   1501       1.1  riastrad  * @dev: DRM device
   1502       1.1  riastrad  *
   1503       1.1  riastrad  * Called by a driver the first time a DVI-I connector is made.
   1504       1.1  riastrad  */
   1505       1.1  riastrad int drm_mode_create_dvi_i_properties(struct drm_device *dev)
   1506       1.1  riastrad {
   1507       1.1  riastrad 	struct drm_property *dvi_i_selector;
   1508       1.1  riastrad 	struct drm_property *dvi_i_subconnector;
   1509       1.1  riastrad 
   1510       1.1  riastrad 	if (dev->mode_config.dvi_i_select_subconnector_property)
   1511       1.1  riastrad 		return 0;
   1512       1.1  riastrad 
   1513       1.1  riastrad 	dvi_i_selector =
   1514       1.1  riastrad 		drm_property_create_enum(dev, 0,
   1515       1.1  riastrad 				    "select subconnector",
   1516       1.1  riastrad 				    drm_dvi_i_select_enum_list,
   1517       1.1  riastrad 				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
   1518       1.1  riastrad 	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
   1519       1.1  riastrad 
   1520       1.1  riastrad 	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
   1521       1.1  riastrad 				    "subconnector",
   1522       1.1  riastrad 				    drm_dvi_i_subconnector_enum_list,
   1523       1.1  riastrad 				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
   1524       1.1  riastrad 	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
   1525       1.1  riastrad 
   1526       1.1  riastrad 	return 0;
   1527       1.1  riastrad }
   1528       1.1  riastrad EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
   1529       1.1  riastrad 
   1530       1.1  riastrad /**
   1531       1.1  riastrad  * drm_create_tv_properties - create TV specific connector properties
   1532       1.1  riastrad  * @dev: DRM device
   1533       1.1  riastrad  * @num_modes: number of different TV formats (modes) supported
   1534       1.1  riastrad  * @modes: array of pointers to strings containing name of each format
   1535       1.1  riastrad  *
   1536       1.1  riastrad  * Called by a driver's TV initialization routine, this function creates
   1537       1.1  riastrad  * the TV specific connector properties for a given device.  Caller is
   1538       1.1  riastrad  * responsible for allocating a list of format names and passing them to
   1539       1.1  riastrad  * this routine.
   1540       1.1  riastrad  */
   1541       1.5  riastrad int drm_mode_create_tv_properties(struct drm_device *dev,
   1542       1.5  riastrad 				  unsigned int num_modes,
   1543       1.5  riastrad 				  const char * const modes[])
   1544       1.1  riastrad {
   1545       1.1  riastrad 	struct drm_property *tv_selector;
   1546       1.1  riastrad 	struct drm_property *tv_subconnector;
   1547       1.5  riastrad 	unsigned int i;
   1548       1.1  riastrad 
   1549       1.1  riastrad 	if (dev->mode_config.tv_select_subconnector_property)
   1550       1.1  riastrad 		return 0;
   1551       1.1  riastrad 
   1552       1.1  riastrad 	/*
   1553       1.1  riastrad 	 * Basic connector properties
   1554       1.1  riastrad 	 */
   1555       1.1  riastrad 	tv_selector = drm_property_create_enum(dev, 0,
   1556       1.1  riastrad 					  "select subconnector",
   1557       1.1  riastrad 					  drm_tv_select_enum_list,
   1558       1.1  riastrad 					  ARRAY_SIZE(drm_tv_select_enum_list));
   1559       1.5  riastrad 	if (!tv_selector)
   1560       1.5  riastrad 		goto nomem;
   1561       1.5  riastrad 
   1562       1.1  riastrad 	dev->mode_config.tv_select_subconnector_property = tv_selector;
   1563       1.1  riastrad 
   1564       1.1  riastrad 	tv_subconnector =
   1565       1.1  riastrad 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
   1566       1.1  riastrad 				    "subconnector",
   1567       1.1  riastrad 				    drm_tv_subconnector_enum_list,
   1568       1.1  riastrad 				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
   1569       1.5  riastrad 	if (!tv_subconnector)
   1570       1.5  riastrad 		goto nomem;
   1571       1.1  riastrad 	dev->mode_config.tv_subconnector_property = tv_subconnector;
   1572       1.1  riastrad 
   1573       1.1  riastrad 	/*
   1574       1.1  riastrad 	 * Other, TV specific properties: margins & TV modes.
   1575       1.1  riastrad 	 */
   1576       1.1  riastrad 	dev->mode_config.tv_left_margin_property =
   1577       1.1  riastrad 		drm_property_create_range(dev, 0, "left margin", 0, 100);
   1578       1.5  riastrad 	if (!dev->mode_config.tv_left_margin_property)
   1579       1.5  riastrad 		goto nomem;
   1580       1.1  riastrad 
   1581       1.1  riastrad 	dev->mode_config.tv_right_margin_property =
   1582       1.1  riastrad 		drm_property_create_range(dev, 0, "right margin", 0, 100);
   1583       1.5  riastrad 	if (!dev->mode_config.tv_right_margin_property)
   1584       1.5  riastrad 		goto nomem;
   1585       1.1  riastrad 
   1586       1.1  riastrad 	dev->mode_config.tv_top_margin_property =
   1587       1.1  riastrad 		drm_property_create_range(dev, 0, "top margin", 0, 100);
   1588       1.5  riastrad 	if (!dev->mode_config.tv_top_margin_property)
   1589       1.5  riastrad 		goto nomem;
   1590       1.1  riastrad 
   1591       1.1  riastrad 	dev->mode_config.tv_bottom_margin_property =
   1592       1.1  riastrad 		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
   1593       1.5  riastrad 	if (!dev->mode_config.tv_bottom_margin_property)
   1594       1.5  riastrad 		goto nomem;
   1595       1.1  riastrad 
   1596       1.1  riastrad 	dev->mode_config.tv_mode_property =
   1597       1.1  riastrad 		drm_property_create(dev, DRM_MODE_PROP_ENUM,
   1598       1.1  riastrad 				    "mode", num_modes);
   1599       1.5  riastrad 	if (!dev->mode_config.tv_mode_property)
   1600       1.5  riastrad 		goto nomem;
   1601       1.5  riastrad 
   1602       1.1  riastrad 	for (i = 0; i < num_modes; i++)
   1603       1.1  riastrad 		drm_property_add_enum(dev->mode_config.tv_mode_property, i,
   1604       1.1  riastrad 				      i, modes[i]);
   1605       1.1  riastrad 
   1606       1.1  riastrad 	dev->mode_config.tv_brightness_property =
   1607       1.1  riastrad 		drm_property_create_range(dev, 0, "brightness", 0, 100);
   1608       1.5  riastrad 	if (!dev->mode_config.tv_brightness_property)
   1609       1.5  riastrad 		goto nomem;
   1610       1.1  riastrad 
   1611       1.1  riastrad 	dev->mode_config.tv_contrast_property =
   1612       1.1  riastrad 		drm_property_create_range(dev, 0, "contrast", 0, 100);
   1613       1.5  riastrad 	if (!dev->mode_config.tv_contrast_property)
   1614       1.5  riastrad 		goto nomem;
   1615       1.1  riastrad 
   1616       1.1  riastrad 	dev->mode_config.tv_flicker_reduction_property =
   1617       1.1  riastrad 		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
   1618       1.5  riastrad 	if (!dev->mode_config.tv_flicker_reduction_property)
   1619       1.5  riastrad 		goto nomem;
   1620       1.1  riastrad 
   1621       1.1  riastrad 	dev->mode_config.tv_overscan_property =
   1622       1.1  riastrad 		drm_property_create_range(dev, 0, "overscan", 0, 100);
   1623       1.5  riastrad 	if (!dev->mode_config.tv_overscan_property)
   1624       1.5  riastrad 		goto nomem;
   1625       1.1  riastrad 
   1626       1.1  riastrad 	dev->mode_config.tv_saturation_property =
   1627       1.1  riastrad 		drm_property_create_range(dev, 0, "saturation", 0, 100);
   1628       1.5  riastrad 	if (!dev->mode_config.tv_saturation_property)
   1629       1.5  riastrad 		goto nomem;
   1630       1.1  riastrad 
   1631       1.1  riastrad 	dev->mode_config.tv_hue_property =
   1632       1.1  riastrad 		drm_property_create_range(dev, 0, "hue", 0, 100);
   1633       1.5  riastrad 	if (!dev->mode_config.tv_hue_property)
   1634       1.5  riastrad 		goto nomem;
   1635       1.1  riastrad 
   1636       1.1  riastrad 	return 0;
   1637       1.5  riastrad nomem:
   1638       1.5  riastrad 	return -ENOMEM;
   1639       1.1  riastrad }
   1640       1.1  riastrad EXPORT_SYMBOL(drm_mode_create_tv_properties);
   1641       1.1  riastrad 
   1642       1.1  riastrad /**
   1643       1.1  riastrad  * drm_mode_create_scaling_mode_property - create scaling mode property
   1644       1.1  riastrad  * @dev: DRM device
   1645       1.1  riastrad  *
   1646       1.1  riastrad  * Called by a driver the first time it's needed, must be attached to desired
   1647       1.1  riastrad  * connectors.
   1648       1.1  riastrad  */
   1649       1.1  riastrad int drm_mode_create_scaling_mode_property(struct drm_device *dev)
   1650       1.1  riastrad {
   1651       1.1  riastrad 	struct drm_property *scaling_mode;
   1652       1.1  riastrad 
   1653       1.1  riastrad 	if (dev->mode_config.scaling_mode_property)
   1654       1.1  riastrad 		return 0;
   1655       1.1  riastrad 
   1656       1.1  riastrad 	scaling_mode =
   1657       1.1  riastrad 		drm_property_create_enum(dev, 0, "scaling mode",
   1658       1.1  riastrad 				drm_scaling_mode_enum_list,
   1659       1.1  riastrad 				    ARRAY_SIZE(drm_scaling_mode_enum_list));
   1660       1.1  riastrad 
   1661       1.1  riastrad 	dev->mode_config.scaling_mode_property = scaling_mode;
   1662       1.1  riastrad 
   1663       1.1  riastrad 	return 0;
   1664       1.1  riastrad }
   1665       1.1  riastrad EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
   1666       1.1  riastrad 
   1667       1.1  riastrad /**
   1668       1.5  riastrad  * drm_mode_create_aspect_ratio_property - create aspect ratio property
   1669       1.5  riastrad  * @dev: DRM device
   1670       1.5  riastrad  *
   1671       1.5  riastrad  * Called by a driver the first time it's needed, must be attached to desired
   1672       1.5  riastrad  * connectors.
   1673       1.5  riastrad  *
   1674       1.5  riastrad  * Returns:
   1675       1.5  riastrad  * Zero on success, negative errno on failure.
   1676       1.5  riastrad  */
   1677       1.5  riastrad int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
   1678       1.5  riastrad {
   1679       1.5  riastrad 	if (dev->mode_config.aspect_ratio_property)
   1680       1.5  riastrad 		return 0;
   1681       1.5  riastrad 
   1682       1.5  riastrad 	dev->mode_config.aspect_ratio_property =
   1683       1.5  riastrad 		drm_property_create_enum(dev, 0, "aspect ratio",
   1684       1.5  riastrad 				drm_aspect_ratio_enum_list,
   1685       1.5  riastrad 				ARRAY_SIZE(drm_aspect_ratio_enum_list));
   1686       1.5  riastrad 
   1687       1.5  riastrad 	if (dev->mode_config.aspect_ratio_property == NULL)
   1688       1.5  riastrad 		return -ENOMEM;
   1689       1.5  riastrad 
   1690       1.5  riastrad 	return 0;
   1691       1.5  riastrad }
   1692       1.5  riastrad EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
   1693       1.5  riastrad 
   1694       1.5  riastrad /**
   1695       1.1  riastrad  * drm_mode_create_dirty_property - create dirty property
   1696       1.1  riastrad  * @dev: DRM device
   1697       1.1  riastrad  *
   1698       1.1  riastrad  * Called by a driver the first time it's needed, must be attached to desired
   1699       1.1  riastrad  * connectors.
   1700       1.1  riastrad  */
   1701       1.1  riastrad int drm_mode_create_dirty_info_property(struct drm_device *dev)
   1702       1.1  riastrad {
   1703       1.1  riastrad 	struct drm_property *dirty_info;
   1704       1.1  riastrad 
   1705       1.1  riastrad 	if (dev->mode_config.dirty_info_property)
   1706       1.1  riastrad 		return 0;
   1707       1.1  riastrad 
   1708       1.1  riastrad 	dirty_info =
   1709       1.1  riastrad 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
   1710       1.1  riastrad 				    "dirty",
   1711       1.1  riastrad 				    drm_dirty_info_enum_list,
   1712       1.1  riastrad 				    ARRAY_SIZE(drm_dirty_info_enum_list));
   1713       1.1  riastrad 	dev->mode_config.dirty_info_property = dirty_info;
   1714       1.1  riastrad 
   1715       1.3  riastrad 	return 0;
   1716       1.1  riastrad }
   1717       1.3  riastrad EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
   1718       1.1  riastrad 
   1719       1.1  riastrad /**
   1720       1.5  riastrad  * drm_mode_create_suggested_offset_properties - create suggests offset properties
   1721       1.5  riastrad  * @dev: DRM device
   1722       1.1  riastrad  *
   1723       1.5  riastrad  * Create the the suggested x/y offset property for connectors.
   1724       1.1  riastrad  */
   1725       1.5  riastrad int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
   1726       1.1  riastrad {
   1727       1.5  riastrad 	if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
   1728       1.5  riastrad 		return 0;
   1729       1.1  riastrad 
   1730       1.5  riastrad 	dev->mode_config.suggested_x_property =
   1731       1.5  riastrad 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
   1732       1.3  riastrad 
   1733       1.5  riastrad 	dev->mode_config.suggested_y_property =
   1734       1.5  riastrad 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
   1735       1.1  riastrad 
   1736       1.5  riastrad 	if (dev->mode_config.suggested_x_property == NULL ||
   1737       1.5  riastrad 	    dev->mode_config.suggested_y_property == NULL)
   1738       1.5  riastrad 		return -ENOMEM;
   1739       1.1  riastrad 	return 0;
   1740       1.1  riastrad }
   1741       1.5  riastrad EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
   1742       1.1  riastrad 
   1743       1.1  riastrad /**
   1744       1.1  riastrad  * drm_mode_getresources - get graphics configuration
   1745       1.3  riastrad  * @dev: drm device for the ioctl
   1746       1.3  riastrad  * @data: data pointer for the ioctl
   1747       1.3  riastrad  * @file_priv: drm file for the ioctl call
   1748       1.1  riastrad  *
   1749       1.1  riastrad  * Construct a set of configuration description structures and return
   1750       1.1  riastrad  * them to the user, including CRTC, connector and framebuffer configuration.
   1751       1.1  riastrad  *
   1752       1.1  riastrad  * Called by the user via ioctl.
   1753       1.1  riastrad  *
   1754       1.3  riastrad  * Returns:
   1755       1.5  riastrad  * Zero on success, negative errno on failure.
   1756       1.1  riastrad  */
   1757       1.1  riastrad int drm_mode_getresources(struct drm_device *dev, void *data,
   1758       1.1  riastrad 			  struct drm_file *file_priv)
   1759       1.1  riastrad {
   1760       1.1  riastrad 	struct drm_mode_card_res *card_res = data;
   1761       1.1  riastrad 	struct list_head *lh;
   1762       1.1  riastrad 	struct drm_framebuffer *fb;
   1763       1.1  riastrad 	struct drm_connector *connector;
   1764       1.1  riastrad 	struct drm_crtc *crtc;
   1765       1.1  riastrad 	struct drm_encoder *encoder;
   1766       1.1  riastrad 	int ret = 0;
   1767       1.1  riastrad 	int connector_count = 0;
   1768       1.1  riastrad 	int crtc_count = 0;
   1769       1.1  riastrad 	int fb_count = 0;
   1770       1.1  riastrad 	int encoder_count = 0;
   1771       1.5  riastrad 	int copied = 0;
   1772       1.1  riastrad 	uint32_t __user *fb_id;
   1773       1.1  riastrad 	uint32_t __user *crtc_id;
   1774       1.1  riastrad 	uint32_t __user *connector_id;
   1775       1.1  riastrad 	uint32_t __user *encoder_id;
   1776       1.1  riastrad 
   1777       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   1778       1.1  riastrad 		return -EINVAL;
   1779       1.1  riastrad 
   1780       1.1  riastrad 
   1781       1.3  riastrad 	mutex_lock(&file_priv->fbs_lock);
   1782       1.1  riastrad 	/*
   1783       1.1  riastrad 	 * For the non-control nodes we need to limit the list of resources
   1784       1.1  riastrad 	 * by IDs in the group list for this node
   1785       1.1  riastrad 	 */
   1786       1.1  riastrad 	list_for_each(lh, &file_priv->fbs)
   1787       1.1  riastrad 		fb_count++;
   1788       1.1  riastrad 
   1789       1.3  riastrad 	/* handle this in 4 parts */
   1790       1.3  riastrad 	/* FBs */
   1791       1.3  riastrad 	if (card_res->count_fbs >= fb_count) {
   1792       1.3  riastrad 		copied = 0;
   1793       1.3  riastrad 		fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
   1794       1.3  riastrad 		list_for_each_entry(fb, &file_priv->fbs, filp_head) {
   1795       1.3  riastrad 			if (put_user(fb->base.id, fb_id + copied)) {
   1796       1.3  riastrad 				mutex_unlock(&file_priv->fbs_lock);
   1797       1.3  riastrad 				return -EFAULT;
   1798       1.3  riastrad 			}
   1799       1.3  riastrad 			copied++;
   1800       1.3  riastrad 		}
   1801       1.3  riastrad 	}
   1802       1.3  riastrad 	card_res->count_fbs = fb_count;
   1803       1.3  riastrad 	mutex_unlock(&file_priv->fbs_lock);
   1804       1.3  riastrad 
   1805       1.5  riastrad 	/* mode_config.mutex protects the connector list against e.g. DP MST
   1806       1.5  riastrad 	 * connector hot-adding. CRTC/Plane lists are invariant. */
   1807       1.5  riastrad 	mutex_lock(&dev->mode_config.mutex);
   1808       1.5  riastrad 	drm_for_each_crtc(crtc, dev)
   1809       1.5  riastrad 		crtc_count++;
   1810       1.1  riastrad 
   1811       1.5  riastrad 	drm_for_each_connector(connector, dev)
   1812       1.5  riastrad 		connector_count++;
   1813       1.1  riastrad 
   1814       1.5  riastrad 	drm_for_each_encoder(encoder, dev)
   1815       1.5  riastrad 		encoder_count++;
   1816       1.1  riastrad 
   1817       1.1  riastrad 	card_res->max_height = dev->mode_config.max_height;
   1818       1.1  riastrad 	card_res->min_height = dev->mode_config.min_height;
   1819       1.1  riastrad 	card_res->max_width = dev->mode_config.max_width;
   1820       1.1  riastrad 	card_res->min_width = dev->mode_config.min_width;
   1821       1.1  riastrad 
   1822       1.1  riastrad 	/* CRTCs */
   1823       1.1  riastrad 	if (card_res->count_crtcs >= crtc_count) {
   1824       1.1  riastrad 		copied = 0;
   1825       1.1  riastrad 		crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
   1826       1.5  riastrad 		drm_for_each_crtc(crtc, dev) {
   1827       1.5  riastrad 			DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
   1828       1.5  riastrad 			if (put_user(crtc->base.id, crtc_id + copied)) {
   1829       1.5  riastrad 				ret = -EFAULT;
   1830       1.5  riastrad 				goto out;
   1831       1.1  riastrad 			}
   1832       1.5  riastrad 			copied++;
   1833       1.1  riastrad 		}
   1834       1.1  riastrad 	}
   1835       1.1  riastrad 	card_res->count_crtcs = crtc_count;
   1836       1.1  riastrad 
   1837       1.1  riastrad 	/* Encoders */
   1838       1.1  riastrad 	if (card_res->count_encoders >= encoder_count) {
   1839       1.1  riastrad 		copied = 0;
   1840       1.1  riastrad 		encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
   1841       1.5  riastrad 		drm_for_each_encoder(encoder, dev) {
   1842       1.5  riastrad 			DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
   1843       1.5  riastrad 					encoder->name);
   1844       1.5  riastrad 			if (put_user(encoder->base.id, encoder_id +
   1845       1.5  riastrad 				     copied)) {
   1846       1.5  riastrad 				ret = -EFAULT;
   1847       1.5  riastrad 				goto out;
   1848       1.1  riastrad 			}
   1849       1.5  riastrad 			copied++;
   1850       1.1  riastrad 		}
   1851       1.1  riastrad 	}
   1852       1.1  riastrad 	card_res->count_encoders = encoder_count;
   1853       1.1  riastrad 
   1854       1.1  riastrad 	/* Connectors */
   1855       1.1  riastrad 	if (card_res->count_connectors >= connector_count) {
   1856       1.1  riastrad 		copied = 0;
   1857       1.1  riastrad 		connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
   1858       1.5  riastrad 		drm_for_each_connector(connector, dev) {
   1859       1.5  riastrad 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
   1860       1.5  riastrad 				connector->base.id,
   1861       1.5  riastrad 				connector->name);
   1862       1.5  riastrad 			if (put_user(connector->base.id,
   1863       1.5  riastrad 				     connector_id + copied)) {
   1864       1.5  riastrad 				ret = -EFAULT;
   1865       1.5  riastrad 				goto out;
   1866       1.1  riastrad 			}
   1867       1.5  riastrad 			copied++;
   1868       1.1  riastrad 		}
   1869       1.1  riastrad 	}
   1870       1.1  riastrad 	card_res->count_connectors = connector_count;
   1871       1.1  riastrad 
   1872       1.1  riastrad 	DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
   1873       1.1  riastrad 		  card_res->count_connectors, card_res->count_encoders);
   1874       1.1  riastrad 
   1875       1.1  riastrad out:
   1876       1.5  riastrad 	mutex_unlock(&dev->mode_config.mutex);
   1877       1.1  riastrad 	return ret;
   1878       1.1  riastrad }
   1879       1.1  riastrad 
   1880       1.1  riastrad /**
   1881       1.1  riastrad  * drm_mode_getcrtc - get CRTC configuration
   1882       1.3  riastrad  * @dev: drm device for the ioctl
   1883       1.3  riastrad  * @data: data pointer for the ioctl
   1884       1.3  riastrad  * @file_priv: drm file for the ioctl call
   1885       1.1  riastrad  *
   1886       1.1  riastrad  * Construct a CRTC configuration structure to return to the user.
   1887       1.1  riastrad  *
   1888       1.1  riastrad  * Called by the user via ioctl.
   1889       1.1  riastrad  *
   1890       1.3  riastrad  * Returns:
   1891       1.5  riastrad  * Zero on success, negative errno on failure.
   1892       1.1  riastrad  */
   1893       1.1  riastrad int drm_mode_getcrtc(struct drm_device *dev,
   1894       1.1  riastrad 		     void *data, struct drm_file *file_priv)
   1895       1.1  riastrad {
   1896       1.1  riastrad 	struct drm_mode_crtc *crtc_resp = data;
   1897       1.1  riastrad 	struct drm_crtc *crtc;
   1898       1.1  riastrad 
   1899       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   1900       1.1  riastrad 		return -EINVAL;
   1901       1.1  riastrad 
   1902       1.5  riastrad 	crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
   1903       1.5  riastrad 	if (!crtc)
   1904       1.5  riastrad 		return -ENOENT;
   1905       1.1  riastrad 
   1906       1.5  riastrad 	drm_modeset_lock_crtc(crtc, crtc->primary);
   1907       1.1  riastrad 	crtc_resp->gamma_size = crtc->gamma_size;
   1908       1.3  riastrad 	if (crtc->primary->fb)
   1909       1.3  riastrad 		crtc_resp->fb_id = crtc->primary->fb->base.id;
   1910       1.1  riastrad 	else
   1911       1.1  riastrad 		crtc_resp->fb_id = 0;
   1912       1.1  riastrad 
   1913       1.5  riastrad 	if (crtc->state) {
   1914       1.5  riastrad 		crtc_resp->x = crtc->primary->state->src_x >> 16;
   1915       1.5  riastrad 		crtc_resp->y = crtc->primary->state->src_y >> 16;
   1916       1.5  riastrad 		if (crtc->state->enable) {
   1917       1.5  riastrad 			drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
   1918       1.5  riastrad 			crtc_resp->mode_valid = 1;
   1919       1.1  riastrad 
   1920       1.5  riastrad 		} else {
   1921       1.5  riastrad 			crtc_resp->mode_valid = 0;
   1922       1.5  riastrad 		}
   1923       1.5  riastrad 	} else {
   1924       1.5  riastrad 		crtc_resp->x = crtc->x;
   1925       1.5  riastrad 		crtc_resp->y = crtc->y;
   1926       1.5  riastrad 		if (crtc->enabled) {
   1927       1.5  riastrad 			drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
   1928       1.5  riastrad 			crtc_resp->mode_valid = 1;
   1929       1.1  riastrad 
   1930       1.5  riastrad 		} else {
   1931       1.5  riastrad 			crtc_resp->mode_valid = 0;
   1932       1.5  riastrad 		}
   1933       1.1  riastrad 	}
   1934       1.5  riastrad 	drm_modeset_unlock_crtc(crtc);
   1935       1.1  riastrad 
   1936       1.5  riastrad 	return 0;
   1937       1.1  riastrad }
   1938       1.1  riastrad 
   1939       1.3  riastrad static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
   1940       1.3  riastrad 					 const struct drm_file *file_priv)
   1941       1.3  riastrad {
   1942       1.3  riastrad 	/*
   1943       1.3  riastrad 	 * If user-space hasn't configured the driver to expose the stereo 3D
   1944       1.3  riastrad 	 * modes, don't expose them.
   1945       1.3  riastrad 	 */
   1946       1.3  riastrad 	if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
   1947       1.3  riastrad 		return false;
   1948       1.3  riastrad 
   1949       1.3  riastrad 	return true;
   1950       1.3  riastrad }
   1951       1.3  riastrad 
   1952       1.5  riastrad static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
   1953       1.5  riastrad {
   1954       1.5  riastrad 	/* For atomic drivers only state objects are synchronously updated and
   1955       1.5  riastrad 	 * protected by modeset locks, so check those first. */
   1956       1.5  riastrad 	if (connector->state)
   1957       1.5  riastrad 		return connector->state->best_encoder;
   1958       1.5  riastrad 	return connector->encoder;
   1959       1.5  riastrad }
   1960       1.5  riastrad 
   1961       1.5  riastrad /* helper for getconnector and getproperties ioctls */
   1962       1.5  riastrad static int get_properties(struct drm_mode_object *obj, bool atomic,
   1963       1.5  riastrad 		uint32_t __user *prop_ptr, uint64_t __user *prop_values,
   1964       1.5  riastrad 		uint32_t *arg_count_props)
   1965       1.5  riastrad {
   1966       1.5  riastrad 	int props_count;
   1967       1.5  riastrad 	int i, ret, copied;
   1968       1.5  riastrad 
   1969       1.5  riastrad 	props_count = obj->properties->count;
   1970       1.5  riastrad 	if (!atomic)
   1971       1.5  riastrad 		props_count -= obj->properties->atomic_count;
   1972       1.5  riastrad 
   1973       1.5  riastrad 	if ((*arg_count_props >= props_count) && props_count) {
   1974       1.5  riastrad 		for (i = 0, copied = 0; copied < props_count; i++) {
   1975       1.5  riastrad 			struct drm_property *prop = obj->properties->properties[i];
   1976       1.5  riastrad 			uint64_t val;
   1977       1.5  riastrad 
   1978       1.5  riastrad 			if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
   1979       1.5  riastrad 				continue;
   1980       1.5  riastrad 
   1981       1.5  riastrad 			ret = drm_object_property_get_value(obj, prop, &val);
   1982       1.5  riastrad 			if (ret)
   1983       1.5  riastrad 				return ret;
   1984       1.5  riastrad 
   1985       1.5  riastrad 			if (put_user(prop->base.id, prop_ptr + copied))
   1986       1.5  riastrad 				return -EFAULT;
   1987       1.5  riastrad 
   1988       1.5  riastrad 			if (put_user(val, prop_values + copied))
   1989       1.5  riastrad 				return -EFAULT;
   1990       1.5  riastrad 
   1991       1.5  riastrad 			copied++;
   1992       1.5  riastrad 		}
   1993       1.5  riastrad 	}
   1994       1.5  riastrad 	*arg_count_props = props_count;
   1995       1.5  riastrad 
   1996       1.5  riastrad 	return 0;
   1997       1.5  riastrad }
   1998       1.5  riastrad 
   1999       1.1  riastrad /**
   2000       1.1  riastrad  * drm_mode_getconnector - get connector configuration
   2001       1.3  riastrad  * @dev: drm device for the ioctl
   2002       1.3  riastrad  * @data: data pointer for the ioctl
   2003       1.3  riastrad  * @file_priv: drm file for the ioctl call
   2004       1.1  riastrad  *
   2005       1.1  riastrad  * Construct a connector configuration structure to return to the user.
   2006       1.1  riastrad  *
   2007       1.1  riastrad  * Called by the user via ioctl.
   2008       1.1  riastrad  *
   2009       1.3  riastrad  * Returns:
   2010       1.5  riastrad  * Zero on success, negative errno on failure.
   2011       1.1  riastrad  */
   2012       1.1  riastrad int drm_mode_getconnector(struct drm_device *dev, void *data,
   2013       1.1  riastrad 			  struct drm_file *file_priv)
   2014       1.1  riastrad {
   2015       1.1  riastrad 	struct drm_mode_get_connector *out_resp = data;
   2016       1.1  riastrad 	struct drm_connector *connector;
   2017       1.5  riastrad 	struct drm_encoder *encoder;
   2018       1.1  riastrad 	struct drm_display_mode *mode;
   2019       1.1  riastrad 	int mode_count = 0;
   2020       1.1  riastrad 	int encoders_count = 0;
   2021       1.1  riastrad 	int ret = 0;
   2022       1.1  riastrad 	int copied = 0;
   2023       1.1  riastrad 	int i;
   2024       1.1  riastrad 	struct drm_mode_modeinfo u_mode;
   2025       1.1  riastrad 	struct drm_mode_modeinfo __user *mode_ptr;
   2026       1.1  riastrad 	uint32_t __user *encoder_ptr;
   2027       1.1  riastrad 
   2028       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2029       1.1  riastrad 		return -EINVAL;
   2030       1.1  riastrad 
   2031       1.1  riastrad 	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
   2032       1.1  riastrad 
   2033       1.1  riastrad 	DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
   2034       1.1  riastrad 
   2035       1.1  riastrad 	mutex_lock(&dev->mode_config.mutex);
   2036       1.1  riastrad 
   2037       1.5  riastrad 	connector = drm_connector_find(dev, out_resp->connector_id);
   2038       1.5  riastrad 	if (!connector) {
   2039       1.3  riastrad 		ret = -ENOENT;
   2040       1.5  riastrad 		goto out_unlock;
   2041       1.1  riastrad 	}
   2042       1.1  riastrad 
   2043       1.5  riastrad 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
   2044       1.5  riastrad 		if (connector->encoder_ids[i] != 0)
   2045       1.1  riastrad 			encoders_count++;
   2046       1.1  riastrad 
   2047       1.1  riastrad 	if (out_resp->count_modes == 0) {
   2048       1.1  riastrad 		connector->funcs->fill_modes(connector,
   2049       1.1  riastrad 					     dev->mode_config.max_width,
   2050       1.1  riastrad 					     dev->mode_config.max_height);
   2051       1.1  riastrad 	}
   2052       1.1  riastrad 
   2053       1.1  riastrad 	/* delayed so we get modes regardless of pre-fill_modes state */
   2054       1.1  riastrad 	list_for_each_entry(mode, &connector->modes, head)
   2055       1.3  riastrad 		if (drm_mode_expose_to_userspace(mode, file_priv))
   2056       1.3  riastrad 			mode_count++;
   2057       1.1  riastrad 
   2058       1.1  riastrad 	out_resp->connector_id = connector->base.id;
   2059       1.1  riastrad 	out_resp->connector_type = connector->connector_type;
   2060       1.1  riastrad 	out_resp->connector_type_id = connector->connector_type_id;
   2061       1.1  riastrad 	out_resp->mm_width = connector->display_info.width_mm;
   2062       1.1  riastrad 	out_resp->mm_height = connector->display_info.height_mm;
   2063       1.1  riastrad 	out_resp->subpixel = connector->display_info.subpixel_order;
   2064       1.1  riastrad 	out_resp->connection = connector->status;
   2065       1.5  riastrad 
   2066       1.5  riastrad 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
   2067       1.5  riastrad 	encoder = drm_connector_get_encoder(connector);
   2068       1.5  riastrad 	if (encoder)
   2069       1.5  riastrad 		out_resp->encoder_id = encoder->base.id;
   2070       1.1  riastrad 	else
   2071       1.1  riastrad 		out_resp->encoder_id = 0;
   2072       1.1  riastrad 
   2073       1.1  riastrad 	/*
   2074       1.1  riastrad 	 * This ioctl is called twice, once to determine how much space is
   2075       1.1  riastrad 	 * needed, and the 2nd time to fill it.
   2076       1.1  riastrad 	 */
   2077       1.1  riastrad 	if ((out_resp->count_modes >= mode_count) && mode_count) {
   2078       1.1  riastrad 		copied = 0;
   2079       1.1  riastrad 		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
   2080       1.1  riastrad 		list_for_each_entry(mode, &connector->modes, head) {
   2081       1.3  riastrad 			if (!drm_mode_expose_to_userspace(mode, file_priv))
   2082       1.3  riastrad 				continue;
   2083       1.3  riastrad 
   2084       1.5  riastrad 			drm_mode_convert_to_umode(&u_mode, mode);
   2085       1.1  riastrad 			if (copy_to_user(mode_ptr + copied,
   2086       1.1  riastrad 					 &u_mode, sizeof(u_mode))) {
   2087       1.1  riastrad 				ret = -EFAULT;
   2088       1.1  riastrad 				goto out;
   2089       1.1  riastrad 			}
   2090       1.1  riastrad 			copied++;
   2091       1.1  riastrad 		}
   2092       1.1  riastrad 	}
   2093       1.1  riastrad 	out_resp->count_modes = mode_count;
   2094       1.1  riastrad 
   2095       1.5  riastrad 	ret = get_properties(&connector->base, file_priv->atomic,
   2096       1.5  riastrad 			(uint32_t __user *)(unsigned long)(out_resp->props_ptr),
   2097       1.5  riastrad 			(uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
   2098       1.5  riastrad 			&out_resp->count_props);
   2099       1.5  riastrad 	if (ret)
   2100       1.5  riastrad 		goto out;
   2101       1.1  riastrad 
   2102       1.1  riastrad 	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
   2103       1.1  riastrad 		copied = 0;
   2104       1.1  riastrad 		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
   2105       1.1  riastrad 		for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
   2106       1.1  riastrad 			if (connector->encoder_ids[i] != 0) {
   2107       1.1  riastrad 				if (put_user(connector->encoder_ids[i],
   2108       1.1  riastrad 					     encoder_ptr + copied)) {
   2109       1.1  riastrad 					ret = -EFAULT;
   2110       1.1  riastrad 					goto out;
   2111       1.1  riastrad 				}
   2112       1.1  riastrad 				copied++;
   2113       1.1  riastrad 			}
   2114       1.1  riastrad 		}
   2115       1.1  riastrad 	}
   2116       1.1  riastrad 	out_resp->count_encoders = encoders_count;
   2117       1.1  riastrad 
   2118       1.1  riastrad out:
   2119       1.5  riastrad 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
   2120       1.5  riastrad 
   2121       1.5  riastrad out_unlock:
   2122       1.1  riastrad 	mutex_unlock(&dev->mode_config.mutex);
   2123       1.3  riastrad 
   2124       1.1  riastrad 	return ret;
   2125       1.1  riastrad }
   2126       1.1  riastrad 
   2127       1.5  riastrad static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
   2128       1.5  riastrad {
   2129       1.5  riastrad 	struct drm_connector *connector;
   2130       1.5  riastrad 	struct drm_device *dev = encoder->dev;
   2131       1.5  riastrad 	bool uses_atomic = false;
   2132       1.5  riastrad 
   2133       1.5  riastrad 	/* For atomic drivers only state objects are synchronously updated and
   2134       1.5  riastrad 	 * protected by modeset locks, so check those first. */
   2135       1.5  riastrad 	drm_for_each_connector(connector, dev) {
   2136       1.5  riastrad 		if (!connector->state)
   2137       1.5  riastrad 			continue;
   2138       1.5  riastrad 
   2139       1.5  riastrad 		uses_atomic = true;
   2140       1.5  riastrad 
   2141       1.5  riastrad 		if (connector->state->best_encoder != encoder)
   2142       1.5  riastrad 			continue;
   2143       1.5  riastrad 
   2144       1.5  riastrad 		return connector->state->crtc;
   2145       1.5  riastrad 	}
   2146       1.5  riastrad 
   2147       1.5  riastrad 	/* Don't return stale data (e.g. pending async disable). */
   2148       1.5  riastrad 	if (uses_atomic)
   2149       1.5  riastrad 		return NULL;
   2150       1.5  riastrad 
   2151       1.5  riastrad 	return encoder->crtc;
   2152       1.5  riastrad }
   2153       1.5  riastrad 
   2154       1.3  riastrad /**
   2155       1.3  riastrad  * drm_mode_getencoder - get encoder configuration
   2156       1.3  riastrad  * @dev: drm device for the ioctl
   2157       1.3  riastrad  * @data: data pointer for the ioctl
   2158       1.3  riastrad  * @file_priv: drm file for the ioctl call
   2159       1.3  riastrad  *
   2160       1.3  riastrad  * Construct a encoder configuration structure to return to the user.
   2161       1.3  riastrad  *
   2162       1.3  riastrad  * Called by the user via ioctl.
   2163       1.3  riastrad  *
   2164       1.3  riastrad  * Returns:
   2165       1.5  riastrad  * Zero on success, negative errno on failure.
   2166       1.3  riastrad  */
   2167       1.1  riastrad int drm_mode_getencoder(struct drm_device *dev, void *data,
   2168       1.1  riastrad 			struct drm_file *file_priv)
   2169       1.1  riastrad {
   2170       1.1  riastrad 	struct drm_mode_get_encoder *enc_resp = data;
   2171       1.1  riastrad 	struct drm_encoder *encoder;
   2172       1.5  riastrad 	struct drm_crtc *crtc;
   2173       1.1  riastrad 
   2174       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2175       1.1  riastrad 		return -EINVAL;
   2176       1.1  riastrad 
   2177       1.5  riastrad 	encoder = drm_encoder_find(dev, enc_resp->encoder_id);
   2178       1.5  riastrad 	if (!encoder)
   2179       1.5  riastrad 		return -ENOENT;
   2180       1.1  riastrad 
   2181       1.5  riastrad 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
   2182       1.5  riastrad 	crtc = drm_encoder_get_crtc(encoder);
   2183       1.5  riastrad 	if (crtc)
   2184       1.5  riastrad 		enc_resp->crtc_id = crtc->base.id;
   2185       1.1  riastrad 	else
   2186       1.1  riastrad 		enc_resp->crtc_id = 0;
   2187       1.5  riastrad 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
   2188       1.5  riastrad 
   2189       1.1  riastrad 	enc_resp->encoder_type = encoder->encoder_type;
   2190       1.1  riastrad 	enc_resp->encoder_id = encoder->base.id;
   2191       1.1  riastrad 	enc_resp->possible_crtcs = encoder->possible_crtcs;
   2192       1.1  riastrad 	enc_resp->possible_clones = encoder->possible_clones;
   2193       1.1  riastrad 
   2194       1.5  riastrad 	return 0;
   2195       1.1  riastrad }
   2196       1.1  riastrad 
   2197       1.1  riastrad /**
   2198       1.3  riastrad  * drm_mode_getplane_res - enumerate all plane resources
   2199       1.1  riastrad  * @dev: DRM device
   2200       1.1  riastrad  * @data: ioctl data
   2201       1.1  riastrad  * @file_priv: DRM file info
   2202       1.1  riastrad  *
   2203       1.3  riastrad  * Construct a list of plane ids to return to the user.
   2204       1.3  riastrad  *
   2205       1.3  riastrad  * Called by the user via ioctl.
   2206       1.1  riastrad  *
   2207       1.3  riastrad  * Returns:
   2208       1.5  riastrad  * Zero on success, negative errno on failure.
   2209       1.1  riastrad  */
   2210       1.1  riastrad int drm_mode_getplane_res(struct drm_device *dev, void *data,
   2211       1.3  riastrad 			  struct drm_file *file_priv)
   2212       1.1  riastrad {
   2213       1.1  riastrad 	struct drm_mode_get_plane_res *plane_resp = data;
   2214       1.1  riastrad 	struct drm_mode_config *config;
   2215       1.1  riastrad 	struct drm_plane *plane;
   2216       1.1  riastrad 	uint32_t __user *plane_ptr;
   2217       1.5  riastrad 	int copied = 0;
   2218       1.3  riastrad 	unsigned num_planes;
   2219       1.1  riastrad 
   2220       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2221       1.1  riastrad 		return -EINVAL;
   2222       1.1  riastrad 
   2223       1.1  riastrad 	config = &dev->mode_config;
   2224       1.1  riastrad 
   2225       1.3  riastrad 	if (file_priv->universal_planes)
   2226       1.3  riastrad 		num_planes = config->num_total_plane;
   2227       1.3  riastrad 	else
   2228       1.3  riastrad 		num_planes = config->num_overlay_plane;
   2229       1.3  riastrad 
   2230       1.1  riastrad 	/*
   2231       1.1  riastrad 	 * This ioctl is called twice, once to determine how much space is
   2232       1.1  riastrad 	 * needed, and the 2nd time to fill it.
   2233       1.1  riastrad 	 */
   2234       1.3  riastrad 	if (num_planes &&
   2235       1.3  riastrad 	    (plane_resp->count_planes >= num_planes)) {
   2236       1.1  riastrad 		plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
   2237       1.1  riastrad 
   2238       1.5  riastrad 		/* Plane lists are invariant, no locking needed. */
   2239       1.5  riastrad 		drm_for_each_plane(plane, dev) {
   2240       1.3  riastrad 			/*
   2241       1.3  riastrad 			 * Unless userspace set the 'universal planes'
   2242       1.3  riastrad 			 * capability bit, only advertise overlays.
   2243       1.3  riastrad 			 */
   2244       1.3  riastrad 			if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
   2245       1.3  riastrad 			    !file_priv->universal_planes)
   2246       1.3  riastrad 				continue;
   2247       1.3  riastrad 
   2248       1.5  riastrad 			if (put_user(plane->base.id, plane_ptr + copied))
   2249       1.5  riastrad 				return -EFAULT;
   2250       1.1  riastrad 			copied++;
   2251       1.1  riastrad 		}
   2252       1.1  riastrad 	}
   2253       1.3  riastrad 	plane_resp->count_planes = num_planes;
   2254       1.1  riastrad 
   2255       1.5  riastrad 	return 0;
   2256       1.1  riastrad }
   2257       1.1  riastrad 
   2258       1.1  riastrad /**
   2259       1.3  riastrad  * drm_mode_getplane - get plane configuration
   2260       1.1  riastrad  * @dev: DRM device
   2261       1.1  riastrad  * @data: ioctl data
   2262       1.1  riastrad  * @file_priv: DRM file info
   2263       1.1  riastrad  *
   2264       1.3  riastrad  * Construct a plane configuration structure to return to the user.
   2265       1.1  riastrad  *
   2266       1.3  riastrad  * Called by the user via ioctl.
   2267       1.3  riastrad  *
   2268       1.3  riastrad  * Returns:
   2269       1.5  riastrad  * Zero on success, negative errno on failure.
   2270       1.1  riastrad  */
   2271       1.1  riastrad int drm_mode_getplane(struct drm_device *dev, void *data,
   2272       1.3  riastrad 		      struct drm_file *file_priv)
   2273       1.1  riastrad {
   2274       1.1  riastrad 	struct drm_mode_get_plane *plane_resp = data;
   2275       1.1  riastrad 	struct drm_plane *plane;
   2276       1.1  riastrad 	uint32_t __user *format_ptr;
   2277       1.1  riastrad 
   2278       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2279       1.1  riastrad 		return -EINVAL;
   2280       1.1  riastrad 
   2281       1.5  riastrad 	plane = drm_plane_find(dev, plane_resp->plane_id);
   2282       1.5  riastrad 	if (!plane)
   2283       1.5  riastrad 		return -ENOENT;
   2284       1.1  riastrad 
   2285       1.5  riastrad 	drm_modeset_lock(&plane->mutex, NULL);
   2286       1.1  riastrad 	if (plane->crtc)
   2287       1.1  riastrad 		plane_resp->crtc_id = plane->crtc->base.id;
   2288       1.1  riastrad 	else
   2289       1.1  riastrad 		plane_resp->crtc_id = 0;
   2290       1.1  riastrad 
   2291       1.1  riastrad 	if (plane->fb)
   2292       1.1  riastrad 		plane_resp->fb_id = plane->fb->base.id;
   2293       1.1  riastrad 	else
   2294       1.1  riastrad 		plane_resp->fb_id = 0;
   2295       1.5  riastrad 	drm_modeset_unlock(&plane->mutex);
   2296       1.1  riastrad 
   2297       1.1  riastrad 	plane_resp->plane_id = plane->base.id;
   2298       1.1  riastrad 	plane_resp->possible_crtcs = plane->possible_crtcs;
   2299       1.3  riastrad 	plane_resp->gamma_size = 0;
   2300       1.1  riastrad 
   2301       1.1  riastrad 	/*
   2302       1.1  riastrad 	 * This ioctl is called twice, once to determine how much space is
   2303       1.1  riastrad 	 * needed, and the 2nd time to fill it.
   2304       1.1  riastrad 	 */
   2305       1.5  riastrad 	if (plane->format_count &&
   2306       1.5  riastrad 	    (plane_resp->count_format_types >= plane->format_count)) {
   2307       1.5  riastrad 		format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
   2308       1.5  riastrad 		if (copy_to_user(format_ptr,
   2309       1.5  riastrad 				 plane->format_types,
   2310       1.5  riastrad 				 sizeof(uint32_t) * plane->format_count)) {
   2311       1.5  riastrad 			return -EFAULT;
   2312       1.5  riastrad 		}
   2313       1.5  riastrad 	}
   2314       1.5  riastrad 	plane_resp->count_format_types = plane->format_count;
   2315       1.5  riastrad 
   2316       1.5  riastrad 	return 0;
   2317       1.5  riastrad }
   2318       1.5  riastrad 
   2319       1.5  riastrad /**
   2320       1.5  riastrad  * drm_plane_check_pixel_format - Check if the plane supports the pixel format
   2321       1.5  riastrad  * @plane: plane to check for format support
   2322       1.5  riastrad  * @format: the pixel format
   2323       1.5  riastrad  *
   2324       1.5  riastrad  * Returns:
   2325       1.5  riastrad  * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
   2326       1.5  riastrad  * otherwise.
   2327       1.5  riastrad  */
   2328       1.5  riastrad int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
   2329       1.5  riastrad {
   2330       1.5  riastrad 	unsigned int i;
   2331       1.5  riastrad 
   2332       1.5  riastrad 	for (i = 0; i < plane->format_count; i++) {
   2333       1.5  riastrad 		if (format == plane->format_types[i])
   2334       1.5  riastrad 			return 0;
   2335       1.5  riastrad 	}
   2336       1.5  riastrad 
   2337       1.5  riastrad 	return -EINVAL;
   2338       1.5  riastrad }
   2339       1.5  riastrad 
   2340       1.5  riastrad static int check_src_coords(uint32_t src_x, uint32_t src_y,
   2341       1.5  riastrad 			    uint32_t src_w, uint32_t src_h,
   2342       1.5  riastrad 			    const struct drm_framebuffer *fb)
   2343       1.5  riastrad {
   2344       1.5  riastrad 	unsigned int fb_width, fb_height;
   2345       1.5  riastrad 
   2346       1.5  riastrad 	fb_width = fb->width << 16;
   2347       1.5  riastrad 	fb_height = fb->height << 16;
   2348       1.5  riastrad 
   2349       1.5  riastrad 	/* Make sure source coordinates are inside the fb. */
   2350       1.5  riastrad 	if (src_w > fb_width ||
   2351       1.5  riastrad 	    src_x > fb_width - src_w ||
   2352       1.5  riastrad 	    src_h > fb_height ||
   2353       1.5  riastrad 	    src_y > fb_height - src_h) {
   2354       1.5  riastrad 		DRM_DEBUG_KMS("Invalid source coordinates "
   2355       1.5  riastrad 			      "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
   2356       1.5  riastrad 			      src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
   2357       1.5  riastrad 			      src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
   2358       1.5  riastrad 			      src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
   2359       1.5  riastrad 			      src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
   2360       1.5  riastrad 		return -ENOSPC;
   2361       1.5  riastrad 	}
   2362       1.5  riastrad 
   2363       1.5  riastrad 	return 0;
   2364       1.5  riastrad }
   2365       1.5  riastrad 
   2366       1.5  riastrad /*
   2367       1.5  riastrad  * setplane_internal - setplane handler for internal callers
   2368       1.5  riastrad  *
   2369       1.5  riastrad  * Note that we assume an extra reference has already been taken on fb.  If the
   2370       1.5  riastrad  * update fails, this reference will be dropped before return; if it succeeds,
   2371       1.5  riastrad  * the previous framebuffer (if any) will be unreferenced instead.
   2372       1.5  riastrad  *
   2373       1.5  riastrad  * src_{x,y,w,h} are provided in 16.16 fixed point format
   2374       1.5  riastrad  */
   2375       1.5  riastrad static int __setplane_internal(struct drm_plane *plane,
   2376       1.5  riastrad 			       struct drm_crtc *crtc,
   2377       1.5  riastrad 			       struct drm_framebuffer *fb,
   2378       1.5  riastrad 			       int32_t crtc_x, int32_t crtc_y,
   2379       1.5  riastrad 			       uint32_t crtc_w, uint32_t crtc_h,
   2380       1.5  riastrad 			       /* src_{x,y,w,h} values are 16.16 fixed point */
   2381       1.5  riastrad 			       uint32_t src_x, uint32_t src_y,
   2382       1.5  riastrad 			       uint32_t src_w, uint32_t src_h)
   2383       1.5  riastrad {
   2384       1.5  riastrad 	int ret = 0;
   2385       1.5  riastrad 
   2386       1.5  riastrad 	/* No fb means shut it down */
   2387       1.5  riastrad 	if (!fb) {
   2388       1.5  riastrad 		plane->old_fb = plane->fb;
   2389       1.5  riastrad 		ret = plane->funcs->disable_plane(plane);
   2390       1.5  riastrad 		if (!ret) {
   2391       1.5  riastrad 			plane->crtc = NULL;
   2392       1.5  riastrad 			plane->fb = NULL;
   2393       1.5  riastrad 		} else {
   2394       1.5  riastrad 			plane->old_fb = NULL;
   2395       1.5  riastrad 		}
   2396       1.5  riastrad 		goto out;
   2397       1.5  riastrad 	}
   2398       1.5  riastrad 
   2399       1.5  riastrad 	/* Check whether this plane is usable on this CRTC */
   2400       1.5  riastrad 	if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
   2401       1.5  riastrad 		DRM_DEBUG_KMS("Invalid crtc for plane\n");
   2402       1.5  riastrad 		ret = -EINVAL;
   2403       1.5  riastrad 		goto out;
   2404       1.5  riastrad 	}
   2405       1.5  riastrad 
   2406       1.5  riastrad 	/* Check whether this plane supports the fb pixel format. */
   2407       1.5  riastrad 	ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
   2408       1.5  riastrad 	if (ret) {
   2409       1.5  riastrad 		DRM_DEBUG_KMS("Invalid pixel format %s\n",
   2410       1.5  riastrad 			      drm_get_format_name(fb->pixel_format));
   2411       1.5  riastrad 		goto out;
   2412       1.5  riastrad 	}
   2413       1.5  riastrad 
   2414       1.5  riastrad 	/* Give drivers some help against integer overflows */
   2415       1.5  riastrad 	if (crtc_w > INT_MAX ||
   2416       1.5  riastrad 	    crtc_x > INT_MAX - (int32_t) crtc_w ||
   2417       1.5  riastrad 	    crtc_h > INT_MAX ||
   2418       1.5  riastrad 	    crtc_y > INT_MAX - (int32_t) crtc_h) {
   2419       1.5  riastrad 		DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
   2420       1.5  riastrad 			      crtc_w, crtc_h, crtc_x, crtc_y);
   2421       1.5  riastrad 		ret = -ERANGE;
   2422       1.5  riastrad 		goto out;
   2423       1.5  riastrad 	}
   2424       1.5  riastrad 
   2425       1.5  riastrad 	ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
   2426       1.5  riastrad 	if (ret)
   2427       1.5  riastrad 		goto out;
   2428       1.5  riastrad 
   2429       1.5  riastrad 	plane->old_fb = plane->fb;
   2430       1.5  riastrad 	ret = plane->funcs->update_plane(plane, crtc, fb,
   2431       1.5  riastrad 					 crtc_x, crtc_y, crtc_w, crtc_h,
   2432       1.5  riastrad 					 src_x, src_y, src_w, src_h);
   2433       1.5  riastrad 	if (!ret) {
   2434       1.5  riastrad 		plane->crtc = crtc;
   2435       1.5  riastrad 		plane->fb = fb;
   2436       1.5  riastrad 		fb = NULL;
   2437       1.5  riastrad 	} else {
   2438       1.5  riastrad 		plane->old_fb = NULL;
   2439       1.1  riastrad 	}
   2440       1.1  riastrad 
   2441       1.1  riastrad out:
   2442       1.5  riastrad 	if (fb)
   2443       1.5  riastrad 		drm_framebuffer_unreference(fb);
   2444       1.5  riastrad 	if (plane->old_fb)
   2445       1.5  riastrad 		drm_framebuffer_unreference(plane->old_fb);
   2446       1.5  riastrad 	plane->old_fb = NULL;
   2447       1.5  riastrad 
   2448       1.5  riastrad 	return ret;
   2449       1.5  riastrad }
   2450       1.5  riastrad 
   2451       1.5  riastrad static int setplane_internal(struct drm_plane *plane,
   2452       1.5  riastrad 			     struct drm_crtc *crtc,
   2453       1.5  riastrad 			     struct drm_framebuffer *fb,
   2454       1.5  riastrad 			     int32_t crtc_x, int32_t crtc_y,
   2455       1.5  riastrad 			     uint32_t crtc_w, uint32_t crtc_h,
   2456       1.5  riastrad 			     /* src_{x,y,w,h} values are 16.16 fixed point */
   2457       1.5  riastrad 			     uint32_t src_x, uint32_t src_y,
   2458       1.5  riastrad 			     uint32_t src_w, uint32_t src_h)
   2459       1.5  riastrad {
   2460       1.5  riastrad 	int ret;
   2461       1.5  riastrad 
   2462       1.5  riastrad 	drm_modeset_lock_all(plane->dev);
   2463       1.5  riastrad 	ret = __setplane_internal(plane, crtc, fb,
   2464       1.5  riastrad 				  crtc_x, crtc_y, crtc_w, crtc_h,
   2465       1.5  riastrad 				  src_x, src_y, src_w, src_h);
   2466       1.5  riastrad 	drm_modeset_unlock_all(plane->dev);
   2467       1.5  riastrad 
   2468       1.1  riastrad 	return ret;
   2469       1.1  riastrad }
   2470       1.1  riastrad 
   2471       1.1  riastrad /**
   2472       1.3  riastrad  * drm_mode_setplane - configure a plane's configuration
   2473       1.1  riastrad  * @dev: DRM device
   2474       1.1  riastrad  * @data: ioctl data*
   2475       1.3  riastrad  * @file_priv: DRM file info
   2476       1.1  riastrad  *
   2477       1.3  riastrad  * Set plane configuration, including placement, fb, scaling, and other factors.
   2478       1.5  riastrad  * Or pass a NULL fb to disable (planes may be disabled without providing a
   2479       1.5  riastrad  * valid crtc).
   2480       1.1  riastrad  *
   2481       1.3  riastrad  * Returns:
   2482       1.5  riastrad  * Zero on success, negative errno on failure.
   2483       1.1  riastrad  */
   2484       1.1  riastrad int drm_mode_setplane(struct drm_device *dev, void *data,
   2485       1.3  riastrad 		      struct drm_file *file_priv)
   2486       1.1  riastrad {
   2487       1.1  riastrad 	struct drm_mode_set_plane *plane_req = data;
   2488       1.1  riastrad 	struct drm_plane *plane;
   2489       1.5  riastrad 	struct drm_crtc *crtc = NULL;
   2490       1.5  riastrad 	struct drm_framebuffer *fb = NULL;
   2491       1.1  riastrad 
   2492       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2493       1.1  riastrad 		return -EINVAL;
   2494       1.1  riastrad 
   2495       1.1  riastrad 	/*
   2496       1.1  riastrad 	 * First, find the plane, crtc, and fb objects.  If not available,
   2497       1.1  riastrad 	 * we don't bother to call the driver.
   2498       1.1  riastrad 	 */
   2499       1.5  riastrad 	plane = drm_plane_find(dev, plane_req->plane_id);
   2500       1.5  riastrad 	if (!plane) {
   2501       1.1  riastrad 		DRM_DEBUG_KMS("Unknown plane ID %d\n",
   2502       1.1  riastrad 			      plane_req->plane_id);
   2503       1.3  riastrad 		return -ENOENT;
   2504       1.1  riastrad 	}
   2505       1.1  riastrad 
   2506       1.5  riastrad 	if (plane_req->fb_id) {
   2507       1.5  riastrad 		fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
   2508       1.5  riastrad 		if (!fb) {
   2509       1.5  riastrad 			DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
   2510       1.5  riastrad 				      plane_req->fb_id);
   2511       1.5  riastrad 			return -ENOENT;
   2512       1.5  riastrad 		}
   2513       1.1  riastrad 
   2514       1.5  riastrad 		crtc = drm_crtc_find(dev, plane_req->crtc_id);
   2515       1.5  riastrad 		if (!crtc) {
   2516       1.5  riastrad 			DRM_DEBUG_KMS("Unknown crtc ID %d\n",
   2517       1.5  riastrad 				      plane_req->crtc_id);
   2518       1.5  riastrad 			return -ENOENT;
   2519       1.5  riastrad 		}
   2520       1.1  riastrad 	}
   2521       1.1  riastrad 
   2522       1.5  riastrad 	/*
   2523       1.5  riastrad 	 * setplane_internal will take care of deref'ing either the old or new
   2524       1.5  riastrad 	 * framebuffer depending on success.
   2525       1.5  riastrad 	 */
   2526       1.5  riastrad 	return setplane_internal(plane, crtc, fb,
   2527       1.5  riastrad 				 plane_req->crtc_x, plane_req->crtc_y,
   2528       1.5  riastrad 				 plane_req->crtc_w, plane_req->crtc_h,
   2529       1.5  riastrad 				 plane_req->src_x, plane_req->src_y,
   2530       1.5  riastrad 				 plane_req->src_w, plane_req->src_h);
   2531       1.3  riastrad }
   2532       1.3  riastrad 
   2533       1.3  riastrad /**
   2534       1.3  riastrad  * drm_mode_set_config_internal - helper to call ->set_config
   2535       1.3  riastrad  * @set: modeset config to set
   2536       1.3  riastrad  *
   2537       1.3  riastrad  * This is a little helper to wrap internal calls to the ->set_config driver
   2538       1.3  riastrad  * interface. The only thing it adds is correct refcounting dance.
   2539       1.5  riastrad  *
   2540       1.3  riastrad  * Returns:
   2541       1.5  riastrad  * Zero on success, negative errno on failure.
   2542       1.3  riastrad  */
   2543       1.3  riastrad int drm_mode_set_config_internal(struct drm_mode_set *set)
   2544       1.3  riastrad {
   2545       1.3  riastrad 	struct drm_crtc *crtc = set->crtc;
   2546       1.3  riastrad 	struct drm_framebuffer *fb;
   2547       1.3  riastrad 	struct drm_crtc *tmp;
   2548       1.3  riastrad 	int ret;
   2549       1.3  riastrad 
   2550       1.3  riastrad 	/*
   2551       1.3  riastrad 	 * NOTE: ->set_config can also disable other crtcs (if we steal all
   2552       1.3  riastrad 	 * connectors from it), hence we need to refcount the fbs across all
   2553       1.3  riastrad 	 * crtcs. Atomic modeset will have saner semantics ...
   2554       1.3  riastrad 	 */
   2555       1.5  riastrad 	drm_for_each_crtc(tmp, crtc->dev)
   2556       1.5  riastrad 		tmp->primary->old_fb = tmp->primary->fb;
   2557       1.3  riastrad 
   2558       1.3  riastrad 	fb = set->fb;
   2559       1.3  riastrad 
   2560       1.3  riastrad 	ret = crtc->funcs->set_config(set);
   2561       1.3  riastrad 	if (ret == 0) {
   2562       1.3  riastrad 		crtc->primary->crtc = crtc;
   2563       1.5  riastrad 		crtc->primary->fb = fb;
   2564       1.3  riastrad 	}
   2565       1.3  riastrad 
   2566       1.5  riastrad 	drm_for_each_crtc(tmp, crtc->dev) {
   2567       1.3  riastrad 		if (tmp->primary->fb)
   2568       1.3  riastrad 			drm_framebuffer_reference(tmp->primary->fb);
   2569       1.5  riastrad 		if (tmp->primary->old_fb)
   2570       1.5  riastrad 			drm_framebuffer_unreference(tmp->primary->old_fb);
   2571       1.5  riastrad 		tmp->primary->old_fb = NULL;
   2572       1.3  riastrad 	}
   2573       1.1  riastrad 
   2574       1.1  riastrad 	return ret;
   2575       1.1  riastrad }
   2576       1.3  riastrad EXPORT_SYMBOL(drm_mode_set_config_internal);
   2577       1.3  riastrad 
   2578       1.3  riastrad /**
   2579       1.5  riastrad  * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
   2580       1.5  riastrad  * @mode: mode to query
   2581       1.5  riastrad  * @hdisplay: hdisplay value to fill in
   2582       1.5  riastrad  * @vdisplay: vdisplay value to fill in
   2583       1.5  riastrad  *
   2584       1.5  riastrad  * The vdisplay value will be doubled if the specified mode is a stereo mode of
   2585       1.5  riastrad  * the appropriate layout.
   2586       1.5  riastrad  */
   2587       1.5  riastrad void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
   2588       1.5  riastrad 			    int *hdisplay, int *vdisplay)
   2589       1.5  riastrad {
   2590       1.5  riastrad 	struct drm_display_mode adjusted;
   2591       1.5  riastrad 
   2592       1.5  riastrad 	drm_mode_copy(&adjusted, mode);
   2593       1.5  riastrad 	drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
   2594       1.5  riastrad 	*hdisplay = adjusted.crtc_hdisplay;
   2595       1.5  riastrad 	*vdisplay = adjusted.crtc_vdisplay;
   2596       1.5  riastrad }
   2597       1.5  riastrad EXPORT_SYMBOL(drm_crtc_get_hv_timing);
   2598       1.5  riastrad 
   2599       1.5  riastrad /**
   2600       1.3  riastrad  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
   2601       1.3  riastrad  *     CRTC viewport
   2602       1.3  riastrad  * @crtc: CRTC that framebuffer will be displayed on
   2603       1.3  riastrad  * @x: x panning
   2604       1.3  riastrad  * @y: y panning
   2605       1.3  riastrad  * @mode: mode that framebuffer will be displayed under
   2606       1.3  riastrad  * @fb: framebuffer to check size of
   2607       1.3  riastrad  */
   2608       1.3  riastrad int drm_crtc_check_viewport(const struct drm_crtc *crtc,
   2609       1.3  riastrad 			    int x, int y,
   2610       1.3  riastrad 			    const struct drm_display_mode *mode,
   2611       1.3  riastrad 			    const struct drm_framebuffer *fb)
   2612       1.3  riastrad 
   2613       1.3  riastrad {
   2614       1.3  riastrad 	int hdisplay, vdisplay;
   2615       1.3  riastrad 
   2616       1.5  riastrad 	drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
   2617       1.3  riastrad 
   2618       1.5  riastrad 	if (crtc->state &&
   2619       1.5  riastrad 	    crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
   2620       1.5  riastrad 					      BIT(DRM_ROTATE_270)))
   2621       1.3  riastrad 		swap(hdisplay, vdisplay);
   2622       1.3  riastrad 
   2623       1.5  riastrad 	return check_src_coords(x << 16, y << 16,
   2624       1.5  riastrad 				hdisplay << 16, vdisplay << 16, fb);
   2625       1.3  riastrad }
   2626       1.3  riastrad EXPORT_SYMBOL(drm_crtc_check_viewport);
   2627       1.1  riastrad 
   2628       1.1  riastrad /**
   2629       1.1  riastrad  * drm_mode_setcrtc - set CRTC configuration
   2630       1.3  riastrad  * @dev: drm device for the ioctl
   2631       1.3  riastrad  * @data: data pointer for the ioctl
   2632       1.3  riastrad  * @file_priv: drm file for the ioctl call
   2633       1.1  riastrad  *
   2634       1.1  riastrad  * Build a new CRTC configuration based on user request.
   2635       1.1  riastrad  *
   2636       1.1  riastrad  * Called by the user via ioctl.
   2637       1.1  riastrad  *
   2638       1.3  riastrad  * Returns:
   2639       1.5  riastrad  * Zero on success, negative errno on failure.
   2640       1.1  riastrad  */
   2641       1.1  riastrad int drm_mode_setcrtc(struct drm_device *dev, void *data,
   2642       1.1  riastrad 		     struct drm_file *file_priv)
   2643       1.1  riastrad {
   2644       1.1  riastrad 	struct drm_mode_config *config = &dev->mode_config;
   2645       1.1  riastrad 	struct drm_mode_crtc *crtc_req = data;
   2646       1.1  riastrad 	struct drm_crtc *crtc;
   2647       1.1  riastrad 	struct drm_connector **connector_set = NULL, *connector;
   2648       1.1  riastrad 	struct drm_framebuffer *fb = NULL;
   2649       1.1  riastrad 	struct drm_display_mode *mode = NULL;
   2650       1.1  riastrad 	struct drm_mode_set set;
   2651       1.1  riastrad 	uint32_t __user *set_connectors_ptr;
   2652       1.1  riastrad 	int ret;
   2653       1.1  riastrad 	int i;
   2654       1.1  riastrad 
   2655       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2656       1.1  riastrad 		return -EINVAL;
   2657       1.1  riastrad 
   2658       1.5  riastrad 	/*
   2659       1.5  riastrad 	 * Universal plane src offsets are only 16.16, prevent havoc for
   2660       1.5  riastrad 	 * drivers using universal plane code internally.
   2661       1.5  riastrad 	 */
   2662       1.5  riastrad 	if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
   2663       1.1  riastrad 		return -ERANGE;
   2664       1.1  riastrad 
   2665       1.3  riastrad 	drm_modeset_lock_all(dev);
   2666       1.5  riastrad 	crtc = drm_crtc_find(dev, crtc_req->crtc_id);
   2667       1.5  riastrad 	if (!crtc) {
   2668       1.1  riastrad 		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
   2669       1.3  riastrad 		ret = -ENOENT;
   2670       1.1  riastrad 		goto out;
   2671       1.1  riastrad 	}
   2672       1.1  riastrad 	DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
   2673       1.1  riastrad 
   2674       1.1  riastrad 	if (crtc_req->mode_valid) {
   2675       1.1  riastrad 		/* If we have a mode we need a framebuffer. */
   2676       1.1  riastrad 		/* If we pass -1, set the mode with the currently bound fb */
   2677       1.1  riastrad 		if (crtc_req->fb_id == -1) {
   2678       1.3  riastrad 			if (!crtc->primary->fb) {
   2679       1.1  riastrad 				DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
   2680       1.1  riastrad 				ret = -EINVAL;
   2681       1.1  riastrad 				goto out;
   2682       1.1  riastrad 			}
   2683       1.3  riastrad 			fb = crtc->primary->fb;
   2684       1.3  riastrad 			/* Make refcounting symmetric with the lookup path. */
   2685       1.3  riastrad 			drm_framebuffer_reference(fb);
   2686       1.1  riastrad 		} else {
   2687       1.3  riastrad 			fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
   2688       1.3  riastrad 			if (!fb) {
   2689       1.1  riastrad 				DRM_DEBUG_KMS("Unknown FB ID%d\n",
   2690       1.1  riastrad 						crtc_req->fb_id);
   2691       1.3  riastrad 				ret = -ENOENT;
   2692       1.1  riastrad 				goto out;
   2693       1.1  riastrad 			}
   2694       1.1  riastrad 		}
   2695       1.1  riastrad 
   2696       1.1  riastrad 		mode = drm_mode_create(dev);
   2697       1.1  riastrad 		if (!mode) {
   2698       1.1  riastrad 			ret = -ENOMEM;
   2699       1.1  riastrad 			goto out;
   2700       1.1  riastrad 		}
   2701       1.1  riastrad 
   2702       1.5  riastrad 		ret = drm_mode_convert_umode(mode, &crtc_req->mode);
   2703       1.1  riastrad 		if (ret) {
   2704       1.1  riastrad 			DRM_DEBUG_KMS("Invalid mode\n");
   2705       1.1  riastrad 			goto out;
   2706       1.1  riastrad 		}
   2707       1.1  riastrad 
   2708       1.5  riastrad 		/*
   2709       1.5  riastrad 		 * Check whether the primary plane supports the fb pixel format.
   2710       1.5  riastrad 		 * Drivers not implementing the universal planes API use a
   2711       1.5  riastrad 		 * default formats list provided by the DRM core which doesn't
   2712       1.5  riastrad 		 * match real hardware capabilities. Skip the check in that
   2713       1.5  riastrad 		 * case.
   2714       1.5  riastrad 		 */
   2715       1.5  riastrad 		if (!crtc->primary->format_default) {
   2716       1.5  riastrad 			ret = drm_plane_check_pixel_format(crtc->primary,
   2717       1.5  riastrad 							   fb->pixel_format);
   2718       1.5  riastrad 			if (ret) {
   2719       1.5  riastrad 				DRM_DEBUG_KMS("Invalid pixel format %s\n",
   2720       1.5  riastrad 					drm_get_format_name(fb->pixel_format));
   2721       1.5  riastrad 				goto out;
   2722       1.5  riastrad 			}
   2723       1.5  riastrad 		}
   2724       1.1  riastrad 
   2725       1.3  riastrad 		ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
   2726       1.3  riastrad 					      mode, fb);
   2727       1.3  riastrad 		if (ret)
   2728       1.3  riastrad 			goto out;
   2729       1.1  riastrad 
   2730       1.1  riastrad 	}
   2731       1.1  riastrad 
   2732       1.1  riastrad 	if (crtc_req->count_connectors == 0 && mode) {
   2733       1.1  riastrad 		DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
   2734       1.1  riastrad 		ret = -EINVAL;
   2735       1.1  riastrad 		goto out;
   2736       1.1  riastrad 	}
   2737       1.1  riastrad 
   2738       1.1  riastrad 	if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
   2739       1.1  riastrad 		DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
   2740       1.1  riastrad 			  crtc_req->count_connectors);
   2741       1.1  riastrad 		ret = -EINVAL;
   2742       1.1  riastrad 		goto out;
   2743       1.1  riastrad 	}
   2744       1.1  riastrad 
   2745       1.1  riastrad 	if (crtc_req->count_connectors > 0) {
   2746       1.1  riastrad 		u32 out_id;
   2747       1.1  riastrad 
   2748       1.1  riastrad 		/* Avoid unbounded kernel memory allocation */
   2749       1.1  riastrad 		if (crtc_req->count_connectors > config->num_connector) {
   2750       1.1  riastrad 			ret = -EINVAL;
   2751       1.1  riastrad 			goto out;
   2752       1.1  riastrad 		}
   2753       1.1  riastrad 
   2754       1.5  riastrad 		connector_set = kmalloc_array(crtc_req->count_connectors,
   2755       1.5  riastrad 					      sizeof(struct drm_connector *),
   2756       1.5  riastrad 					      GFP_KERNEL);
   2757       1.1  riastrad 		if (!connector_set) {
   2758       1.1  riastrad 			ret = -ENOMEM;
   2759       1.1  riastrad 			goto out;
   2760       1.1  riastrad 		}
   2761       1.1  riastrad 
   2762       1.1  riastrad 		for (i = 0; i < crtc_req->count_connectors; i++) {
   2763       1.1  riastrad 			set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
   2764       1.1  riastrad 			if (get_user(out_id, &set_connectors_ptr[i])) {
   2765       1.1  riastrad 				ret = -EFAULT;
   2766       1.1  riastrad 				goto out;
   2767       1.1  riastrad 			}
   2768       1.1  riastrad 
   2769       1.5  riastrad 			connector = drm_connector_find(dev, out_id);
   2770       1.5  riastrad 			if (!connector) {
   2771       1.1  riastrad 				DRM_DEBUG_KMS("Connector id %d unknown\n",
   2772       1.1  riastrad 						out_id);
   2773       1.3  riastrad 				ret = -ENOENT;
   2774       1.1  riastrad 				goto out;
   2775       1.1  riastrad 			}
   2776       1.1  riastrad 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
   2777       1.1  riastrad 					connector->base.id,
   2778       1.5  riastrad 					connector->name);
   2779       1.1  riastrad 
   2780       1.1  riastrad 			connector_set[i] = connector;
   2781       1.1  riastrad 		}
   2782       1.1  riastrad 	}
   2783       1.1  riastrad 
   2784       1.1  riastrad 	set.crtc = crtc;
   2785       1.1  riastrad 	set.x = crtc_req->x;
   2786       1.1  riastrad 	set.y = crtc_req->y;
   2787       1.1  riastrad 	set.mode = mode;
   2788       1.1  riastrad 	set.connectors = connector_set;
   2789       1.1  riastrad 	set.num_connectors = crtc_req->count_connectors;
   2790       1.1  riastrad 	set.fb = fb;
   2791       1.3  riastrad 	ret = drm_mode_set_config_internal(&set);
   2792       1.1  riastrad 
   2793       1.1  riastrad out:
   2794       1.3  riastrad 	if (fb)
   2795       1.3  riastrad 		drm_framebuffer_unreference(fb);
   2796       1.3  riastrad 
   2797       1.1  riastrad 	kfree(connector_set);
   2798       1.1  riastrad 	drm_mode_destroy(dev, mode);
   2799       1.3  riastrad 	drm_modeset_unlock_all(dev);
   2800       1.1  riastrad 	return ret;
   2801       1.1  riastrad }
   2802       1.1  riastrad 
   2803       1.5  riastrad /**
   2804       1.5  riastrad  * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
   2805       1.5  riastrad  *     universal plane handler call
   2806       1.5  riastrad  * @crtc: crtc to update cursor for
   2807       1.5  riastrad  * @req: data pointer for the ioctl
   2808       1.5  riastrad  * @file_priv: drm file for the ioctl call
   2809       1.5  riastrad  *
   2810       1.5  riastrad  * Legacy cursor ioctl's work directly with driver buffer handles.  To
   2811       1.5  riastrad  * translate legacy ioctl calls into universal plane handler calls, we need to
   2812       1.5  riastrad  * wrap the native buffer handle in a drm_framebuffer.
   2813       1.5  riastrad  *
   2814       1.5  riastrad  * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
   2815       1.5  riastrad  * buffer with a pitch of 4*width; the universal plane interface should be used
   2816       1.5  riastrad  * directly in cases where the hardware can support other buffer settings and
   2817       1.5  riastrad  * userspace wants to make use of these capabilities.
   2818       1.5  riastrad  *
   2819       1.5  riastrad  * Returns:
   2820       1.5  riastrad  * Zero on success, negative errno on failure.
   2821       1.5  riastrad  */
   2822       1.5  riastrad static int drm_mode_cursor_universal(struct drm_crtc *crtc,
   2823       1.5  riastrad 				     struct drm_mode_cursor2 *req,
   2824       1.5  riastrad 				     struct drm_file *file_priv)
   2825       1.5  riastrad {
   2826       1.5  riastrad 	struct drm_device *dev = crtc->dev;
   2827       1.5  riastrad 	struct drm_framebuffer *fb = NULL;
   2828       1.5  riastrad 	struct drm_mode_fb_cmd2 fbreq = {
   2829       1.5  riastrad 		.width = req->width,
   2830       1.5  riastrad 		.height = req->height,
   2831       1.5  riastrad 		.pixel_format = DRM_FORMAT_ARGB8888,
   2832       1.5  riastrad 		.pitches = { req->width * 4 },
   2833       1.5  riastrad 		.handles = { req->handle },
   2834       1.5  riastrad 	};
   2835       1.5  riastrad 	int32_t crtc_x, crtc_y;
   2836       1.5  riastrad 	uint32_t crtc_w = 0, crtc_h = 0;
   2837       1.5  riastrad 	uint32_t src_w = 0, src_h = 0;
   2838       1.5  riastrad 	int ret = 0;
   2839       1.5  riastrad 
   2840       1.5  riastrad 	BUG_ON(!crtc->cursor);
   2841       1.5  riastrad 	WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
   2842       1.5  riastrad 
   2843       1.5  riastrad 	/*
   2844       1.5  riastrad 	 * Obtain fb we'll be using (either new or existing) and take an extra
   2845       1.5  riastrad 	 * reference to it if fb != null.  setplane will take care of dropping
   2846       1.5  riastrad 	 * the reference if the plane update fails.
   2847       1.5  riastrad 	 */
   2848       1.5  riastrad 	if (req->flags & DRM_MODE_CURSOR_BO) {
   2849       1.5  riastrad 		if (req->handle) {
   2850       1.5  riastrad 			fb = internal_framebuffer_create(dev, &fbreq, file_priv);
   2851       1.5  riastrad 			if (IS_ERR(fb)) {
   2852       1.5  riastrad 				DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
   2853       1.5  riastrad 				return PTR_ERR(fb);
   2854       1.5  riastrad 			}
   2855       1.5  riastrad 		} else {
   2856       1.5  riastrad 			fb = NULL;
   2857       1.5  riastrad 		}
   2858       1.5  riastrad 	} else {
   2859       1.5  riastrad 		fb = crtc->cursor->fb;
   2860       1.5  riastrad 		if (fb)
   2861       1.5  riastrad 			drm_framebuffer_reference(fb);
   2862       1.5  riastrad 	}
   2863       1.5  riastrad 
   2864       1.5  riastrad 	if (req->flags & DRM_MODE_CURSOR_MOVE) {
   2865       1.5  riastrad 		crtc_x = req->x;
   2866       1.5  riastrad 		crtc_y = req->y;
   2867       1.5  riastrad 	} else {
   2868       1.5  riastrad 		crtc_x = crtc->cursor_x;
   2869       1.5  riastrad 		crtc_y = crtc->cursor_y;
   2870       1.5  riastrad 	}
   2871       1.5  riastrad 
   2872       1.5  riastrad 	if (fb) {
   2873       1.5  riastrad 		crtc_w = fb->width;
   2874       1.5  riastrad 		crtc_h = fb->height;
   2875       1.5  riastrad 		src_w = fb->width << 16;
   2876       1.5  riastrad 		src_h = fb->height << 16;
   2877       1.5  riastrad 	}
   2878       1.5  riastrad 
   2879       1.5  riastrad 	/*
   2880       1.5  riastrad 	 * setplane_internal will take care of deref'ing either the old or new
   2881       1.5  riastrad 	 * framebuffer depending on success.
   2882       1.5  riastrad 	 */
   2883       1.5  riastrad 	ret = __setplane_internal(crtc->cursor, crtc, fb,
   2884       1.5  riastrad 				crtc_x, crtc_y, crtc_w, crtc_h,
   2885       1.5  riastrad 				0, 0, src_w, src_h);
   2886       1.5  riastrad 
   2887       1.5  riastrad 	/* Update successful; save new cursor position, if necessary */
   2888       1.5  riastrad 	if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
   2889       1.5  riastrad 		crtc->cursor_x = req->x;
   2890       1.5  riastrad 		crtc->cursor_y = req->y;
   2891       1.5  riastrad 	}
   2892       1.5  riastrad 
   2893       1.5  riastrad 	return ret;
   2894       1.5  riastrad }
   2895       1.5  riastrad 
   2896       1.3  riastrad static int drm_mode_cursor_common(struct drm_device *dev,
   2897       1.3  riastrad 				  struct drm_mode_cursor2 *req,
   2898       1.3  riastrad 				  struct drm_file *file_priv)
   2899       1.1  riastrad {
   2900       1.1  riastrad 	struct drm_crtc *crtc;
   2901       1.1  riastrad 	int ret = 0;
   2902       1.1  riastrad 
   2903       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2904       1.1  riastrad 		return -EINVAL;
   2905       1.1  riastrad 
   2906       1.1  riastrad 	if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
   2907       1.1  riastrad 		return -EINVAL;
   2908       1.1  riastrad 
   2909       1.5  riastrad 	crtc = drm_crtc_find(dev, req->crtc_id);
   2910       1.5  riastrad 	if (!crtc) {
   2911       1.1  riastrad 		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
   2912       1.3  riastrad 		return -ENOENT;
   2913       1.1  riastrad 	}
   2914       1.1  riastrad 
   2915       1.5  riastrad 	/*
   2916       1.5  riastrad 	 * If this crtc has a universal cursor plane, call that plane's update
   2917       1.5  riastrad 	 * handler rather than using legacy cursor handlers.
   2918       1.5  riastrad 	 */
   2919       1.5  riastrad 	drm_modeset_lock_crtc(crtc, crtc->cursor);
   2920       1.5  riastrad 	if (crtc->cursor) {
   2921       1.5  riastrad 		ret = drm_mode_cursor_universal(crtc, req, file_priv);
   2922       1.5  riastrad 		goto out;
   2923       1.5  riastrad 	}
   2924       1.5  riastrad 
   2925       1.1  riastrad 	if (req->flags & DRM_MODE_CURSOR_BO) {
   2926       1.3  riastrad 		if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
   2927       1.1  riastrad 			ret = -ENXIO;
   2928       1.1  riastrad 			goto out;
   2929       1.1  riastrad 		}
   2930       1.1  riastrad 		/* Turns off the cursor if handle is 0 */
   2931       1.3  riastrad 		if (crtc->funcs->cursor_set2)
   2932       1.3  riastrad 			ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
   2933       1.3  riastrad 						      req->width, req->height, req->hot_x, req->hot_y);
   2934       1.3  riastrad 		else
   2935       1.3  riastrad 			ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
   2936       1.3  riastrad 						      req->width, req->height);
   2937       1.1  riastrad 	}
   2938       1.1  riastrad 
   2939       1.1  riastrad 	if (req->flags & DRM_MODE_CURSOR_MOVE) {
   2940       1.1  riastrad 		if (crtc->funcs->cursor_move) {
   2941       1.1  riastrad 			ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
   2942       1.1  riastrad 		} else {
   2943       1.1  riastrad 			ret = -EFAULT;
   2944       1.1  riastrad 			goto out;
   2945       1.1  riastrad 		}
   2946       1.1  riastrad 	}
   2947       1.1  riastrad out:
   2948       1.5  riastrad 	drm_modeset_unlock_crtc(crtc);
   2949       1.3  riastrad 
   2950       1.1  riastrad 	return ret;
   2951       1.3  riastrad 
   2952       1.3  riastrad }
   2953       1.3  riastrad 
   2954       1.3  riastrad 
   2955       1.3  riastrad /**
   2956       1.3  riastrad  * drm_mode_cursor_ioctl - set CRTC's cursor configuration
   2957       1.3  riastrad  * @dev: drm device for the ioctl
   2958       1.3  riastrad  * @data: data pointer for the ioctl
   2959       1.3  riastrad  * @file_priv: drm file for the ioctl call
   2960       1.3  riastrad  *
   2961       1.3  riastrad  * Set the cursor configuration based on user request.
   2962       1.3  riastrad  *
   2963       1.3  riastrad  * Called by the user via ioctl.
   2964       1.3  riastrad  *
   2965       1.3  riastrad  * Returns:
   2966       1.5  riastrad  * Zero on success, negative errno on failure.
   2967       1.3  riastrad  */
   2968       1.3  riastrad int drm_mode_cursor_ioctl(struct drm_device *dev,
   2969       1.3  riastrad 			  void *data, struct drm_file *file_priv)
   2970       1.3  riastrad {
   2971       1.3  riastrad 	struct drm_mode_cursor *req = data;
   2972       1.3  riastrad 	struct drm_mode_cursor2 new_req;
   2973       1.3  riastrad 
   2974       1.3  riastrad 	memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
   2975       1.3  riastrad 	new_req.hot_x = new_req.hot_y = 0;
   2976       1.3  riastrad 
   2977       1.3  riastrad 	return drm_mode_cursor_common(dev, &new_req, file_priv);
   2978       1.3  riastrad }
   2979       1.3  riastrad 
   2980       1.3  riastrad /**
   2981       1.3  riastrad  * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
   2982       1.3  riastrad  * @dev: drm device for the ioctl
   2983       1.3  riastrad  * @data: data pointer for the ioctl
   2984       1.3  riastrad  * @file_priv: drm file for the ioctl call
   2985       1.3  riastrad  *
   2986       1.3  riastrad  * Set the cursor configuration based on user request. This implements the 2nd
   2987       1.3  riastrad  * version of the cursor ioctl, which allows userspace to additionally specify
   2988       1.3  riastrad  * the hotspot of the pointer.
   2989       1.3  riastrad  *
   2990       1.3  riastrad  * Called by the user via ioctl.
   2991       1.3  riastrad  *
   2992       1.3  riastrad  * Returns:
   2993       1.5  riastrad  * Zero on success, negative errno on failure.
   2994       1.3  riastrad  */
   2995       1.3  riastrad int drm_mode_cursor2_ioctl(struct drm_device *dev,
   2996       1.3  riastrad 			   void *data, struct drm_file *file_priv)
   2997       1.3  riastrad {
   2998       1.3  riastrad 	struct drm_mode_cursor2 *req = data;
   2999       1.5  riastrad 
   3000       1.3  riastrad 	return drm_mode_cursor_common(dev, req, file_priv);
   3001       1.1  riastrad }
   3002       1.1  riastrad 
   3003       1.3  riastrad /**
   3004       1.3  riastrad  * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
   3005       1.3  riastrad  * @bpp: bits per pixels
   3006       1.3  riastrad  * @depth: bit depth per pixel
   3007       1.3  riastrad  *
   3008       1.3  riastrad  * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
   3009       1.3  riastrad  * Useful in fbdev emulation code, since that deals in those values.
   3010       1.3  riastrad  */
   3011       1.1  riastrad uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
   3012       1.1  riastrad {
   3013       1.1  riastrad 	uint32_t fmt;
   3014       1.1  riastrad 
   3015       1.1  riastrad 	switch (bpp) {
   3016       1.1  riastrad 	case 8:
   3017       1.3  riastrad 		fmt = DRM_FORMAT_C8;
   3018       1.1  riastrad 		break;
   3019       1.1  riastrad 	case 16:
   3020       1.1  riastrad 		if (depth == 15)
   3021       1.1  riastrad 			fmt = DRM_FORMAT_XRGB1555;
   3022       1.1  riastrad 		else
   3023       1.1  riastrad 			fmt = DRM_FORMAT_RGB565;
   3024       1.1  riastrad 		break;
   3025       1.1  riastrad 	case 24:
   3026       1.1  riastrad 		fmt = DRM_FORMAT_RGB888;
   3027       1.1  riastrad 		break;
   3028       1.1  riastrad 	case 32:
   3029       1.1  riastrad 		if (depth == 24)
   3030       1.1  riastrad 			fmt = DRM_FORMAT_XRGB8888;
   3031       1.1  riastrad 		else if (depth == 30)
   3032       1.1  riastrad 			fmt = DRM_FORMAT_XRGB2101010;
   3033       1.1  riastrad 		else
   3034       1.1  riastrad 			fmt = DRM_FORMAT_ARGB8888;
   3035       1.1  riastrad 		break;
   3036       1.1  riastrad 	default:
   3037       1.1  riastrad 		DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
   3038       1.1  riastrad 		fmt = DRM_FORMAT_XRGB8888;
   3039       1.1  riastrad 		break;
   3040       1.1  riastrad 	}
   3041       1.1  riastrad 
   3042       1.1  riastrad 	return fmt;
   3043       1.1  riastrad }
   3044       1.1  riastrad EXPORT_SYMBOL(drm_mode_legacy_fb_format);
   3045       1.1  riastrad 
   3046       1.1  riastrad /**
   3047       1.1  riastrad  * drm_mode_addfb - add an FB to the graphics configuration
   3048       1.3  riastrad  * @dev: drm device for the ioctl
   3049       1.3  riastrad  * @data: data pointer for the ioctl
   3050       1.3  riastrad  * @file_priv: drm file for the ioctl call
   3051       1.1  riastrad  *
   3052       1.3  riastrad  * Add a new FB to the specified CRTC, given a user request. This is the
   3053       1.5  riastrad  * original addfb ioctl which only supported RGB formats.
   3054       1.1  riastrad  *
   3055       1.1  riastrad  * Called by the user via ioctl.
   3056       1.1  riastrad  *
   3057       1.3  riastrad  * Returns:
   3058       1.5  riastrad  * Zero on success, negative errno on failure.
   3059       1.1  riastrad  */
   3060       1.1  riastrad int drm_mode_addfb(struct drm_device *dev,
   3061       1.1  riastrad 		   void *data, struct drm_file *file_priv)
   3062       1.1  riastrad {
   3063       1.1  riastrad 	struct drm_mode_fb_cmd *or = data;
   3064  1.11.6.1        ad 	struct drm_mode_fb_cmd2 r = {};
   3065       1.8  riastrad 	int ret;
   3066       1.1  riastrad 
   3067       1.5  riastrad 	/* convert to new format and call new ioctl */
   3068       1.1  riastrad 	r.fb_id = or->fb_id;
   3069       1.1  riastrad 	r.width = or->width;
   3070       1.1  riastrad 	r.height = or->height;
   3071       1.1  riastrad 	r.pitches[0] = or->pitch;
   3072       1.1  riastrad 	r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
   3073       1.1  riastrad 	r.handles[0] = or->handle;
   3074       1.1  riastrad 
   3075       1.5  riastrad 	ret = drm_mode_addfb2(dev, &r, file_priv);
   3076       1.5  riastrad 	if (ret)
   3077       1.5  riastrad 		return ret;
   3078       1.1  riastrad 
   3079       1.5  riastrad 	or->fb_id = r.fb_id;
   3080       1.1  riastrad 
   3081       1.5  riastrad 	return 0;
   3082       1.1  riastrad }
   3083       1.1  riastrad 
   3084       1.1  riastrad static int format_check(const struct drm_mode_fb_cmd2 *r)
   3085       1.1  riastrad {
   3086       1.1  riastrad 	uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
   3087       1.1  riastrad 
   3088       1.1  riastrad 	switch (format) {
   3089       1.1  riastrad 	case DRM_FORMAT_C8:
   3090       1.1  riastrad 	case DRM_FORMAT_RGB332:
   3091       1.1  riastrad 	case DRM_FORMAT_BGR233:
   3092       1.1  riastrad 	case DRM_FORMAT_XRGB4444:
   3093       1.1  riastrad 	case DRM_FORMAT_XBGR4444:
   3094       1.1  riastrad 	case DRM_FORMAT_RGBX4444:
   3095       1.1  riastrad 	case DRM_FORMAT_BGRX4444:
   3096       1.1  riastrad 	case DRM_FORMAT_ARGB4444:
   3097       1.1  riastrad 	case DRM_FORMAT_ABGR4444:
   3098       1.1  riastrad 	case DRM_FORMAT_RGBA4444:
   3099       1.1  riastrad 	case DRM_FORMAT_BGRA4444:
   3100       1.1  riastrad 	case DRM_FORMAT_XRGB1555:
   3101       1.1  riastrad 	case DRM_FORMAT_XBGR1555:
   3102       1.1  riastrad 	case DRM_FORMAT_RGBX5551:
   3103       1.1  riastrad 	case DRM_FORMAT_BGRX5551:
   3104       1.1  riastrad 	case DRM_FORMAT_ARGB1555:
   3105       1.1  riastrad 	case DRM_FORMAT_ABGR1555:
   3106       1.1  riastrad 	case DRM_FORMAT_RGBA5551:
   3107       1.1  riastrad 	case DRM_FORMAT_BGRA5551:
   3108       1.1  riastrad 	case DRM_FORMAT_RGB565:
   3109       1.1  riastrad 	case DRM_FORMAT_BGR565:
   3110       1.1  riastrad 	case DRM_FORMAT_RGB888:
   3111       1.1  riastrad 	case DRM_FORMAT_BGR888:
   3112       1.1  riastrad 	case DRM_FORMAT_XRGB8888:
   3113       1.1  riastrad 	case DRM_FORMAT_XBGR8888:
   3114       1.1  riastrad 	case DRM_FORMAT_RGBX8888:
   3115       1.1  riastrad 	case DRM_FORMAT_BGRX8888:
   3116       1.1  riastrad 	case DRM_FORMAT_ARGB8888:
   3117       1.1  riastrad 	case DRM_FORMAT_ABGR8888:
   3118       1.1  riastrad 	case DRM_FORMAT_RGBA8888:
   3119       1.1  riastrad 	case DRM_FORMAT_BGRA8888:
   3120       1.1  riastrad 	case DRM_FORMAT_XRGB2101010:
   3121       1.1  riastrad 	case DRM_FORMAT_XBGR2101010:
   3122       1.1  riastrad 	case DRM_FORMAT_RGBX1010102:
   3123       1.1  riastrad 	case DRM_FORMAT_BGRX1010102:
   3124       1.1  riastrad 	case DRM_FORMAT_ARGB2101010:
   3125       1.1  riastrad 	case DRM_FORMAT_ABGR2101010:
   3126       1.1  riastrad 	case DRM_FORMAT_RGBA1010102:
   3127       1.1  riastrad 	case DRM_FORMAT_BGRA1010102:
   3128       1.1  riastrad 	case DRM_FORMAT_YUYV:
   3129       1.1  riastrad 	case DRM_FORMAT_YVYU:
   3130       1.1  riastrad 	case DRM_FORMAT_UYVY:
   3131       1.1  riastrad 	case DRM_FORMAT_VYUY:
   3132       1.1  riastrad 	case DRM_FORMAT_AYUV:
   3133       1.1  riastrad 	case DRM_FORMAT_NV12:
   3134       1.1  riastrad 	case DRM_FORMAT_NV21:
   3135       1.1  riastrad 	case DRM_FORMAT_NV16:
   3136       1.1  riastrad 	case DRM_FORMAT_NV61:
   3137       1.1  riastrad 	case DRM_FORMAT_NV24:
   3138       1.1  riastrad 	case DRM_FORMAT_NV42:
   3139       1.1  riastrad 	case DRM_FORMAT_YUV410:
   3140       1.1  riastrad 	case DRM_FORMAT_YVU410:
   3141       1.1  riastrad 	case DRM_FORMAT_YUV411:
   3142       1.1  riastrad 	case DRM_FORMAT_YVU411:
   3143       1.1  riastrad 	case DRM_FORMAT_YUV420:
   3144       1.1  riastrad 	case DRM_FORMAT_YVU420:
   3145       1.1  riastrad 	case DRM_FORMAT_YUV422:
   3146       1.1  riastrad 	case DRM_FORMAT_YVU422:
   3147       1.1  riastrad 	case DRM_FORMAT_YUV444:
   3148       1.1  riastrad 	case DRM_FORMAT_YVU444:
   3149       1.1  riastrad 		return 0;
   3150       1.1  riastrad 	default:
   3151       1.3  riastrad 		DRM_DEBUG_KMS("invalid pixel format %s\n",
   3152       1.3  riastrad 			      drm_get_format_name(r->pixel_format));
   3153       1.1  riastrad 		return -EINVAL;
   3154       1.1  riastrad 	}
   3155       1.1  riastrad }
   3156       1.1  riastrad 
   3157       1.1  riastrad static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
   3158       1.1  riastrad {
   3159       1.1  riastrad 	int ret, hsub, vsub, num_planes, i;
   3160       1.1  riastrad 
   3161       1.1  riastrad 	ret = format_check(r);
   3162       1.1  riastrad 	if (ret) {
   3163       1.3  riastrad 		DRM_DEBUG_KMS("bad framebuffer format %s\n",
   3164       1.3  riastrad 			      drm_get_format_name(r->pixel_format));
   3165       1.1  riastrad 		return ret;
   3166       1.1  riastrad 	}
   3167       1.1  riastrad 
   3168       1.1  riastrad 	hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
   3169       1.1  riastrad 	vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
   3170       1.1  riastrad 	num_planes = drm_format_num_planes(r->pixel_format);
   3171       1.1  riastrad 
   3172       1.1  riastrad 	if (r->width == 0 || r->width % hsub) {
   3173       1.5  riastrad 		DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
   3174       1.1  riastrad 		return -EINVAL;
   3175       1.1  riastrad 	}
   3176       1.1  riastrad 
   3177       1.1  riastrad 	if (r->height == 0 || r->height % vsub) {
   3178       1.1  riastrad 		DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
   3179       1.1  riastrad 		return -EINVAL;
   3180       1.1  riastrad 	}
   3181       1.1  riastrad 
   3182       1.1  riastrad 	for (i = 0; i < num_planes; i++) {
   3183       1.1  riastrad 		unsigned int width = r->width / (i != 0 ? hsub : 1);
   3184       1.1  riastrad 		unsigned int height = r->height / (i != 0 ? vsub : 1);
   3185       1.1  riastrad 		unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
   3186       1.1  riastrad 
   3187       1.1  riastrad 		if (!r->handles[i]) {
   3188       1.1  riastrad 			DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
   3189       1.1  riastrad 			return -EINVAL;
   3190       1.1  riastrad 		}
   3191       1.1  riastrad 
   3192       1.1  riastrad 		if ((uint64_t) width * cpp > UINT_MAX)
   3193       1.1  riastrad 			return -ERANGE;
   3194       1.1  riastrad 
   3195       1.1  riastrad 		if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
   3196       1.1  riastrad 			return -ERANGE;
   3197       1.1  riastrad 
   3198       1.1  riastrad 		if (r->pitches[i] < width * cpp) {
   3199       1.1  riastrad 			DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
   3200       1.1  riastrad 			return -EINVAL;
   3201       1.1  riastrad 		}
   3202       1.5  riastrad 
   3203       1.5  riastrad 		if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
   3204       1.8  riastrad 			DRM_DEBUG_KMS("bad fb modifier %"PRIu64" for plane %d\n",
   3205       1.5  riastrad 				      r->modifier[i], i);
   3206       1.5  riastrad 			return -EINVAL;
   3207       1.5  riastrad 		}
   3208       1.5  riastrad 
   3209       1.5  riastrad 		/* modifier specific checks: */
   3210       1.5  riastrad 		switch (r->modifier[i]) {
   3211       1.5  riastrad 		case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
   3212       1.5  riastrad 			/* NOTE: the pitch restriction may be lifted later if it turns
   3213       1.5  riastrad 			 * out that no hw has this restriction:
   3214       1.5  riastrad 			 */
   3215       1.5  riastrad 			if (r->pixel_format != DRM_FORMAT_NV12 ||
   3216       1.5  riastrad 					width % 128 || height % 32 ||
   3217       1.5  riastrad 					r->pitches[i] % 128) {
   3218       1.5  riastrad 				DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
   3219       1.5  riastrad 				return -EINVAL;
   3220       1.5  riastrad 			}
   3221       1.5  riastrad 			break;
   3222       1.5  riastrad 
   3223       1.5  riastrad 		default:
   3224       1.5  riastrad 			break;
   3225       1.5  riastrad 		}
   3226       1.5  riastrad 	}
   3227       1.5  riastrad 
   3228       1.5  riastrad 	for (i = num_planes; i < 4; i++) {
   3229       1.5  riastrad 		if (r->modifier[i]) {
   3230       1.5  riastrad 			DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
   3231       1.5  riastrad 			return -EINVAL;
   3232       1.5  riastrad 		}
   3233       1.5  riastrad 
   3234       1.5  riastrad 		/* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
   3235       1.5  riastrad 		if (!(r->flags & DRM_MODE_FB_MODIFIERS))
   3236       1.5  riastrad 			continue;
   3237       1.5  riastrad 
   3238       1.5  riastrad 		if (r->handles[i]) {
   3239       1.5  riastrad 			DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
   3240       1.5  riastrad 			return -EINVAL;
   3241       1.5  riastrad 		}
   3242       1.5  riastrad 
   3243       1.5  riastrad 		if (r->pitches[i]) {
   3244       1.5  riastrad 			DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
   3245       1.5  riastrad 			return -EINVAL;
   3246       1.5  riastrad 		}
   3247       1.5  riastrad 
   3248       1.5  riastrad 		if (r->offsets[i]) {
   3249       1.5  riastrad 			DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
   3250       1.5  riastrad 			return -EINVAL;
   3251       1.5  riastrad 		}
   3252       1.1  riastrad 	}
   3253       1.1  riastrad 
   3254       1.1  riastrad 	return 0;
   3255       1.1  riastrad }
   3256       1.1  riastrad 
   3257       1.5  riastrad static struct drm_framebuffer *
   3258       1.5  riastrad internal_framebuffer_create(struct drm_device *dev,
   3259       1.5  riastrad 			    struct drm_mode_fb_cmd2 *r,
   3260       1.5  riastrad 			    struct drm_file *file_priv)
   3261       1.5  riastrad {
   3262       1.5  riastrad 	struct drm_mode_config *config = &dev->mode_config;
   3263       1.5  riastrad 	struct drm_framebuffer *fb;
   3264       1.5  riastrad 	int ret;
   3265       1.5  riastrad 
   3266       1.5  riastrad 	if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
   3267       1.5  riastrad 		DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
   3268       1.5  riastrad 		return ERR_PTR(-EINVAL);
   3269       1.5  riastrad 	}
   3270       1.5  riastrad 
   3271       1.5  riastrad 	if ((config->min_width > r->width) || (r->width > config->max_width)) {
   3272       1.5  riastrad 		DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
   3273       1.5  riastrad 			  r->width, config->min_width, config->max_width);
   3274       1.5  riastrad 		return ERR_PTR(-EINVAL);
   3275       1.5  riastrad 	}
   3276       1.5  riastrad 	if ((config->min_height > r->height) || (r->height > config->max_height)) {
   3277       1.5  riastrad 		DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
   3278       1.5  riastrad 			  r->height, config->min_height, config->max_height);
   3279       1.5  riastrad 		return ERR_PTR(-EINVAL);
   3280       1.5  riastrad 	}
   3281       1.5  riastrad 
   3282       1.5  riastrad 	if (r->flags & DRM_MODE_FB_MODIFIERS &&
   3283       1.5  riastrad 	    !dev->mode_config.allow_fb_modifiers) {
   3284       1.5  riastrad 		DRM_DEBUG_KMS("driver does not support fb modifiers\n");
   3285       1.5  riastrad 		return ERR_PTR(-EINVAL);
   3286       1.5  riastrad 	}
   3287       1.5  riastrad 
   3288       1.5  riastrad 	ret = framebuffer_check(r);
   3289       1.5  riastrad 	if (ret)
   3290       1.5  riastrad 		return ERR_PTR(ret);
   3291       1.5  riastrad 
   3292       1.5  riastrad 	fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
   3293       1.5  riastrad 	if (IS_ERR(fb)) {
   3294       1.5  riastrad 		DRM_DEBUG_KMS("could not create framebuffer\n");
   3295       1.5  riastrad 		return fb;
   3296       1.5  riastrad 	}
   3297       1.5  riastrad 
   3298       1.5  riastrad 	return fb;
   3299       1.5  riastrad }
   3300       1.5  riastrad 
   3301       1.1  riastrad /**
   3302       1.1  riastrad  * drm_mode_addfb2 - add an FB to the graphics configuration
   3303       1.3  riastrad  * @dev: drm device for the ioctl
   3304       1.3  riastrad  * @data: data pointer for the ioctl
   3305       1.3  riastrad  * @file_priv: drm file for the ioctl call
   3306       1.3  riastrad  *
   3307       1.3  riastrad  * Add a new FB to the specified CRTC, given a user request with format. This is
   3308       1.3  riastrad  * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
   3309       1.3  riastrad  * and uses fourcc codes as pixel format specifiers.
   3310       1.1  riastrad  *
   3311       1.1  riastrad  * Called by the user via ioctl.
   3312       1.1  riastrad  *
   3313       1.3  riastrad  * Returns:
   3314       1.5  riastrad  * Zero on success, negative errno on failure.
   3315       1.1  riastrad  */
   3316       1.1  riastrad int drm_mode_addfb2(struct drm_device *dev,
   3317       1.1  riastrad 		    void *data, struct drm_file *file_priv)
   3318       1.1  riastrad {
   3319       1.1  riastrad 	struct drm_mode_fb_cmd2 *r = data;
   3320       1.1  riastrad 	struct drm_framebuffer *fb;
   3321       1.1  riastrad 
   3322       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   3323       1.1  riastrad 		return -EINVAL;
   3324       1.1  riastrad 
   3325       1.5  riastrad 	fb = internal_framebuffer_create(dev, r, file_priv);
   3326       1.5  riastrad 	if (IS_ERR(fb))
   3327       1.3  riastrad 		return PTR_ERR(fb);
   3328       1.1  riastrad 
   3329       1.5  riastrad 	/* Transfer ownership to the filp for reaping on close */
   3330       1.5  riastrad 
   3331       1.5  riastrad 	DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
   3332       1.3  riastrad 	mutex_lock(&file_priv->fbs_lock);
   3333       1.1  riastrad 	r->fb_id = fb->base.id;
   3334       1.1  riastrad 	list_add(&fb->filp_head, &file_priv->fbs);
   3335       1.3  riastrad 	mutex_unlock(&file_priv->fbs_lock);
   3336       1.3  riastrad 
   3337       1.5  riastrad 	return 0;
   3338       1.5  riastrad }
   3339       1.5  riastrad 
   3340       1.5  riastrad struct drm_mode_rmfb_work {
   3341       1.5  riastrad 	struct work_struct work;
   3342       1.5  riastrad 	struct list_head fbs;
   3343       1.5  riastrad };
   3344       1.5  riastrad 
   3345       1.5  riastrad static void drm_mode_rmfb_work_fn(struct work_struct *w)
   3346       1.5  riastrad {
   3347       1.5  riastrad 	struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
   3348       1.5  riastrad 
   3349       1.5  riastrad 	while (!list_empty(&arg->fbs)) {
   3350       1.5  riastrad 		struct drm_framebuffer *fb =
   3351       1.5  riastrad 			list_first_entry(&arg->fbs, typeof(*fb), filp_head);
   3352       1.1  riastrad 
   3353       1.5  riastrad 		list_del_init(&fb->filp_head);
   3354       1.5  riastrad 		drm_framebuffer_remove(fb);
   3355       1.5  riastrad 	}
   3356       1.1  riastrad }
   3357       1.1  riastrad 
   3358       1.1  riastrad /**
   3359       1.1  riastrad  * drm_mode_rmfb - remove an FB from the configuration
   3360       1.3  riastrad  * @dev: drm device for the ioctl
   3361       1.3  riastrad  * @data: data pointer for the ioctl
   3362       1.3  riastrad  * @file_priv: drm file for the ioctl call
   3363       1.1  riastrad  *
   3364       1.1  riastrad  * Remove the FB specified by the user.
   3365       1.1  riastrad  *
   3366       1.1  riastrad  * Called by the user via ioctl.
   3367       1.1  riastrad  *
   3368       1.3  riastrad  * Returns:
   3369       1.5  riastrad  * Zero on success, negative errno on failure.
   3370       1.1  riastrad  */
   3371       1.1  riastrad int drm_mode_rmfb(struct drm_device *dev,
   3372       1.1  riastrad 		   void *data, struct drm_file *file_priv)
   3373       1.1  riastrad {
   3374       1.1  riastrad 	struct drm_framebuffer *fb = NULL;
   3375       1.1  riastrad 	struct drm_framebuffer *fbl = NULL;
   3376       1.1  riastrad 	uint32_t *id = data;
   3377       1.1  riastrad 	int found = 0;
   3378       1.1  riastrad 
   3379       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   3380       1.1  riastrad 		return -EINVAL;
   3381       1.1  riastrad 
   3382       1.3  riastrad 	mutex_lock(&file_priv->fbs_lock);
   3383       1.3  riastrad 	mutex_lock(&dev->mode_config.fb_lock);
   3384       1.3  riastrad 	fb = __drm_framebuffer_lookup(dev, *id);
   3385       1.3  riastrad 	if (!fb)
   3386       1.3  riastrad 		goto fail_lookup;
   3387       1.1  riastrad 
   3388       1.1  riastrad 	list_for_each_entry(fbl, &file_priv->fbs, filp_head)
   3389       1.1  riastrad 		if (fb == fbl)
   3390       1.1  riastrad 			found = 1;
   3391       1.3  riastrad 	if (!found)
   3392       1.3  riastrad 		goto fail_lookup;
   3393       1.1  riastrad 
   3394       1.3  riastrad 	list_del_init(&fb->filp_head);
   3395       1.3  riastrad 	mutex_unlock(&dev->mode_config.fb_lock);
   3396       1.3  riastrad 	mutex_unlock(&file_priv->fbs_lock);
   3397       1.1  riastrad 
   3398       1.5  riastrad 	/*
   3399       1.5  riastrad 	 * we now own the reference that was stored in the fbs list
   3400       1.5  riastrad 	 *
   3401       1.5  riastrad 	 * drm_framebuffer_remove may fail with -EINTR on pending signals,
   3402       1.5  riastrad 	 * so run this in a separate stack as there's no way to correctly
   3403       1.5  riastrad 	 * handle this after the fb is already removed from the lookup table.
   3404       1.5  riastrad 	 */
   3405       1.6  riastrad 	if (!kref_exclusive_p(&fb->refcount)) {
   3406       1.5  riastrad 		struct drm_mode_rmfb_work arg;
   3407       1.5  riastrad 
   3408       1.5  riastrad 		INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
   3409       1.5  riastrad 		INIT_LIST_HEAD(&arg.fbs);
   3410       1.5  riastrad 		list_add_tail(&fb->filp_head, &arg.fbs);
   3411       1.5  riastrad 
   3412       1.5  riastrad 		schedule_work(&arg.work);
   3413       1.5  riastrad 		flush_work(&arg.work);
   3414       1.5  riastrad 		destroy_work_on_stack(&arg.work);
   3415       1.5  riastrad 	} else
   3416       1.5  riastrad 		drm_framebuffer_unreference(fb);
   3417       1.1  riastrad 
   3418       1.3  riastrad 	return 0;
   3419       1.3  riastrad 
   3420       1.3  riastrad fail_lookup:
   3421       1.3  riastrad 	mutex_unlock(&dev->mode_config.fb_lock);
   3422       1.3  riastrad 	mutex_unlock(&file_priv->fbs_lock);
   3423       1.3  riastrad 
   3424       1.3  riastrad 	return -ENOENT;
   3425       1.1  riastrad }
   3426       1.1  riastrad 
   3427       1.1  riastrad /**
   3428       1.1  riastrad  * drm_mode_getfb - get FB info
   3429       1.3  riastrad  * @dev: drm device for the ioctl
   3430       1.3  riastrad  * @data: data pointer for the ioctl
   3431       1.3  riastrad  * @file_priv: drm file for the ioctl call
   3432       1.1  riastrad  *
   3433       1.1  riastrad  * Lookup the FB given its ID and return info about it.
   3434       1.1  riastrad  *
   3435       1.1  riastrad  * Called by the user via ioctl.
   3436       1.1  riastrad  *
   3437       1.3  riastrad  * Returns:
   3438       1.5  riastrad  * Zero on success, negative errno on failure.
   3439       1.1  riastrad  */
   3440       1.1  riastrad int drm_mode_getfb(struct drm_device *dev,
   3441       1.1  riastrad 		   void *data, struct drm_file *file_priv)
   3442       1.1  riastrad {
   3443       1.1  riastrad 	struct drm_mode_fb_cmd *r = data;
   3444       1.1  riastrad 	struct drm_framebuffer *fb;
   3445       1.3  riastrad 	int ret;
   3446       1.1  riastrad 
   3447       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   3448       1.1  riastrad 		return -EINVAL;
   3449       1.1  riastrad 
   3450       1.3  riastrad 	fb = drm_framebuffer_lookup(dev, r->fb_id);
   3451       1.3  riastrad 	if (!fb)
   3452       1.3  riastrad 		return -ENOENT;
   3453       1.1  riastrad 
   3454       1.1  riastrad 	r->height = fb->height;
   3455       1.1  riastrad 	r->width = fb->width;
   3456       1.1  riastrad 	r->depth = fb->depth;
   3457       1.1  riastrad 	r->bpp = fb->bits_per_pixel;
   3458       1.1  riastrad 	r->pitch = fb->pitches[0];
   3459       1.3  riastrad 	if (fb->funcs->create_handle) {
   3460  1.11.6.1        ad 		if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
   3461       1.3  riastrad 		    drm_is_control_client(file_priv)) {
   3462       1.3  riastrad 			ret = fb->funcs->create_handle(fb, file_priv,
   3463       1.3  riastrad 						       &r->handle);
   3464       1.3  riastrad 		} else {
   3465       1.3  riastrad 			/* GET_FB() is an unprivileged ioctl so we must not
   3466       1.3  riastrad 			 * return a buffer-handle to non-master processes! For
   3467       1.3  riastrad 			 * backwards-compatibility reasons, we cannot make
   3468       1.3  riastrad 			 * GET_FB() privileged, so just return an invalid handle
   3469       1.3  riastrad 			 * for non-masters. */
   3470       1.3  riastrad 			r->handle = 0;
   3471       1.3  riastrad 			ret = 0;
   3472       1.3  riastrad 		}
   3473       1.3  riastrad 	} else {
   3474       1.3  riastrad 		ret = -ENODEV;
   3475       1.3  riastrad 	}
   3476       1.3  riastrad 
   3477       1.3  riastrad 	drm_framebuffer_unreference(fb);
   3478       1.1  riastrad 
   3479       1.1  riastrad 	return ret;
   3480       1.1  riastrad }
   3481       1.1  riastrad 
   3482       1.3  riastrad /**
   3483       1.3  riastrad  * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
   3484       1.3  riastrad  * @dev: drm device for the ioctl
   3485       1.3  riastrad  * @data: data pointer for the ioctl
   3486       1.3  riastrad  * @file_priv: drm file for the ioctl call
   3487       1.3  riastrad  *
   3488       1.3  riastrad  * Lookup the FB and flush out the damaged area supplied by userspace as a clip
   3489       1.3  riastrad  * rectangle list. Generic userspace which does frontbuffer rendering must call
   3490       1.3  riastrad  * this ioctl to flush out the changes on manual-update display outputs, e.g.
   3491       1.3  riastrad  * usb display-link, mipi manual update panels or edp panel self refresh modes.
   3492       1.3  riastrad  *
   3493       1.3  riastrad  * Modesetting drivers which always update the frontbuffer do not need to
   3494       1.3  riastrad  * implement the corresponding ->dirty framebuffer callback.
   3495       1.3  riastrad  *
   3496       1.3  riastrad  * Called by the user via ioctl.
   3497       1.3  riastrad  *
   3498       1.3  riastrad  * Returns:
   3499       1.5  riastrad  * Zero on success, negative errno on failure.
   3500       1.3  riastrad  */
   3501       1.1  riastrad int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
   3502       1.1  riastrad 			   void *data, struct drm_file *file_priv)
   3503       1.1  riastrad {
   3504       1.1  riastrad 	struct drm_clip_rect __user *clips_ptr;
   3505       1.1  riastrad 	struct drm_clip_rect *clips = NULL;
   3506       1.1  riastrad 	struct drm_mode_fb_dirty_cmd *r = data;
   3507       1.1  riastrad 	struct drm_framebuffer *fb;
   3508       1.1  riastrad 	unsigned flags;
   3509       1.1  riastrad 	int num_clips;
   3510       1.1  riastrad 	int ret;
   3511       1.1  riastrad 
   3512       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   3513       1.1  riastrad 		return -EINVAL;
   3514       1.1  riastrad 
   3515       1.3  riastrad 	fb = drm_framebuffer_lookup(dev, r->fb_id);
   3516       1.3  riastrad 	if (!fb)
   3517       1.3  riastrad 		return -ENOENT;
   3518       1.1  riastrad 
   3519       1.1  riastrad 	num_clips = r->num_clips;
   3520       1.1  riastrad 	clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
   3521       1.1  riastrad 
   3522       1.1  riastrad 	if (!num_clips != !clips_ptr) {
   3523       1.1  riastrad 		ret = -EINVAL;
   3524       1.1  riastrad 		goto out_err1;
   3525       1.1  riastrad 	}
   3526       1.1  riastrad 
   3527       1.1  riastrad 	flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
   3528       1.1  riastrad 
   3529       1.1  riastrad 	/* If userspace annotates copy, clips must come in pairs */
   3530       1.1  riastrad 	if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
   3531       1.1  riastrad 		ret = -EINVAL;
   3532       1.1  riastrad 		goto out_err1;
   3533       1.1  riastrad 	}
   3534       1.1  riastrad 
   3535       1.1  riastrad 	if (num_clips && clips_ptr) {
   3536       1.1  riastrad 		if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
   3537       1.1  riastrad 			ret = -EINVAL;
   3538       1.1  riastrad 			goto out_err1;
   3539       1.1  riastrad 		}
   3540       1.5  riastrad 		clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
   3541       1.1  riastrad 		if (!clips) {
   3542       1.1  riastrad 			ret = -ENOMEM;
   3543       1.1  riastrad 			goto out_err1;
   3544       1.1  riastrad 		}
   3545       1.1  riastrad 
   3546       1.1  riastrad 		ret = copy_from_user(clips, clips_ptr,
   3547       1.1  riastrad 				     num_clips * sizeof(*clips));
   3548       1.1  riastrad 		if (ret) {
   3549       1.1  riastrad 			ret = -EFAULT;
   3550       1.1  riastrad 			goto out_err2;
   3551       1.1  riastrad 		}
   3552       1.1  riastrad 	}
   3553       1.1  riastrad 
   3554       1.1  riastrad 	if (fb->funcs->dirty) {
   3555       1.1  riastrad 		ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
   3556       1.1  riastrad 				       clips, num_clips);
   3557       1.1  riastrad 	} else {
   3558       1.1  riastrad 		ret = -ENOSYS;
   3559       1.1  riastrad 	}
   3560       1.1  riastrad 
   3561       1.1  riastrad out_err2:
   3562       1.1  riastrad 	kfree(clips);
   3563       1.1  riastrad out_err1:
   3564       1.3  riastrad 	drm_framebuffer_unreference(fb);
   3565       1.3  riastrad 
   3566       1.1  riastrad 	return ret;
   3567       1.1  riastrad }
   3568       1.1  riastrad 
   3569       1.1  riastrad /**
   3570       1.1  riastrad  * drm_fb_release - remove and free the FBs on this file
   3571       1.3  riastrad  * @priv: drm file for the ioctl
   3572       1.1  riastrad  *
   3573       1.1  riastrad  * Destroy all the FBs associated with @filp.
   3574       1.1  riastrad  *
   3575       1.1  riastrad  * Called by the user via ioctl.
   3576       1.1  riastrad  *
   3577       1.3  riastrad  * Returns:
   3578       1.5  riastrad  * Zero on success, negative errno on failure.
   3579       1.1  riastrad  */
   3580       1.1  riastrad void drm_fb_release(struct drm_file *priv)
   3581       1.1  riastrad {
   3582       1.1  riastrad 	struct drm_framebuffer *fb, *tfb;
   3583       1.5  riastrad 	struct drm_mode_rmfb_work arg;
   3584       1.1  riastrad 
   3585       1.5  riastrad 	INIT_LIST_HEAD(&arg.fbs);
   3586       1.5  riastrad 
   3587       1.5  riastrad 	/*
   3588       1.5  riastrad 	 * When the file gets released that means no one else can access the fb
   3589       1.5  riastrad 	 * list any more, so no need to grab fpriv->fbs_lock. And we need to
   3590       1.5  riastrad 	 * avoid upsetting lockdep since the universal cursor code adds a
   3591       1.5  riastrad 	 * framebuffer while holding mutex locks.
   3592       1.5  riastrad 	 *
   3593       1.5  riastrad 	 * Note that a real deadlock between fpriv->fbs_lock and the modeset
   3594       1.5  riastrad 	 * locks is impossible here since no one else but this function can get
   3595       1.5  riastrad 	 * at it any more.
   3596       1.5  riastrad 	 */
   3597       1.1  riastrad 	list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
   3598       1.6  riastrad 		if (!kref_exclusive_p(&fb->refcount)) {
   3599       1.5  riastrad 			list_move_tail(&fb->filp_head, &arg.fbs);
   3600       1.5  riastrad 		} else {
   3601       1.5  riastrad 			list_del_init(&fb->filp_head);
   3602       1.1  riastrad 
   3603       1.5  riastrad 			/* This drops the fpriv->fbs reference. */
   3604       1.5  riastrad 			drm_framebuffer_unreference(fb);
   3605       1.5  riastrad 		}
   3606       1.5  riastrad 	}
   3607       1.1  riastrad 
   3608       1.5  riastrad 	if (!list_empty(&arg.fbs)) {
   3609       1.5  riastrad 		INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
   3610       1.1  riastrad 
   3611       1.5  riastrad 		schedule_work(&arg.work);
   3612       1.5  riastrad 		flush_work(&arg.work);
   3613       1.5  riastrad 		destroy_work_on_stack(&arg.work);
   3614       1.1  riastrad 	}
   3615       1.1  riastrad }
   3616       1.1  riastrad 
   3617       1.1  riastrad /**
   3618       1.3  riastrad  * drm_property_create - create a new property type
   3619       1.3  riastrad  * @dev: drm device
   3620       1.3  riastrad  * @flags: flags specifying the property type
   3621       1.3  riastrad  * @name: name of the property
   3622       1.3  riastrad  * @num_values: number of pre-defined values
   3623       1.1  riastrad  *
   3624       1.3  riastrad  * This creates a new generic drm property which can then be attached to a drm
   3625       1.3  riastrad  * object with drm_object_attach_property. The returned property object must be
   3626       1.3  riastrad  * freed with drm_property_destroy.
   3627       1.1  riastrad  *
   3628       1.5  riastrad  * Note that the DRM core keeps a per-device list of properties and that, if
   3629       1.5  riastrad  * drm_mode_config_cleanup() is called, it will destroy all properties created
   3630       1.5  riastrad  * by the driver.
   3631       1.5  riastrad  *
   3632       1.3  riastrad  * Returns:
   3633       1.3  riastrad  * A pointer to the newly created property on success, NULL on failure.
   3634       1.1  riastrad  */
   3635       1.1  riastrad struct drm_property *drm_property_create(struct drm_device *dev, int flags,
   3636       1.1  riastrad 					 const char *name, int num_values)
   3637       1.1  riastrad {
   3638       1.1  riastrad 	struct drm_property *property = NULL;
   3639       1.1  riastrad 	int ret;
   3640       1.1  riastrad 
   3641       1.1  riastrad 	property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
   3642       1.1  riastrad 	if (!property)
   3643       1.1  riastrad 		return NULL;
   3644       1.1  riastrad 
   3645       1.5  riastrad 	property->dev = dev;
   3646       1.5  riastrad 
   3647       1.1  riastrad 	if (num_values) {
   3648       1.5  riastrad 		property->values = kcalloc(num_values, sizeof(uint64_t),
   3649       1.5  riastrad 					   GFP_KERNEL);
   3650       1.1  riastrad 		if (!property->values)
   3651       1.1  riastrad 			goto fail;
   3652       1.1  riastrad 	}
   3653       1.1  riastrad 
   3654       1.1  riastrad 	ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
   3655       1.1  riastrad 	if (ret)
   3656       1.1  riastrad 		goto fail;
   3657       1.1  riastrad 
   3658       1.1  riastrad 	property->flags = flags;
   3659       1.1  riastrad 	property->num_values = num_values;
   3660       1.5  riastrad 	INIT_LIST_HEAD(&property->enum_list);
   3661       1.1  riastrad 
   3662       1.1  riastrad 	if (name) {
   3663       1.1  riastrad 		strncpy(property->name, name, DRM_PROP_NAME_LEN);
   3664       1.1  riastrad 		property->name[DRM_PROP_NAME_LEN-1] = '\0';
   3665       1.1  riastrad 	}
   3666       1.1  riastrad 
   3667       1.1  riastrad 	list_add_tail(&property->head, &dev->mode_config.property_list);
   3668       1.5  riastrad 
   3669       1.5  riastrad 	WARN_ON(!drm_property_type_valid(property));
   3670       1.5  riastrad 
   3671       1.1  riastrad 	return property;
   3672       1.1  riastrad fail:
   3673       1.1  riastrad 	kfree(property->values);
   3674       1.1  riastrad 	kfree(property);
   3675       1.1  riastrad 	return NULL;
   3676       1.1  riastrad }
   3677       1.1  riastrad EXPORT_SYMBOL(drm_property_create);
   3678       1.1  riastrad 
   3679       1.3  riastrad /**
   3680       1.5  riastrad  * drm_property_create_enum - create a new enumeration property type
   3681       1.3  riastrad  * @dev: drm device
   3682       1.3  riastrad  * @flags: flags specifying the property type
   3683       1.3  riastrad  * @name: name of the property
   3684       1.3  riastrad  * @props: enumeration lists with property values
   3685       1.3  riastrad  * @num_values: number of pre-defined values
   3686       1.3  riastrad  *
   3687       1.3  riastrad  * This creates a new generic drm property which can then be attached to a drm
   3688       1.3  riastrad  * object with drm_object_attach_property. The returned property object must be
   3689       1.3  riastrad  * freed with drm_property_destroy.
   3690       1.3  riastrad  *
   3691       1.3  riastrad  * Userspace is only allowed to set one of the predefined values for enumeration
   3692       1.3  riastrad  * properties.
   3693       1.3  riastrad  *
   3694       1.3  riastrad  * Returns:
   3695       1.3  riastrad  * A pointer to the newly created property on success, NULL on failure.
   3696       1.3  riastrad  */
   3697       1.1  riastrad struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
   3698       1.1  riastrad 					 const char *name,
   3699       1.1  riastrad 					 const struct drm_prop_enum_list *props,
   3700       1.1  riastrad 					 int num_values)
   3701       1.1  riastrad {
   3702       1.1  riastrad 	struct drm_property *property;
   3703       1.1  riastrad 	int i, ret;
   3704       1.1  riastrad 
   3705       1.1  riastrad 	flags |= DRM_MODE_PROP_ENUM;
   3706       1.1  riastrad 
   3707       1.1  riastrad 	property = drm_property_create(dev, flags, name, num_values);
   3708       1.1  riastrad 	if (!property)
   3709       1.1  riastrad 		return NULL;
   3710       1.1  riastrad 
   3711       1.1  riastrad 	for (i = 0; i < num_values; i++) {
   3712       1.1  riastrad 		ret = drm_property_add_enum(property, i,
   3713       1.1  riastrad 				      props[i].type,
   3714       1.1  riastrad 				      props[i].name);
   3715       1.1  riastrad 		if (ret) {
   3716       1.1  riastrad 			drm_property_destroy(dev, property);
   3717       1.1  riastrad 			return NULL;
   3718       1.1  riastrad 		}
   3719       1.1  riastrad 	}
   3720       1.1  riastrad 
   3721       1.1  riastrad 	return property;
   3722       1.1  riastrad }
   3723       1.1  riastrad EXPORT_SYMBOL(drm_property_create_enum);
   3724       1.1  riastrad 
   3725       1.3  riastrad /**
   3726       1.5  riastrad  * drm_property_create_bitmask - create a new bitmask property type
   3727       1.3  riastrad  * @dev: drm device
   3728       1.3  riastrad  * @flags: flags specifying the property type
   3729       1.3  riastrad  * @name: name of the property
   3730       1.3  riastrad  * @props: enumeration lists with property bitflags
   3731       1.5  riastrad  * @num_props: size of the @props array
   3732       1.5  riastrad  * @supported_bits: bitmask of all supported enumeration values
   3733       1.3  riastrad  *
   3734       1.5  riastrad  * This creates a new bitmask drm property which can then be attached to a drm
   3735       1.3  riastrad  * object with drm_object_attach_property. The returned property object must be
   3736       1.3  riastrad  * freed with drm_property_destroy.
   3737       1.3  riastrad  *
   3738       1.3  riastrad  * Compared to plain enumeration properties userspace is allowed to set any
   3739       1.3  riastrad  * or'ed together combination of the predefined property bitflag values
   3740       1.3  riastrad  *
   3741       1.3  riastrad  * Returns:
   3742       1.3  riastrad  * A pointer to the newly created property on success, NULL on failure.
   3743       1.3  riastrad  */
   3744       1.1  riastrad struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
   3745       1.1  riastrad 					 int flags, const char *name,
   3746       1.1  riastrad 					 const struct drm_prop_enum_list *props,
   3747       1.5  riastrad 					 int num_props,
   3748       1.5  riastrad 					 uint64_t supported_bits)
   3749       1.1  riastrad {
   3750       1.1  riastrad 	struct drm_property *property;
   3751       1.5  riastrad 	int i, ret, index = 0;
   3752       1.5  riastrad 	int num_values = hweight64(supported_bits);
   3753       1.1  riastrad 
   3754       1.1  riastrad 	flags |= DRM_MODE_PROP_BITMASK;
   3755       1.1  riastrad 
   3756       1.1  riastrad 	property = drm_property_create(dev, flags, name, num_values);
   3757       1.1  riastrad 	if (!property)
   3758       1.1  riastrad 		return NULL;
   3759       1.5  riastrad 	for (i = 0; i < num_props; i++) {
   3760       1.5  riastrad 		if (!(supported_bits & (1ULL << props[i].type)))
   3761       1.5  riastrad 			continue;
   3762       1.1  riastrad 
   3763       1.5  riastrad 		if (WARN_ON(index >= num_values)) {
   3764       1.5  riastrad 			drm_property_destroy(dev, property);
   3765       1.5  riastrad 			return NULL;
   3766       1.5  riastrad 		}
   3767       1.5  riastrad 
   3768       1.5  riastrad 		ret = drm_property_add_enum(property, index++,
   3769       1.1  riastrad 				      props[i].type,
   3770       1.1  riastrad 				      props[i].name);
   3771       1.1  riastrad 		if (ret) {
   3772       1.1  riastrad 			drm_property_destroy(dev, property);
   3773       1.1  riastrad 			return NULL;
   3774       1.1  riastrad 		}
   3775       1.1  riastrad 	}
   3776       1.1  riastrad 
   3777       1.1  riastrad 	return property;
   3778       1.1  riastrad }
   3779       1.1  riastrad EXPORT_SYMBOL(drm_property_create_bitmask);
   3780       1.1  riastrad 
   3781       1.5  riastrad static struct drm_property *property_create_range(struct drm_device *dev,
   3782       1.5  riastrad 					 int flags, const char *name,
   3783       1.5  riastrad 					 uint64_t min, uint64_t max)
   3784       1.5  riastrad {
   3785       1.5  riastrad 	struct drm_property *property;
   3786       1.5  riastrad 
   3787       1.5  riastrad 	property = drm_property_create(dev, flags, name, 2);
   3788       1.5  riastrad 	if (!property)
   3789       1.5  riastrad 		return NULL;
   3790       1.5  riastrad 
   3791       1.5  riastrad 	property->values[0] = min;
   3792       1.5  riastrad 	property->values[1] = max;
   3793       1.5  riastrad 
   3794       1.5  riastrad 	return property;
   3795       1.5  riastrad }
   3796       1.5  riastrad 
   3797       1.3  riastrad /**
   3798       1.5  riastrad  * drm_property_create_range - create a new unsigned ranged property type
   3799       1.3  riastrad  * @dev: drm device
   3800       1.3  riastrad  * @flags: flags specifying the property type
   3801       1.3  riastrad  * @name: name of the property
   3802       1.3  riastrad  * @min: minimum value of the property
   3803       1.3  riastrad  * @max: maximum value of the property
   3804       1.3  riastrad  *
   3805       1.3  riastrad  * This creates a new generic drm property which can then be attached to a drm
   3806       1.3  riastrad  * object with drm_object_attach_property. The returned property object must be
   3807       1.3  riastrad  * freed with drm_property_destroy.
   3808       1.3  riastrad  *
   3809       1.5  riastrad  * Userspace is allowed to set any unsigned integer value in the (min, max)
   3810       1.5  riastrad  * range inclusive.
   3811       1.3  riastrad  *
   3812       1.3  riastrad  * Returns:
   3813       1.3  riastrad  * A pointer to the newly created property on success, NULL on failure.
   3814       1.3  riastrad  */
   3815       1.1  riastrad struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
   3816       1.1  riastrad 					 const char *name,
   3817       1.1  riastrad 					 uint64_t min, uint64_t max)
   3818       1.1  riastrad {
   3819       1.5  riastrad 	return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
   3820       1.5  riastrad 			name, min, max);
   3821       1.5  riastrad }
   3822       1.5  riastrad EXPORT_SYMBOL(drm_property_create_range);
   3823       1.5  riastrad 
   3824       1.5  riastrad /**
   3825       1.5  riastrad  * drm_property_create_signed_range - create a new signed ranged property type
   3826       1.5  riastrad  * @dev: drm device
   3827       1.5  riastrad  * @flags: flags specifying the property type
   3828       1.5  riastrad  * @name: name of the property
   3829       1.5  riastrad  * @min: minimum value of the property
   3830       1.5  riastrad  * @max: maximum value of the property
   3831       1.5  riastrad  *
   3832       1.5  riastrad  * This creates a new generic drm property which can then be attached to a drm
   3833       1.5  riastrad  * object with drm_object_attach_property. The returned property object must be
   3834       1.5  riastrad  * freed with drm_property_destroy.
   3835       1.5  riastrad  *
   3836       1.5  riastrad  * Userspace is allowed to set any signed integer value in the (min, max)
   3837       1.5  riastrad  * range inclusive.
   3838       1.5  riastrad  *
   3839       1.5  riastrad  * Returns:
   3840       1.5  riastrad  * A pointer to the newly created property on success, NULL on failure.
   3841       1.5  riastrad  */
   3842       1.5  riastrad struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
   3843       1.5  riastrad 					 int flags, const char *name,
   3844       1.5  riastrad 					 int64_t min, int64_t max)
   3845       1.5  riastrad {
   3846       1.5  riastrad 	return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
   3847       1.5  riastrad 			name, I642U64(min), I642U64(max));
   3848       1.5  riastrad }
   3849       1.5  riastrad EXPORT_SYMBOL(drm_property_create_signed_range);
   3850       1.5  riastrad 
   3851       1.5  riastrad /**
   3852       1.5  riastrad  * drm_property_create_object - create a new object property type
   3853       1.5  riastrad  * @dev: drm device
   3854       1.5  riastrad  * @flags: flags specifying the property type
   3855       1.5  riastrad  * @name: name of the property
   3856       1.5  riastrad  * @type: object type from DRM_MODE_OBJECT_* defines
   3857       1.5  riastrad  *
   3858       1.5  riastrad  * This creates a new generic drm property which can then be attached to a drm
   3859       1.5  riastrad  * object with drm_object_attach_property. The returned property object must be
   3860       1.5  riastrad  * freed with drm_property_destroy.
   3861       1.5  riastrad  *
   3862       1.5  riastrad  * Userspace is only allowed to set this to any property value of the given
   3863       1.5  riastrad  * @type. Only useful for atomic properties, which is enforced.
   3864       1.5  riastrad  *
   3865       1.5  riastrad  * Returns:
   3866       1.5  riastrad  * A pointer to the newly created property on success, NULL on failure.
   3867       1.5  riastrad  */
   3868       1.5  riastrad struct drm_property *drm_property_create_object(struct drm_device *dev,
   3869       1.5  riastrad 					 int flags, const char *name, uint32_t type)
   3870       1.5  riastrad {
   3871       1.1  riastrad 	struct drm_property *property;
   3872       1.1  riastrad 
   3873       1.5  riastrad 	flags |= DRM_MODE_PROP_OBJECT;
   3874       1.5  riastrad 
   3875       1.5  riastrad 	if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
   3876       1.5  riastrad 		return NULL;
   3877       1.1  riastrad 
   3878       1.5  riastrad 	property = drm_property_create(dev, flags, name, 1);
   3879       1.1  riastrad 	if (!property)
   3880       1.1  riastrad 		return NULL;
   3881       1.1  riastrad 
   3882       1.5  riastrad 	property->values[0] = type;
   3883       1.1  riastrad 
   3884       1.1  riastrad 	return property;
   3885       1.1  riastrad }
   3886       1.5  riastrad EXPORT_SYMBOL(drm_property_create_object);
   3887       1.5  riastrad 
   3888       1.5  riastrad /**
   3889       1.5  riastrad  * drm_property_create_bool - create a new boolean property type
   3890       1.5  riastrad  * @dev: drm device
   3891       1.5  riastrad  * @flags: flags specifying the property type
   3892       1.5  riastrad  * @name: name of the property
   3893       1.5  riastrad  *
   3894       1.5  riastrad  * This creates a new generic drm property which can then be attached to a drm
   3895       1.5  riastrad  * object with drm_object_attach_property. The returned property object must be
   3896       1.5  riastrad  * freed with drm_property_destroy.
   3897       1.5  riastrad  *
   3898       1.5  riastrad  * This is implemented as a ranged property with only {0, 1} as valid values.
   3899       1.5  riastrad  *
   3900       1.5  riastrad  * Returns:
   3901       1.5  riastrad  * A pointer to the newly created property on success, NULL on failure.
   3902       1.5  riastrad  */
   3903       1.5  riastrad struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
   3904       1.5  riastrad 					 const char *name)
   3905       1.5  riastrad {
   3906       1.5  riastrad 	return drm_property_create_range(dev, flags, name, 0, 1);
   3907       1.5  riastrad }
   3908       1.5  riastrad EXPORT_SYMBOL(drm_property_create_bool);
   3909       1.1  riastrad 
   3910       1.3  riastrad /**
   3911       1.3  riastrad  * drm_property_add_enum - add a possible value to an enumeration property
   3912       1.3  riastrad  * @property: enumeration property to change
   3913       1.3  riastrad  * @index: index of the new enumeration
   3914       1.3  riastrad  * @value: value of the new enumeration
   3915       1.3  riastrad  * @name: symbolic name of the new enumeration
   3916       1.3  riastrad  *
   3917       1.3  riastrad  * This functions adds enumerations to a property.
   3918       1.3  riastrad  *
   3919       1.3  riastrad  * It's use is deprecated, drivers should use one of the more specific helpers
   3920       1.3  riastrad  * to directly create the property with all enumerations already attached.
   3921       1.3  riastrad  *
   3922       1.3  riastrad  * Returns:
   3923       1.3  riastrad  * Zero on success, error code on failure.
   3924       1.3  riastrad  */
   3925       1.1  riastrad int drm_property_add_enum(struct drm_property *property, int index,
   3926       1.1  riastrad 			  uint64_t value, const char *name)
   3927       1.1  riastrad {
   3928       1.1  riastrad 	struct drm_property_enum *prop_enum;
   3929       1.1  riastrad 
   3930       1.5  riastrad 	if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
   3931       1.5  riastrad 			drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
   3932       1.1  riastrad 		return -EINVAL;
   3933       1.1  riastrad 
   3934       1.1  riastrad 	/*
   3935       1.1  riastrad 	 * Bitmask enum properties have the additional constraint of values
   3936       1.1  riastrad 	 * from 0 to 63
   3937       1.1  riastrad 	 */
   3938       1.5  riastrad 	if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
   3939       1.5  riastrad 			(value > 63))
   3940       1.1  riastrad 		return -EINVAL;
   3941       1.1  riastrad 
   3942       1.5  riastrad 	if (!list_empty(&property->enum_list)) {
   3943       1.5  riastrad 		list_for_each_entry(prop_enum, &property->enum_list, head) {
   3944       1.1  riastrad 			if (prop_enum->value == value) {
   3945       1.1  riastrad 				strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
   3946       1.1  riastrad 				prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
   3947       1.1  riastrad 				return 0;
   3948       1.1  riastrad 			}
   3949       1.1  riastrad 		}
   3950       1.1  riastrad 	}
   3951       1.1  riastrad 
   3952       1.1  riastrad 	prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
   3953       1.1  riastrad 	if (!prop_enum)
   3954       1.1  riastrad 		return -ENOMEM;
   3955       1.1  riastrad 
   3956       1.1  riastrad 	strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
   3957       1.1  riastrad 	prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
   3958       1.1  riastrad 	prop_enum->value = value;
   3959       1.1  riastrad 
   3960       1.1  riastrad 	property->values[index] = value;
   3961       1.5  riastrad 	list_add_tail(&prop_enum->head, &property->enum_list);
   3962       1.1  riastrad 	return 0;
   3963       1.1  riastrad }
   3964       1.1  riastrad EXPORT_SYMBOL(drm_property_add_enum);
   3965       1.1  riastrad 
   3966       1.3  riastrad /**
   3967       1.3  riastrad  * drm_property_destroy - destroy a drm property
   3968       1.3  riastrad  * @dev: drm device
   3969       1.3  riastrad  * @property: property to destry
   3970       1.3  riastrad  *
   3971       1.3  riastrad  * This function frees a property including any attached resources like
   3972       1.3  riastrad  * enumeration values.
   3973       1.3  riastrad  */
   3974       1.1  riastrad void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
   3975       1.1  riastrad {
   3976       1.1  riastrad 	struct drm_property_enum *prop_enum, *pt;
   3977       1.1  riastrad 
   3978       1.5  riastrad 	list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
   3979       1.1  riastrad 		list_del(&prop_enum->head);
   3980       1.1  riastrad 		kfree(prop_enum);
   3981       1.1  riastrad 	}
   3982       1.1  riastrad 
   3983       1.1  riastrad 	if (property->num_values)
   3984       1.1  riastrad 		kfree(property->values);
   3985       1.1  riastrad 	drm_mode_object_put(dev, &property->base);
   3986       1.1  riastrad 	list_del(&property->head);
   3987       1.1  riastrad 	kfree(property);
   3988       1.1  riastrad }
   3989       1.1  riastrad EXPORT_SYMBOL(drm_property_destroy);
   3990       1.1  riastrad 
   3991       1.3  riastrad /**
   3992       1.3  riastrad  * drm_object_attach_property - attach a property to a modeset object
   3993       1.3  riastrad  * @obj: drm modeset object
   3994       1.3  riastrad  * @property: property to attach
   3995       1.3  riastrad  * @init_val: initial value of the property
   3996       1.3  riastrad  *
   3997       1.3  riastrad  * This attaches the given property to the modeset object with the given initial
   3998       1.3  riastrad  * value. Currently this function cannot fail since the properties are stored in
   3999       1.3  riastrad  * a statically sized array.
   4000       1.3  riastrad  */
   4001       1.1  riastrad void drm_object_attach_property(struct drm_mode_object *obj,
   4002       1.1  riastrad 				struct drm_property *property,
   4003       1.1  riastrad 				uint64_t init_val)
   4004       1.1  riastrad {
   4005       1.1  riastrad 	int count = obj->properties->count;
   4006       1.1  riastrad 
   4007       1.1  riastrad 	if (count == DRM_OBJECT_MAX_PROPERTY) {
   4008       1.1  riastrad 		WARN(1, "Failed to attach object property (type: 0x%x). Please "
   4009       1.1  riastrad 			"increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
   4010       1.1  riastrad 			"you see this message on the same object type.\n",
   4011       1.1  riastrad 			obj->type);
   4012       1.1  riastrad 		return;
   4013       1.1  riastrad 	}
   4014       1.1  riastrad 
   4015       1.5  riastrad 	obj->properties->properties[count] = property;
   4016       1.1  riastrad 	obj->properties->values[count] = init_val;
   4017       1.1  riastrad 	obj->properties->count++;
   4018       1.5  riastrad 	if (property->flags & DRM_MODE_PROP_ATOMIC)
   4019       1.5  riastrad 		obj->properties->atomic_count++;
   4020       1.1  riastrad }
   4021       1.1  riastrad EXPORT_SYMBOL(drm_object_attach_property);
   4022       1.1  riastrad 
   4023       1.3  riastrad /**
   4024       1.3  riastrad  * drm_object_property_set_value - set the value of a property
   4025       1.3  riastrad  * @obj: drm mode object to set property value for
   4026       1.3  riastrad  * @property: property to set
   4027       1.3  riastrad  * @val: value the property should be set to
   4028       1.3  riastrad  *
   4029       1.3  riastrad  * This functions sets a given property on a given object. This function only
   4030       1.3  riastrad  * changes the software state of the property, it does not call into the
   4031       1.3  riastrad  * driver's ->set_property callback.
   4032       1.3  riastrad  *
   4033       1.3  riastrad  * Returns:
   4034       1.3  riastrad  * Zero on success, error code on failure.
   4035       1.3  riastrad  */
   4036       1.1  riastrad int drm_object_property_set_value(struct drm_mode_object *obj,
   4037       1.1  riastrad 				  struct drm_property *property, uint64_t val)
   4038       1.1  riastrad {
   4039       1.1  riastrad 	int i;
   4040       1.1  riastrad 
   4041       1.1  riastrad 	for (i = 0; i < obj->properties->count; i++) {
   4042       1.5  riastrad 		if (obj->properties->properties[i] == property) {
   4043       1.1  riastrad 			obj->properties->values[i] = val;
   4044       1.1  riastrad 			return 0;
   4045       1.1  riastrad 		}
   4046       1.1  riastrad 	}
   4047       1.1  riastrad 
   4048       1.1  riastrad 	return -EINVAL;
   4049       1.1  riastrad }
   4050       1.1  riastrad EXPORT_SYMBOL(drm_object_property_set_value);
   4051       1.1  riastrad 
   4052       1.3  riastrad /**
   4053       1.3  riastrad  * drm_object_property_get_value - retrieve the value of a property
   4054       1.3  riastrad  * @obj: drm mode object to get property value from
   4055       1.3  riastrad  * @property: property to retrieve
   4056       1.3  riastrad  * @val: storage for the property value
   4057       1.3  riastrad  *
   4058       1.3  riastrad  * This function retrieves the softare state of the given property for the given
   4059       1.3  riastrad  * property. Since there is no driver callback to retrieve the current property
   4060       1.3  riastrad  * value this might be out of sync with the hardware, depending upon the driver
   4061       1.3  riastrad  * and property.
   4062       1.3  riastrad  *
   4063       1.3  riastrad  * Returns:
   4064       1.3  riastrad  * Zero on success, error code on failure.
   4065       1.3  riastrad  */
   4066       1.1  riastrad int drm_object_property_get_value(struct drm_mode_object *obj,
   4067       1.1  riastrad 				  struct drm_property *property, uint64_t *val)
   4068       1.1  riastrad {
   4069       1.1  riastrad 	int i;
   4070       1.1  riastrad 
   4071       1.5  riastrad 	/* read-only properties bypass atomic mechanism and still store
   4072       1.5  riastrad 	 * their value in obj->properties->values[].. mostly to avoid
   4073       1.5  riastrad 	 * having to deal w/ EDID and similar props in atomic paths:
   4074       1.5  riastrad 	 */
   4075       1.5  riastrad 	if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
   4076       1.5  riastrad 			!(property->flags & DRM_MODE_PROP_IMMUTABLE))
   4077       1.5  riastrad 		return drm_atomic_get_property(obj, property, val);
   4078       1.5  riastrad 
   4079       1.1  riastrad 	for (i = 0; i < obj->properties->count; i++) {
   4080       1.5  riastrad 		if (obj->properties->properties[i] == property) {
   4081       1.1  riastrad 			*val = obj->properties->values[i];
   4082       1.1  riastrad 			return 0;
   4083       1.1  riastrad 		}
   4084       1.1  riastrad 	}
   4085       1.1  riastrad 
   4086       1.1  riastrad 	return -EINVAL;
   4087       1.1  riastrad }
   4088       1.1  riastrad EXPORT_SYMBOL(drm_object_property_get_value);
   4089       1.1  riastrad 
   4090       1.3  riastrad /**
   4091       1.5  riastrad  * drm_mode_getproperty_ioctl - get the property metadata
   4092       1.3  riastrad  * @dev: DRM device
   4093       1.3  riastrad  * @data: ioctl data
   4094       1.3  riastrad  * @file_priv: DRM file info
   4095       1.3  riastrad  *
   4096       1.5  riastrad  * This function retrieves the metadata for a given property, like the different
   4097       1.5  riastrad  * possible values for an enum property or the limits for a range property.
   4098       1.5  riastrad  *
   4099       1.5  riastrad  * Blob properties are special
   4100       1.3  riastrad  *
   4101       1.3  riastrad  * Called by the user via ioctl.
   4102       1.3  riastrad  *
   4103       1.3  riastrad  * Returns:
   4104       1.5  riastrad  * Zero on success, negative errno on failure.
   4105       1.3  riastrad  */
   4106       1.1  riastrad int drm_mode_getproperty_ioctl(struct drm_device *dev,
   4107       1.1  riastrad 			       void *data, struct drm_file *file_priv)
   4108       1.1  riastrad {
   4109       1.1  riastrad 	struct drm_mode_get_property *out_resp = data;
   4110       1.1  riastrad 	struct drm_property *property;
   4111       1.1  riastrad 	int enum_count = 0;
   4112       1.1  riastrad 	int value_count = 0;
   4113       1.1  riastrad 	int ret = 0, i;
   4114       1.1  riastrad 	int copied;
   4115       1.1  riastrad 	struct drm_property_enum *prop_enum;
   4116       1.1  riastrad 	struct drm_mode_property_enum __user *enum_ptr;
   4117       1.1  riastrad 	uint64_t __user *values_ptr;
   4118       1.1  riastrad 
   4119       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   4120       1.1  riastrad 		return -EINVAL;
   4121       1.1  riastrad 
   4122       1.3  riastrad 	drm_modeset_lock_all(dev);
   4123       1.5  riastrad 	property = drm_property_find(dev, out_resp->prop_id);
   4124       1.5  riastrad 	if (!property) {
   4125       1.3  riastrad 		ret = -ENOENT;
   4126       1.1  riastrad 		goto done;
   4127       1.1  riastrad 	}
   4128       1.1  riastrad 
   4129       1.5  riastrad 	if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
   4130       1.5  riastrad 			drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
   4131       1.5  riastrad 		list_for_each_entry(prop_enum, &property->enum_list, head)
   4132       1.1  riastrad 			enum_count++;
   4133       1.1  riastrad 	}
   4134       1.1  riastrad 
   4135       1.1  riastrad 	value_count = property->num_values;
   4136       1.1  riastrad 
   4137       1.1  riastrad 	strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
   4138       1.1  riastrad 	out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
   4139       1.1  riastrad 	out_resp->flags = property->flags;
   4140       1.1  riastrad 
   4141       1.1  riastrad 	if ((out_resp->count_values >= value_count) && value_count) {
   4142       1.1  riastrad 		values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
   4143       1.1  riastrad 		for (i = 0; i < value_count; i++) {
   4144       1.1  riastrad 			if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
   4145       1.1  riastrad 				ret = -EFAULT;
   4146       1.1  riastrad 				goto done;
   4147       1.1  riastrad 			}
   4148       1.1  riastrad 		}
   4149       1.1  riastrad 	}
   4150       1.1  riastrad 	out_resp->count_values = value_count;
   4151       1.1  riastrad 
   4152       1.5  riastrad 	if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
   4153       1.5  riastrad 			drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
   4154       1.1  riastrad 		if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
   4155       1.1  riastrad 			copied = 0;
   4156       1.1  riastrad 			enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
   4157       1.5  riastrad 			list_for_each_entry(prop_enum, &property->enum_list, head) {
   4158       1.1  riastrad 
   4159       1.1  riastrad 				if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
   4160       1.1  riastrad 					ret = -EFAULT;
   4161       1.1  riastrad 					goto done;
   4162       1.1  riastrad 				}
   4163       1.1  riastrad 
   4164       1.1  riastrad 				if (copy_to_user(&enum_ptr[copied].name,
   4165       1.1  riastrad 						 &prop_enum->name, DRM_PROP_NAME_LEN)) {
   4166       1.1  riastrad 					ret = -EFAULT;
   4167       1.1  riastrad 					goto done;
   4168       1.1  riastrad 				}
   4169       1.1  riastrad 				copied++;
   4170       1.1  riastrad 			}
   4171       1.1  riastrad 		}
   4172       1.1  riastrad 		out_resp->count_enum_blobs = enum_count;
   4173       1.1  riastrad 	}
   4174       1.1  riastrad 
   4175       1.5  riastrad 	/*
   4176       1.5  riastrad 	 * NOTE: The idea seems to have been to use this to read all the blob
   4177       1.5  riastrad 	 * property values. But nothing ever added them to the corresponding
   4178       1.5  riastrad 	 * list, userspace always used the special-purpose get_blob ioctl to
   4179       1.5  riastrad 	 * read the value for a blob property. It also doesn't make a lot of
   4180       1.5  riastrad 	 * sense to return values here when everything else is just metadata for
   4181       1.5  riastrad 	 * the property itself.
   4182       1.5  riastrad 	 */
   4183       1.5  riastrad 	if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
   4184       1.5  riastrad 		out_resp->count_enum_blobs = 0;
   4185       1.1  riastrad done:
   4186       1.3  riastrad 	drm_modeset_unlock_all(dev);
   4187       1.1  riastrad 	return ret;
   4188       1.1  riastrad }
   4189       1.1  riastrad 
   4190       1.5  riastrad /**
   4191       1.5  riastrad  * drm_property_create_blob - Create new blob property
   4192       1.5  riastrad  *
   4193       1.5  riastrad  * Creates a new blob property for a specified DRM device, optionally
   4194       1.5  riastrad  * copying data.
   4195       1.5  riastrad  *
   4196       1.5  riastrad  * @dev: DRM device to create property for
   4197       1.5  riastrad  * @length: Length to allocate for blob data
   4198       1.5  riastrad  * @data: If specified, copies data into blob
   4199       1.5  riastrad  *
   4200       1.5  riastrad  * Returns:
   4201       1.5  riastrad  * New blob property with a single reference on success, or an ERR_PTR
   4202       1.5  riastrad  * value on failure.
   4203       1.5  riastrad  */
   4204       1.5  riastrad struct drm_property_blob *
   4205       1.5  riastrad drm_property_create_blob(struct drm_device *dev, size_t length,
   4206       1.5  riastrad 			 const void *data)
   4207       1.1  riastrad {
   4208       1.1  riastrad 	struct drm_property_blob *blob;
   4209       1.1  riastrad 	int ret;
   4210       1.1  riastrad 
   4211       1.5  riastrad 	if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
   4212       1.5  riastrad 		return ERR_PTR(-EINVAL);
   4213       1.1  riastrad 
   4214       1.1  riastrad 	blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
   4215       1.1  riastrad 	if (!blob)
   4216       1.5  riastrad 		return ERR_PTR(-ENOMEM);
   4217       1.5  riastrad 
   4218       1.5  riastrad 	/* This must be explicitly initialised, so we can safely call list_del
   4219       1.5  riastrad 	 * on it in the removal handler, even if it isn't in a file list. */
   4220       1.5  riastrad 	INIT_LIST_HEAD(&blob->head_file);
   4221       1.5  riastrad 	blob->length = length;
   4222       1.5  riastrad 	blob->dev = dev;
   4223       1.5  riastrad 
   4224       1.5  riastrad 	if (data)
   4225       1.5  riastrad 		memcpy(blob->data, data, length);
   4226       1.5  riastrad 
   4227       1.5  riastrad 	mutex_lock(&dev->mode_config.blob_lock);
   4228       1.1  riastrad 
   4229       1.1  riastrad 	ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
   4230       1.1  riastrad 	if (ret) {
   4231       1.1  riastrad 		kfree(blob);
   4232       1.5  riastrad 		mutex_unlock(&dev->mode_config.blob_lock);
   4233       1.5  riastrad 		return ERR_PTR(-EINVAL);
   4234       1.1  riastrad 	}
   4235       1.1  riastrad 
   4236       1.5  riastrad 	kref_init(&blob->refcount);
   4237       1.5  riastrad 
   4238       1.5  riastrad 	list_add_tail(&blob->head_global,
   4239       1.5  riastrad 	              &dev->mode_config.property_blob_list);
   4240       1.1  riastrad 
   4241       1.5  riastrad 	mutex_unlock(&dev->mode_config.blob_lock);
   4242       1.1  riastrad 
   4243       1.1  riastrad 	return blob;
   4244       1.1  riastrad }
   4245       1.5  riastrad EXPORT_SYMBOL(drm_property_create_blob);
   4246       1.1  riastrad 
   4247       1.5  riastrad /**
   4248       1.5  riastrad  * drm_property_free_blob - Blob property destructor
   4249       1.5  riastrad  *
   4250       1.5  riastrad  * Internal free function for blob properties; must not be used directly.
   4251       1.5  riastrad  *
   4252       1.5  riastrad  * @kref: Reference
   4253       1.5  riastrad  */
   4254       1.5  riastrad static void drm_property_free_blob(struct kref *kref)
   4255       1.1  riastrad {
   4256       1.5  riastrad 	struct drm_property_blob *blob =
   4257       1.5  riastrad 		container_of(kref, struct drm_property_blob, refcount);
   4258       1.5  riastrad 
   4259       1.5  riastrad 	WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
   4260       1.5  riastrad 
   4261       1.5  riastrad 	list_del(&blob->head_global);
   4262       1.5  riastrad 	list_del(&blob->head_file);
   4263       1.5  riastrad 	drm_mode_object_put(blob->dev, &blob->base);
   4264       1.5  riastrad 
   4265       1.1  riastrad 	kfree(blob);
   4266       1.1  riastrad }
   4267       1.1  riastrad 
   4268       1.3  riastrad /**
   4269       1.5  riastrad  * drm_property_unreference_blob - Unreference a blob property
   4270       1.5  riastrad  *
   4271       1.5  riastrad  * Drop a reference on a blob property. May free the object.
   4272       1.5  riastrad  *
   4273       1.5  riastrad  * @blob: Pointer to blob property
   4274       1.5  riastrad  */
   4275       1.5  riastrad void drm_property_unreference_blob(struct drm_property_blob *blob)
   4276       1.5  riastrad {
   4277       1.5  riastrad 	struct drm_device *dev;
   4278       1.5  riastrad 
   4279       1.5  riastrad 	if (!blob)
   4280       1.5  riastrad 		return;
   4281       1.5  riastrad 
   4282       1.5  riastrad 	dev = blob->dev;
   4283       1.5  riastrad 
   4284       1.7  riastrad 	DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, blob->refcount.kr_count);
   4285       1.5  riastrad 
   4286       1.5  riastrad 	if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
   4287       1.5  riastrad 			   &dev->mode_config.blob_lock))
   4288       1.5  riastrad 		mutex_unlock(&dev->mode_config.blob_lock);
   4289       1.5  riastrad 	else
   4290       1.5  riastrad 		might_lock(&dev->mode_config.blob_lock);
   4291       1.5  riastrad }
   4292       1.5  riastrad EXPORT_SYMBOL(drm_property_unreference_blob);
   4293       1.5  riastrad 
   4294       1.5  riastrad /**
   4295       1.5  riastrad  * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
   4296       1.5  riastrad  *
   4297       1.5  riastrad  * Drop a reference on a blob property. May free the object. This must be
   4298       1.5  riastrad  * called with blob_lock held.
   4299       1.5  riastrad  *
   4300       1.5  riastrad  * @blob: Pointer to blob property
   4301       1.5  riastrad  */
   4302       1.5  riastrad static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
   4303       1.5  riastrad {
   4304       1.5  riastrad 	if (!blob)
   4305       1.5  riastrad 		return;
   4306       1.5  riastrad 
   4307       1.7  riastrad 	DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, blob->refcount.kr_count);
   4308       1.5  riastrad 
   4309       1.5  riastrad 	kref_put(&blob->refcount, drm_property_free_blob);
   4310       1.5  riastrad }
   4311       1.5  riastrad 
   4312       1.5  riastrad /**
   4313       1.5  riastrad  * drm_property_destroy_user_blobs - destroy all blobs created by this client
   4314       1.5  riastrad  * @dev:       DRM device
   4315       1.5  riastrad  * @file_priv: destroy all blobs owned by this file handle
   4316       1.5  riastrad  */
   4317       1.5  riastrad void drm_property_destroy_user_blobs(struct drm_device *dev,
   4318       1.5  riastrad 				     struct drm_file *file_priv)
   4319       1.5  riastrad {
   4320       1.5  riastrad 	struct drm_property_blob *blob, *bt;
   4321       1.5  riastrad 
   4322       1.5  riastrad 	mutex_lock(&dev->mode_config.blob_lock);
   4323       1.5  riastrad 
   4324       1.5  riastrad 	list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
   4325       1.5  riastrad 		list_del_init(&blob->head_file);
   4326       1.5  riastrad 		drm_property_unreference_blob_locked(blob);
   4327       1.5  riastrad 	}
   4328       1.5  riastrad 
   4329       1.5  riastrad 	mutex_unlock(&dev->mode_config.blob_lock);
   4330       1.5  riastrad }
   4331       1.5  riastrad 
   4332       1.5  riastrad /**
   4333       1.5  riastrad  * drm_property_reference_blob - Take a reference on an existing property
   4334       1.5  riastrad  *
   4335       1.5  riastrad  * Take a new reference on an existing blob property.
   4336       1.5  riastrad  *
   4337       1.5  riastrad  * @blob: Pointer to blob property
   4338       1.5  riastrad  */
   4339       1.5  riastrad struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
   4340       1.5  riastrad {
   4341       1.7  riastrad 	DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, blob->refcount.kr_count);
   4342       1.5  riastrad 	kref_get(&blob->refcount);
   4343       1.5  riastrad 	return blob;
   4344       1.5  riastrad }
   4345       1.5  riastrad EXPORT_SYMBOL(drm_property_reference_blob);
   4346       1.5  riastrad 
   4347       1.5  riastrad /*
   4348       1.5  riastrad  * Like drm_property_lookup_blob, but does not return an additional reference.
   4349       1.5  riastrad  * Must be called with blob_lock held.
   4350       1.5  riastrad  */
   4351       1.5  riastrad static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
   4352       1.5  riastrad 							    uint32_t id)
   4353       1.5  riastrad {
   4354       1.5  riastrad 	struct drm_mode_object *obj = NULL;
   4355       1.5  riastrad 	struct drm_property_blob *blob;
   4356       1.5  riastrad 
   4357       1.5  riastrad 	WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
   4358       1.5  riastrad 
   4359       1.5  riastrad 	mutex_lock(&dev->mode_config.idr_mutex);
   4360       1.5  riastrad 	obj = idr_find(&dev->mode_config.crtc_idr, id);
   4361       1.5  riastrad 	if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
   4362       1.5  riastrad 		blob = NULL;
   4363       1.5  riastrad 	else
   4364       1.5  riastrad 		blob = obj_to_blob(obj);
   4365       1.5  riastrad 	mutex_unlock(&dev->mode_config.idr_mutex);
   4366       1.5  riastrad 
   4367       1.5  riastrad 	return blob;
   4368       1.5  riastrad }
   4369       1.5  riastrad 
   4370       1.5  riastrad /**
   4371       1.5  riastrad  * drm_property_lookup_blob - look up a blob property and take a reference
   4372       1.5  riastrad  * @dev: drm device
   4373       1.5  riastrad  * @id: id of the blob property
   4374       1.5  riastrad  *
   4375       1.5  riastrad  * If successful, this takes an additional reference to the blob property.
   4376       1.5  riastrad  * callers need to make sure to eventually unreference the returned property
   4377       1.5  riastrad  * again, using @drm_property_unreference_blob.
   4378       1.5  riastrad  */
   4379       1.5  riastrad struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
   4380       1.5  riastrad 					           uint32_t id)
   4381       1.5  riastrad {
   4382       1.5  riastrad 	struct drm_property_blob *blob;
   4383       1.5  riastrad 
   4384       1.5  riastrad 	mutex_lock(&dev->mode_config.blob_lock);
   4385       1.5  riastrad 	blob = __drm_property_lookup_blob(dev, id);
   4386       1.5  riastrad 	if (blob) {
   4387       1.5  riastrad 		if (!kref_get_unless_zero(&blob->refcount))
   4388       1.5  riastrad 			blob = NULL;
   4389       1.5  riastrad 	}
   4390       1.5  riastrad 	mutex_unlock(&dev->mode_config.blob_lock);
   4391       1.5  riastrad 
   4392       1.5  riastrad 	return blob;
   4393       1.5  riastrad }
   4394       1.5  riastrad EXPORT_SYMBOL(drm_property_lookup_blob);
   4395       1.5  riastrad 
   4396       1.5  riastrad /**
   4397       1.5  riastrad  * drm_property_replace_global_blob - atomically replace existing blob property
   4398       1.5  riastrad  * @dev: drm device
   4399       1.5  riastrad  * @replace: location of blob property pointer to be replaced
   4400       1.5  riastrad  * @length: length of data for new blob, or 0 for no data
   4401       1.5  riastrad  * @data: content for new blob, or NULL for no data
   4402       1.5  riastrad  * @obj_holds_id: optional object for property holding blob ID
   4403       1.5  riastrad  * @prop_holds_id: optional property holding blob ID
   4404       1.5  riastrad  * @return 0 on success or error on failure
   4405       1.5  riastrad  *
   4406       1.5  riastrad  * This function will atomically replace a global property in the blob list,
   4407       1.5  riastrad  * optionally updating a property which holds the ID of that property. It is
   4408       1.5  riastrad  * guaranteed to be atomic: no caller will be allowed to see intermediate
   4409       1.5  riastrad  * results, and either the entire operation will succeed and clean up the
   4410       1.5  riastrad  * previous property, or it will fail and the state will be unchanged.
   4411       1.5  riastrad  *
   4412       1.5  riastrad  * If length is 0 or data is NULL, no new blob will be created, and the holding
   4413       1.5  riastrad  * property, if specified, will be set to 0.
   4414       1.5  riastrad  *
   4415       1.5  riastrad  * Access to the replace pointer is assumed to be protected by the caller, e.g.
   4416       1.5  riastrad  * by holding the relevant modesetting object lock for its parent.
   4417       1.5  riastrad  *
   4418       1.5  riastrad  * For example, a drm_connector has a 'PATH' property, which contains the ID
   4419       1.5  riastrad  * of a blob property with the value of the MST path information. Calling this
   4420       1.5  riastrad  * function with replace pointing to the connector's path_blob_ptr, length and
   4421       1.5  riastrad  * data set for the new path information, obj_holds_id set to the connector's
   4422       1.5  riastrad  * base object, and prop_holds_id set to the path property name, will perform
   4423       1.5  riastrad  * a completely atomic update. The access to path_blob_ptr is protected by the
   4424       1.5  riastrad  * caller holding a lock on the connector.
   4425       1.5  riastrad  */
   4426       1.5  riastrad static int drm_property_replace_global_blob(struct drm_device *dev,
   4427       1.5  riastrad                                             struct drm_property_blob **replace,
   4428       1.5  riastrad                                             size_t length,
   4429       1.5  riastrad                                             const void *data,
   4430       1.5  riastrad                                             struct drm_mode_object *obj_holds_id,
   4431       1.5  riastrad                                             struct drm_property *prop_holds_id)
   4432       1.5  riastrad {
   4433       1.5  riastrad 	struct drm_property_blob *new_blob = NULL;
   4434       1.5  riastrad 	struct drm_property_blob *old_blob = NULL;
   4435       1.5  riastrad 	int ret;
   4436       1.5  riastrad 
   4437       1.5  riastrad 	WARN_ON(replace == NULL);
   4438       1.5  riastrad 
   4439       1.5  riastrad 	old_blob = *replace;
   4440       1.5  riastrad 
   4441       1.5  riastrad 	if (length && data) {
   4442       1.5  riastrad 		new_blob = drm_property_create_blob(dev, length, data);
   4443       1.5  riastrad 		if (IS_ERR(new_blob))
   4444       1.5  riastrad 			return PTR_ERR(new_blob);
   4445       1.5  riastrad 	}
   4446       1.5  riastrad 
   4447       1.5  riastrad 	/* This does not need to be synchronised with blob_lock, as the
   4448       1.5  riastrad 	 * get_properties ioctl locks all modesetting objects, and
   4449       1.5  riastrad 	 * obj_holds_id must be locked before calling here, so we cannot
   4450       1.5  riastrad 	 * have its value out of sync with the list membership modified
   4451       1.5  riastrad 	 * below under blob_lock. */
   4452       1.5  riastrad 	if (obj_holds_id) {
   4453       1.5  riastrad 		ret = drm_object_property_set_value(obj_holds_id,
   4454       1.5  riastrad 						    prop_holds_id,
   4455       1.5  riastrad 						    new_blob ?
   4456       1.5  riastrad 						        new_blob->base.id : 0);
   4457       1.5  riastrad 		if (ret != 0)
   4458       1.5  riastrad 			goto err_created;
   4459       1.5  riastrad 	}
   4460       1.5  riastrad 
   4461       1.5  riastrad 	drm_property_unreference_blob(old_blob);
   4462       1.5  riastrad 	*replace = new_blob;
   4463       1.5  riastrad 
   4464       1.5  riastrad 	return 0;
   4465       1.5  riastrad 
   4466       1.5  riastrad err_created:
   4467       1.5  riastrad 	drm_property_unreference_blob(new_blob);
   4468       1.5  riastrad 	return ret;
   4469       1.5  riastrad }
   4470       1.5  riastrad 
   4471       1.5  riastrad /**
   4472       1.3  riastrad  * drm_mode_getblob_ioctl - get the contents of a blob property value
   4473       1.3  riastrad  * @dev: DRM device
   4474       1.3  riastrad  * @data: ioctl data
   4475       1.3  riastrad  * @file_priv: DRM file info
   4476       1.3  riastrad  *
   4477       1.3  riastrad  * This function retrieves the contents of a blob property. The value stored in
   4478       1.3  riastrad  * an object's blob property is just a normal modeset object id.
   4479       1.3  riastrad  *
   4480       1.3  riastrad  * Called by the user via ioctl.
   4481       1.3  riastrad  *
   4482       1.3  riastrad  * Returns:
   4483       1.5  riastrad  * Zero on success, negative errno on failure.
   4484       1.3  riastrad  */
   4485       1.1  riastrad int drm_mode_getblob_ioctl(struct drm_device *dev,
   4486       1.1  riastrad 			   void *data, struct drm_file *file_priv)
   4487       1.1  riastrad {
   4488       1.1  riastrad 	struct drm_mode_get_blob *out_resp = data;
   4489       1.1  riastrad 	struct drm_property_blob *blob;
   4490       1.1  riastrad 	int ret = 0;
   4491       1.1  riastrad 	void __user *blob_ptr;
   4492       1.1  riastrad 
   4493       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   4494       1.1  riastrad 		return -EINVAL;
   4495       1.1  riastrad 
   4496       1.3  riastrad 	drm_modeset_lock_all(dev);
   4497       1.5  riastrad 	mutex_lock(&dev->mode_config.blob_lock);
   4498       1.5  riastrad 	blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
   4499       1.5  riastrad 	if (!blob) {
   4500       1.3  riastrad 		ret = -ENOENT;
   4501       1.1  riastrad 		goto done;
   4502       1.1  riastrad 	}
   4503       1.1  riastrad 
   4504       1.1  riastrad 	if (out_resp->length == blob->length) {
   4505       1.1  riastrad 		blob_ptr = (void __user *)(unsigned long)out_resp->data;
   4506       1.5  riastrad 		if (copy_to_user(blob_ptr, blob->data, blob->length)) {
   4507       1.1  riastrad 			ret = -EFAULT;
   4508       1.1  riastrad 			goto done;
   4509       1.1  riastrad 		}
   4510       1.1  riastrad 	}
   4511       1.1  riastrad 	out_resp->length = blob->length;
   4512       1.1  riastrad 
   4513       1.1  riastrad done:
   4514       1.5  riastrad 	mutex_unlock(&dev->mode_config.blob_lock);
   4515       1.3  riastrad 	drm_modeset_unlock_all(dev);
   4516       1.1  riastrad 	return ret;
   4517       1.1  riastrad }
   4518       1.1  riastrad 
   4519       1.3  riastrad /**
   4520       1.5  riastrad  * drm_mode_createblob_ioctl - create a new blob property
   4521       1.5  riastrad  * @dev: DRM device
   4522       1.5  riastrad  * @data: ioctl data
   4523       1.5  riastrad  * @file_priv: DRM file info
   4524       1.5  riastrad  *
   4525       1.5  riastrad  * This function creates a new blob property with user-defined values. In order
   4526       1.5  riastrad  * to give us sensible validation and checking when creating, rather than at
   4527       1.5  riastrad  * every potential use, we also require a type to be provided upfront.
   4528       1.5  riastrad  *
   4529       1.5  riastrad  * Called by the user via ioctl.
   4530       1.5  riastrad  *
   4531       1.5  riastrad  * Returns:
   4532       1.5  riastrad  * Zero on success, negative errno on failure.
   4533       1.5  riastrad  */
   4534       1.5  riastrad int drm_mode_createblob_ioctl(struct drm_device *dev,
   4535       1.5  riastrad 			      void *data, struct drm_file *file_priv)
   4536       1.5  riastrad {
   4537       1.5  riastrad 	struct drm_mode_create_blob *out_resp = data;
   4538       1.5  riastrad 	struct drm_property_blob *blob;
   4539       1.5  riastrad 	void __user *blob_ptr;
   4540       1.5  riastrad 	int ret = 0;
   4541       1.5  riastrad 
   4542       1.5  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   4543       1.5  riastrad 		return -EINVAL;
   4544       1.5  riastrad 
   4545       1.5  riastrad 	blob = drm_property_create_blob(dev, out_resp->length, NULL);
   4546       1.5  riastrad 	if (IS_ERR(blob))
   4547       1.5  riastrad 		return PTR_ERR(blob);
   4548       1.5  riastrad 
   4549       1.5  riastrad 	blob_ptr = (void __user *)(unsigned long)out_resp->data;
   4550       1.5  riastrad 	if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
   4551       1.5  riastrad 		ret = -EFAULT;
   4552       1.5  riastrad 		goto out_blob;
   4553       1.5  riastrad 	}
   4554       1.5  riastrad 
   4555       1.5  riastrad 	/* Dropping the lock between create_blob and our access here is safe
   4556       1.5  riastrad 	 * as only the same file_priv can remove the blob; at this point, it is
   4557       1.5  riastrad 	 * not associated with any file_priv. */
   4558       1.5  riastrad 	mutex_lock(&dev->mode_config.blob_lock);
   4559       1.5  riastrad 	out_resp->blob_id = blob->base.id;
   4560       1.5  riastrad 	list_add_tail(&blob->head_file, &file_priv->blobs);
   4561       1.5  riastrad 	mutex_unlock(&dev->mode_config.blob_lock);
   4562       1.5  riastrad 
   4563       1.5  riastrad 	return 0;
   4564       1.5  riastrad 
   4565       1.5  riastrad out_blob:
   4566       1.5  riastrad 	drm_property_unreference_blob(blob);
   4567       1.5  riastrad 	return ret;
   4568       1.5  riastrad }
   4569       1.5  riastrad 
   4570       1.5  riastrad /**
   4571       1.5  riastrad  * drm_mode_destroyblob_ioctl - destroy a user blob property
   4572       1.5  riastrad  * @dev: DRM device
   4573       1.5  riastrad  * @data: ioctl data
   4574       1.5  riastrad  * @file_priv: DRM file info
   4575       1.5  riastrad  *
   4576       1.5  riastrad  * Destroy an existing user-defined blob property.
   4577       1.5  riastrad  *
   4578       1.5  riastrad  * Called by the user via ioctl.
   4579       1.5  riastrad  *
   4580       1.5  riastrad  * Returns:
   4581       1.5  riastrad  * Zero on success, negative errno on failure.
   4582       1.5  riastrad  */
   4583       1.5  riastrad int drm_mode_destroyblob_ioctl(struct drm_device *dev,
   4584       1.5  riastrad 			       void *data, struct drm_file *file_priv)
   4585       1.5  riastrad {
   4586       1.5  riastrad 	struct drm_mode_destroy_blob *out_resp = data;
   4587       1.5  riastrad 	struct drm_property_blob *blob = NULL, *bt;
   4588       1.5  riastrad 	bool found = false;
   4589       1.5  riastrad 	int ret = 0;
   4590       1.5  riastrad 
   4591       1.5  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   4592       1.5  riastrad 		return -EINVAL;
   4593       1.5  riastrad 
   4594       1.5  riastrad 	mutex_lock(&dev->mode_config.blob_lock);
   4595       1.5  riastrad 	blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
   4596       1.5  riastrad 	if (!blob) {
   4597       1.5  riastrad 		ret = -ENOENT;
   4598       1.5  riastrad 		goto err;
   4599       1.5  riastrad 	}
   4600       1.5  riastrad 
   4601       1.5  riastrad 	/* Ensure the property was actually created by this user. */
   4602       1.5  riastrad 	list_for_each_entry(bt, &file_priv->blobs, head_file) {
   4603       1.5  riastrad 		if (bt == blob) {
   4604       1.5  riastrad 			found = true;
   4605       1.5  riastrad 			break;
   4606       1.5  riastrad 		}
   4607       1.5  riastrad 	}
   4608       1.5  riastrad 
   4609       1.5  riastrad 	if (!found) {
   4610       1.5  riastrad 		ret = -EPERM;
   4611       1.5  riastrad 		goto err;
   4612       1.5  riastrad 	}
   4613       1.5  riastrad 
   4614       1.5  riastrad 	/* We must drop head_file here, because we may not be the last
   4615       1.5  riastrad 	 * reference on the blob. */
   4616       1.5  riastrad 	list_del_init(&blob->head_file);
   4617       1.5  riastrad 	drm_property_unreference_blob_locked(blob);
   4618       1.5  riastrad 	mutex_unlock(&dev->mode_config.blob_lock);
   4619       1.5  riastrad 
   4620       1.5  riastrad 	return 0;
   4621       1.5  riastrad 
   4622       1.5  riastrad err:
   4623       1.5  riastrad 	mutex_unlock(&dev->mode_config.blob_lock);
   4624       1.5  riastrad 	return ret;
   4625       1.5  riastrad }
   4626       1.5  riastrad 
   4627       1.5  riastrad /**
   4628       1.5  riastrad  * drm_mode_connector_set_path_property - set tile property on connector
   4629       1.5  riastrad  * @connector: connector to set property on.
   4630       1.5  riastrad  * @path: path to use for property; must not be NULL.
   4631       1.5  riastrad  *
   4632       1.5  riastrad  * This creates a property to expose to userspace to specify a
   4633       1.5  riastrad  * connector path. This is mainly used for DisplayPort MST where
   4634       1.5  riastrad  * connectors have a topology and we want to allow userspace to give
   4635       1.5  riastrad  * them more meaningful names.
   4636       1.5  riastrad  *
   4637       1.5  riastrad  * Returns:
   4638       1.5  riastrad  * Zero on success, negative errno on failure.
   4639       1.5  riastrad  */
   4640       1.5  riastrad int drm_mode_connector_set_path_property(struct drm_connector *connector,
   4641       1.5  riastrad 					 const char *path)
   4642       1.5  riastrad {
   4643       1.5  riastrad 	struct drm_device *dev = connector->dev;
   4644       1.5  riastrad 	int ret;
   4645       1.5  riastrad 
   4646       1.5  riastrad 	ret = drm_property_replace_global_blob(dev,
   4647       1.5  riastrad 	                                       &connector->path_blob_ptr,
   4648       1.5  riastrad 	                                       strlen(path) + 1,
   4649       1.5  riastrad 	                                       path,
   4650       1.5  riastrad 	                                       &connector->base,
   4651       1.5  riastrad 	                                       dev->mode_config.path_property);
   4652       1.5  riastrad 	return ret;
   4653       1.5  riastrad }
   4654       1.5  riastrad EXPORT_SYMBOL(drm_mode_connector_set_path_property);
   4655       1.5  riastrad 
   4656       1.5  riastrad /**
   4657       1.5  riastrad  * drm_mode_connector_set_tile_property - set tile property on connector
   4658       1.5  riastrad  * @connector: connector to set property on.
   4659       1.5  riastrad  *
   4660       1.5  riastrad  * This looks up the tile information for a connector, and creates a
   4661       1.5  riastrad  * property for userspace to parse if it exists. The property is of
   4662       1.5  riastrad  * the form of 8 integers using ':' as a separator.
   4663       1.5  riastrad  *
   4664       1.5  riastrad  * Returns:
   4665       1.5  riastrad  * Zero on success, errno on failure.
   4666       1.5  riastrad  */
   4667       1.5  riastrad int drm_mode_connector_set_tile_property(struct drm_connector *connector)
   4668       1.5  riastrad {
   4669       1.5  riastrad 	struct drm_device *dev = connector->dev;
   4670       1.5  riastrad 	char tile[256];
   4671       1.5  riastrad 	int ret;
   4672       1.5  riastrad 
   4673       1.5  riastrad 	if (!connector->has_tile) {
   4674       1.5  riastrad 		ret  = drm_property_replace_global_blob(dev,
   4675       1.5  riastrad 		                                        &connector->tile_blob_ptr,
   4676       1.5  riastrad 		                                        0,
   4677       1.5  riastrad 		                                        NULL,
   4678       1.5  riastrad 		                                        &connector->base,
   4679       1.5  riastrad 		                                        dev->mode_config.tile_property);
   4680       1.5  riastrad 		return ret;
   4681       1.5  riastrad 	}
   4682       1.5  riastrad 
   4683       1.5  riastrad 	snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
   4684       1.5  riastrad 		 connector->tile_group->id, connector->tile_is_single_monitor,
   4685       1.5  riastrad 		 connector->num_h_tile, connector->num_v_tile,
   4686       1.5  riastrad 		 connector->tile_h_loc, connector->tile_v_loc,
   4687       1.5  riastrad 		 connector->tile_h_size, connector->tile_v_size);
   4688       1.5  riastrad 
   4689       1.5  riastrad 	ret = drm_property_replace_global_blob(dev,
   4690       1.5  riastrad 	                                       &connector->tile_blob_ptr,
   4691       1.5  riastrad 	                                       strlen(tile) + 1,
   4692       1.5  riastrad 	                                       tile,
   4693       1.5  riastrad 	                                       &connector->base,
   4694       1.5  riastrad 	                                       dev->mode_config.tile_property);
   4695       1.5  riastrad 	return ret;
   4696       1.5  riastrad }
   4697       1.5  riastrad EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
   4698       1.5  riastrad 
   4699       1.5  riastrad /**
   4700       1.3  riastrad  * drm_mode_connector_update_edid_property - update the edid property of a connector
   4701       1.3  riastrad  * @connector: drm connector
   4702       1.3  riastrad  * @edid: new value of the edid property
   4703       1.3  riastrad  *
   4704       1.3  riastrad  * This function creates a new blob modeset object and assigns its id to the
   4705       1.3  riastrad  * connector's edid property.
   4706       1.3  riastrad  *
   4707       1.3  riastrad  * Returns:
   4708       1.5  riastrad  * Zero on success, negative errno on failure.
   4709       1.3  riastrad  */
   4710       1.1  riastrad int drm_mode_connector_update_edid_property(struct drm_connector *connector,
   4711       1.5  riastrad 					    const struct edid *edid)
   4712       1.1  riastrad {
   4713       1.1  riastrad 	struct drm_device *dev = connector->dev;
   4714       1.5  riastrad 	size_t size = 0;
   4715       1.5  riastrad 	int ret;
   4716       1.1  riastrad 
   4717       1.5  riastrad 	/* ignore requests to set edid when overridden */
   4718       1.5  riastrad 	if (connector->override_edid)
   4719       1.5  riastrad 		return 0;
   4720       1.1  riastrad 
   4721       1.5  riastrad 	if (edid)
   4722       1.5  riastrad 		size = EDID_LENGTH * (1 + edid->extensions);
   4723       1.1  riastrad 
   4724       1.5  riastrad 	ret = drm_property_replace_global_blob(dev,
   4725       1.5  riastrad 					       &connector->edid_blob_ptr,
   4726       1.5  riastrad 	                                       size,
   4727       1.5  riastrad 	                                       edid,
   4728       1.5  riastrad 	                                       &connector->base,
   4729       1.5  riastrad 	                                       dev->mode_config.edid_property);
   4730       1.1  riastrad 	return ret;
   4731       1.1  riastrad }
   4732       1.1  riastrad EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
   4733       1.1  riastrad 
   4734       1.5  riastrad /* Some properties could refer to dynamic refcnt'd objects, or things that
   4735       1.5  riastrad  * need special locking to handle lifetime issues (ie. to ensure the prop
   4736       1.5  riastrad  * value doesn't become invalid part way through the property update due to
   4737       1.5  riastrad  * race).  The value returned by reference via 'obj' should be passed back
   4738       1.5  riastrad  * to drm_property_change_valid_put() after the property is set (and the
   4739       1.5  riastrad  * object to which the property is attached has a chance to take it's own
   4740       1.5  riastrad  * reference).
   4741       1.5  riastrad  */
   4742       1.5  riastrad bool drm_property_change_valid_get(struct drm_property *property,
   4743       1.5  riastrad 					 uint64_t value, struct drm_mode_object **ref)
   4744       1.1  riastrad {
   4745       1.5  riastrad 	int i;
   4746       1.5  riastrad 
   4747       1.1  riastrad 	if (property->flags & DRM_MODE_PROP_IMMUTABLE)
   4748       1.1  riastrad 		return false;
   4749       1.5  riastrad 
   4750       1.5  riastrad 	*ref = NULL;
   4751       1.5  riastrad 
   4752       1.5  riastrad 	if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
   4753       1.1  riastrad 		if (value < property->values[0] || value > property->values[1])
   4754       1.1  riastrad 			return false;
   4755       1.1  riastrad 		return true;
   4756       1.5  riastrad 	} else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
   4757       1.5  riastrad 		int64_t svalue = U642I64(value);
   4758       1.5  riastrad 
   4759       1.5  riastrad 		if (svalue < U642I64(property->values[0]) ||
   4760       1.5  riastrad 				svalue > U642I64(property->values[1]))
   4761       1.5  riastrad 			return false;
   4762       1.5  riastrad 		return true;
   4763       1.5  riastrad 	} else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
   4764       1.1  riastrad 		uint64_t valid_mask = 0;
   4765       1.5  riastrad 
   4766       1.1  riastrad 		for (i = 0; i < property->num_values; i++)
   4767       1.1  riastrad 			valid_mask |= (1ULL << property->values[i]);
   4768       1.1  riastrad 		return !(value & ~valid_mask);
   4769       1.5  riastrad 	} else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
   4770       1.5  riastrad 		struct drm_property_blob *blob;
   4771       1.5  riastrad 
   4772       1.5  riastrad 		if (value == 0)
   4773       1.5  riastrad 			return true;
   4774       1.5  riastrad 
   4775       1.5  riastrad 		blob = drm_property_lookup_blob(property->dev, value);
   4776       1.5  riastrad 		if (blob) {
   4777       1.5  riastrad 			*ref = &blob->base;
   4778       1.5  riastrad 			return true;
   4779       1.5  riastrad 		} else {
   4780       1.5  riastrad 			return false;
   4781       1.5  riastrad 		}
   4782       1.5  riastrad 	} else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
   4783       1.5  riastrad 		/* a zero value for an object property translates to null: */
   4784       1.5  riastrad 		if (value == 0)
   4785       1.5  riastrad 			return true;
   4786       1.5  riastrad 
   4787       1.5  riastrad 		/* handle refcnt'd objects specially: */
   4788       1.5  riastrad 		if (property->values[0] == DRM_MODE_OBJECT_FB) {
   4789       1.5  riastrad 			struct drm_framebuffer *fb;
   4790       1.5  riastrad 			fb = drm_framebuffer_lookup(property->dev, value);
   4791       1.5  riastrad 			if (fb) {
   4792       1.5  riastrad 				*ref = &fb->base;
   4793       1.1  riastrad 				return true;
   4794       1.5  riastrad 			} else {
   4795       1.5  riastrad 				return false;
   4796       1.5  riastrad 			}
   4797       1.5  riastrad 		} else {
   4798       1.5  riastrad 			return _object_find(property->dev, value, property->values[0]) != NULL;
   4799       1.5  riastrad 		}
   4800       1.3  riastrad 	}
   4801       1.5  riastrad 
   4802       1.5  riastrad 	for (i = 0; i < property->num_values; i++)
   4803       1.5  riastrad 		if (property->values[i] == value)
   4804       1.5  riastrad 			return true;
   4805       1.5  riastrad 	return false;
   4806       1.5  riastrad }
   4807       1.5  riastrad 
   4808       1.5  riastrad void drm_property_change_valid_put(struct drm_property *property,
   4809       1.5  riastrad 		struct drm_mode_object *ref)
   4810       1.5  riastrad {
   4811       1.5  riastrad 	if (!ref)
   4812       1.5  riastrad 		return;
   4813       1.5  riastrad 
   4814       1.5  riastrad 	if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
   4815       1.5  riastrad 		if (property->values[0] == DRM_MODE_OBJECT_FB)
   4816       1.5  riastrad 			drm_framebuffer_unreference(obj_to_fb(ref));
   4817       1.5  riastrad 	} else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
   4818       1.5  riastrad 		drm_property_unreference_blob(obj_to_blob(ref));
   4819       1.3  riastrad }
   4820       1.3  riastrad 
   4821       1.3  riastrad /**
   4822       1.3  riastrad  * drm_mode_connector_property_set_ioctl - set the current value of a connector property
   4823       1.3  riastrad  * @dev: DRM device
   4824       1.3  riastrad  * @data: ioctl data
   4825       1.3  riastrad  * @file_priv: DRM file info
   4826       1.3  riastrad  *
   4827       1.3  riastrad  * This function sets the current value for a connectors's property. It also
   4828       1.3  riastrad  * calls into a driver's ->set_property callback to update the hardware state
   4829       1.3  riastrad  *
   4830       1.3  riastrad  * Called by the user via ioctl.
   4831       1.3  riastrad  *
   4832       1.3  riastrad  * Returns:
   4833       1.5  riastrad  * Zero on success, negative errno on failure.
   4834       1.3  riastrad  */
   4835       1.1  riastrad int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
   4836       1.1  riastrad 				       void *data, struct drm_file *file_priv)
   4837       1.1  riastrad {
   4838       1.1  riastrad 	struct drm_mode_connector_set_property *conn_set_prop = data;
   4839       1.1  riastrad 	struct drm_mode_obj_set_property obj_set_prop = {
   4840       1.1  riastrad 		.value = conn_set_prop->value,
   4841       1.1  riastrad 		.prop_id = conn_set_prop->prop_id,
   4842       1.1  riastrad 		.obj_id = conn_set_prop->connector_id,
   4843       1.1  riastrad 		.obj_type = DRM_MODE_OBJECT_CONNECTOR
   4844       1.1  riastrad 	};
   4845       1.1  riastrad 
   4846       1.1  riastrad 	/* It does all the locking and checking we need */
   4847       1.1  riastrad 	return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
   4848       1.1  riastrad }
   4849       1.1  riastrad 
   4850       1.1  riastrad static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
   4851       1.1  riastrad 					   struct drm_property *property,
   4852       1.1  riastrad 					   uint64_t value)
   4853       1.1  riastrad {
   4854       1.1  riastrad 	int ret = -EINVAL;
   4855       1.1  riastrad 	struct drm_connector *connector = obj_to_connector(obj);
   4856       1.1  riastrad 
   4857       1.1  riastrad 	/* Do DPMS ourselves */
   4858       1.1  riastrad 	if (property == connector->dev->mode_config.dpms_property) {
   4859       1.5  riastrad 		ret = 0;
   4860       1.1  riastrad 		if (connector->funcs->dpms)
   4861       1.5  riastrad 			ret = (*connector->funcs->dpms)(connector, (int)value);
   4862       1.1  riastrad 	} else if (connector->funcs->set_property)
   4863       1.1  riastrad 		ret = connector->funcs->set_property(connector, property, value);
   4864       1.1  riastrad 
   4865       1.1  riastrad 	/* store the property value if successful */
   4866       1.1  riastrad 	if (!ret)
   4867       1.1  riastrad 		drm_object_property_set_value(&connector->base, property, value);
   4868       1.1  riastrad 	return ret;
   4869       1.1  riastrad }
   4870       1.1  riastrad 
   4871       1.1  riastrad static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
   4872       1.1  riastrad 				      struct drm_property *property,
   4873       1.1  riastrad 				      uint64_t value)
   4874       1.1  riastrad {
   4875       1.1  riastrad 	int ret = -EINVAL;
   4876       1.1  riastrad 	struct drm_crtc *crtc = obj_to_crtc(obj);
   4877       1.1  riastrad 
   4878       1.1  riastrad 	if (crtc->funcs->set_property)
   4879       1.1  riastrad 		ret = crtc->funcs->set_property(crtc, property, value);
   4880       1.1  riastrad 	if (!ret)
   4881       1.1  riastrad 		drm_object_property_set_value(obj, property, value);
   4882       1.1  riastrad 
   4883       1.1  riastrad 	return ret;
   4884       1.1  riastrad }
   4885       1.1  riastrad 
   4886       1.5  riastrad /**
   4887       1.5  riastrad  * drm_mode_plane_set_obj_prop - set the value of a property
   4888       1.5  riastrad  * @plane: drm plane object to set property value for
   4889       1.5  riastrad  * @property: property to set
   4890       1.5  riastrad  * @value: value the property should be set to
   4891       1.5  riastrad  *
   4892       1.5  riastrad  * This functions sets a given property on a given plane object. This function
   4893       1.5  riastrad  * calls the driver's ->set_property callback and changes the software state of
   4894       1.5  riastrad  * the property if the callback succeeds.
   4895       1.5  riastrad  *
   4896       1.5  riastrad  * Returns:
   4897       1.5  riastrad  * Zero on success, error code on failure.
   4898       1.5  riastrad  */
   4899       1.5  riastrad int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
   4900       1.5  riastrad 				struct drm_property *property,
   4901       1.5  riastrad 				uint64_t value)
   4902       1.1  riastrad {
   4903       1.1  riastrad 	int ret = -EINVAL;
   4904       1.5  riastrad 	struct drm_mode_object *obj = &plane->base;
   4905       1.1  riastrad 
   4906       1.1  riastrad 	if (plane->funcs->set_property)
   4907       1.1  riastrad 		ret = plane->funcs->set_property(plane, property, value);
   4908       1.1  riastrad 	if (!ret)
   4909       1.1  riastrad 		drm_object_property_set_value(obj, property, value);
   4910       1.1  riastrad 
   4911       1.1  riastrad 	return ret;
   4912       1.1  riastrad }
   4913       1.5  riastrad EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
   4914       1.1  riastrad 
   4915       1.3  riastrad /**
   4916       1.5  riastrad  * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
   4917       1.3  riastrad  * @dev: DRM device
   4918       1.3  riastrad  * @data: ioctl data
   4919       1.3  riastrad  * @file_priv: DRM file info
   4920       1.3  riastrad  *
   4921       1.3  riastrad  * This function retrieves the current value for an object's property. Compared
   4922       1.3  riastrad  * to the connector specific ioctl this one is extended to also work on crtc and
   4923       1.3  riastrad  * plane objects.
   4924       1.3  riastrad  *
   4925       1.3  riastrad  * Called by the user via ioctl.
   4926       1.3  riastrad  *
   4927       1.3  riastrad  * Returns:
   4928       1.5  riastrad  * Zero on success, negative errno on failure.
   4929       1.3  riastrad  */
   4930       1.1  riastrad int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
   4931       1.1  riastrad 				      struct drm_file *file_priv)
   4932       1.1  riastrad {
   4933       1.1  riastrad 	struct drm_mode_obj_get_properties *arg = data;
   4934       1.1  riastrad 	struct drm_mode_object *obj;
   4935       1.1  riastrad 	int ret = 0;
   4936       1.1  riastrad 
   4937       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   4938       1.1  riastrad 		return -EINVAL;
   4939       1.1  riastrad 
   4940       1.3  riastrad 	drm_modeset_lock_all(dev);
   4941       1.1  riastrad 
   4942       1.1  riastrad 	obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
   4943       1.1  riastrad 	if (!obj) {
   4944       1.3  riastrad 		ret = -ENOENT;
   4945       1.1  riastrad 		goto out;
   4946       1.1  riastrad 	}
   4947       1.1  riastrad 	if (!obj->properties) {
   4948       1.1  riastrad 		ret = -EINVAL;
   4949       1.1  riastrad 		goto out;
   4950       1.1  riastrad 	}
   4951       1.1  riastrad 
   4952       1.5  riastrad 	ret = get_properties(obj, file_priv->atomic,
   4953       1.5  riastrad 			(uint32_t __user *)(unsigned long)(arg->props_ptr),
   4954       1.5  riastrad 			(uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
   4955       1.5  riastrad 			&arg->count_props);
   4956       1.1  riastrad 
   4957       1.1  riastrad out:
   4958       1.3  riastrad 	drm_modeset_unlock_all(dev);
   4959       1.1  riastrad 	return ret;
   4960       1.1  riastrad }
   4961       1.1  riastrad 
   4962       1.3  riastrad /**
   4963       1.3  riastrad  * drm_mode_obj_set_property_ioctl - set the current value of an object's property
   4964       1.3  riastrad  * @dev: DRM device
   4965       1.3  riastrad  * @data: ioctl data
   4966       1.3  riastrad  * @file_priv: DRM file info
   4967       1.3  riastrad  *
   4968       1.3  riastrad  * This function sets the current value for an object's property. It also calls
   4969       1.3  riastrad  * into a driver's ->set_property callback to update the hardware state.
   4970       1.3  riastrad  * Compared to the connector specific ioctl this one is extended to also work on
   4971       1.3  riastrad  * crtc and plane objects.
   4972       1.3  riastrad  *
   4973       1.3  riastrad  * Called by the user via ioctl.
   4974       1.3  riastrad  *
   4975       1.3  riastrad  * Returns:
   4976       1.5  riastrad  * Zero on success, negative errno on failure.
   4977       1.3  riastrad  */
   4978       1.1  riastrad int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
   4979       1.1  riastrad 				    struct drm_file *file_priv)
   4980       1.1  riastrad {
   4981       1.1  riastrad 	struct drm_mode_obj_set_property *arg = data;
   4982       1.1  riastrad 	struct drm_mode_object *arg_obj;
   4983       1.1  riastrad 	struct drm_mode_object *prop_obj;
   4984       1.1  riastrad 	struct drm_property *property;
   4985       1.5  riastrad 	int i, ret = -EINVAL;
   4986       1.5  riastrad 	struct drm_mode_object *ref;
   4987       1.1  riastrad 
   4988       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   4989       1.1  riastrad 		return -EINVAL;
   4990       1.1  riastrad 
   4991       1.3  riastrad 	drm_modeset_lock_all(dev);
   4992       1.1  riastrad 
   4993       1.1  riastrad 	arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
   4994       1.3  riastrad 	if (!arg_obj) {
   4995       1.3  riastrad 		ret = -ENOENT;
   4996       1.1  riastrad 		goto out;
   4997       1.3  riastrad 	}
   4998       1.1  riastrad 	if (!arg_obj->properties)
   4999       1.1  riastrad 		goto out;
   5000       1.1  riastrad 
   5001       1.1  riastrad 	for (i = 0; i < arg_obj->properties->count; i++)
   5002       1.5  riastrad 		if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
   5003       1.1  riastrad 			break;
   5004       1.1  riastrad 
   5005       1.1  riastrad 	if (i == arg_obj->properties->count)
   5006       1.1  riastrad 		goto out;
   5007       1.1  riastrad 
   5008       1.1  riastrad 	prop_obj = drm_mode_object_find(dev, arg->prop_id,
   5009       1.1  riastrad 					DRM_MODE_OBJECT_PROPERTY);
   5010       1.3  riastrad 	if (!prop_obj) {
   5011       1.3  riastrad 		ret = -ENOENT;
   5012       1.1  riastrad 		goto out;
   5013       1.3  riastrad 	}
   5014       1.1  riastrad 	property = obj_to_property(prop_obj);
   5015       1.1  riastrad 
   5016       1.5  riastrad 	if (!drm_property_change_valid_get(property, arg->value, &ref))
   5017       1.1  riastrad 		goto out;
   5018       1.1  riastrad 
   5019       1.1  riastrad 	switch (arg_obj->type) {
   5020       1.1  riastrad 	case DRM_MODE_OBJECT_CONNECTOR:
   5021       1.1  riastrad 		ret = drm_mode_connector_set_obj_prop(arg_obj, property,
   5022       1.1  riastrad 						      arg->value);
   5023       1.1  riastrad 		break;
   5024       1.1  riastrad 	case DRM_MODE_OBJECT_CRTC:
   5025       1.1  riastrad 		ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
   5026       1.1  riastrad 		break;
   5027       1.1  riastrad 	case DRM_MODE_OBJECT_PLANE:
   5028       1.5  riastrad 		ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
   5029       1.5  riastrad 						  property, arg->value);
   5030       1.1  riastrad 		break;
   5031       1.1  riastrad 	}
   5032       1.1  riastrad 
   5033       1.5  riastrad 	drm_property_change_valid_put(property, ref);
   5034       1.5  riastrad 
   5035       1.1  riastrad out:
   5036       1.3  riastrad 	drm_modeset_unlock_all(dev);
   5037       1.1  riastrad 	return ret;
   5038       1.1  riastrad }
   5039       1.1  riastrad 
   5040       1.3  riastrad /**
   5041       1.3  riastrad  * drm_mode_connector_attach_encoder - attach a connector to an encoder
   5042       1.3  riastrad  * @connector: connector to attach
   5043       1.3  riastrad  * @encoder: encoder to attach @connector to
   5044       1.3  riastrad  *
   5045       1.3  riastrad  * This function links up a connector to an encoder. Note that the routing
   5046       1.3  riastrad  * restrictions between encoders and crtcs are exposed to userspace through the
   5047       1.3  riastrad  * possible_clones and possible_crtcs bitmasks.
   5048       1.3  riastrad  *
   5049       1.3  riastrad  * Returns:
   5050       1.5  riastrad  * Zero on success, negative errno on failure.
   5051       1.3  riastrad  */
   5052       1.1  riastrad int drm_mode_connector_attach_encoder(struct drm_connector *connector,
   5053       1.1  riastrad 				      struct drm_encoder *encoder)
   5054       1.1  riastrad {
   5055       1.1  riastrad 	int i;
   5056       1.1  riastrad 
   5057       1.1  riastrad 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
   5058       1.1  riastrad 		if (connector->encoder_ids[i] == 0) {
   5059       1.1  riastrad 			connector->encoder_ids[i] = encoder->base.id;
   5060       1.1  riastrad 			return 0;
   5061       1.1  riastrad 		}
   5062       1.1  riastrad 	}
   5063       1.1  riastrad 	return -ENOMEM;
   5064       1.1  riastrad }
   5065       1.1  riastrad EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
   5066       1.1  riastrad 
   5067       1.3  riastrad /**
   5068       1.3  riastrad  * drm_mode_crtc_set_gamma_size - set the gamma table size
   5069       1.3  riastrad  * @crtc: CRTC to set the gamma table size for
   5070       1.3  riastrad  * @gamma_size: size of the gamma table
   5071       1.3  riastrad  *
   5072       1.3  riastrad  * Drivers which support gamma tables should set this to the supported gamma
   5073       1.3  riastrad  * table size when initializing the CRTC. Currently the drm core only supports a
   5074       1.3  riastrad  * fixed gamma table size.
   5075       1.3  riastrad  *
   5076       1.3  riastrad  * Returns:
   5077       1.5  riastrad  * Zero on success, negative errno on failure.
   5078       1.3  riastrad  */
   5079       1.1  riastrad int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
   5080       1.3  riastrad 				 int gamma_size)
   5081       1.1  riastrad {
   5082       1.1  riastrad 	crtc->gamma_size = gamma_size;
   5083       1.1  riastrad 
   5084       1.5  riastrad 	crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
   5085       1.5  riastrad 				    GFP_KERNEL);
   5086       1.1  riastrad 	if (!crtc->gamma_store) {
   5087       1.1  riastrad 		crtc->gamma_size = 0;
   5088       1.1  riastrad 		return -ENOMEM;
   5089       1.1  riastrad 	}
   5090       1.1  riastrad 
   5091       1.1  riastrad 	return 0;
   5092       1.1  riastrad }
   5093       1.1  riastrad EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
   5094       1.1  riastrad 
   5095       1.3  riastrad /**
   5096       1.3  riastrad  * drm_mode_gamma_set_ioctl - set the gamma table
   5097       1.3  riastrad  * @dev: DRM device
   5098       1.3  riastrad  * @data: ioctl data
   5099       1.3  riastrad  * @file_priv: DRM file info
   5100       1.3  riastrad  *
   5101       1.3  riastrad  * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
   5102       1.3  riastrad  * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
   5103       1.3  riastrad  *
   5104       1.3  riastrad  * Called by the user via ioctl.
   5105       1.3  riastrad  *
   5106       1.3  riastrad  * Returns:
   5107       1.5  riastrad  * Zero on success, negative errno on failure.
   5108       1.3  riastrad  */
   5109       1.1  riastrad int drm_mode_gamma_set_ioctl(struct drm_device *dev,
   5110       1.1  riastrad 			     void *data, struct drm_file *file_priv)
   5111       1.1  riastrad {
   5112       1.1  riastrad 	struct drm_mode_crtc_lut *crtc_lut = data;
   5113       1.1  riastrad 	struct drm_crtc *crtc;
   5114       1.1  riastrad 	void *r_base, *g_base, *b_base;
   5115       1.1  riastrad 	int size;
   5116       1.1  riastrad 	int ret = 0;
   5117       1.1  riastrad 
   5118       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   5119       1.1  riastrad 		return -EINVAL;
   5120       1.1  riastrad 
   5121       1.3  riastrad 	drm_modeset_lock_all(dev);
   5122       1.5  riastrad 	crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
   5123       1.5  riastrad 	if (!crtc) {
   5124       1.3  riastrad 		ret = -ENOENT;
   5125       1.1  riastrad 		goto out;
   5126       1.1  riastrad 	}
   5127       1.1  riastrad 
   5128       1.1  riastrad 	if (crtc->funcs->gamma_set == NULL) {
   5129       1.1  riastrad 		ret = -ENOSYS;
   5130       1.1  riastrad 		goto out;
   5131       1.1  riastrad 	}
   5132       1.1  riastrad 
   5133       1.1  riastrad 	/* memcpy into gamma store */
   5134       1.1  riastrad 	if (crtc_lut->gamma_size != crtc->gamma_size) {
   5135       1.1  riastrad 		ret = -EINVAL;
   5136       1.1  riastrad 		goto out;
   5137       1.1  riastrad 	}
   5138       1.1  riastrad 
   5139       1.1  riastrad 	size = crtc_lut->gamma_size * (sizeof(uint16_t));
   5140       1.1  riastrad 	r_base = crtc->gamma_store;
   5141       1.1  riastrad 	if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
   5142       1.1  riastrad 		ret = -EFAULT;
   5143       1.1  riastrad 		goto out;
   5144       1.1  riastrad 	}
   5145       1.1  riastrad 
   5146  1.11.6.1        ad 	g_base = r_base + size;
   5147       1.1  riastrad 	if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
   5148       1.1  riastrad 		ret = -EFAULT;
   5149       1.1  riastrad 		goto out;
   5150       1.1  riastrad 	}
   5151       1.1  riastrad 
   5152  1.11.6.1        ad 	b_base = g_base + size;
   5153       1.1  riastrad 	if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
   5154       1.1  riastrad 		ret = -EFAULT;
   5155       1.1  riastrad 		goto out;
   5156       1.1  riastrad 	}
   5157       1.1  riastrad 
   5158       1.1  riastrad 	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
   5159       1.1  riastrad 
   5160       1.1  riastrad out:
   5161       1.3  riastrad 	drm_modeset_unlock_all(dev);
   5162       1.1  riastrad 	return ret;
   5163       1.1  riastrad 
   5164       1.1  riastrad }
   5165       1.1  riastrad 
   5166       1.3  riastrad /**
   5167       1.3  riastrad  * drm_mode_gamma_get_ioctl - get the gamma table
   5168       1.3  riastrad  * @dev: DRM device
   5169       1.3  riastrad  * @data: ioctl data
   5170       1.3  riastrad  * @file_priv: DRM file info
   5171       1.3  riastrad  *
   5172       1.3  riastrad  * Copy the current gamma table into the storage provided. This also provides
   5173       1.3  riastrad  * the gamma table size the driver expects, which can be used to size the
   5174       1.3  riastrad  * allocated storage.
   5175       1.3  riastrad  *
   5176       1.3  riastrad  * Called by the user via ioctl.
   5177       1.3  riastrad  *
   5178       1.3  riastrad  * Returns:
   5179       1.5  riastrad  * Zero on success, negative errno on failure.
   5180       1.3  riastrad  */
   5181       1.1  riastrad int drm_mode_gamma_get_ioctl(struct drm_device *dev,
   5182       1.1  riastrad 			     void *data, struct drm_file *file_priv)
   5183       1.1  riastrad {
   5184       1.1  riastrad 	struct drm_mode_crtc_lut *crtc_lut = data;
   5185       1.1  riastrad 	struct drm_crtc *crtc;
   5186       1.1  riastrad 	void *r_base, *g_base, *b_base;
   5187       1.1  riastrad 	int size;
   5188       1.1  riastrad 	int ret = 0;
   5189       1.1  riastrad 
   5190       1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   5191       1.1  riastrad 		return -EINVAL;
   5192       1.1  riastrad 
   5193       1.3  riastrad 	drm_modeset_lock_all(dev);
   5194       1.5  riastrad 	crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
   5195       1.5  riastrad 	if (!crtc) {
   5196       1.3  riastrad 		ret = -ENOENT;
   5197       1.1  riastrad 		goto out;
   5198       1.1  riastrad 	}
   5199       1.1  riastrad 
   5200       1.1  riastrad 	/* memcpy into gamma store */
   5201       1.1  riastrad 	if (crtc_lut->gamma_size != crtc->gamma_size) {
   5202       1.1  riastrad 		ret = -EINVAL;
   5203       1.1  riastrad 		goto out;
   5204       1.1  riastrad 	}
   5205       1.1  riastrad 
   5206       1.1  riastrad 	size = crtc_lut->gamma_size * (sizeof(uint16_t));
   5207       1.1  riastrad 	r_base = crtc->gamma_store;
   5208       1.1  riastrad 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
   5209       1.1  riastrad 		ret = -EFAULT;
   5210       1.1  riastrad 		goto out;
   5211       1.1  riastrad 	}
   5212       1.1  riastrad 
   5213  1.11.6.1        ad 	g_base = r_base + size;
   5214       1.1  riastrad 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
   5215       1.1  riastrad 		ret = -EFAULT;
   5216       1.1  riastrad 		goto out;
   5217       1.1  riastrad 	}
   5218       1.1  riastrad 
   5219  1.11.6.1        ad 	b_base = g_base + size;
   5220       1.1  riastrad 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
   5221       1.1  riastrad 		ret = -EFAULT;
   5222       1.1  riastrad 		goto out;
   5223       1.1  riastrad 	}
   5224       1.1  riastrad out:
   5225       1.3  riastrad 	drm_modeset_unlock_all(dev);
   5226       1.1  riastrad 	return ret;
   5227       1.1  riastrad }
   5228       1.1  riastrad 
   5229       1.3  riastrad /**
   5230       1.3  riastrad  * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
   5231       1.3  riastrad  * @dev: DRM device
   5232       1.3  riastrad  * @data: ioctl data
   5233       1.3  riastrad  * @file_priv: DRM file info
   5234       1.3  riastrad  *
   5235       1.3  riastrad  * This schedules an asynchronous update on a given CRTC, called page flip.
   5236       1.3  riastrad  * Optionally a drm event is generated to signal the completion of the event.
   5237       1.3  riastrad  * Generic drivers cannot assume that a pageflip with changed framebuffer
   5238       1.3  riastrad  * properties (including driver specific metadata like tiling layout) will work,
   5239       1.3  riastrad  * but some drivers support e.g. pixel format changes through the pageflip
   5240       1.3  riastrad  * ioctl.
   5241       1.3  riastrad  *
   5242       1.3  riastrad  * Called by the user via ioctl.
   5243       1.3  riastrad  *
   5244       1.3  riastrad  * Returns:
   5245       1.5  riastrad  * Zero on success, negative errno on failure.
   5246       1.3  riastrad  */
   5247       1.1  riastrad int drm_mode_page_flip_ioctl(struct drm_device *dev,
   5248       1.1  riastrad 			     void *data, struct drm_file *file_priv)
   5249       1.1  riastrad {
   5250       1.1  riastrad 	struct drm_mode_crtc_page_flip *page_flip = data;
   5251       1.1  riastrad 	struct drm_crtc *crtc;
   5252       1.5  riastrad 	struct drm_framebuffer *fb = NULL;
   5253       1.1  riastrad 	struct drm_pending_vblank_event *e = NULL;
   5254       1.1  riastrad 	unsigned long flags;
   5255       1.1  riastrad 	int ret = -EINVAL;
   5256       1.1  riastrad 
   5257       1.5  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   5258       1.5  riastrad 		return -EINVAL;
   5259       1.5  riastrad 
   5260       1.1  riastrad 	if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
   5261       1.1  riastrad 	    page_flip->reserved != 0)
   5262       1.1  riastrad 		return -EINVAL;
   5263       1.1  riastrad 
   5264       1.3  riastrad 	if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
   5265       1.3  riastrad 		return -EINVAL;
   5266       1.3  riastrad 
   5267       1.5  riastrad 	crtc = drm_crtc_find(dev, page_flip->crtc_id);
   5268       1.5  riastrad 	if (!crtc)
   5269       1.3  riastrad 		return -ENOENT;
   5270       1.1  riastrad 
   5271       1.5  riastrad 	drm_modeset_lock_crtc(crtc, crtc->primary);
   5272       1.3  riastrad 	if (crtc->primary->fb == NULL) {
   5273       1.1  riastrad 		/* The framebuffer is currently unbound, presumably
   5274       1.1  riastrad 		 * due to a hotplug event, that userspace has not
   5275       1.1  riastrad 		 * yet discovered.
   5276       1.1  riastrad 		 */
   5277       1.1  riastrad 		ret = -EBUSY;
   5278       1.1  riastrad 		goto out;
   5279       1.1  riastrad 	}
   5280       1.1  riastrad 
   5281       1.1  riastrad 	if (crtc->funcs->page_flip == NULL)
   5282       1.1  riastrad 		goto out;
   5283       1.1  riastrad 
   5284       1.3  riastrad 	fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
   5285       1.3  riastrad 	if (!fb) {
   5286       1.3  riastrad 		ret = -ENOENT;
   5287       1.1  riastrad 		goto out;
   5288       1.3  riastrad 	}
   5289       1.1  riastrad 
   5290       1.5  riastrad 	if (crtc->state) {
   5291       1.5  riastrad 		const struct drm_plane_state *state = crtc->primary->state;
   5292       1.5  riastrad 
   5293       1.5  riastrad 		ret = check_src_coords(state->src_x, state->src_y,
   5294       1.5  riastrad 				       state->src_w, state->src_h, fb);
   5295       1.5  riastrad 	} else {
   5296       1.5  riastrad 		ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
   5297       1.5  riastrad 	}
   5298       1.3  riastrad 	if (ret)
   5299       1.3  riastrad 		goto out;
   5300       1.1  riastrad 
   5301       1.3  riastrad 	if (crtc->primary->fb->pixel_format != fb->pixel_format) {
   5302       1.3  riastrad 		DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
   5303       1.3  riastrad 		ret = -EINVAL;
   5304       1.1  riastrad 		goto out;
   5305       1.1  riastrad 	}
   5306       1.1  riastrad 
   5307       1.1  riastrad 	if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
   5308       1.1  riastrad 		ret = -ENOMEM;
   5309       1.1  riastrad 		spin_lock_irqsave(&dev->event_lock, flags);
   5310       1.5  riastrad 		if (file_priv->event_space < sizeof(e->event)) {
   5311       1.1  riastrad 			spin_unlock_irqrestore(&dev->event_lock, flags);
   5312       1.1  riastrad 			goto out;
   5313       1.1  riastrad 		}
   5314       1.5  riastrad 		file_priv->event_space -= sizeof(e->event);
   5315       1.1  riastrad 		spin_unlock_irqrestore(&dev->event_lock, flags);
   5316       1.1  riastrad 
   5317       1.5  riastrad 		e = kzalloc(sizeof(*e), GFP_KERNEL);
   5318       1.1  riastrad 		if (e == NULL) {
   5319       1.1  riastrad 			spin_lock_irqsave(&dev->event_lock, flags);
   5320       1.5  riastrad 			file_priv->event_space += sizeof(e->event);
   5321       1.1  riastrad 			spin_unlock_irqrestore(&dev->event_lock, flags);
   5322       1.1  riastrad 			goto out;
   5323       1.1  riastrad 		}
   5324       1.1  riastrad 
   5325       1.1  riastrad 		e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
   5326       1.5  riastrad 		e->event.base.length = sizeof(e->event);
   5327       1.1  riastrad 		e->event.user_data = page_flip->user_data;
   5328       1.1  riastrad 		e->base.event = &e->event.base;
   5329       1.1  riastrad 		e->base.file_priv = file_priv;
   5330       1.1  riastrad 		e->base.destroy =
   5331       1.1  riastrad 			(void (*) (struct drm_pending_event *)) kfree;
   5332       1.1  riastrad 	}
   5333       1.1  riastrad 
   5334       1.5  riastrad 	crtc->primary->old_fb = crtc->primary->fb;
   5335       1.3  riastrad 	ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
   5336       1.1  riastrad 	if (ret) {
   5337       1.1  riastrad 		if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
   5338       1.1  riastrad 			spin_lock_irqsave(&dev->event_lock, flags);
   5339       1.5  riastrad 			file_priv->event_space += sizeof(e->event);
   5340       1.1  riastrad 			spin_unlock_irqrestore(&dev->event_lock, flags);
   5341       1.1  riastrad 			kfree(e);
   5342       1.1  riastrad 		}
   5343       1.3  riastrad 		/* Keep the old fb, don't unref it. */
   5344       1.5  riastrad 		crtc->primary->old_fb = NULL;
   5345       1.3  riastrad 	} else {
   5346       1.5  riastrad 		crtc->primary->fb = fb;
   5347       1.3  riastrad 		/* Unref only the old framebuffer. */
   5348       1.3  riastrad 		fb = NULL;
   5349       1.1  riastrad 	}
   5350       1.1  riastrad 
   5351       1.1  riastrad out:
   5352       1.3  riastrad 	if (fb)
   5353       1.3  riastrad 		drm_framebuffer_unreference(fb);
   5354       1.5  riastrad 	if (crtc->primary->old_fb)
   5355       1.5  riastrad 		drm_framebuffer_unreference(crtc->primary->old_fb);
   5356       1.5  riastrad 	crtc->primary->old_fb = NULL;
   5357       1.5  riastrad 	drm_modeset_unlock_crtc(crtc);
   5358       1.3  riastrad 
   5359       1.1  riastrad 	return ret;
   5360       1.1  riastrad }
   5361       1.1  riastrad 
   5362       1.3  riastrad /**
   5363       1.3  riastrad  * drm_mode_config_reset - call ->reset callbacks
   5364       1.3  riastrad  * @dev: drm device
   5365       1.3  riastrad  *
   5366       1.3  riastrad  * This functions calls all the crtc's, encoder's and connector's ->reset
   5367       1.3  riastrad  * callback. Drivers can use this in e.g. their driver load or resume code to
   5368       1.3  riastrad  * reset hardware and software state.
   5369       1.3  riastrad  */
   5370       1.1  riastrad void drm_mode_config_reset(struct drm_device *dev)
   5371       1.1  riastrad {
   5372       1.1  riastrad 	struct drm_crtc *crtc;
   5373       1.5  riastrad 	struct drm_plane *plane;
   5374       1.1  riastrad 	struct drm_encoder *encoder;
   5375       1.1  riastrad 	struct drm_connector *connector;
   5376       1.1  riastrad 
   5377       1.5  riastrad 	drm_for_each_plane(plane, dev)
   5378       1.5  riastrad 		if (plane->funcs->reset)
   5379       1.5  riastrad 			plane->funcs->reset(plane);
   5380       1.5  riastrad 
   5381       1.5  riastrad 	drm_for_each_crtc(crtc, dev)
   5382       1.1  riastrad 		if (crtc->funcs->reset)
   5383       1.1  riastrad 			crtc->funcs->reset(crtc);
   5384       1.1  riastrad 
   5385       1.5  riastrad 	drm_for_each_encoder(encoder, dev)
   5386       1.1  riastrad 		if (encoder->funcs->reset)
   5387       1.1  riastrad 			encoder->funcs->reset(encoder);
   5388       1.1  riastrad 
   5389       1.5  riastrad 	mutex_lock(&dev->mode_config.mutex);
   5390       1.5  riastrad 	drm_for_each_connector(connector, dev)
   5391       1.1  riastrad 		if (connector->funcs->reset)
   5392       1.1  riastrad 			connector->funcs->reset(connector);
   5393       1.5  riastrad 	mutex_unlock(&dev->mode_config.mutex);
   5394       1.1  riastrad }
   5395       1.1  riastrad EXPORT_SYMBOL(drm_mode_config_reset);
   5396       1.1  riastrad 
   5397       1.3  riastrad /**
   5398       1.3  riastrad  * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
   5399       1.3  riastrad  * @dev: DRM device
   5400       1.3  riastrad  * @data: ioctl data
   5401       1.3  riastrad  * @file_priv: DRM file info
   5402       1.3  riastrad  *
   5403       1.3  riastrad  * This creates a new dumb buffer in the driver's backing storage manager (GEM,
   5404       1.3  riastrad  * TTM or something else entirely) and returns the resulting buffer handle. This
   5405       1.3  riastrad  * handle can then be wrapped up into a framebuffer modeset object.
   5406       1.3  riastrad  *
   5407       1.3  riastrad  * Note that userspace is not allowed to use such objects for render
   5408       1.3  riastrad  * acceleration - drivers must create their own private ioctls for such a use
   5409       1.3  riastrad  * case.
   5410       1.3  riastrad  *
   5411       1.3  riastrad  * Called by the user via ioctl.
   5412       1.3  riastrad  *
   5413       1.3  riastrad  * Returns:
   5414       1.5  riastrad  * Zero on success, negative errno on failure.
   5415       1.3  riastrad  */
   5416       1.1  riastrad int drm_mode_create_dumb_ioctl(struct drm_device *dev,
   5417       1.1  riastrad 			       void *data, struct drm_file *file_priv)
   5418       1.1  riastrad {
   5419       1.1  riastrad 	struct drm_mode_create_dumb *args = data;
   5420       1.3  riastrad 	u32 cpp, stride, size;
   5421       1.1  riastrad 
   5422       1.1  riastrad 	if (!dev->driver->dumb_create)
   5423       1.1  riastrad 		return -ENOSYS;
   5424       1.3  riastrad 	if (!args->width || !args->height || !args->bpp)
   5425       1.3  riastrad 		return -EINVAL;
   5426       1.3  riastrad 
   5427       1.3  riastrad 	/* overflow checks for 32bit size calculations */
   5428       1.5  riastrad 	/* NOTE: DIV_ROUND_UP() can overflow */
   5429       1.3  riastrad 	cpp = DIV_ROUND_UP(args->bpp, 8);
   5430       1.5  riastrad 	if (!cpp || cpp > 0xffffffffU / args->width)
   5431       1.3  riastrad 		return -EINVAL;
   5432       1.3  riastrad 	stride = cpp * args->width;
   5433       1.3  riastrad 	if (args->height > 0xffffffffU / stride)
   5434       1.3  riastrad 		return -EINVAL;
   5435       1.3  riastrad 
   5436       1.3  riastrad 	/* test for wrap-around */
   5437       1.3  riastrad 	size = args->height * stride;
   5438       1.3  riastrad 	if (PAGE_ALIGN(size) == 0)
   5439       1.3  riastrad 		return -EINVAL;
   5440       1.3  riastrad 
   5441       1.5  riastrad 	/*
   5442       1.5  riastrad 	 * handle, pitch and size are output parameters. Zero them out to
   5443       1.5  riastrad 	 * prevent drivers from accidentally using uninitialized data. Since
   5444       1.5  riastrad 	 * not all existing userspace is clearing these fields properly we
   5445       1.5  riastrad 	 * cannot reject IOCTL with garbage in them.
   5446       1.5  riastrad 	 */
   5447       1.5  riastrad 	args->handle = 0;
   5448       1.5  riastrad 	args->pitch = 0;
   5449       1.5  riastrad 	args->size = 0;
   5450       1.5  riastrad 
   5451       1.1  riastrad 	return dev->driver->dumb_create(file_priv, dev, args);
   5452       1.1  riastrad }
   5453       1.1  riastrad 
   5454       1.3  riastrad /**
   5455       1.3  riastrad  * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
   5456       1.3  riastrad  * @dev: DRM device
   5457       1.3  riastrad  * @data: ioctl data
   5458       1.3  riastrad  * @file_priv: DRM file info
   5459       1.3  riastrad  *
   5460       1.3  riastrad  * Allocate an offset in the drm device node's address space to be able to
   5461       1.3  riastrad  * memory map a dumb buffer.
   5462       1.3  riastrad  *
   5463       1.3  riastrad  * Called by the user via ioctl.
   5464       1.3  riastrad  *
   5465       1.3  riastrad  * Returns:
   5466       1.5  riastrad  * Zero on success, negative errno on failure.
   5467       1.3  riastrad  */
   5468       1.1  riastrad int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
   5469       1.1  riastrad 			     void *data, struct drm_file *file_priv)
   5470       1.1  riastrad {
   5471       1.1  riastrad 	struct drm_mode_map_dumb *args = data;
   5472       1.1  riastrad 
   5473       1.1  riastrad 	/* call driver ioctl to get mmap offset */
   5474       1.1  riastrad 	if (!dev->driver->dumb_map_offset)
   5475       1.1  riastrad 		return -ENOSYS;
   5476       1.1  riastrad 
   5477       1.1  riastrad 	return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
   5478       1.1  riastrad }
   5479       1.1  riastrad 
   5480       1.3  riastrad /**
   5481       1.3  riastrad  * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
   5482       1.3  riastrad  * @dev: DRM device
   5483       1.3  riastrad  * @data: ioctl data
   5484       1.3  riastrad  * @file_priv: DRM file info
   5485       1.3  riastrad  *
   5486       1.3  riastrad  * This destroys the userspace handle for the given dumb backing storage buffer.
   5487       1.3  riastrad  * Since buffer objects must be reference counted in the kernel a buffer object
   5488       1.3  riastrad  * won't be immediately freed if a framebuffer modeset object still uses it.
   5489       1.3  riastrad  *
   5490       1.3  riastrad  * Called by the user via ioctl.
   5491       1.3  riastrad  *
   5492       1.3  riastrad  * Returns:
   5493       1.5  riastrad  * Zero on success, negative errno on failure.
   5494       1.3  riastrad  */
   5495       1.1  riastrad int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
   5496       1.1  riastrad 				void *data, struct drm_file *file_priv)
   5497       1.1  riastrad {
   5498       1.1  riastrad 	struct drm_mode_destroy_dumb *args = data;
   5499       1.1  riastrad 
   5500       1.1  riastrad 	if (!dev->driver->dumb_destroy)
   5501       1.1  riastrad 		return -ENOSYS;
   5502       1.1  riastrad 
   5503       1.1  riastrad 	return dev->driver->dumb_destroy(file_priv, dev, args->handle);
   5504       1.1  riastrad }
   5505       1.1  riastrad 
   5506       1.3  riastrad /**
   5507       1.3  riastrad  * drm_fb_get_bpp_depth - get the bpp/depth values for format
   5508       1.3  riastrad  * @format: pixel format (DRM_FORMAT_*)
   5509       1.3  riastrad  * @depth: storage for the depth value
   5510       1.3  riastrad  * @bpp: storage for the bpp value
   5511       1.3  riastrad  *
   5512       1.3  riastrad  * This only supports RGB formats here for compat with code that doesn't use
   5513       1.3  riastrad  * pixel formats directly yet.
   5514       1.1  riastrad  */
   5515       1.1  riastrad void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
   5516       1.1  riastrad 			  int *bpp)
   5517       1.1  riastrad {
   5518       1.1  riastrad 	switch (format) {
   5519       1.3  riastrad 	case DRM_FORMAT_C8:
   5520       1.1  riastrad 	case DRM_FORMAT_RGB332:
   5521       1.1  riastrad 	case DRM_FORMAT_BGR233:
   5522       1.1  riastrad 		*depth = 8;
   5523       1.1  riastrad 		*bpp = 8;
   5524       1.1  riastrad 		break;
   5525       1.1  riastrad 	case DRM_FORMAT_XRGB1555:
   5526       1.1  riastrad 	case DRM_FORMAT_XBGR1555:
   5527       1.1  riastrad 	case DRM_FORMAT_RGBX5551:
   5528       1.1  riastrad 	case DRM_FORMAT_BGRX5551:
   5529       1.1  riastrad 	case DRM_FORMAT_ARGB1555:
   5530       1.1  riastrad 	case DRM_FORMAT_ABGR1555:
   5531       1.1  riastrad 	case DRM_FORMAT_RGBA5551:
   5532       1.1  riastrad 	case DRM_FORMAT_BGRA5551:
   5533       1.1  riastrad 		*depth = 15;
   5534       1.1  riastrad 		*bpp = 16;
   5535       1.1  riastrad 		break;
   5536       1.1  riastrad 	case DRM_FORMAT_RGB565:
   5537       1.1  riastrad 	case DRM_FORMAT_BGR565:
   5538       1.1  riastrad 		*depth = 16;
   5539       1.1  riastrad 		*bpp = 16;
   5540       1.1  riastrad 		break;
   5541       1.1  riastrad 	case DRM_FORMAT_RGB888:
   5542       1.1  riastrad 	case DRM_FORMAT_BGR888:
   5543       1.1  riastrad 		*depth = 24;
   5544       1.1  riastrad 		*bpp = 24;
   5545       1.1  riastrad 		break;
   5546       1.1  riastrad 	case DRM_FORMAT_XRGB8888:
   5547       1.1  riastrad 	case DRM_FORMAT_XBGR8888:
   5548       1.1  riastrad 	case DRM_FORMAT_RGBX8888:
   5549       1.1  riastrad 	case DRM_FORMAT_BGRX8888:
   5550       1.1  riastrad 		*depth = 24;
   5551       1.1  riastrad 		*bpp = 32;
   5552       1.1  riastrad 		break;
   5553       1.1  riastrad 	case DRM_FORMAT_XRGB2101010:
   5554       1.1  riastrad 	case DRM_FORMAT_XBGR2101010:
   5555       1.1  riastrad 	case DRM_FORMAT_RGBX1010102:
   5556       1.1  riastrad 	case DRM_FORMAT_BGRX1010102:
   5557       1.1  riastrad 	case DRM_FORMAT_ARGB2101010:
   5558       1.1  riastrad 	case DRM_FORMAT_ABGR2101010:
   5559       1.1  riastrad 	case DRM_FORMAT_RGBA1010102:
   5560       1.1  riastrad 	case DRM_FORMAT_BGRA1010102:
   5561       1.1  riastrad 		*depth = 30;
   5562       1.1  riastrad 		*bpp = 32;
   5563       1.1  riastrad 		break;
   5564       1.1  riastrad 	case DRM_FORMAT_ARGB8888:
   5565       1.1  riastrad 	case DRM_FORMAT_ABGR8888:
   5566       1.1  riastrad 	case DRM_FORMAT_RGBA8888:
   5567       1.1  riastrad 	case DRM_FORMAT_BGRA8888:
   5568       1.1  riastrad 		*depth = 32;
   5569       1.1  riastrad 		*bpp = 32;
   5570       1.1  riastrad 		break;
   5571       1.1  riastrad 	default:
   5572       1.3  riastrad 		DRM_DEBUG_KMS("unsupported pixel format %s\n",
   5573       1.3  riastrad 			      drm_get_format_name(format));
   5574       1.1  riastrad 		*depth = 0;
   5575       1.1  riastrad 		*bpp = 0;
   5576       1.1  riastrad 		break;
   5577       1.1  riastrad 	}
   5578       1.1  riastrad }
   5579       1.1  riastrad EXPORT_SYMBOL(drm_fb_get_bpp_depth);
   5580       1.1  riastrad 
   5581       1.1  riastrad /**
   5582       1.1  riastrad  * drm_format_num_planes - get the number of planes for format
   5583       1.1  riastrad  * @format: pixel format (DRM_FORMAT_*)
   5584       1.1  riastrad  *
   5585       1.3  riastrad  * Returns:
   5586       1.1  riastrad  * The number of planes used by the specified pixel format.
   5587       1.1  riastrad  */
   5588       1.1  riastrad int drm_format_num_planes(uint32_t format)
   5589       1.1  riastrad {
   5590       1.1  riastrad 	switch (format) {
   5591       1.1  riastrad 	case DRM_FORMAT_YUV410:
   5592       1.1  riastrad 	case DRM_FORMAT_YVU410:
   5593       1.1  riastrad 	case DRM_FORMAT_YUV411:
   5594       1.1  riastrad 	case DRM_FORMAT_YVU411:
   5595       1.1  riastrad 	case DRM_FORMAT_YUV420:
   5596       1.1  riastrad 	case DRM_FORMAT_YVU420:
   5597       1.1  riastrad 	case DRM_FORMAT_YUV422:
   5598       1.1  riastrad 	case DRM_FORMAT_YVU422:
   5599       1.1  riastrad 	case DRM_FORMAT_YUV444:
   5600       1.1  riastrad 	case DRM_FORMAT_YVU444:
   5601       1.1  riastrad 		return 3;
   5602       1.1  riastrad 	case DRM_FORMAT_NV12:
   5603       1.1  riastrad 	case DRM_FORMAT_NV21:
   5604       1.1  riastrad 	case DRM_FORMAT_NV16:
   5605       1.1  riastrad 	case DRM_FORMAT_NV61:
   5606       1.1  riastrad 	case DRM_FORMAT_NV24:
   5607       1.1  riastrad 	case DRM_FORMAT_NV42:
   5608       1.1  riastrad 		return 2;
   5609       1.1  riastrad 	default:
   5610       1.1  riastrad 		return 1;
   5611       1.1  riastrad 	}
   5612       1.1  riastrad }
   5613       1.1  riastrad EXPORT_SYMBOL(drm_format_num_planes);
   5614       1.1  riastrad 
   5615       1.1  riastrad /**
   5616       1.1  riastrad  * drm_format_plane_cpp - determine the bytes per pixel value
   5617       1.1  riastrad  * @format: pixel format (DRM_FORMAT_*)
   5618       1.1  riastrad  * @plane: plane index
   5619       1.1  riastrad  *
   5620       1.3  riastrad  * Returns:
   5621       1.1  riastrad  * The bytes per pixel value for the specified plane.
   5622       1.1  riastrad  */
   5623       1.1  riastrad int drm_format_plane_cpp(uint32_t format, int plane)
   5624       1.1  riastrad {
   5625       1.1  riastrad 	unsigned int depth;
   5626       1.1  riastrad 	int bpp;
   5627       1.1  riastrad 
   5628       1.1  riastrad 	if (plane >= drm_format_num_planes(format))
   5629       1.1  riastrad 		return 0;
   5630       1.1  riastrad 
   5631       1.1  riastrad 	switch (format) {
   5632       1.1  riastrad 	case DRM_FORMAT_YUYV:
   5633       1.1  riastrad 	case DRM_FORMAT_YVYU:
   5634       1.1  riastrad 	case DRM_FORMAT_UYVY:
   5635       1.1  riastrad 	case DRM_FORMAT_VYUY:
   5636       1.1  riastrad 		return 2;
   5637       1.1  riastrad 	case DRM_FORMAT_NV12:
   5638       1.1  riastrad 	case DRM_FORMAT_NV21:
   5639       1.1  riastrad 	case DRM_FORMAT_NV16:
   5640       1.1  riastrad 	case DRM_FORMAT_NV61:
   5641       1.1  riastrad 	case DRM_FORMAT_NV24:
   5642       1.1  riastrad 	case DRM_FORMAT_NV42:
   5643       1.1  riastrad 		return plane ? 2 : 1;
   5644       1.1  riastrad 	case DRM_FORMAT_YUV410:
   5645       1.1  riastrad 	case DRM_FORMAT_YVU410:
   5646       1.1  riastrad 	case DRM_FORMAT_YUV411:
   5647       1.1  riastrad 	case DRM_FORMAT_YVU411:
   5648       1.1  riastrad 	case DRM_FORMAT_YUV420:
   5649       1.1  riastrad 	case DRM_FORMAT_YVU420:
   5650       1.1  riastrad 	case DRM_FORMAT_YUV422:
   5651       1.1  riastrad 	case DRM_FORMAT_YVU422:
   5652       1.1  riastrad 	case DRM_FORMAT_YUV444:
   5653       1.1  riastrad 	case DRM_FORMAT_YVU444:
   5654       1.1  riastrad 		return 1;
   5655       1.1  riastrad 	default:
   5656       1.1  riastrad 		drm_fb_get_bpp_depth(format, &depth, &bpp);
   5657       1.1  riastrad 		return bpp >> 3;
   5658       1.1  riastrad 	}
   5659       1.1  riastrad }
   5660       1.1  riastrad EXPORT_SYMBOL(drm_format_plane_cpp);
   5661       1.1  riastrad 
   5662       1.1  riastrad /**
   5663       1.1  riastrad  * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
   5664       1.1  riastrad  * @format: pixel format (DRM_FORMAT_*)
   5665       1.1  riastrad  *
   5666       1.3  riastrad  * Returns:
   5667       1.1  riastrad  * The horizontal chroma subsampling factor for the
   5668       1.1  riastrad  * specified pixel format.
   5669       1.1  riastrad  */
   5670       1.1  riastrad int drm_format_horz_chroma_subsampling(uint32_t format)
   5671       1.1  riastrad {
   5672       1.1  riastrad 	switch (format) {
   5673       1.1  riastrad 	case DRM_FORMAT_YUV411:
   5674       1.1  riastrad 	case DRM_FORMAT_YVU411:
   5675       1.1  riastrad 	case DRM_FORMAT_YUV410:
   5676       1.1  riastrad 	case DRM_FORMAT_YVU410:
   5677       1.1  riastrad 		return 4;
   5678       1.1  riastrad 	case DRM_FORMAT_YUYV:
   5679       1.1  riastrad 	case DRM_FORMAT_YVYU:
   5680       1.1  riastrad 	case DRM_FORMAT_UYVY:
   5681       1.1  riastrad 	case DRM_FORMAT_VYUY:
   5682       1.1  riastrad 	case DRM_FORMAT_NV12:
   5683       1.1  riastrad 	case DRM_FORMAT_NV21:
   5684       1.1  riastrad 	case DRM_FORMAT_NV16:
   5685       1.1  riastrad 	case DRM_FORMAT_NV61:
   5686       1.1  riastrad 	case DRM_FORMAT_YUV422:
   5687       1.1  riastrad 	case DRM_FORMAT_YVU422:
   5688       1.1  riastrad 	case DRM_FORMAT_YUV420:
   5689       1.1  riastrad 	case DRM_FORMAT_YVU420:
   5690       1.1  riastrad 		return 2;
   5691       1.1  riastrad 	default:
   5692       1.1  riastrad 		return 1;
   5693       1.1  riastrad 	}
   5694       1.1  riastrad }
   5695       1.1  riastrad EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
   5696       1.1  riastrad 
   5697       1.1  riastrad /**
   5698       1.1  riastrad  * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
   5699       1.1  riastrad  * @format: pixel format (DRM_FORMAT_*)
   5700       1.1  riastrad  *
   5701       1.3  riastrad  * Returns:
   5702       1.1  riastrad  * The vertical chroma subsampling factor for the
   5703       1.1  riastrad  * specified pixel format.
   5704       1.1  riastrad  */
   5705       1.1  riastrad int drm_format_vert_chroma_subsampling(uint32_t format)
   5706       1.1  riastrad {
   5707       1.1  riastrad 	switch (format) {
   5708       1.1  riastrad 	case DRM_FORMAT_YUV410:
   5709       1.1  riastrad 	case DRM_FORMAT_YVU410:
   5710       1.1  riastrad 		return 4;
   5711       1.1  riastrad 	case DRM_FORMAT_YUV420:
   5712       1.1  riastrad 	case DRM_FORMAT_YVU420:
   5713       1.1  riastrad 	case DRM_FORMAT_NV12:
   5714       1.1  riastrad 	case DRM_FORMAT_NV21:
   5715       1.1  riastrad 		return 2;
   5716       1.1  riastrad 	default:
   5717       1.1  riastrad 		return 1;
   5718       1.1  riastrad 	}
   5719       1.1  riastrad }
   5720       1.1  riastrad EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
   5721       1.3  riastrad 
   5722       1.3  riastrad /**
   5723       1.5  riastrad  * drm_rotation_simplify() - Try to simplify the rotation
   5724       1.5  riastrad  * @rotation: Rotation to be simplified
   5725       1.5  riastrad  * @supported_rotations: Supported rotations
   5726       1.5  riastrad  *
   5727       1.5  riastrad  * Attempt to simplify the rotation to a form that is supported.
   5728       1.5  riastrad  * Eg. if the hardware supports everything except DRM_REFLECT_X
   5729       1.5  riastrad  * one could call this function like this:
   5730       1.5  riastrad  *
   5731       1.5  riastrad  * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
   5732       1.5  riastrad  *                       BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
   5733       1.5  riastrad  *                       BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
   5734       1.5  riastrad  *
   5735       1.5  riastrad  * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
   5736       1.5  riastrad  * transforms the hardware supports, this function may not
   5737       1.5  riastrad  * be able to produce a supported transform, so the caller should
   5738       1.5  riastrad  * check the result afterwards.
   5739       1.5  riastrad  */
   5740       1.5  riastrad unsigned int drm_rotation_simplify(unsigned int rotation,
   5741       1.5  riastrad 				   unsigned int supported_rotations)
   5742       1.5  riastrad {
   5743       1.5  riastrad 	if (rotation & ~supported_rotations) {
   5744       1.5  riastrad 		rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
   5745       1.5  riastrad 		rotation = (rotation & DRM_REFLECT_MASK) |
   5746       1.5  riastrad 		           BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
   5747       1.5  riastrad 	}
   5748       1.5  riastrad 
   5749       1.5  riastrad 	return rotation;
   5750       1.5  riastrad }
   5751       1.5  riastrad EXPORT_SYMBOL(drm_rotation_simplify);
   5752       1.5  riastrad 
   5753       1.5  riastrad /**
   5754       1.3  riastrad  * drm_mode_config_init - initialize DRM mode_configuration structure
   5755       1.3  riastrad  * @dev: DRM device
   5756       1.3  riastrad  *
   5757       1.3  riastrad  * Initialize @dev's mode_config structure, used for tracking the graphics
   5758       1.3  riastrad  * configuration of @dev.
   5759       1.3  riastrad  *
   5760       1.3  riastrad  * Since this initializes the modeset locks, no locking is possible. Which is no
   5761       1.3  riastrad  * problem, since this should happen single threaded at init time. It is the
   5762       1.3  riastrad  * driver's problem to ensure this guarantee.
   5763       1.3  riastrad  *
   5764       1.3  riastrad  */
   5765       1.3  riastrad void drm_mode_config_init(struct drm_device *dev)
   5766       1.3  riastrad {
   5767       1.3  riastrad 	mutex_init(&dev->mode_config.mutex);
   5768       1.5  riastrad 	drm_modeset_lock_init(&dev->mode_config.connection_mutex);
   5769       1.3  riastrad 	mutex_init(&dev->mode_config.idr_mutex);
   5770       1.3  riastrad 	mutex_init(&dev->mode_config.fb_lock);
   5771       1.5  riastrad 	mutex_init(&dev->mode_config.blob_lock);
   5772       1.3  riastrad 	INIT_LIST_HEAD(&dev->mode_config.fb_list);
   5773       1.3  riastrad 	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
   5774       1.3  riastrad 	INIT_LIST_HEAD(&dev->mode_config.connector_list);
   5775       1.3  riastrad 	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
   5776       1.3  riastrad 	INIT_LIST_HEAD(&dev->mode_config.property_list);
   5777       1.3  riastrad 	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
   5778       1.3  riastrad 	INIT_LIST_HEAD(&dev->mode_config.plane_list);
   5779       1.3  riastrad 	idr_init(&dev->mode_config.crtc_idr);
   5780       1.5  riastrad 	idr_init(&dev->mode_config.tile_idr);
   5781       1.3  riastrad 
   5782       1.3  riastrad 	drm_modeset_lock_all(dev);
   5783       1.5  riastrad 	drm_mode_create_standard_properties(dev);
   5784       1.3  riastrad 	drm_modeset_unlock_all(dev);
   5785       1.3  riastrad 
   5786       1.3  riastrad 	/* Just to be sure */
   5787       1.3  riastrad 	dev->mode_config.num_fb = 0;
   5788       1.3  riastrad 	dev->mode_config.num_connector = 0;
   5789       1.3  riastrad 	dev->mode_config.num_crtc = 0;
   5790       1.3  riastrad 	dev->mode_config.num_encoder = 0;
   5791       1.3  riastrad 	dev->mode_config.num_overlay_plane = 0;
   5792       1.3  riastrad 	dev->mode_config.num_total_plane = 0;
   5793       1.3  riastrad }
   5794       1.3  riastrad EXPORT_SYMBOL(drm_mode_config_init);
   5795       1.3  riastrad 
   5796       1.3  riastrad /**
   5797       1.3  riastrad  * drm_mode_config_cleanup - free up DRM mode_config info
   5798       1.3  riastrad  * @dev: DRM device
   5799       1.3  riastrad  *
   5800       1.3  riastrad  * Free up all the connectors and CRTCs associated with this DRM device, then
   5801       1.3  riastrad  * free up the framebuffers and associated buffer objects.
   5802       1.3  riastrad  *
   5803       1.3  riastrad  * Note that since this /should/ happen single-threaded at driver/device
   5804       1.3  riastrad  * teardown time, no locking is required. It's the driver's job to ensure that
   5805       1.3  riastrad  * this guarantee actually holds true.
   5806       1.3  riastrad  *
   5807       1.3  riastrad  * FIXME: cleanup any dangling user buffer objects too
   5808       1.3  riastrad  */
   5809       1.3  riastrad void drm_mode_config_cleanup(struct drm_device *dev)
   5810       1.3  riastrad {
   5811       1.3  riastrad 	struct drm_connector *connector, *ot;
   5812       1.3  riastrad 	struct drm_crtc *crtc, *ct;
   5813       1.3  riastrad 	struct drm_encoder *encoder, *enct;
   5814       1.3  riastrad 	struct drm_framebuffer *fb, *fbt;
   5815       1.3  riastrad 	struct drm_property *property, *pt;
   5816       1.3  riastrad 	struct drm_property_blob *blob, *bt;
   5817       1.3  riastrad 	struct drm_plane *plane, *plt;
   5818       1.3  riastrad 
   5819       1.3  riastrad 	list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
   5820       1.3  riastrad 				 head) {
   5821       1.3  riastrad 		encoder->funcs->destroy(encoder);
   5822       1.3  riastrad 	}
   5823       1.3  riastrad 
   5824       1.3  riastrad 	list_for_each_entry_safe(connector, ot,
   5825       1.3  riastrad 				 &dev->mode_config.connector_list, head) {
   5826       1.3  riastrad 		connector->funcs->destroy(connector);
   5827       1.3  riastrad 	}
   5828       1.3  riastrad 
   5829       1.3  riastrad 	list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
   5830       1.3  riastrad 				 head) {
   5831       1.3  riastrad 		drm_property_destroy(dev, property);
   5832       1.3  riastrad 	}
   5833       1.3  riastrad 
   5834       1.3  riastrad 	list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
   5835       1.5  riastrad 				 head_global) {
   5836       1.5  riastrad 		drm_property_unreference_blob(blob);
   5837       1.3  riastrad 	}
   5838       1.3  riastrad 
   5839       1.3  riastrad 	/*
   5840       1.3  riastrad 	 * Single-threaded teardown context, so it's not required to grab the
   5841       1.3  riastrad 	 * fb_lock to protect against concurrent fb_list access. Contrary, it
   5842       1.3  riastrad 	 * would actually deadlock with the drm_framebuffer_cleanup function.
   5843       1.3  riastrad 	 *
   5844       1.3  riastrad 	 * Also, if there are any framebuffers left, that's a driver leak now,
   5845       1.3  riastrad 	 * so politely WARN about this.
   5846       1.3  riastrad 	 */
   5847       1.3  riastrad 	WARN_ON(!list_empty(&dev->mode_config.fb_list));
   5848       1.3  riastrad 	list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
   5849       1.5  riastrad 		drm_framebuffer_free(&fb->refcount);
   5850       1.3  riastrad 	}
   5851       1.3  riastrad 
   5852       1.3  riastrad 	list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
   5853       1.3  riastrad 				 head) {
   5854       1.3  riastrad 		plane->funcs->destroy(plane);
   5855       1.3  riastrad 	}
   5856       1.3  riastrad 
   5857       1.3  riastrad 	list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
   5858       1.3  riastrad 		crtc->funcs->destroy(crtc);
   5859       1.3  riastrad 	}
   5860       1.3  riastrad 
   5861       1.5  riastrad 	idr_destroy(&dev->mode_config.tile_idr);
   5862       1.3  riastrad 	idr_destroy(&dev->mode_config.crtc_idr);
   5863  1.11.6.1        ad 	mutex_destroy(&dev->mode_config.blob_lock);
   5864  1.11.6.1        ad 	mutex_destroy(&dev->mode_config.fb_lock);
   5865  1.11.6.1        ad 	mutex_destroy(&dev->mode_config.idr_mutex);
   5866  1.11.6.1        ad 	mutex_destroy(&dev->mode_config.mutex);
   5867       1.5  riastrad 	drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
   5868       1.3  riastrad }
   5869       1.3  riastrad EXPORT_SYMBOL(drm_mode_config_cleanup);
   5870       1.5  riastrad 
   5871       1.5  riastrad struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
   5872       1.5  riastrad 						       unsigned int supported_rotations)
   5873       1.5  riastrad {
   5874       1.5  riastrad 	static const struct drm_prop_enum_list props[] = {
   5875       1.5  riastrad 		{ DRM_ROTATE_0,   "rotate-0" },
   5876       1.5  riastrad 		{ DRM_ROTATE_90,  "rotate-90" },
   5877       1.5  riastrad 		{ DRM_ROTATE_180, "rotate-180" },
   5878       1.5  riastrad 		{ DRM_ROTATE_270, "rotate-270" },
   5879       1.5  riastrad 		{ DRM_REFLECT_X,  "reflect-x" },
   5880       1.5  riastrad 		{ DRM_REFLECT_Y,  "reflect-y" },
   5881       1.5  riastrad 	};
   5882       1.5  riastrad 
   5883       1.5  riastrad 	return drm_property_create_bitmask(dev, 0, "rotation",
   5884       1.5  riastrad 					   props, ARRAY_SIZE(props),
   5885       1.5  riastrad 					   supported_rotations);
   5886       1.5  riastrad }
   5887       1.5  riastrad EXPORT_SYMBOL(drm_mode_create_rotation_property);
   5888       1.5  riastrad 
   5889       1.5  riastrad /**
   5890       1.5  riastrad  * DOC: Tile group
   5891       1.5  riastrad  *
   5892       1.5  riastrad  * Tile groups are used to represent tiled monitors with a unique
   5893       1.5  riastrad  * integer identifier. Tiled monitors using DisplayID v1.3 have
   5894       1.5  riastrad  * a unique 8-byte handle, we store this in a tile group, so we
   5895       1.5  riastrad  * have a common identifier for all tiles in a monitor group.
   5896       1.5  riastrad  */
   5897       1.5  riastrad static void drm_tile_group_free(struct kref *kref)
   5898       1.5  riastrad {
   5899       1.5  riastrad 	struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
   5900       1.5  riastrad 	struct drm_device *dev = tg->dev;
   5901       1.5  riastrad 	mutex_lock(&dev->mode_config.idr_mutex);
   5902       1.5  riastrad 	idr_remove(&dev->mode_config.tile_idr, tg->id);
   5903       1.5  riastrad 	mutex_unlock(&dev->mode_config.idr_mutex);
   5904       1.5  riastrad 	kfree(tg);
   5905       1.5  riastrad }
   5906       1.5  riastrad 
   5907       1.5  riastrad /**
   5908       1.5  riastrad  * drm_mode_put_tile_group - drop a reference to a tile group.
   5909       1.5  riastrad  * @dev: DRM device
   5910       1.5  riastrad  * @tg: tile group to drop reference to.
   5911       1.5  riastrad  *
   5912       1.5  riastrad  * drop reference to tile group and free if 0.
   5913       1.5  riastrad  */
   5914       1.5  riastrad void drm_mode_put_tile_group(struct drm_device *dev,
   5915       1.5  riastrad 			     struct drm_tile_group *tg)
   5916       1.5  riastrad {
   5917       1.5  riastrad 	kref_put(&tg->refcount, drm_tile_group_free);
   5918       1.5  riastrad }
   5919       1.5  riastrad 
   5920       1.5  riastrad /**
   5921       1.5  riastrad  * drm_mode_get_tile_group - get a reference to an existing tile group
   5922       1.5  riastrad  * @dev: DRM device
   5923       1.5  riastrad  * @topology: 8-bytes unique per monitor.
   5924       1.5  riastrad  *
   5925       1.5  riastrad  * Use the unique bytes to get a reference to an existing tile group.
   5926       1.5  riastrad  *
   5927       1.5  riastrad  * RETURNS:
   5928       1.5  riastrad  * tile group or NULL if not found.
   5929       1.5  riastrad  */
   5930       1.5  riastrad struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
   5931       1.5  riastrad 					       char topology[8])
   5932       1.5  riastrad {
   5933       1.5  riastrad 	struct drm_tile_group *tg;
   5934       1.5  riastrad 	int id;
   5935       1.5  riastrad 	mutex_lock(&dev->mode_config.idr_mutex);
   5936       1.5  riastrad 	idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
   5937       1.5  riastrad 		if (!memcmp(tg->group_data, topology, 8)) {
   5938       1.5  riastrad 			if (!kref_get_unless_zero(&tg->refcount))
   5939       1.5  riastrad 				tg = NULL;
   5940       1.5  riastrad 			mutex_unlock(&dev->mode_config.idr_mutex);
   5941       1.5  riastrad 			return tg;
   5942       1.5  riastrad 		}
   5943       1.5  riastrad 	}
   5944       1.5  riastrad 	mutex_unlock(&dev->mode_config.idr_mutex);
   5945       1.5  riastrad 	return NULL;
   5946       1.5  riastrad }
   5947       1.5  riastrad EXPORT_SYMBOL(drm_mode_get_tile_group);
   5948       1.5  riastrad 
   5949       1.5  riastrad /**
   5950       1.5  riastrad  * drm_mode_create_tile_group - create a tile group from a displayid description
   5951       1.5  riastrad  * @dev: DRM device
   5952       1.5  riastrad  * @topology: 8-bytes unique per monitor.
   5953       1.5  riastrad  *
   5954       1.5  riastrad  * Create a tile group for the unique monitor, and get a unique
   5955       1.5  riastrad  * identifier for the tile group.
   5956       1.5  riastrad  *
   5957       1.5  riastrad  * RETURNS:
   5958       1.5  riastrad  * new tile group or error.
   5959       1.5  riastrad  */
   5960       1.5  riastrad struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
   5961       1.5  riastrad 						  char topology[8])
   5962       1.5  riastrad {
   5963       1.5  riastrad 	struct drm_tile_group *tg;
   5964       1.5  riastrad 	int ret;
   5965       1.5  riastrad 
   5966       1.5  riastrad 	tg = kzalloc(sizeof(*tg), GFP_KERNEL);
   5967       1.5  riastrad 	if (!tg)
   5968       1.5  riastrad 		return ERR_PTR(-ENOMEM);
   5969       1.5  riastrad 
   5970       1.5  riastrad 	kref_init(&tg->refcount);
   5971       1.5  riastrad 	memcpy(tg->group_data, topology, 8);
   5972       1.5  riastrad 	tg->dev = dev;
   5973       1.5  riastrad 
   5974       1.9  riastrad 	idr_preload(GFP_KERNEL);
   5975       1.5  riastrad 	mutex_lock(&dev->mode_config.idr_mutex);
   5976       1.5  riastrad 	ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
   5977       1.5  riastrad 	if (ret >= 0) {
   5978       1.5  riastrad 		tg->id = ret;
   5979       1.5  riastrad 	} else {
   5980       1.5  riastrad 		kfree(tg);
   5981       1.5  riastrad 		tg = ERR_PTR(ret);
   5982       1.5  riastrad 	}
   5983       1.5  riastrad 
   5984       1.5  riastrad 	mutex_unlock(&dev->mode_config.idr_mutex);
   5985       1.9  riastrad 	idr_preload_end();
   5986       1.5  riastrad 	return tg;
   5987       1.5  riastrad }
   5988       1.5  riastrad EXPORT_SYMBOL(drm_mode_create_tile_group);
   5989