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