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