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