xref: /dflybsd-src/sys/dev/drm/drm_crtc.c (revision b5162e19595c53a775b64d13116edf69b4734416)
15718399fSFrançois Tigeot /*
25718399fSFrançois Tigeot  * Copyright (c) 2006-2008 Intel Corporation
35718399fSFrançois Tigeot  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
45718399fSFrançois Tigeot  * Copyright (c) 2008 Red Hat Inc.
55718399fSFrançois Tigeot  *
65718399fSFrançois Tigeot  * DRM core CRTC related functions
75718399fSFrançois Tigeot  *
85718399fSFrançois Tigeot  * Permission to use, copy, modify, distribute, and sell this software and its
95718399fSFrançois Tigeot  * documentation for any purpose is hereby granted without fee, provided that
105718399fSFrançois Tigeot  * the above copyright notice appear in all copies and that both that copyright
115718399fSFrançois Tigeot  * notice and this permission notice appear in supporting documentation, and
125718399fSFrançois Tigeot  * that the name of the copyright holders not be used in advertising or
135718399fSFrançois Tigeot  * publicity pertaining to distribution of the software without specific,
145718399fSFrançois Tigeot  * written prior permission.  The copyright holders make no representations
155718399fSFrançois Tigeot  * about the suitability of this software for any purpose.  It is provided "as
165718399fSFrançois Tigeot  * is" without express or implied warranty.
175718399fSFrançois Tigeot  *
185718399fSFrançois Tigeot  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
195718399fSFrançois Tigeot  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
205718399fSFrançois Tigeot  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
215718399fSFrançois Tigeot  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
225718399fSFrançois Tigeot  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
235718399fSFrançois Tigeot  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
245718399fSFrançois Tigeot  * OF THIS SOFTWARE.
255718399fSFrançois Tigeot  *
265718399fSFrançois Tigeot  * Authors:
275718399fSFrançois Tigeot  *      Keith Packard
285718399fSFrançois Tigeot  *	Eric Anholt <eric@anholt.net>
295718399fSFrançois Tigeot  *      Dave Airlie <airlied@linux.ie>
305718399fSFrançois Tigeot  *      Jesse Barnes <jesse.barnes@intel.com>
315718399fSFrançois Tigeot  */
325718399fSFrançois Tigeot 
33*b5162e19SFrançois Tigeot #include <linux/err.h>
34*b5162e19SFrançois Tigeot #include <linux/list.h>
35*b5162e19SFrançois Tigeot #include <linux/export.h>
36*b5162e19SFrançois Tigeot #include <linux/kernel.h>
3718e26a6dSFrançois Tigeot #include <drm/drmP.h>
3818e26a6dSFrançois Tigeot #include <drm/drm_crtc.h>
3918e26a6dSFrançois Tigeot #include <drm/drm_edid.h>
40*b5162e19SFrançois Tigeot #include <uapi_drm/drm_fourcc.h>
415718399fSFrançois Tigeot 
425718399fSFrançois Tigeot /* Avoid boilerplate.  I'm tired of typing. */
435718399fSFrançois Tigeot #define DRM_ENUM_NAME_FN(fnname, list)				\
445718399fSFrançois Tigeot 	char *fnname(int val)					\
455718399fSFrançois Tigeot 	{							\
465718399fSFrançois Tigeot 		int i;						\
47*b5162e19SFrançois Tigeot 		for (i = 0; i < ARRAY_SIZE(list); i++) {	\
485718399fSFrançois Tigeot 			if (list[i].type == val)		\
495718399fSFrançois Tigeot 				return list[i].name;		\
505718399fSFrançois Tigeot 		}						\
515718399fSFrançois Tigeot 		return "(unknown)";				\
525718399fSFrançois Tigeot 	}
535718399fSFrançois Tigeot 
545718399fSFrançois Tigeot /*
555718399fSFrançois Tigeot  * Global properties
565718399fSFrançois Tigeot  */
575718399fSFrançois Tigeot static struct drm_prop_enum_list drm_dpms_enum_list[] =
585718399fSFrançois Tigeot {	{ DRM_MODE_DPMS_ON, "On" },
595718399fSFrançois Tigeot 	{ DRM_MODE_DPMS_STANDBY, "Standby" },
605718399fSFrançois Tigeot 	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
615718399fSFrançois Tigeot 	{ DRM_MODE_DPMS_OFF, "Off" }
625718399fSFrançois Tigeot };
635718399fSFrançois Tigeot 
645718399fSFrançois Tigeot DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
655718399fSFrançois Tigeot 
665718399fSFrançois Tigeot /*
675718399fSFrançois Tigeot  * Optional properties
685718399fSFrançois Tigeot  */
695718399fSFrançois Tigeot static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
705718399fSFrançois Tigeot {
715718399fSFrançois Tigeot 	{ DRM_MODE_SCALE_NONE, "None" },
725718399fSFrançois Tigeot 	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
735718399fSFrançois Tigeot 	{ DRM_MODE_SCALE_CENTER, "Center" },
745718399fSFrançois Tigeot 	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
755718399fSFrançois Tigeot };
765718399fSFrançois Tigeot 
775718399fSFrançois Tigeot static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
785718399fSFrançois Tigeot {
795718399fSFrançois Tigeot 	{ DRM_MODE_DITHERING_OFF, "Off" },
805718399fSFrançois Tigeot 	{ DRM_MODE_DITHERING_ON, "On" },
815718399fSFrançois Tigeot 	{ DRM_MODE_DITHERING_AUTO, "Automatic" },
825718399fSFrançois Tigeot };
835718399fSFrançois Tigeot 
845718399fSFrançois Tigeot /*
855718399fSFrançois Tigeot  * Non-global properties, but "required" for certain connectors.
865718399fSFrançois Tigeot  */
875718399fSFrançois Tigeot static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
885718399fSFrançois Tigeot {
895718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
905718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
915718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
925718399fSFrançois Tigeot };
935718399fSFrançois Tigeot 
945718399fSFrançois Tigeot DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
955718399fSFrançois Tigeot 
965718399fSFrançois Tigeot static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
975718399fSFrançois Tigeot {
985718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
995718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
1005718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
1015718399fSFrançois Tigeot };
1025718399fSFrançois Tigeot 
1035718399fSFrançois Tigeot DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
1045718399fSFrançois Tigeot 		 drm_dvi_i_subconnector_enum_list)
1055718399fSFrançois Tigeot 
1065718399fSFrançois Tigeot static struct drm_prop_enum_list drm_tv_select_enum_list[] =
1075718399fSFrançois Tigeot {
1085718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
1095718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
1105718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
1115718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
1125718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
1135718399fSFrançois Tigeot };
1145718399fSFrançois Tigeot 
1155718399fSFrançois Tigeot DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
1165718399fSFrançois Tigeot 
1175718399fSFrançois Tigeot static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
1185718399fSFrançois Tigeot {
1195718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
1205718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
1215718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
1225718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
1235718399fSFrançois Tigeot 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
1245718399fSFrançois Tigeot };
1255718399fSFrançois Tigeot 
1265718399fSFrançois Tigeot DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
1275718399fSFrançois Tigeot 		 drm_tv_subconnector_enum_list)
1285718399fSFrançois Tigeot 
1295718399fSFrançois Tigeot static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
1305718399fSFrançois Tigeot 	{ DRM_MODE_DIRTY_OFF,      "Off"      },
1315718399fSFrançois Tigeot 	{ DRM_MODE_DIRTY_ON,       "On"       },
1325718399fSFrançois Tigeot 	{ DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
1335718399fSFrançois Tigeot };
1345718399fSFrançois Tigeot 
1355718399fSFrançois Tigeot struct drm_conn_prop_enum_list {
1365718399fSFrançois Tigeot 	int type;
1375718399fSFrançois Tigeot 	char *name;
1385718399fSFrançois Tigeot 	int count;
1395718399fSFrançois Tigeot };
1405718399fSFrançois Tigeot 
1415718399fSFrançois Tigeot /*
1425718399fSFrançois Tigeot  * Connector and encoder types.
1435718399fSFrançois Tigeot  */
1445718399fSFrançois Tigeot static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
1455718399fSFrançois Tigeot {	{ DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
1465718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
1475718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
1485718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
1495718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
1505718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
1515718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
1525718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
1535718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_Component, "Component", 0 },
1545718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
1555718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
1565718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
1575718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
1585718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_TV, "TV", 0 },
1595718399fSFrançois Tigeot 	{ DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
160*b5162e19SFrançois Tigeot 	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
1615718399fSFrançois Tigeot };
1625718399fSFrançois Tigeot 
1635718399fSFrançois Tigeot static struct drm_prop_enum_list drm_encoder_enum_list[] =
1645718399fSFrançois Tigeot {	{ DRM_MODE_ENCODER_NONE, "None" },
1655718399fSFrançois Tigeot 	{ DRM_MODE_ENCODER_DAC, "DAC" },
1665718399fSFrançois Tigeot 	{ DRM_MODE_ENCODER_TMDS, "TMDS" },
1675718399fSFrançois Tigeot 	{ DRM_MODE_ENCODER_LVDS, "LVDS" },
1685718399fSFrançois Tigeot 	{ DRM_MODE_ENCODER_TVDAC, "TV" },
169*b5162e19SFrançois Tigeot 	{ DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
1705718399fSFrançois Tigeot };
1715718399fSFrançois Tigeot 
1725718399fSFrançois Tigeot char *drm_get_encoder_name(struct drm_encoder *encoder)
1735718399fSFrançois Tigeot {
1745718399fSFrançois Tigeot 	static char buf[32];
1755718399fSFrançois Tigeot 
1765718399fSFrançois Tigeot 	ksnprintf(buf, 32, "%s-%d",
1775718399fSFrançois Tigeot 		 drm_encoder_enum_list[encoder->encoder_type].name,
1785718399fSFrançois Tigeot 		 encoder->base.id);
1795718399fSFrançois Tigeot 	return buf;
1805718399fSFrançois Tigeot }
181*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_get_encoder_name);
1825718399fSFrançois Tigeot 
1835718399fSFrançois Tigeot char *drm_get_connector_name(struct drm_connector *connector)
1845718399fSFrançois Tigeot {
1855718399fSFrançois Tigeot 	static char buf[32];
1865718399fSFrançois Tigeot 
1875718399fSFrançois Tigeot 	ksnprintf(buf, 32, "%s-%d",
1885718399fSFrançois Tigeot 		 drm_connector_enum_list[connector->connector_type].name,
1895718399fSFrançois Tigeot 		 connector->connector_type_id);
1905718399fSFrançois Tigeot 	return buf;
1915718399fSFrançois Tigeot }
192*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_get_connector_name);
1935718399fSFrançois Tigeot 
1945718399fSFrançois Tigeot char *drm_get_connector_status_name(enum drm_connector_status status)
1955718399fSFrançois Tigeot {
1965718399fSFrançois Tigeot 	if (status == connector_status_connected)
1975718399fSFrançois Tigeot 		return "connected";
1985718399fSFrançois Tigeot 	else if (status == connector_status_disconnected)
1995718399fSFrançois Tigeot 		return "disconnected";
2005718399fSFrançois Tigeot 	else
2015718399fSFrançois Tigeot 		return "unknown";
2025718399fSFrançois Tigeot }
2035718399fSFrançois Tigeot 
2045718399fSFrançois Tigeot /**
2055718399fSFrançois Tigeot  * drm_mode_object_get - allocate a new identifier
2065718399fSFrançois Tigeot  * @dev: DRM device
2075718399fSFrançois Tigeot  * @ptr: object pointer, used to generate unique ID
2085718399fSFrançois Tigeot  * @type: object type
2095718399fSFrançois Tigeot  *
2105718399fSFrançois Tigeot  * LOCKING:
2115718399fSFrançois Tigeot  *
2125718399fSFrançois Tigeot  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
2135718399fSFrançois Tigeot  * for tracking modes, CRTCs and connectors.
2145718399fSFrançois Tigeot  *
2155718399fSFrançois Tigeot  * RETURNS:
2165718399fSFrançois Tigeot  * New unique (relative to other objects in @dev) integer identifier for the
2175718399fSFrançois Tigeot  * object.
2185718399fSFrançois Tigeot  */
2195718399fSFrançois Tigeot static int drm_mode_object_get(struct drm_device *dev,
2205718399fSFrançois Tigeot 			       struct drm_mode_object *obj, uint32_t obj_type)
2215718399fSFrançois Tigeot {
222aea8bdbdSFrançois Tigeot 	int new_id = 0;
2235718399fSFrançois Tigeot 	int ret;
2245718399fSFrançois Tigeot 
225aea8bdbdSFrançois Tigeot again:
226c824b4f9SFrançois Tigeot 	if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
227aea8bdbdSFrançois Tigeot 		DRM_ERROR("Ran out memory getting a mode number\n");
228aea8bdbdSFrançois Tigeot 		return -ENOMEM;
229aea8bdbdSFrançois Tigeot 	}
230aea8bdbdSFrançois Tigeot 
23198a11977SFrançois Tigeot 	lockmgr(&dev->mode_config.idr_mutex, LK_EXCLUSIVE);
232aea8bdbdSFrançois Tigeot 	ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
23398a11977SFrançois Tigeot 	lockmgr(&dev->mode_config.idr_mutex, LK_RELEASE);
234aea8bdbdSFrançois Tigeot 	if (ret == -EAGAIN)
235aea8bdbdSFrançois Tigeot 		goto again;
236aea8bdbdSFrançois Tigeot 	else if (ret)
237aea8bdbdSFrançois Tigeot 		return ret;
2385718399fSFrançois Tigeot 
2395718399fSFrançois Tigeot 	obj->id = new_id;
2405718399fSFrançois Tigeot 	obj->type = obj_type;
2415718399fSFrançois Tigeot 	return 0;
2425718399fSFrançois Tigeot }
2435718399fSFrançois Tigeot 
2445718399fSFrançois Tigeot /**
2455718399fSFrançois Tigeot  * drm_mode_object_put - free an identifer
2465718399fSFrançois Tigeot  * @dev: DRM device
2475718399fSFrançois Tigeot  * @id: ID to free
2485718399fSFrançois Tigeot  *
2495718399fSFrançois Tigeot  * LOCKING:
2505718399fSFrançois Tigeot  * Caller must hold DRM mode_config lock.
2515718399fSFrançois Tigeot  *
2525718399fSFrançois Tigeot  * Free @id from @dev's unique identifier pool.
2535718399fSFrançois Tigeot  */
2545718399fSFrançois Tigeot static void drm_mode_object_put(struct drm_device *dev,
2555718399fSFrançois Tigeot 				struct drm_mode_object *object)
2565718399fSFrançois Tigeot {
25798a11977SFrançois Tigeot 	lockmgr(&dev->mode_config.idr_mutex, LK_EXCLUSIVE);
258aea8bdbdSFrançois Tigeot 	idr_remove(&dev->mode_config.crtc_idr, object->id);
25998a11977SFrançois Tigeot 	lockmgr(&dev->mode_config.idr_mutex, LK_RELEASE);
2605718399fSFrançois Tigeot }
2615718399fSFrançois Tigeot 
2625718399fSFrançois Tigeot struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
2635718399fSFrançois Tigeot 		uint32_t id, uint32_t type)
2645718399fSFrançois Tigeot {
265aea8bdbdSFrançois Tigeot 	struct drm_mode_object *obj = NULL;
2665718399fSFrançois Tigeot 
26798a11977SFrançois Tigeot 	lockmgr(&dev->mode_config.idr_mutex, LK_EXCLUSIVE);
268aea8bdbdSFrançois Tigeot 	obj = idr_find(&dev->mode_config.crtc_idr, id);
2695718399fSFrançois Tigeot 	if (!obj || (obj->type != type) || (obj->id != id))
2705718399fSFrançois Tigeot 		obj = NULL;
27198a11977SFrançois Tigeot 	lockmgr(&dev->mode_config.idr_mutex, LK_RELEASE);
2725718399fSFrançois Tigeot 
2735718399fSFrançois Tigeot 	return obj;
2745718399fSFrançois Tigeot }
275*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_object_find);
2765718399fSFrançois Tigeot 
2775718399fSFrançois Tigeot /**
2785718399fSFrançois Tigeot  * drm_framebuffer_init - initialize a framebuffer
2795718399fSFrançois Tigeot  * @dev: DRM device
2805718399fSFrançois Tigeot  *
2815718399fSFrançois Tigeot  * LOCKING:
2825718399fSFrançois Tigeot  * Caller must hold mode config lock.
2835718399fSFrançois Tigeot  *
2845718399fSFrançois Tigeot  * Allocates an ID for the framebuffer's parent mode object, sets its mode
2855718399fSFrançois Tigeot  * functions & device file and adds it to the master fd list.
2865718399fSFrançois Tigeot  *
2875718399fSFrançois Tigeot  * RETURNS:
2885718399fSFrançois Tigeot  * Zero on success, error code on failure.
2895718399fSFrançois Tigeot  */
2905718399fSFrançois Tigeot int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
2915718399fSFrançois Tigeot 			 const struct drm_framebuffer_funcs *funcs)
2925718399fSFrançois Tigeot {
2935718399fSFrançois Tigeot 	int ret;
2945718399fSFrançois Tigeot 
295*b5162e19SFrançois Tigeot 	kref_init(&fb->refcount);
296*b5162e19SFrançois Tigeot 
2975718399fSFrançois Tigeot 	ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
2985718399fSFrançois Tigeot 	if (ret)
2995718399fSFrançois Tigeot 		return ret;
3005718399fSFrançois Tigeot 
3015718399fSFrançois Tigeot 	fb->dev = dev;
3025718399fSFrançois Tigeot 	fb->funcs = funcs;
3035718399fSFrançois Tigeot 	dev->mode_config.num_fb++;
3045718399fSFrançois Tigeot 	list_add(&fb->head, &dev->mode_config.fb_list);
3055718399fSFrançois Tigeot 
3065718399fSFrançois Tigeot 	return 0;
3075718399fSFrançois Tigeot }
308*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_framebuffer_init);
309*b5162e19SFrançois Tigeot 
310*b5162e19SFrançois Tigeot static void drm_framebuffer_free(struct kref *kref)
311*b5162e19SFrançois Tigeot {
312*b5162e19SFrançois Tigeot 	struct drm_framebuffer *fb =
313*b5162e19SFrançois Tigeot 			container_of(kref, struct drm_framebuffer, refcount);
314*b5162e19SFrançois Tigeot 	fb->funcs->destroy(fb);
315*b5162e19SFrançois Tigeot }
316*b5162e19SFrançois Tigeot 
317*b5162e19SFrançois Tigeot /**
318*b5162e19SFrançois Tigeot  * drm_framebuffer_unreference - unref a framebuffer
319*b5162e19SFrançois Tigeot  *
320*b5162e19SFrançois Tigeot  * LOCKING:
321*b5162e19SFrançois Tigeot  * Caller must hold mode config lock.
322*b5162e19SFrançois Tigeot  */
323*b5162e19SFrançois Tigeot void drm_framebuffer_unreference(struct drm_framebuffer *fb)
324*b5162e19SFrançois Tigeot {
325*b5162e19SFrançois Tigeot 	struct drm_device *dev = fb->dev;
326*b5162e19SFrançois Tigeot 	DRM_DEBUG("FB ID: %d\n", fb->base.id);
327*b5162e19SFrançois Tigeot 	WARN_ON(!lockstatus(&dev->mode_config.mutex, curthread));
328*b5162e19SFrançois Tigeot 	kref_put(&fb->refcount, drm_framebuffer_free);
329*b5162e19SFrançois Tigeot }
330*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_framebuffer_unreference);
331*b5162e19SFrançois Tigeot 
332*b5162e19SFrançois Tigeot /**
333*b5162e19SFrançois Tigeot  * drm_framebuffer_reference - incr the fb refcnt
334*b5162e19SFrançois Tigeot  */
335*b5162e19SFrançois Tigeot void drm_framebuffer_reference(struct drm_framebuffer *fb)
336*b5162e19SFrançois Tigeot {
337*b5162e19SFrançois Tigeot 	DRM_DEBUG("FB ID: %d\n", fb->base.id);
338*b5162e19SFrançois Tigeot 	kref_get(&fb->refcount);
339*b5162e19SFrançois Tigeot }
340*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_framebuffer_reference);
3415718399fSFrançois Tigeot 
3425718399fSFrançois Tigeot /**
3435718399fSFrançois Tigeot  * drm_framebuffer_cleanup - remove a framebuffer object
3445718399fSFrançois Tigeot  * @fb: framebuffer to remove
3455718399fSFrançois Tigeot  *
3465718399fSFrançois Tigeot  * LOCKING:
3475718399fSFrançois Tigeot  * Caller must hold mode config lock.
3485718399fSFrançois Tigeot  *
3495718399fSFrançois Tigeot  * Scans all the CRTCs in @dev's mode_config.  If they're using @fb, removes
3505718399fSFrançois Tigeot  * it, setting it to NULL.
3515718399fSFrançois Tigeot  */
3525718399fSFrançois Tigeot void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
3535718399fSFrançois Tigeot {
3545718399fSFrançois Tigeot 	struct drm_device *dev = fb->dev;
355*b5162e19SFrançois Tigeot 	/*
356*b5162e19SFrançois Tigeot 	 * This could be moved to drm_framebuffer_remove(), but for
357*b5162e19SFrançois Tigeot 	 * debugging is nice to keep around the list of fb's that are
358*b5162e19SFrançois Tigeot 	 * no longer associated w/ a drm_file but are not unreferenced
359*b5162e19SFrançois Tigeot 	 * yet.  (i915 and omapdrm have debugfs files which will show
360*b5162e19SFrançois Tigeot 	 * this.)
361*b5162e19SFrançois Tigeot 	 */
362*b5162e19SFrançois Tigeot 	drm_mode_object_put(dev, &fb->base);
363*b5162e19SFrançois Tigeot 	list_del(&fb->head);
364*b5162e19SFrançois Tigeot 	dev->mode_config.num_fb--;
365*b5162e19SFrançois Tigeot }
366*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_framebuffer_cleanup);
367*b5162e19SFrançois Tigeot 
368*b5162e19SFrançois Tigeot /**
369*b5162e19SFrançois Tigeot  * drm_framebuffer_remove - remove and unreference a framebuffer object
370*b5162e19SFrançois Tigeot  * @fb: framebuffer to remove
371*b5162e19SFrançois Tigeot  *
372*b5162e19SFrançois Tigeot  * LOCKING:
373*b5162e19SFrançois Tigeot  * Caller must hold mode config lock.
374*b5162e19SFrançois Tigeot  *
375*b5162e19SFrançois Tigeot  * Scans all the CRTCs and planes in @dev's mode_config.  If they're
376*b5162e19SFrançois Tigeot  * using @fb, removes it, setting it to NULL.
377*b5162e19SFrançois Tigeot  */
378*b5162e19SFrançois Tigeot void drm_framebuffer_remove(struct drm_framebuffer *fb)
379*b5162e19SFrançois Tigeot {
380*b5162e19SFrançois Tigeot 	struct drm_device *dev = fb->dev;
3815718399fSFrançois Tigeot 	struct drm_crtc *crtc;
3825718399fSFrançois Tigeot 	struct drm_plane *plane;
3835718399fSFrançois Tigeot 	struct drm_mode_set set;
3845718399fSFrançois Tigeot 	int ret;
3855718399fSFrançois Tigeot 
3865718399fSFrançois Tigeot 	/* remove from any CRTC */
3875718399fSFrançois Tigeot 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3885718399fSFrançois Tigeot 		if (crtc->fb == fb) {
3895718399fSFrançois Tigeot 			/* should turn off the crtc */
3905718399fSFrançois Tigeot 			memset(&set, 0, sizeof(struct drm_mode_set));
3915718399fSFrançois Tigeot 			set.crtc = crtc;
3925718399fSFrançois Tigeot 			set.fb = NULL;
3935718399fSFrançois Tigeot 			ret = crtc->funcs->set_config(&set);
3945718399fSFrançois Tigeot 			if (ret)
3955718399fSFrançois Tigeot 				DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
3965718399fSFrançois Tigeot 		}
3975718399fSFrançois Tigeot 	}
3985718399fSFrançois Tigeot 
3995718399fSFrançois Tigeot 	list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
4005718399fSFrançois Tigeot 		if (plane->fb == fb) {
4015718399fSFrançois Tigeot 			/* should turn off the crtc */
4025718399fSFrançois Tigeot 			ret = plane->funcs->disable_plane(plane);
4035718399fSFrançois Tigeot 			if (ret)
4045718399fSFrançois Tigeot 				DRM_ERROR("failed to disable plane with busy fb\n");
4055718399fSFrançois Tigeot 			/* disconnect the plane from the fb and crtc: */
4065718399fSFrançois Tigeot 			plane->fb = NULL;
4075718399fSFrançois Tigeot 			plane->crtc = NULL;
4085718399fSFrançois Tigeot 		}
4095718399fSFrançois Tigeot 	}
4105718399fSFrançois Tigeot 
411*b5162e19SFrançois Tigeot 	list_del(&fb->filp_head);
412*b5162e19SFrançois Tigeot 
413*b5162e19SFrançois Tigeot 	drm_framebuffer_unreference(fb);
4145718399fSFrançois Tigeot }
415*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_framebuffer_remove);
4165718399fSFrançois Tigeot 
4175718399fSFrançois Tigeot /**
4185718399fSFrançois Tigeot  * drm_crtc_init - Initialise a new CRTC object
4195718399fSFrançois Tigeot  * @dev: DRM device
4205718399fSFrançois Tigeot  * @crtc: CRTC object to init
4215718399fSFrançois Tigeot  * @funcs: callbacks for the new CRTC
4225718399fSFrançois Tigeot  *
4235718399fSFrançois Tigeot  * LOCKING:
424*b5162e19SFrançois Tigeot  * Takes mode_config lock.
4255718399fSFrançois Tigeot  *
4265718399fSFrançois Tigeot  * Inits a new object created as base part of an driver crtc object.
4275718399fSFrançois Tigeot  *
4285718399fSFrançois Tigeot  * RETURNS:
4295718399fSFrançois Tigeot  * Zero on success, error code on failure.
4305718399fSFrançois Tigeot  */
4315718399fSFrançois Tigeot int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
4325718399fSFrançois Tigeot 		   const struct drm_crtc_funcs *funcs)
4335718399fSFrançois Tigeot {
4345718399fSFrançois Tigeot 	int ret;
4355718399fSFrançois Tigeot 
4365718399fSFrançois Tigeot 	crtc->dev = dev;
4375718399fSFrançois Tigeot 	crtc->funcs = funcs;
438*b5162e19SFrançois Tigeot 	crtc->invert_dimensions = false;
4395718399fSFrançois Tigeot 
440af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
441*b5162e19SFrançois Tigeot 
4425718399fSFrançois Tigeot 	ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
4435718399fSFrançois Tigeot 	if (ret)
4445718399fSFrançois Tigeot 		goto out;
4455718399fSFrançois Tigeot 
446*b5162e19SFrançois Tigeot 	crtc->base.properties = &crtc->properties;
447*b5162e19SFrançois Tigeot 
4485718399fSFrançois Tigeot 	list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
4495718399fSFrançois Tigeot 	dev->mode_config.num_crtc++;
450*b5162e19SFrançois Tigeot 
4515718399fSFrançois Tigeot  out:
452af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
4535718399fSFrançois Tigeot 
4545718399fSFrançois Tigeot 	return ret;
4555718399fSFrançois Tigeot }
456*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_crtc_init);
4575718399fSFrançois Tigeot 
4585718399fSFrançois Tigeot /**
4595718399fSFrançois Tigeot  * drm_crtc_cleanup - Cleans up the core crtc usage.
4605718399fSFrançois Tigeot  * @crtc: CRTC to cleanup
4615718399fSFrançois Tigeot  *
4625718399fSFrançois Tigeot  * LOCKING:
4635718399fSFrançois Tigeot  * Caller must hold mode config lock.
4645718399fSFrançois Tigeot  *
4655718399fSFrançois Tigeot  * Cleanup @crtc. Removes from drm modesetting space
4665718399fSFrançois Tigeot  * does NOT free object, caller does that.
4675718399fSFrançois Tigeot  */
4685718399fSFrançois Tigeot void drm_crtc_cleanup(struct drm_crtc *crtc)
4695718399fSFrançois Tigeot {
4705718399fSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
4715718399fSFrançois Tigeot 
4725718399fSFrançois Tigeot 	drm_free(crtc->gamma_store, DRM_MEM_KMS);
4735718399fSFrançois Tigeot 	crtc->gamma_store = NULL;
4745718399fSFrançois Tigeot 
4755718399fSFrançois Tigeot 	drm_mode_object_put(dev, &crtc->base);
4765718399fSFrançois Tigeot 	list_del(&crtc->head);
4775718399fSFrançois Tigeot 	dev->mode_config.num_crtc--;
4785718399fSFrançois Tigeot }
479*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_crtc_cleanup);
4805718399fSFrançois Tigeot 
4815718399fSFrançois Tigeot /**
4825718399fSFrançois Tigeot  * drm_mode_probed_add - add a mode to a connector's probed mode list
4835718399fSFrançois Tigeot  * @connector: connector the new mode
4845718399fSFrançois Tigeot  * @mode: mode data
4855718399fSFrançois Tigeot  *
4865718399fSFrançois Tigeot  * LOCKING:
4875718399fSFrançois Tigeot  * Caller must hold mode config lock.
4885718399fSFrançois Tigeot  *
4895718399fSFrançois Tigeot  * Add @mode to @connector's mode list for later use.
4905718399fSFrançois Tigeot  */
4915718399fSFrançois Tigeot void drm_mode_probed_add(struct drm_connector *connector,
4925718399fSFrançois Tigeot 			 struct drm_display_mode *mode)
4935718399fSFrançois Tigeot {
4945718399fSFrançois Tigeot 	list_add(&mode->head, &connector->probed_modes);
4955718399fSFrançois Tigeot }
496*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_probed_add);
4975718399fSFrançois Tigeot 
4985718399fSFrançois Tigeot /**
4995718399fSFrançois Tigeot  * drm_mode_remove - remove and free a mode
5005718399fSFrançois Tigeot  * @connector: connector list to modify
5015718399fSFrançois Tigeot  * @mode: mode to remove
5025718399fSFrançois Tigeot  *
5035718399fSFrançois Tigeot  * LOCKING:
5045718399fSFrançois Tigeot  * Caller must hold mode config lock.
5055718399fSFrançois Tigeot  *
5065718399fSFrançois Tigeot  * Remove @mode from @connector's mode list, then free it.
5075718399fSFrançois Tigeot  */
5085718399fSFrançois Tigeot void drm_mode_remove(struct drm_connector *connector,
5095718399fSFrançois Tigeot 		     struct drm_display_mode *mode)
5105718399fSFrançois Tigeot {
5115718399fSFrançois Tigeot 	list_del(&mode->head);
5125718399fSFrançois Tigeot 	drm_mode_destroy(connector->dev, mode);
5135718399fSFrançois Tigeot }
514*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_remove);
5155718399fSFrançois Tigeot 
5165718399fSFrançois Tigeot /**
5175718399fSFrançois Tigeot  * drm_connector_init - Init a preallocated connector
5185718399fSFrançois Tigeot  * @dev: DRM device
5195718399fSFrançois Tigeot  * @connector: the connector to init
5205718399fSFrançois Tigeot  * @funcs: callbacks for this connector
5215718399fSFrançois Tigeot  * @name: user visible name of the connector
5225718399fSFrançois Tigeot  *
5235718399fSFrançois Tigeot  * LOCKING:
5245718399fSFrançois Tigeot  * Takes mode config lock.
5255718399fSFrançois Tigeot  *
5265718399fSFrançois Tigeot  * Initialises a preallocated connector. Connectors should be
5275718399fSFrançois Tigeot  * subclassed as part of driver connector objects.
5285718399fSFrançois Tigeot  *
5295718399fSFrançois Tigeot  * RETURNS:
5305718399fSFrançois Tigeot  * Zero on success, error code on failure.
5315718399fSFrançois Tigeot  */
5325718399fSFrançois Tigeot int drm_connector_init(struct drm_device *dev,
5335718399fSFrançois Tigeot 		       struct drm_connector *connector,
5345718399fSFrançois Tigeot 		       const struct drm_connector_funcs *funcs,
5355718399fSFrançois Tigeot 		       int connector_type)
5365718399fSFrançois Tigeot {
5375718399fSFrançois Tigeot 	int ret;
5385718399fSFrançois Tigeot 
539af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
5405718399fSFrançois Tigeot 
5415718399fSFrançois Tigeot 	ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
5425718399fSFrançois Tigeot 	if (ret)
5435718399fSFrançois Tigeot 		goto out;
5445718399fSFrançois Tigeot 
545*b5162e19SFrançois Tigeot 	connector->base.properties = &connector->properties;
5465718399fSFrançois Tigeot 	connector->dev = dev;
5475718399fSFrançois Tigeot 	connector->funcs = funcs;
5485718399fSFrançois Tigeot 	connector->connector_type = connector_type;
5495718399fSFrançois Tigeot 	connector->connector_type_id =
5505718399fSFrançois Tigeot 		++drm_connector_enum_list[connector_type].count; /* TODO */
5515718399fSFrançois Tigeot 	INIT_LIST_HEAD(&connector->user_modes);
5525718399fSFrançois Tigeot 	INIT_LIST_HEAD(&connector->probed_modes);
5535718399fSFrançois Tigeot 	INIT_LIST_HEAD(&connector->modes);
5545718399fSFrançois Tigeot 	connector->edid_blob_ptr = NULL;
555*b5162e19SFrançois Tigeot 	connector->status = connector_status_unknown;
5565718399fSFrançois Tigeot 
5575718399fSFrançois Tigeot 	list_add_tail(&connector->head, &dev->mode_config.connector_list);
5585718399fSFrançois Tigeot 	dev->mode_config.num_connector++;
5595718399fSFrançois Tigeot 
560*b5162e19SFrançois Tigeot 	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
561*b5162e19SFrançois Tigeot 		drm_object_attach_property(&connector->base,
562*b5162e19SFrançois Tigeot 					      dev->mode_config.edid_property,
563*b5162e19SFrançois Tigeot 					      0);
5645718399fSFrançois Tigeot 
565*b5162e19SFrançois Tigeot 	drm_object_attach_property(&connector->base,
5665718399fSFrançois Tigeot 				      dev->mode_config.dpms_property, 0);
5675718399fSFrançois Tigeot 
5685718399fSFrançois Tigeot  out:
569af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
5705718399fSFrançois Tigeot 
5715718399fSFrançois Tigeot 	return ret;
5725718399fSFrançois Tigeot }
573*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_connector_init);
5745718399fSFrançois Tigeot 
5755718399fSFrançois Tigeot /**
5765718399fSFrançois Tigeot  * drm_connector_cleanup - cleans up an initialised connector
5775718399fSFrançois Tigeot  * @connector: connector to cleanup
5785718399fSFrançois Tigeot  *
5795718399fSFrançois Tigeot  * LOCKING:
5805718399fSFrançois Tigeot  * Takes mode config lock.
5815718399fSFrançois Tigeot  *
5825718399fSFrançois Tigeot  * Cleans up the connector but doesn't free the object.
5835718399fSFrançois Tigeot  */
5845718399fSFrançois Tigeot void drm_connector_cleanup(struct drm_connector *connector)
5855718399fSFrançois Tigeot {
5865718399fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
5875718399fSFrançois Tigeot 	struct drm_display_mode *mode, *t;
5885718399fSFrançois Tigeot 
5895718399fSFrançois Tigeot 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
5905718399fSFrançois Tigeot 		drm_mode_remove(connector, mode);
5915718399fSFrançois Tigeot 
5925718399fSFrançois Tigeot 	list_for_each_entry_safe(mode, t, &connector->modes, head)
5935718399fSFrançois Tigeot 		drm_mode_remove(connector, mode);
5945718399fSFrançois Tigeot 
5955718399fSFrançois Tigeot 	list_for_each_entry_safe(mode, t, &connector->user_modes, head)
5965718399fSFrançois Tigeot 		drm_mode_remove(connector, mode);
5975718399fSFrançois Tigeot 
598af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
5995718399fSFrançois Tigeot 	drm_mode_object_put(dev, &connector->base);
6005718399fSFrançois Tigeot 	list_del(&connector->head);
6015718399fSFrançois Tigeot 	dev->mode_config.num_connector--;
602af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
6035718399fSFrançois Tigeot }
604*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_connector_cleanup);
605*b5162e19SFrançois Tigeot 
606*b5162e19SFrançois Tigeot void drm_connector_unplug_all(struct drm_device *dev)
607*b5162e19SFrançois Tigeot {
608*b5162e19SFrançois Tigeot #if 0
609*b5162e19SFrançois Tigeot 	struct drm_connector *connector;
610*b5162e19SFrançois Tigeot 
611*b5162e19SFrançois Tigeot 	/* taking the mode config mutex ends up in a clash with sysfs */
612*b5162e19SFrançois Tigeot 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
613*b5162e19SFrançois Tigeot 		drm_sysfs_connector_remove(connector);
614*b5162e19SFrançois Tigeot 
615*b5162e19SFrançois Tigeot #endif
616*b5162e19SFrançois Tigeot }
617*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_connector_unplug_all);
6185718399fSFrançois Tigeot 
6195718399fSFrançois Tigeot int drm_encoder_init(struct drm_device *dev,
6205718399fSFrançois Tigeot 		      struct drm_encoder *encoder,
6215718399fSFrançois Tigeot 		      const struct drm_encoder_funcs *funcs,
6225718399fSFrançois Tigeot 		      int encoder_type)
6235718399fSFrançois Tigeot {
6245718399fSFrançois Tigeot 	int ret;
6255718399fSFrançois Tigeot 
626af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
6275718399fSFrançois Tigeot 
6285718399fSFrançois Tigeot 	ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
6295718399fSFrançois Tigeot 	if (ret)
6305718399fSFrançois Tigeot 		goto out;
6315718399fSFrançois Tigeot 
6325718399fSFrançois Tigeot 	encoder->dev = dev;
6335718399fSFrançois Tigeot 	encoder->encoder_type = encoder_type;
6345718399fSFrançois Tigeot 	encoder->funcs = funcs;
6355718399fSFrançois Tigeot 
6365718399fSFrançois Tigeot 	list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
6375718399fSFrançois Tigeot 	dev->mode_config.num_encoder++;
6385718399fSFrançois Tigeot 
6395718399fSFrançois Tigeot  out:
640af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
6415718399fSFrançois Tigeot 
6425718399fSFrançois Tigeot 	return ret;
6435718399fSFrançois Tigeot }
644*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_encoder_init);
6455718399fSFrançois Tigeot 
6465718399fSFrançois Tigeot void drm_encoder_cleanup(struct drm_encoder *encoder)
6475718399fSFrançois Tigeot {
6485718399fSFrançois Tigeot 	struct drm_device *dev = encoder->dev;
649af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
6505718399fSFrançois Tigeot 	drm_mode_object_put(dev, &encoder->base);
6515718399fSFrançois Tigeot 	list_del(&encoder->head);
6525718399fSFrançois Tigeot 	dev->mode_config.num_encoder--;
653af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
6545718399fSFrançois Tigeot }
655*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_encoder_cleanup);
6565718399fSFrançois Tigeot 
6575718399fSFrançois Tigeot int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
6585718399fSFrançois Tigeot 		   unsigned long possible_crtcs,
6595718399fSFrançois Tigeot 		   const struct drm_plane_funcs *funcs,
6605718399fSFrançois Tigeot 		   const uint32_t *formats, uint32_t format_count,
6615718399fSFrançois Tigeot 		   bool priv)
6625718399fSFrançois Tigeot {
6635718399fSFrançois Tigeot 	int ret;
6645718399fSFrançois Tigeot 
665af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
6665718399fSFrançois Tigeot 
6675718399fSFrançois Tigeot 	ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
6685718399fSFrançois Tigeot 	if (ret)
6695718399fSFrançois Tigeot 		goto out;
6705718399fSFrançois Tigeot 
671*b5162e19SFrançois Tigeot 	plane->base.properties = &plane->properties;
6725718399fSFrançois Tigeot 	plane->dev = dev;
6735718399fSFrançois Tigeot 	plane->funcs = funcs;
6745718399fSFrançois Tigeot 	plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
6755718399fSFrançois Tigeot 	    DRM_MEM_KMS, M_WAITOK);
676*b5162e19SFrançois Tigeot 	if (!plane->format_types) {
677*b5162e19SFrançois Tigeot 		DRM_DEBUG_KMS("out of memory when allocating plane\n");
678*b5162e19SFrançois Tigeot 		drm_mode_object_put(dev, &plane->base);
679*b5162e19SFrançois Tigeot 		ret = -ENOMEM;
680*b5162e19SFrançois Tigeot 		goto out;
681*b5162e19SFrançois Tigeot 	}
6825718399fSFrançois Tigeot 
6835718399fSFrançois Tigeot 	memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
6845718399fSFrançois Tigeot 	plane->format_count = format_count;
6855718399fSFrançois Tigeot 	plane->possible_crtcs = possible_crtcs;
6865718399fSFrançois Tigeot 
6875718399fSFrançois Tigeot 	/* private planes are not exposed to userspace, but depending on
6885718399fSFrançois Tigeot 	 * display hardware, might be convenient to allow sharing programming
6895718399fSFrançois Tigeot 	 * for the scanout engine with the crtc implementation.
6905718399fSFrançois Tigeot 	 */
6915718399fSFrançois Tigeot 	if (!priv) {
6925718399fSFrançois Tigeot 		list_add_tail(&plane->head, &dev->mode_config.plane_list);
6935718399fSFrançois Tigeot 		dev->mode_config.num_plane++;
6945718399fSFrançois Tigeot 	} else {
6955718399fSFrançois Tigeot 		INIT_LIST_HEAD(&plane->head);
6965718399fSFrançois Tigeot 	}
6975718399fSFrançois Tigeot 
6985718399fSFrançois Tigeot  out:
699af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
7005718399fSFrançois Tigeot 
7015718399fSFrançois Tigeot 	return ret;
7025718399fSFrançois Tigeot }
703*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_plane_init);
7045718399fSFrançois Tigeot 
7055718399fSFrançois Tigeot void drm_plane_cleanup(struct drm_plane *plane)
7065718399fSFrançois Tigeot {
7075718399fSFrançois Tigeot 	struct drm_device *dev = plane->dev;
7085718399fSFrançois Tigeot 
709af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
7105718399fSFrançois Tigeot 	drm_free(plane->format_types, DRM_MEM_KMS);
7115718399fSFrançois Tigeot 	drm_mode_object_put(dev, &plane->base);
7125718399fSFrançois Tigeot 	/* if not added to a list, it must be a private plane */
7135718399fSFrançois Tigeot 	if (!list_empty(&plane->head)) {
7145718399fSFrançois Tigeot 		list_del(&plane->head);
7155718399fSFrançois Tigeot 		dev->mode_config.num_plane--;
7165718399fSFrançois Tigeot 	}
717af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
7185718399fSFrançois Tigeot }
719*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_plane_cleanup);
7205718399fSFrançois Tigeot 
7215718399fSFrançois Tigeot /**
7225718399fSFrançois Tigeot  * drm_mode_create - create a new display mode
7235718399fSFrançois Tigeot  * @dev: DRM device
7245718399fSFrançois Tigeot  *
7255718399fSFrançois Tigeot  * LOCKING:
726*b5162e19SFrançois Tigeot  * Caller must hold DRM mode_config lock.
7275718399fSFrançois Tigeot  *
7285718399fSFrançois Tigeot  * Create a new drm_display_mode, give it an ID, and return it.
7295718399fSFrançois Tigeot  *
7305718399fSFrançois Tigeot  * RETURNS:
7315718399fSFrançois Tigeot  * Pointer to new mode on success, NULL on error.
7325718399fSFrançois Tigeot  */
7335718399fSFrançois Tigeot struct drm_display_mode *drm_mode_create(struct drm_device *dev)
7345718399fSFrançois Tigeot {
7355718399fSFrançois Tigeot 	struct drm_display_mode *nmode;
7365718399fSFrançois Tigeot 
7375718399fSFrançois Tigeot 	nmode = kmalloc(sizeof(struct drm_display_mode), DRM_MEM_KMS,
7385718399fSFrançois Tigeot 	    M_WAITOK | M_ZERO);
739*b5162e19SFrançois Tigeot 	if (!nmode)
740*b5162e19SFrançois Tigeot 		return NULL;
7415718399fSFrançois Tigeot 
7425718399fSFrançois Tigeot 	if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
7435718399fSFrançois Tigeot 		drm_free(nmode, DRM_MEM_KMS);
744*b5162e19SFrançois Tigeot 		return NULL;
7455718399fSFrançois Tigeot 	}
746*b5162e19SFrançois Tigeot 
7475718399fSFrançois Tigeot 	return nmode;
7485718399fSFrançois Tigeot }
749*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_create);
7505718399fSFrançois Tigeot 
7515718399fSFrançois Tigeot /**
7525718399fSFrançois Tigeot  * drm_mode_destroy - remove a mode
7535718399fSFrançois Tigeot  * @dev: DRM device
7545718399fSFrançois Tigeot  * @mode: mode to remove
7555718399fSFrançois Tigeot  *
7565718399fSFrançois Tigeot  * LOCKING:
7575718399fSFrançois Tigeot  * Caller must hold mode config lock.
7585718399fSFrançois Tigeot  *
7595718399fSFrançois Tigeot  * Free @mode's unique identifier, then free it.
7605718399fSFrançois Tigeot  */
7615718399fSFrançois Tigeot void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
7625718399fSFrançois Tigeot {
7635718399fSFrançois Tigeot 	if (!mode)
7645718399fSFrançois Tigeot 		return;
7655718399fSFrançois Tigeot 
7665718399fSFrançois Tigeot 	drm_mode_object_put(dev, &mode->base);
7675718399fSFrançois Tigeot 
7685718399fSFrançois Tigeot 	drm_free(mode, DRM_MEM_KMS);
7695718399fSFrançois Tigeot }
770*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_destroy);
7715718399fSFrançois Tigeot 
7725718399fSFrançois Tigeot static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
7735718399fSFrançois Tigeot {
7745718399fSFrançois Tigeot 	struct drm_property *edid;
7755718399fSFrançois Tigeot 	struct drm_property *dpms;
7765718399fSFrançois Tigeot 
7775718399fSFrançois Tigeot 	/*
7785718399fSFrançois Tigeot 	 * Standard properties (apply to all connectors)
7795718399fSFrançois Tigeot 	 */
7805718399fSFrançois Tigeot 	edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
7815718399fSFrançois Tigeot 				   DRM_MODE_PROP_IMMUTABLE,
7825718399fSFrançois Tigeot 				   "EDID", 0);
7835718399fSFrançois Tigeot 	dev->mode_config.edid_property = edid;
7845718399fSFrançois Tigeot 
7855718399fSFrançois Tigeot 	dpms = drm_property_create_enum(dev, 0,
7865718399fSFrançois Tigeot 				   "DPMS", drm_dpms_enum_list,
787*b5162e19SFrançois Tigeot 				   ARRAY_SIZE(drm_dpms_enum_list));
7885718399fSFrançois Tigeot 	dev->mode_config.dpms_property = dpms;
7895718399fSFrançois Tigeot 
7905718399fSFrançois Tigeot 	return 0;
7915718399fSFrançois Tigeot }
7925718399fSFrançois Tigeot 
7935718399fSFrançois Tigeot /**
7945718399fSFrançois Tigeot  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
7955718399fSFrançois Tigeot  * @dev: DRM device
7965718399fSFrançois Tigeot  *
7975718399fSFrançois Tigeot  * Called by a driver the first time a DVI-I connector is made.
7985718399fSFrançois Tigeot  */
7995718399fSFrançois Tigeot int drm_mode_create_dvi_i_properties(struct drm_device *dev)
8005718399fSFrançois Tigeot {
8015718399fSFrançois Tigeot 	struct drm_property *dvi_i_selector;
8025718399fSFrançois Tigeot 	struct drm_property *dvi_i_subconnector;
8035718399fSFrançois Tigeot 
8045718399fSFrançois Tigeot 	if (dev->mode_config.dvi_i_select_subconnector_property)
8055718399fSFrançois Tigeot 		return 0;
8065718399fSFrançois Tigeot 
8075718399fSFrançois Tigeot 	dvi_i_selector =
8085718399fSFrançois Tigeot 		drm_property_create_enum(dev, 0,
8095718399fSFrançois Tigeot 				    "select subconnector",
8105718399fSFrançois Tigeot 				    drm_dvi_i_select_enum_list,
811*b5162e19SFrançois Tigeot 				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
8125718399fSFrançois Tigeot 	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
8135718399fSFrançois Tigeot 
8145718399fSFrançois Tigeot 	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
8155718399fSFrançois Tigeot 				    "subconnector",
8165718399fSFrançois Tigeot 				    drm_dvi_i_subconnector_enum_list,
817*b5162e19SFrançois Tigeot 				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
8185718399fSFrançois Tigeot 	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
8195718399fSFrançois Tigeot 
8205718399fSFrançois Tigeot 	return 0;
8215718399fSFrançois Tigeot }
822*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
8235718399fSFrançois Tigeot 
8245718399fSFrançois Tigeot /**
8255718399fSFrançois Tigeot  * drm_create_tv_properties - create TV specific connector properties
8265718399fSFrançois Tigeot  * @dev: DRM device
8275718399fSFrançois Tigeot  * @num_modes: number of different TV formats (modes) supported
8285718399fSFrançois Tigeot  * @modes: array of pointers to strings containing name of each format
8295718399fSFrançois Tigeot  *
8305718399fSFrançois Tigeot  * Called by a driver's TV initialization routine, this function creates
8315718399fSFrançois Tigeot  * the TV specific connector properties for a given device.  Caller is
8325718399fSFrançois Tigeot  * responsible for allocating a list of format names and passing them to
8335718399fSFrançois Tigeot  * this routine.
8345718399fSFrançois Tigeot  */
8355718399fSFrançois Tigeot int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
8365718399fSFrançois Tigeot 				  char *modes[])
8375718399fSFrançois Tigeot {
8385718399fSFrançois Tigeot 	struct drm_property *tv_selector;
8395718399fSFrançois Tigeot 	struct drm_property *tv_subconnector;
8405718399fSFrançois Tigeot 	int i;
8415718399fSFrançois Tigeot 
8425718399fSFrançois Tigeot 	if (dev->mode_config.tv_select_subconnector_property)
8435718399fSFrançois Tigeot 		return 0;
8445718399fSFrançois Tigeot 
8455718399fSFrançois Tigeot 	/*
8465718399fSFrançois Tigeot 	 * Basic connector properties
8475718399fSFrançois Tigeot 	 */
8485718399fSFrançois Tigeot 	tv_selector = drm_property_create_enum(dev, 0,
8495718399fSFrançois Tigeot 					  "select subconnector",
8505718399fSFrançois Tigeot 					  drm_tv_select_enum_list,
851*b5162e19SFrançois Tigeot 					  ARRAY_SIZE(drm_tv_select_enum_list));
8525718399fSFrançois Tigeot 	dev->mode_config.tv_select_subconnector_property = tv_selector;
8535718399fSFrançois Tigeot 
8545718399fSFrançois Tigeot 	tv_subconnector =
8555718399fSFrançois Tigeot 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
8565718399fSFrançois Tigeot 				    "subconnector",
8575718399fSFrançois Tigeot 				    drm_tv_subconnector_enum_list,
858*b5162e19SFrançois Tigeot 				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
8595718399fSFrançois Tigeot 	dev->mode_config.tv_subconnector_property = tv_subconnector;
8605718399fSFrançois Tigeot 
8615718399fSFrançois Tigeot 	/*
8625718399fSFrançois Tigeot 	 * Other, TV specific properties: margins & TV modes.
8635718399fSFrançois Tigeot 	 */
8645718399fSFrançois Tigeot 	dev->mode_config.tv_left_margin_property =
8655718399fSFrançois Tigeot 		drm_property_create_range(dev, 0, "left margin", 0, 100);
8665718399fSFrançois Tigeot 
8675718399fSFrançois Tigeot 	dev->mode_config.tv_right_margin_property =
8685718399fSFrançois Tigeot 		drm_property_create_range(dev, 0, "right margin", 0, 100);
8695718399fSFrançois Tigeot 
8705718399fSFrançois Tigeot 	dev->mode_config.tv_top_margin_property =
8715718399fSFrançois Tigeot 		drm_property_create_range(dev, 0, "top margin", 0, 100);
8725718399fSFrançois Tigeot 
8735718399fSFrançois Tigeot 	dev->mode_config.tv_bottom_margin_property =
8745718399fSFrançois Tigeot 		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
8755718399fSFrançois Tigeot 
8765718399fSFrançois Tigeot 	dev->mode_config.tv_mode_property =
8775718399fSFrançois Tigeot 		drm_property_create(dev, DRM_MODE_PROP_ENUM,
8785718399fSFrançois Tigeot 				    "mode", num_modes);
8795718399fSFrançois Tigeot 	for (i = 0; i < num_modes; i++)
8805718399fSFrançois Tigeot 		drm_property_add_enum(dev->mode_config.tv_mode_property, i,
8815718399fSFrançois Tigeot 				      i, modes[i]);
8825718399fSFrançois Tigeot 
8835718399fSFrançois Tigeot 	dev->mode_config.tv_brightness_property =
8845718399fSFrançois Tigeot 		drm_property_create_range(dev, 0, "brightness", 0, 100);
8855718399fSFrançois Tigeot 
8865718399fSFrançois Tigeot 	dev->mode_config.tv_contrast_property =
8875718399fSFrançois Tigeot 		drm_property_create_range(dev, 0, "contrast", 0, 100);
8885718399fSFrançois Tigeot 
8895718399fSFrançois Tigeot 	dev->mode_config.tv_flicker_reduction_property =
8905718399fSFrançois Tigeot 		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
8915718399fSFrançois Tigeot 
8925718399fSFrançois Tigeot 	dev->mode_config.tv_overscan_property =
8935718399fSFrançois Tigeot 		drm_property_create_range(dev, 0, "overscan", 0, 100);
8945718399fSFrançois Tigeot 
8955718399fSFrançois Tigeot 	dev->mode_config.tv_saturation_property =
8965718399fSFrançois Tigeot 		drm_property_create_range(dev, 0, "saturation", 0, 100);
8975718399fSFrançois Tigeot 
8985718399fSFrançois Tigeot 	dev->mode_config.tv_hue_property =
8995718399fSFrançois Tigeot 		drm_property_create_range(dev, 0, "hue", 0, 100);
9005718399fSFrançois Tigeot 
9015718399fSFrançois Tigeot 	return 0;
9025718399fSFrançois Tigeot }
903*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_create_tv_properties);
9045718399fSFrançois Tigeot 
9055718399fSFrançois Tigeot /**
9065718399fSFrançois Tigeot  * drm_mode_create_scaling_mode_property - create scaling mode property
9075718399fSFrançois Tigeot  * @dev: DRM device
9085718399fSFrançois Tigeot  *
9095718399fSFrançois Tigeot  * Called by a driver the first time it's needed, must be attached to desired
9105718399fSFrançois Tigeot  * connectors.
9115718399fSFrançois Tigeot  */
9125718399fSFrançois Tigeot int drm_mode_create_scaling_mode_property(struct drm_device *dev)
9135718399fSFrançois Tigeot {
9145718399fSFrançois Tigeot 	struct drm_property *scaling_mode;
9155718399fSFrançois Tigeot 
9165718399fSFrançois Tigeot 	if (dev->mode_config.scaling_mode_property)
9175718399fSFrançois Tigeot 		return 0;
9185718399fSFrançois Tigeot 
9195718399fSFrançois Tigeot 	scaling_mode =
9205718399fSFrançois Tigeot 		drm_property_create_enum(dev, 0, "scaling mode",
9215718399fSFrançois Tigeot 				drm_scaling_mode_enum_list,
922*b5162e19SFrançois Tigeot 				    ARRAY_SIZE(drm_scaling_mode_enum_list));
9235718399fSFrançois Tigeot 
9245718399fSFrançois Tigeot 	dev->mode_config.scaling_mode_property = scaling_mode;
9255718399fSFrançois Tigeot 
9265718399fSFrançois Tigeot 	return 0;
9275718399fSFrançois Tigeot }
928*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
9295718399fSFrançois Tigeot 
9305718399fSFrançois Tigeot /**
9315718399fSFrançois Tigeot  * drm_mode_create_dithering_property - create dithering property
9325718399fSFrançois Tigeot  * @dev: DRM device
9335718399fSFrançois Tigeot  *
9345718399fSFrançois Tigeot  * Called by a driver the first time it's needed, must be attached to desired
9355718399fSFrançois Tigeot  * connectors.
9365718399fSFrançois Tigeot  */
9375718399fSFrançois Tigeot int drm_mode_create_dithering_property(struct drm_device *dev)
9385718399fSFrançois Tigeot {
9395718399fSFrançois Tigeot 	struct drm_property *dithering_mode;
9405718399fSFrançois Tigeot 
9415718399fSFrançois Tigeot 	if (dev->mode_config.dithering_mode_property)
9425718399fSFrançois Tigeot 		return 0;
9435718399fSFrançois Tigeot 
9445718399fSFrançois Tigeot 	dithering_mode =
9455718399fSFrançois Tigeot 		drm_property_create_enum(dev, 0, "dithering",
9465718399fSFrançois Tigeot 				drm_dithering_mode_enum_list,
947*b5162e19SFrançois Tigeot 				    ARRAY_SIZE(drm_dithering_mode_enum_list));
9485718399fSFrançois Tigeot 	dev->mode_config.dithering_mode_property = dithering_mode;
9495718399fSFrançois Tigeot 
9505718399fSFrançois Tigeot 	return 0;
9515718399fSFrançois Tigeot }
952*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_create_dithering_property);
9535718399fSFrançois Tigeot 
9545718399fSFrançois Tigeot /**
9555718399fSFrançois Tigeot  * drm_mode_create_dirty_property - create dirty property
9565718399fSFrançois Tigeot  * @dev: DRM device
9575718399fSFrançois Tigeot  *
9585718399fSFrançois Tigeot  * Called by a driver the first time it's needed, must be attached to desired
9595718399fSFrançois Tigeot  * connectors.
9605718399fSFrançois Tigeot  */
9615718399fSFrançois Tigeot int drm_mode_create_dirty_info_property(struct drm_device *dev)
9625718399fSFrançois Tigeot {
9635718399fSFrançois Tigeot 	struct drm_property *dirty_info;
9645718399fSFrançois Tigeot 
9655718399fSFrançois Tigeot 	if (dev->mode_config.dirty_info_property)
9665718399fSFrançois Tigeot 		return 0;
9675718399fSFrançois Tigeot 
9685718399fSFrançois Tigeot 	dirty_info =
9695718399fSFrançois Tigeot 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
9705718399fSFrançois Tigeot 				    "dirty",
9715718399fSFrançois Tigeot 				    drm_dirty_info_enum_list,
972*b5162e19SFrançois Tigeot 				    ARRAY_SIZE(drm_dirty_info_enum_list));
9735718399fSFrançois Tigeot 	dev->mode_config.dirty_info_property = dirty_info;
9745718399fSFrançois Tigeot 
9755718399fSFrançois Tigeot 	return 0;
9765718399fSFrançois Tigeot }
977*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
9785718399fSFrançois Tigeot 
9795718399fSFrançois Tigeot /**
9805718399fSFrançois Tigeot  * drm_mode_config_init - initialize DRM mode_configuration structure
9815718399fSFrançois Tigeot  * @dev: DRM device
9825718399fSFrançois Tigeot  *
9835718399fSFrançois Tigeot  * LOCKING:
9845718399fSFrançois Tigeot  * None, should happen single threaded at init time.
9855718399fSFrançois Tigeot  *
9865718399fSFrançois Tigeot  * Initialize @dev's mode_config structure, used for tracking the graphics
9875718399fSFrançois Tigeot  * configuration of @dev.
9885718399fSFrançois Tigeot  */
9895718399fSFrançois Tigeot void drm_mode_config_init(struct drm_device *dev)
9905718399fSFrançois Tigeot {
991af4b81b9SFrançois Tigeot 	lockinit(&dev->mode_config.mutex, "kmslk", 0, LK_CANRECURSE);
99298a11977SFrançois Tigeot 	lockinit(&dev->mode_config.idr_mutex, "mcfgidr", 0, LK_CANRECURSE);
9935718399fSFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.fb_list);
9945718399fSFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
9955718399fSFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.connector_list);
9965718399fSFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
9975718399fSFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.property_list);
9985718399fSFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
9995718399fSFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.plane_list);
1000aea8bdbdSFrançois Tigeot 	idr_init(&dev->mode_config.crtc_idr);
10015718399fSFrançois Tigeot 
1002af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
10035718399fSFrançois Tigeot 	drm_mode_create_standard_connector_properties(dev);
1004af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
10055718399fSFrançois Tigeot 
10065718399fSFrançois Tigeot 	/* Just to be sure */
10075718399fSFrançois Tigeot 	dev->mode_config.num_fb = 0;
10085718399fSFrançois Tigeot 	dev->mode_config.num_connector = 0;
10095718399fSFrançois Tigeot 	dev->mode_config.num_crtc = 0;
10105718399fSFrançois Tigeot 	dev->mode_config.num_encoder = 0;
10115718399fSFrançois Tigeot }
1012*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_config_init);
10135718399fSFrançois Tigeot 
1014*b5162e19SFrançois Tigeot static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
10155718399fSFrançois Tigeot {
10165718399fSFrançois Tigeot 	uint32_t total_objects = 0;
10175718399fSFrançois Tigeot 
10185718399fSFrançois Tigeot 	total_objects += dev->mode_config.num_crtc;
10195718399fSFrançois Tigeot 	total_objects += dev->mode_config.num_connector;
10205718399fSFrançois Tigeot 	total_objects += dev->mode_config.num_encoder;
10215718399fSFrançois Tigeot 
10225718399fSFrançois Tigeot 	group->id_list = kmalloc(total_objects * sizeof(uint32_t),
10235718399fSFrançois Tigeot 	    DRM_MEM_KMS, M_WAITOK | M_ZERO);
1024*b5162e19SFrançois Tigeot 	if (!group->id_list)
1025*b5162e19SFrançois Tigeot 		return -ENOMEM;
10265718399fSFrançois Tigeot 
10275718399fSFrançois Tigeot 	group->num_crtcs = 0;
10285718399fSFrançois Tigeot 	group->num_connectors = 0;
10295718399fSFrançois Tigeot 	group->num_encoders = 0;
10305718399fSFrançois Tigeot 	return 0;
10315718399fSFrançois Tigeot }
10325718399fSFrançois Tigeot 
10335718399fSFrançois Tigeot int drm_mode_group_init_legacy_group(struct drm_device *dev,
10345718399fSFrançois Tigeot 				     struct drm_mode_group *group)
10355718399fSFrançois Tigeot {
10365718399fSFrançois Tigeot 	struct drm_crtc *crtc;
10375718399fSFrançois Tigeot 	struct drm_encoder *encoder;
10385718399fSFrançois Tigeot 	struct drm_connector *connector;
10395718399fSFrançois Tigeot 	int ret;
10405718399fSFrançois Tigeot 
10415718399fSFrançois Tigeot 	if ((ret = drm_mode_group_init(dev, group)))
10425718399fSFrançois Tigeot 		return ret;
10435718399fSFrançois Tigeot 
10445718399fSFrançois Tigeot 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
10455718399fSFrançois Tigeot 		group->id_list[group->num_crtcs++] = crtc->base.id;
10465718399fSFrançois Tigeot 
10475718399fSFrançois Tigeot 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10485718399fSFrançois Tigeot 		group->id_list[group->num_crtcs + group->num_encoders++] =
10495718399fSFrançois Tigeot 		encoder->base.id;
10505718399fSFrançois Tigeot 
10515718399fSFrançois Tigeot 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
10525718399fSFrançois Tigeot 		group->id_list[group->num_crtcs + group->num_encoders +
10535718399fSFrançois Tigeot 			       group->num_connectors++] = connector->base.id;
10545718399fSFrançois Tigeot 
10555718399fSFrançois Tigeot 	return 0;
10565718399fSFrançois Tigeot }
1057*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
10585718399fSFrançois Tigeot 
10595718399fSFrançois Tigeot /**
10605718399fSFrançois Tigeot  * drm_mode_config_cleanup - free up DRM mode_config info
10615718399fSFrançois Tigeot  * @dev: DRM device
10625718399fSFrançois Tigeot  *
10635718399fSFrançois Tigeot  * LOCKING:
10645718399fSFrançois Tigeot  * Caller must hold mode config lock.
10655718399fSFrançois Tigeot  *
10665718399fSFrançois Tigeot  * Free up all the connectors and CRTCs associated with this DRM device, then
10675718399fSFrançois Tigeot  * free up the framebuffers and associated buffer objects.
10685718399fSFrançois Tigeot  *
10695718399fSFrançois Tigeot  * FIXME: cleanup any dangling user buffer objects too
10705718399fSFrançois Tigeot  */
10715718399fSFrançois Tigeot void drm_mode_config_cleanup(struct drm_device *dev)
10725718399fSFrançois Tigeot {
10735718399fSFrançois Tigeot 	struct drm_connector *connector, *ot;
10745718399fSFrançois Tigeot 	struct drm_crtc *crtc, *ct;
10755718399fSFrançois Tigeot 	struct drm_encoder *encoder, *enct;
10765718399fSFrançois Tigeot 	struct drm_framebuffer *fb, *fbt;
10775718399fSFrançois Tigeot 	struct drm_property *property, *pt;
10785718399fSFrançois Tigeot 	struct drm_plane *plane, *plt;
10795718399fSFrançois Tigeot 
10805718399fSFrançois Tigeot 	list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
10815718399fSFrançois Tigeot 				 head) {
10825718399fSFrançois Tigeot 		encoder->funcs->destroy(encoder);
10835718399fSFrançois Tigeot 	}
10845718399fSFrançois Tigeot 
10855718399fSFrançois Tigeot 	list_for_each_entry_safe(connector, ot,
10865718399fSFrançois Tigeot 				 &dev->mode_config.connector_list, head) {
10875718399fSFrançois Tigeot 		connector->funcs->destroy(connector);
10885718399fSFrançois Tigeot 	}
10895718399fSFrançois Tigeot 
10905718399fSFrançois Tigeot 	list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
10915718399fSFrançois Tigeot 				 head) {
10925718399fSFrançois Tigeot 		drm_property_destroy(dev, property);
10935718399fSFrançois Tigeot 	}
10945718399fSFrançois Tigeot 
10955718399fSFrançois Tigeot 	list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1096*b5162e19SFrançois Tigeot 		drm_framebuffer_remove(fb);
10975718399fSFrançois Tigeot 	}
10985718399fSFrançois Tigeot 
10995718399fSFrançois Tigeot 	list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
11005718399fSFrançois Tigeot 				 head) {
11015718399fSFrançois Tigeot 		plane->funcs->destroy(plane);
11025718399fSFrançois Tigeot 	}
1103aea8bdbdSFrançois Tigeot 
1104aea8bdbdSFrançois Tigeot 	list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1105aea8bdbdSFrançois Tigeot 		crtc->funcs->destroy(crtc);
1106aea8bdbdSFrançois Tigeot 	}
1107aea8bdbdSFrançois Tigeot 
1108aea8bdbdSFrançois Tigeot 	idr_remove_all(&dev->mode_config.crtc_idr);
1109aea8bdbdSFrançois Tigeot 	idr_destroy(&dev->mode_config.crtc_idr);
11105718399fSFrançois Tigeot }
1111*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_config_cleanup);
11125718399fSFrançois Tigeot 
11135718399fSFrançois Tigeot /**
11145718399fSFrançois Tigeot  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
11155718399fSFrançois Tigeot  * @out: drm_mode_modeinfo struct to return to the user
11165718399fSFrançois Tigeot  * @in: drm_display_mode to use
11175718399fSFrançois Tigeot  *
11185718399fSFrançois Tigeot  * LOCKING:
11195718399fSFrançois Tigeot  * None.
11205718399fSFrançois Tigeot  *
11215718399fSFrançois Tigeot  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
11225718399fSFrançois Tigeot  * the user.
11235718399fSFrançois Tigeot  */
11245718399fSFrançois Tigeot static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
11255718399fSFrançois Tigeot 				      const struct drm_display_mode *in)
11265718399fSFrançois Tigeot {
1127*b5162e19SFrançois Tigeot 	WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
11285718399fSFrançois Tigeot 	     in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
11295718399fSFrançois Tigeot 	     in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
11305718399fSFrançois Tigeot 	     in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1131*b5162e19SFrançois Tigeot 	     in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1132*b5162e19SFrançois Tigeot 	     "timing values too large for mode info\n");
11335718399fSFrançois Tigeot 
11345718399fSFrançois Tigeot 	out->clock = in->clock;
11355718399fSFrançois Tigeot 	out->hdisplay = in->hdisplay;
11365718399fSFrançois Tigeot 	out->hsync_start = in->hsync_start;
11375718399fSFrançois Tigeot 	out->hsync_end = in->hsync_end;
11385718399fSFrançois Tigeot 	out->htotal = in->htotal;
11395718399fSFrançois Tigeot 	out->hskew = in->hskew;
11405718399fSFrançois Tigeot 	out->vdisplay = in->vdisplay;
11415718399fSFrançois Tigeot 	out->vsync_start = in->vsync_start;
11425718399fSFrançois Tigeot 	out->vsync_end = in->vsync_end;
11435718399fSFrançois Tigeot 	out->vtotal = in->vtotal;
11445718399fSFrançois Tigeot 	out->vscan = in->vscan;
11455718399fSFrançois Tigeot 	out->vrefresh = in->vrefresh;
11465718399fSFrançois Tigeot 	out->flags = in->flags;
11475718399fSFrançois Tigeot 	out->type = in->type;
11485718399fSFrançois Tigeot 	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
11495718399fSFrançois Tigeot 	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
11505718399fSFrançois Tigeot }
11515718399fSFrançois Tigeot 
11525718399fSFrançois Tigeot /**
11535718399fSFrançois Tigeot  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
11545718399fSFrançois Tigeot  * @out: drm_display_mode to return to the user
11555718399fSFrançois Tigeot  * @in: drm_mode_modeinfo to use
11565718399fSFrançois Tigeot  *
11575718399fSFrançois Tigeot  * LOCKING:
11585718399fSFrançois Tigeot  * None.
11595718399fSFrançois Tigeot  *
11605718399fSFrançois Tigeot  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
11615718399fSFrançois Tigeot  * the caller.
11625718399fSFrançois Tigeot  *
11635718399fSFrançois Tigeot  * RETURNS:
11645718399fSFrançois Tigeot  * Zero on success, errno on failure.
11655718399fSFrançois Tigeot  */
11665718399fSFrançois Tigeot static int drm_crtc_convert_umode(struct drm_display_mode *out,
11675718399fSFrançois Tigeot 				  const struct drm_mode_modeinfo *in)
11685718399fSFrançois Tigeot {
11695718399fSFrançois Tigeot 	if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1170*b5162e19SFrançois Tigeot 		return -ERANGE;
11715718399fSFrançois Tigeot 
11725718399fSFrançois Tigeot 	out->clock = in->clock;
11735718399fSFrançois Tigeot 	out->hdisplay = in->hdisplay;
11745718399fSFrançois Tigeot 	out->hsync_start = in->hsync_start;
11755718399fSFrançois Tigeot 	out->hsync_end = in->hsync_end;
11765718399fSFrançois Tigeot 	out->htotal = in->htotal;
11775718399fSFrançois Tigeot 	out->hskew = in->hskew;
11785718399fSFrançois Tigeot 	out->vdisplay = in->vdisplay;
11795718399fSFrançois Tigeot 	out->vsync_start = in->vsync_start;
11805718399fSFrançois Tigeot 	out->vsync_end = in->vsync_end;
11815718399fSFrançois Tigeot 	out->vtotal = in->vtotal;
11825718399fSFrançois Tigeot 	out->vscan = in->vscan;
11835718399fSFrançois Tigeot 	out->vrefresh = in->vrefresh;
11845718399fSFrançois Tigeot 	out->flags = in->flags;
11855718399fSFrançois Tigeot 	out->type = in->type;
11865718399fSFrançois Tigeot 	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
11875718399fSFrançois Tigeot 	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
11885718399fSFrançois Tigeot 
11895718399fSFrançois Tigeot 	return 0;
11905718399fSFrançois Tigeot }
11915718399fSFrançois Tigeot 
11925718399fSFrançois Tigeot /**
11935718399fSFrançois Tigeot  * drm_mode_getresources - get graphics configuration
11945718399fSFrançois Tigeot  * @inode: inode from the ioctl
11955718399fSFrançois Tigeot  * @filp: file * from the ioctl
11965718399fSFrançois Tigeot  * @cmd: cmd from ioctl
11975718399fSFrançois Tigeot  * @arg: arg from ioctl
11985718399fSFrançois Tigeot  *
11995718399fSFrançois Tigeot  * LOCKING:
12005718399fSFrançois Tigeot  * Takes mode config lock.
12015718399fSFrançois Tigeot  *
12025718399fSFrançois Tigeot  * Construct a set of configuration description structures and return
12035718399fSFrançois Tigeot  * them to the user, including CRTC, connector and framebuffer configuration.
12045718399fSFrançois Tigeot  *
12055718399fSFrançois Tigeot  * Called by the user via ioctl.
12065718399fSFrançois Tigeot  *
12075718399fSFrançois Tigeot  * RETURNS:
12085718399fSFrançois Tigeot  * Zero on success, errno on failure.
12095718399fSFrançois Tigeot  */
12105718399fSFrançois Tigeot int drm_mode_getresources(struct drm_device *dev, void *data,
12115718399fSFrançois Tigeot 			  struct drm_file *file_priv)
12125718399fSFrançois Tigeot {
12135718399fSFrançois Tigeot 	struct drm_mode_card_res *card_res = data;
12145718399fSFrançois Tigeot 	struct list_head *lh;
12155718399fSFrançois Tigeot 	struct drm_framebuffer *fb;
12165718399fSFrançois Tigeot 	struct drm_connector *connector;
12175718399fSFrançois Tigeot 	struct drm_crtc *crtc;
12185718399fSFrançois Tigeot 	struct drm_encoder *encoder;
12195718399fSFrançois Tigeot 	int ret = 0;
12205718399fSFrançois Tigeot 	int connector_count = 0;
12215718399fSFrançois Tigeot 	int crtc_count = 0;
12225718399fSFrançois Tigeot 	int fb_count = 0;
12235718399fSFrançois Tigeot 	int encoder_count = 0;
12245718399fSFrançois Tigeot 	int copied = 0, i;
12255718399fSFrançois Tigeot 	uint32_t __user *fb_id;
12265718399fSFrançois Tigeot 	uint32_t __user *crtc_id;
12275718399fSFrançois Tigeot 	uint32_t __user *connector_id;
12285718399fSFrançois Tigeot 	uint32_t __user *encoder_id;
12295718399fSFrançois Tigeot 	struct drm_mode_group *mode_group;
12305718399fSFrançois Tigeot 
12315718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1232*b5162e19SFrançois Tigeot 		return -EINVAL;
12335718399fSFrançois Tigeot 
1234af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
12355718399fSFrançois Tigeot 
12365718399fSFrançois Tigeot 	/*
12375718399fSFrançois Tigeot 	 * For the non-control nodes we need to limit the list of resources
12385718399fSFrançois Tigeot 	 * by IDs in the group list for this node
12395718399fSFrançois Tigeot 	 */
12405718399fSFrançois Tigeot 	list_for_each(lh, &file_priv->fbs)
12415718399fSFrançois Tigeot 		fb_count++;
12425718399fSFrançois Tigeot 
12435718399fSFrançois Tigeot #if 1
12445718399fSFrançois Tigeot 	mode_group = NULL; /* XXXKIB */
12455718399fSFrançois Tigeot 	if (1 || file_priv->master) {
12465718399fSFrançois Tigeot #else
12475718399fSFrançois Tigeot 	mode_group = &file_priv->masterp->minor->mode_group;
12485718399fSFrançois Tigeot 	if (file_priv->masterp->minor->type == DRM_MINOR_CONTROL) {
12495718399fSFrançois Tigeot #endif
12505718399fSFrançois Tigeot 
12515718399fSFrançois Tigeot 		list_for_each(lh, &dev->mode_config.crtc_list)
12525718399fSFrançois Tigeot 			crtc_count++;
12535718399fSFrançois Tigeot 
12545718399fSFrançois Tigeot 		list_for_each(lh, &dev->mode_config.connector_list)
12555718399fSFrançois Tigeot 			connector_count++;
12565718399fSFrançois Tigeot 
12575718399fSFrançois Tigeot 		list_for_each(lh, &dev->mode_config.encoder_list)
12585718399fSFrançois Tigeot 			encoder_count++;
12595718399fSFrançois Tigeot 	} else {
12605718399fSFrançois Tigeot 
12615718399fSFrançois Tigeot 		crtc_count = mode_group->num_crtcs;
12625718399fSFrançois Tigeot 		connector_count = mode_group->num_connectors;
12635718399fSFrançois Tigeot 		encoder_count = mode_group->num_encoders;
12645718399fSFrançois Tigeot 	}
12655718399fSFrançois Tigeot 
12665718399fSFrançois Tigeot 	card_res->max_height = dev->mode_config.max_height;
12675718399fSFrançois Tigeot 	card_res->min_height = dev->mode_config.min_height;
12685718399fSFrançois Tigeot 	card_res->max_width = dev->mode_config.max_width;
12695718399fSFrançois Tigeot 	card_res->min_width = dev->mode_config.min_width;
12705718399fSFrançois Tigeot 
12715718399fSFrançois Tigeot 	/* handle this in 4 parts */
12725718399fSFrançois Tigeot 	/* FBs */
12735718399fSFrançois Tigeot 	if (card_res->count_fbs >= fb_count) {
12745718399fSFrançois Tigeot 		copied = 0;
1275*b5162e19SFrançois Tigeot 		fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
12765718399fSFrançois Tigeot 		list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1277*b5162e19SFrançois Tigeot 			if (put_user(fb->base.id, fb_id + copied)) {
1278*b5162e19SFrançois Tigeot 				ret = -EFAULT;
12795718399fSFrançois Tigeot 				goto out;
12805718399fSFrançois Tigeot 			}
12815718399fSFrançois Tigeot 			copied++;
12825718399fSFrançois Tigeot 		}
12835718399fSFrançois Tigeot 	}
12845718399fSFrançois Tigeot 	card_res->count_fbs = fb_count;
12855718399fSFrançois Tigeot 
12865718399fSFrançois Tigeot 	/* CRTCs */
12875718399fSFrançois Tigeot 	if (card_res->count_crtcs >= crtc_count) {
12885718399fSFrançois Tigeot 		copied = 0;
1289*b5162e19SFrançois Tigeot 		crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
12905718399fSFrançois Tigeot #if 1
12915718399fSFrançois Tigeot 		if (1 || file_priv->master) {
12925718399fSFrançois Tigeot #else
12935718399fSFrançois Tigeot 		if (file_priv->masterp->minor->type == DRM_MINOR_CONTROL) {
12945718399fSFrançois Tigeot #endif
12955718399fSFrançois Tigeot 			list_for_each_entry(crtc, &dev->mode_config.crtc_list,
12965718399fSFrançois Tigeot 					    head) {
12975718399fSFrançois Tigeot 				DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1298*b5162e19SFrançois Tigeot 				if (put_user(crtc->base.id, crtc_id + copied)) {
1299*b5162e19SFrançois Tigeot 					ret = -EFAULT;
13005718399fSFrançois Tigeot 					goto out;
13015718399fSFrançois Tigeot 				}
13025718399fSFrançois Tigeot 				copied++;
13035718399fSFrançois Tigeot 			}
13045718399fSFrançois Tigeot 		} else {
13055718399fSFrançois Tigeot 			for (i = 0; i < mode_group->num_crtcs; i++) {
1306*b5162e19SFrançois Tigeot 				if (put_user(mode_group->id_list[i],
1307*b5162e19SFrançois Tigeot 					     crtc_id + copied)) {
1308*b5162e19SFrançois Tigeot 					ret = -EFAULT;
13095718399fSFrançois Tigeot 					goto out;
13105718399fSFrançois Tigeot 				}
13115718399fSFrançois Tigeot 				copied++;
13125718399fSFrançois Tigeot 			}
13135718399fSFrançois Tigeot 		}
13145718399fSFrançois Tigeot 	}
13155718399fSFrançois Tigeot 	card_res->count_crtcs = crtc_count;
13165718399fSFrançois Tigeot 
13175718399fSFrançois Tigeot 	/* Encoders */
13185718399fSFrançois Tigeot 	if (card_res->count_encoders >= encoder_count) {
13195718399fSFrançois Tigeot 		copied = 0;
1320*b5162e19SFrançois Tigeot 		encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
13215718399fSFrançois Tigeot #if 1
13225718399fSFrançois Tigeot 		if (file_priv->master) {
13235718399fSFrançois Tigeot #else
13245718399fSFrançois Tigeot 		if (file_priv->masterp->minor->type == DRM_MINOR_CONTROL) {
13255718399fSFrançois Tigeot #endif
13265718399fSFrançois Tigeot 			list_for_each_entry(encoder,
13275718399fSFrançois Tigeot 					    &dev->mode_config.encoder_list,
13285718399fSFrançois Tigeot 					    head) {
13295718399fSFrançois Tigeot 				DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
13305718399fSFrançois Tigeot 						drm_get_encoder_name(encoder));
1331*b5162e19SFrançois Tigeot 				if (put_user(encoder->base.id, encoder_id +
1332*b5162e19SFrançois Tigeot 					     copied)) {
1333*b5162e19SFrançois Tigeot 					ret = -EFAULT;
13345718399fSFrançois Tigeot 					goto out;
13355718399fSFrançois Tigeot 				}
13365718399fSFrançois Tigeot 				copied++;
13375718399fSFrançois Tigeot 			}
13385718399fSFrançois Tigeot 		} else {
1339*b5162e19SFrançois Tigeot 			for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1340*b5162e19SFrançois Tigeot 				if (put_user(mode_group->id_list[i],
1341*b5162e19SFrançois Tigeot 					     encoder_id + copied)) {
1342*b5162e19SFrançois Tigeot 					ret = -EFAULT;
13435718399fSFrançois Tigeot 					goto out;
13445718399fSFrançois Tigeot 				}
13455718399fSFrançois Tigeot 				copied++;
13465718399fSFrançois Tigeot 			}
13475718399fSFrançois Tigeot 
13485718399fSFrançois Tigeot 		}
13495718399fSFrançois Tigeot 	}
13505718399fSFrançois Tigeot 	card_res->count_encoders = encoder_count;
13515718399fSFrançois Tigeot 
13525718399fSFrançois Tigeot 	/* Connectors */
13535718399fSFrançois Tigeot 	if (card_res->count_connectors >= connector_count) {
13545718399fSFrançois Tigeot 		copied = 0;
1355*b5162e19SFrançois Tigeot 		connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
13565718399fSFrançois Tigeot #if 1
13575718399fSFrançois Tigeot 		if (file_priv->master) {
13585718399fSFrançois Tigeot #else
13595718399fSFrançois Tigeot 		if (file_priv->masterp->minor->type == DRM_MINOR_CONTROL) {
13605718399fSFrançois Tigeot #endif
13615718399fSFrançois Tigeot 			list_for_each_entry(connector,
13625718399fSFrançois Tigeot 					    &dev->mode_config.connector_list,
13635718399fSFrançois Tigeot 					    head) {
13645718399fSFrançois Tigeot 				DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
13655718399fSFrançois Tigeot 					connector->base.id,
13665718399fSFrançois Tigeot 					drm_get_connector_name(connector));
1367*b5162e19SFrançois Tigeot 				if (put_user(connector->base.id,
1368*b5162e19SFrançois Tigeot 					     connector_id + copied)) {
1369*b5162e19SFrançois Tigeot 					ret = -EFAULT;
13705718399fSFrançois Tigeot 					goto out;
13715718399fSFrançois Tigeot 				}
13725718399fSFrançois Tigeot 				copied++;
13735718399fSFrançois Tigeot 			}
13745718399fSFrançois Tigeot 		} else {
13755718399fSFrançois Tigeot 			int start = mode_group->num_crtcs +
13765718399fSFrançois Tigeot 				mode_group->num_encoders;
13775718399fSFrançois Tigeot 			for (i = start; i < start + mode_group->num_connectors; i++) {
1378*b5162e19SFrançois Tigeot 				if (put_user(mode_group->id_list[i],
1379*b5162e19SFrançois Tigeot 					     connector_id + copied)) {
1380*b5162e19SFrançois Tigeot 					ret = -EFAULT;
13815718399fSFrançois Tigeot 					goto out;
13825718399fSFrançois Tigeot 				}
13835718399fSFrançois Tigeot 				copied++;
13845718399fSFrançois Tigeot 			}
13855718399fSFrançois Tigeot 		}
13865718399fSFrançois Tigeot 	}
13875718399fSFrançois Tigeot 	card_res->count_connectors = connector_count;
13885718399fSFrançois Tigeot 
13895718399fSFrançois Tigeot 	DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
13905718399fSFrançois Tigeot 		  card_res->count_connectors, card_res->count_encoders);
13915718399fSFrançois Tigeot 
13925718399fSFrançois Tigeot out:
1393af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
13945718399fSFrançois Tigeot 	return ret;
13955718399fSFrançois Tigeot }
13965718399fSFrançois Tigeot 
13975718399fSFrançois Tigeot /**
13985718399fSFrançois Tigeot  * drm_mode_getcrtc - get CRTC configuration
13995718399fSFrançois Tigeot  * @inode: inode from the ioctl
14005718399fSFrançois Tigeot  * @filp: file * from the ioctl
14015718399fSFrançois Tigeot  * @cmd: cmd from ioctl
14025718399fSFrançois Tigeot  * @arg: arg from ioctl
14035718399fSFrançois Tigeot  *
14045718399fSFrançois Tigeot  * LOCKING:
14055718399fSFrançois Tigeot  * Takes mode config lock.
14065718399fSFrançois Tigeot  *
14075718399fSFrançois Tigeot  * Construct a CRTC configuration structure to return to the user.
14085718399fSFrançois Tigeot  *
14095718399fSFrançois Tigeot  * Called by the user via ioctl.
14105718399fSFrançois Tigeot  *
14115718399fSFrançois Tigeot  * RETURNS:
14125718399fSFrançois Tigeot  * Zero on success, errno on failure.
14135718399fSFrançois Tigeot  */
14145718399fSFrançois Tigeot int drm_mode_getcrtc(struct drm_device *dev,
14155718399fSFrançois Tigeot 		     void *data, struct drm_file *file_priv)
14165718399fSFrançois Tigeot {
14175718399fSFrançois Tigeot 	struct drm_mode_crtc *crtc_resp = data;
14185718399fSFrançois Tigeot 	struct drm_crtc *crtc;
14195718399fSFrançois Tigeot 	struct drm_mode_object *obj;
14205718399fSFrançois Tigeot 	int ret = 0;
14215718399fSFrançois Tigeot 
14225718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1423*b5162e19SFrançois Tigeot 		return -EINVAL;
14245718399fSFrançois Tigeot 
1425af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
14265718399fSFrançois Tigeot 
14275718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
14285718399fSFrançois Tigeot 				   DRM_MODE_OBJECT_CRTC);
14295718399fSFrançois Tigeot 	if (!obj) {
1430*b5162e19SFrançois Tigeot 		ret = -EINVAL;
14315718399fSFrançois Tigeot 		goto out;
14325718399fSFrançois Tigeot 	}
14335718399fSFrançois Tigeot 	crtc = obj_to_crtc(obj);
14345718399fSFrançois Tigeot 
14355718399fSFrançois Tigeot 	crtc_resp->x = crtc->x;
14365718399fSFrançois Tigeot 	crtc_resp->y = crtc->y;
14375718399fSFrançois Tigeot 	crtc_resp->gamma_size = crtc->gamma_size;
14385718399fSFrançois Tigeot 	if (crtc->fb)
14395718399fSFrançois Tigeot 		crtc_resp->fb_id = crtc->fb->base.id;
14405718399fSFrançois Tigeot 	else
14415718399fSFrançois Tigeot 		crtc_resp->fb_id = 0;
14425718399fSFrançois Tigeot 
14435718399fSFrançois Tigeot 	if (crtc->enabled) {
14445718399fSFrançois Tigeot 
14455718399fSFrançois Tigeot 		drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
14465718399fSFrançois Tigeot 		crtc_resp->mode_valid = 1;
14475718399fSFrançois Tigeot 
14485718399fSFrançois Tigeot 	} else {
14495718399fSFrançois Tigeot 		crtc_resp->mode_valid = 0;
14505718399fSFrançois Tigeot 	}
14515718399fSFrançois Tigeot 
14525718399fSFrançois Tigeot out:
1453af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
14545718399fSFrançois Tigeot 	return ret;
14555718399fSFrançois Tigeot }
14565718399fSFrançois Tigeot 
14575718399fSFrançois Tigeot /**
14585718399fSFrançois Tigeot  * drm_mode_getconnector - get connector configuration
14595718399fSFrançois Tigeot  * @inode: inode from the ioctl
14605718399fSFrançois Tigeot  * @filp: file * from the ioctl
14615718399fSFrançois Tigeot  * @cmd: cmd from ioctl
14625718399fSFrançois Tigeot  * @arg: arg from ioctl
14635718399fSFrançois Tigeot  *
14645718399fSFrançois Tigeot  * LOCKING:
14655718399fSFrançois Tigeot  * Takes mode config lock.
14665718399fSFrançois Tigeot  *
14675718399fSFrançois Tigeot  * Construct a connector configuration structure to return to the user.
14685718399fSFrançois Tigeot  *
14695718399fSFrançois Tigeot  * Called by the user via ioctl.
14705718399fSFrançois Tigeot  *
14715718399fSFrançois Tigeot  * RETURNS:
14725718399fSFrançois Tigeot  * Zero on success, errno on failure.
14735718399fSFrançois Tigeot  */
14745718399fSFrançois Tigeot int drm_mode_getconnector(struct drm_device *dev, void *data,
14755718399fSFrançois Tigeot 			  struct drm_file *file_priv)
14765718399fSFrançois Tigeot {
14775718399fSFrançois Tigeot 	struct drm_mode_get_connector *out_resp = data;
14785718399fSFrançois Tigeot 	struct drm_mode_object *obj;
14795718399fSFrançois Tigeot 	struct drm_connector *connector;
14805718399fSFrançois Tigeot 	struct drm_display_mode *mode;
14815718399fSFrançois Tigeot 	int mode_count = 0;
14825718399fSFrançois Tigeot 	int props_count = 0;
14835718399fSFrançois Tigeot 	int encoders_count = 0;
14845718399fSFrançois Tigeot 	int ret = 0;
14855718399fSFrançois Tigeot 	int copied = 0;
14865718399fSFrançois Tigeot 	int i;
14875718399fSFrançois Tigeot 	struct drm_mode_modeinfo u_mode;
14885718399fSFrançois Tigeot 	struct drm_mode_modeinfo __user *mode_ptr;
1489*b5162e19SFrançois Tigeot 	uint32_t __user *prop_ptr;
1490*b5162e19SFrançois Tigeot 	uint64_t __user *prop_values;
1491*b5162e19SFrançois Tigeot 	uint32_t __user *encoder_ptr;
14925718399fSFrançois Tigeot 
14935718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494*b5162e19SFrançois Tigeot 		return -EINVAL;
14955718399fSFrançois Tigeot 
14965718399fSFrançois Tigeot 	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
14975718399fSFrançois Tigeot 
14985718399fSFrançois Tigeot 	DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
14995718399fSFrançois Tigeot 
1500af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
15015718399fSFrançois Tigeot 
15025718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, out_resp->connector_id,
15035718399fSFrançois Tigeot 				   DRM_MODE_OBJECT_CONNECTOR);
15045718399fSFrançois Tigeot 	if (!obj) {
1505*b5162e19SFrançois Tigeot 		ret = -EINVAL;
15065718399fSFrançois Tigeot 		goto out;
15075718399fSFrançois Tigeot 	}
15085718399fSFrançois Tigeot 	connector = obj_to_connector(obj);
15095718399fSFrançois Tigeot 
1510*b5162e19SFrançois Tigeot 	props_count = connector->properties.count;
15115718399fSFrançois Tigeot 
15125718399fSFrançois Tigeot 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
15135718399fSFrançois Tigeot 		if (connector->encoder_ids[i] != 0) {
15145718399fSFrançois Tigeot 			encoders_count++;
15155718399fSFrançois Tigeot 		}
15165718399fSFrançois Tigeot 	}
15175718399fSFrançois Tigeot 
15185718399fSFrançois Tigeot 	if (out_resp->count_modes == 0) {
15195718399fSFrançois Tigeot 		connector->funcs->fill_modes(connector,
15205718399fSFrançois Tigeot 					     dev->mode_config.max_width,
15215718399fSFrançois Tigeot 					     dev->mode_config.max_height);
15225718399fSFrançois Tigeot 	}
15235718399fSFrançois Tigeot 
15245718399fSFrançois Tigeot 	/* delayed so we get modes regardless of pre-fill_modes state */
15255718399fSFrançois Tigeot 	list_for_each_entry(mode, &connector->modes, head)
15265718399fSFrançois Tigeot 		mode_count++;
15275718399fSFrançois Tigeot 
15285718399fSFrançois Tigeot 	out_resp->connector_id = connector->base.id;
15295718399fSFrançois Tigeot 	out_resp->connector_type = connector->connector_type;
15305718399fSFrançois Tigeot 	out_resp->connector_type_id = connector->connector_type_id;
15315718399fSFrançois Tigeot 	out_resp->mm_width = connector->display_info.width_mm;
15325718399fSFrançois Tigeot 	out_resp->mm_height = connector->display_info.height_mm;
15335718399fSFrançois Tigeot 	out_resp->subpixel = connector->display_info.subpixel_order;
15345718399fSFrançois Tigeot 	out_resp->connection = connector->status;
15355718399fSFrançois Tigeot 	if (connector->encoder)
15365718399fSFrançois Tigeot 		out_resp->encoder_id = connector->encoder->base.id;
15375718399fSFrançois Tigeot 	else
15385718399fSFrançois Tigeot 		out_resp->encoder_id = 0;
15395718399fSFrançois Tigeot 
15405718399fSFrançois Tigeot 	/*
15415718399fSFrançois Tigeot 	 * This ioctl is called twice, once to determine how much space is
15425718399fSFrançois Tigeot 	 * needed, and the 2nd time to fill it.
15435718399fSFrançois Tigeot 	 */
15445718399fSFrançois Tigeot 	if ((out_resp->count_modes >= mode_count) && mode_count) {
15455718399fSFrançois Tigeot 		copied = 0;
1546*b5162e19SFrançois Tigeot 		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
15475718399fSFrançois Tigeot 		list_for_each_entry(mode, &connector->modes, head) {
15485718399fSFrançois Tigeot 			drm_crtc_convert_to_umode(&u_mode, mode);
1549*b5162e19SFrançois Tigeot 			if (copy_to_user(mode_ptr + copied,
1550*b5162e19SFrançois Tigeot 					 &u_mode, sizeof(u_mode))) {
1551*b5162e19SFrançois Tigeot 				ret = -EFAULT;
15525718399fSFrançois Tigeot 				goto out;
15535718399fSFrançois Tigeot 			}
15545718399fSFrançois Tigeot 			copied++;
15555718399fSFrançois Tigeot 		}
15565718399fSFrançois Tigeot 	}
15575718399fSFrançois Tigeot 	out_resp->count_modes = mode_count;
15585718399fSFrançois Tigeot 
15595718399fSFrançois Tigeot 	if ((out_resp->count_props >= props_count) && props_count) {
15605718399fSFrançois Tigeot 		copied = 0;
1561*b5162e19SFrançois Tigeot 		prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1562*b5162e19SFrançois Tigeot 		prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1563*b5162e19SFrançois Tigeot 		for (i = 0; i < connector->properties.count; i++) {
1564*b5162e19SFrançois Tigeot 			if (put_user(connector->properties.ids[i],
1565*b5162e19SFrançois Tigeot 				     prop_ptr + copied)) {
1566*b5162e19SFrançois Tigeot 				ret = -EFAULT;
15675718399fSFrançois Tigeot 				goto out;
15685718399fSFrançois Tigeot 			}
15695718399fSFrançois Tigeot 
1570*b5162e19SFrançois Tigeot 			if (put_user(connector->properties.values[i],
1571*b5162e19SFrançois Tigeot 				     prop_values + copied)) {
1572*b5162e19SFrançois Tigeot 				ret = -EFAULT;
15735718399fSFrançois Tigeot 				goto out;
15745718399fSFrançois Tigeot 			}
15755718399fSFrançois Tigeot 			copied++;
15765718399fSFrançois Tigeot 		}
15775718399fSFrançois Tigeot 	}
15785718399fSFrançois Tigeot 	out_resp->count_props = props_count;
15795718399fSFrançois Tigeot 
15805718399fSFrançois Tigeot 	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
15815718399fSFrançois Tigeot 		copied = 0;
1582*b5162e19SFrançois Tigeot 		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
15835718399fSFrançois Tigeot 		for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
15845718399fSFrançois Tigeot 			if (connector->encoder_ids[i] != 0) {
1585*b5162e19SFrançois Tigeot 				if (put_user(connector->encoder_ids[i],
1586*b5162e19SFrançois Tigeot 					     encoder_ptr + copied)) {
1587*b5162e19SFrançois Tigeot 					ret = -EFAULT;
15885718399fSFrançois Tigeot 					goto out;
15895718399fSFrançois Tigeot 				}
15905718399fSFrançois Tigeot 				copied++;
15915718399fSFrançois Tigeot 			}
15925718399fSFrançois Tigeot 		}
15935718399fSFrançois Tigeot 	}
15945718399fSFrançois Tigeot 	out_resp->count_encoders = encoders_count;
15955718399fSFrançois Tigeot 
15965718399fSFrançois Tigeot out:
1597af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
15985718399fSFrançois Tigeot 	return ret;
15995718399fSFrançois Tigeot }
16005718399fSFrançois Tigeot 
16015718399fSFrançois Tigeot int drm_mode_getencoder(struct drm_device *dev, void *data,
16025718399fSFrançois Tigeot 			struct drm_file *file_priv)
16035718399fSFrançois Tigeot {
16045718399fSFrançois Tigeot 	struct drm_mode_get_encoder *enc_resp = data;
16055718399fSFrançois Tigeot 	struct drm_mode_object *obj;
16065718399fSFrançois Tigeot 	struct drm_encoder *encoder;
16075718399fSFrançois Tigeot 	int ret = 0;
16085718399fSFrançois Tigeot 
16095718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1610*b5162e19SFrançois Tigeot 		return -EINVAL;
16115718399fSFrançois Tigeot 
1612af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
16135718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, enc_resp->encoder_id,
16145718399fSFrançois Tigeot 				   DRM_MODE_OBJECT_ENCODER);
16155718399fSFrançois Tigeot 	if (!obj) {
1616*b5162e19SFrançois Tigeot 		ret = -EINVAL;
16175718399fSFrançois Tigeot 		goto out;
16185718399fSFrançois Tigeot 	}
16195718399fSFrançois Tigeot 	encoder = obj_to_encoder(obj);
16205718399fSFrançois Tigeot 
16215718399fSFrançois Tigeot 	if (encoder->crtc)
16225718399fSFrançois Tigeot 		enc_resp->crtc_id = encoder->crtc->base.id;
16235718399fSFrançois Tigeot 	else
16245718399fSFrançois Tigeot 		enc_resp->crtc_id = 0;
16255718399fSFrançois Tigeot 	enc_resp->encoder_type = encoder->encoder_type;
16265718399fSFrançois Tigeot 	enc_resp->encoder_id = encoder->base.id;
16275718399fSFrançois Tigeot 	enc_resp->possible_crtcs = encoder->possible_crtcs;
16285718399fSFrançois Tigeot 	enc_resp->possible_clones = encoder->possible_clones;
16295718399fSFrançois Tigeot 
16305718399fSFrançois Tigeot out:
1631af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
16325718399fSFrançois Tigeot 	return ret;
16335718399fSFrançois Tigeot }
16345718399fSFrançois Tigeot 
16355718399fSFrançois Tigeot /**
16365718399fSFrançois Tigeot  * drm_mode_getplane_res - get plane info
16375718399fSFrançois Tigeot  * @dev: DRM device
16385718399fSFrançois Tigeot  * @data: ioctl data
16395718399fSFrançois Tigeot  * @file_priv: DRM file info
16405718399fSFrançois Tigeot  *
16415718399fSFrançois Tigeot  * LOCKING:
16425718399fSFrançois Tigeot  * Takes mode config lock.
16435718399fSFrançois Tigeot  *
16445718399fSFrançois Tigeot  * Return an plane count and set of IDs.
16455718399fSFrançois Tigeot  */
16465718399fSFrançois Tigeot int drm_mode_getplane_res(struct drm_device *dev, void *data,
16475718399fSFrançois Tigeot 			    struct drm_file *file_priv)
16485718399fSFrançois Tigeot {
16495718399fSFrançois Tigeot 	struct drm_mode_get_plane_res *plane_resp = data;
16505718399fSFrançois Tigeot 	struct drm_mode_config *config;
16515718399fSFrançois Tigeot 	struct drm_plane *plane;
1652*b5162e19SFrançois Tigeot 	uint32_t __user *plane_ptr;
16535718399fSFrançois Tigeot 	int copied = 0, ret = 0;
16545718399fSFrançois Tigeot 
16555718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1656*b5162e19SFrançois Tigeot 		return -EINVAL;
16575718399fSFrançois Tigeot 
1658af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
16595718399fSFrançois Tigeot 	config = &dev->mode_config;
16605718399fSFrançois Tigeot 
16615718399fSFrançois Tigeot 	/*
16625718399fSFrançois Tigeot 	 * This ioctl is called twice, once to determine how much space is
16635718399fSFrançois Tigeot 	 * needed, and the 2nd time to fill it.
16645718399fSFrançois Tigeot 	 */
16655718399fSFrançois Tigeot 	if (config->num_plane &&
16665718399fSFrançois Tigeot 	    (plane_resp->count_planes >= config->num_plane)) {
1667*b5162e19SFrançois Tigeot 		plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
16685718399fSFrançois Tigeot 
16695718399fSFrançois Tigeot 		list_for_each_entry(plane, &config->plane_list, head) {
1670*b5162e19SFrançois Tigeot 			if (put_user(plane->base.id, plane_ptr + copied)) {
1671*b5162e19SFrançois Tigeot 				ret = -EFAULT;
16725718399fSFrançois Tigeot 				goto out;
16735718399fSFrançois Tigeot 			}
16745718399fSFrançois Tigeot 			copied++;
16755718399fSFrançois Tigeot 		}
16765718399fSFrançois Tigeot 	}
16775718399fSFrançois Tigeot 	plane_resp->count_planes = config->num_plane;
16785718399fSFrançois Tigeot 
16795718399fSFrançois Tigeot out:
1680af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
16815718399fSFrançois Tigeot 	return ret;
16825718399fSFrançois Tigeot }
16835718399fSFrançois Tigeot 
16845718399fSFrançois Tigeot /**
16855718399fSFrançois Tigeot  * drm_mode_getplane - get plane info
16865718399fSFrançois Tigeot  * @dev: DRM device
16875718399fSFrançois Tigeot  * @data: ioctl data
16885718399fSFrançois Tigeot  * @file_priv: DRM file info
16895718399fSFrançois Tigeot  *
16905718399fSFrançois Tigeot  * LOCKING:
16915718399fSFrançois Tigeot  * Takes mode config lock.
16925718399fSFrançois Tigeot  *
16935718399fSFrançois Tigeot  * Return plane info, including formats supported, gamma size, any
16945718399fSFrançois Tigeot  * current fb, etc.
16955718399fSFrançois Tigeot  */
16965718399fSFrançois Tigeot int drm_mode_getplane(struct drm_device *dev, void *data,
16975718399fSFrançois Tigeot 			struct drm_file *file_priv)
16985718399fSFrançois Tigeot {
16995718399fSFrançois Tigeot 	struct drm_mode_get_plane *plane_resp = data;
17005718399fSFrançois Tigeot 	struct drm_mode_object *obj;
17015718399fSFrançois Tigeot 	struct drm_plane *plane;
1702*b5162e19SFrançois Tigeot 	uint32_t __user *format_ptr;
17035718399fSFrançois Tigeot 	int ret = 0;
17045718399fSFrançois Tigeot 
17055718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1706*b5162e19SFrançois Tigeot 		return -EINVAL;
17075718399fSFrançois Tigeot 
1708af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
17095718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, plane_resp->plane_id,
17105718399fSFrançois Tigeot 				   DRM_MODE_OBJECT_PLANE);
17115718399fSFrançois Tigeot 	if (!obj) {
1712*b5162e19SFrançois Tigeot 		ret = -ENOENT;
17135718399fSFrançois Tigeot 		goto out;
17145718399fSFrançois Tigeot 	}
17155718399fSFrançois Tigeot 	plane = obj_to_plane(obj);
17165718399fSFrançois Tigeot 
17175718399fSFrançois Tigeot 	if (plane->crtc)
17185718399fSFrançois Tigeot 		plane_resp->crtc_id = plane->crtc->base.id;
17195718399fSFrançois Tigeot 	else
17205718399fSFrançois Tigeot 		plane_resp->crtc_id = 0;
17215718399fSFrançois Tigeot 
17225718399fSFrançois Tigeot 	if (plane->fb)
17235718399fSFrançois Tigeot 		plane_resp->fb_id = plane->fb->base.id;
17245718399fSFrançois Tigeot 	else
17255718399fSFrançois Tigeot 		plane_resp->fb_id = 0;
17265718399fSFrançois Tigeot 
17275718399fSFrançois Tigeot 	plane_resp->plane_id = plane->base.id;
17285718399fSFrançois Tigeot 	plane_resp->possible_crtcs = plane->possible_crtcs;
17295718399fSFrançois Tigeot 	plane_resp->gamma_size = plane->gamma_size;
17305718399fSFrançois Tigeot 
17315718399fSFrançois Tigeot 	/*
17325718399fSFrançois Tigeot 	 * This ioctl is called twice, once to determine how much space is
17335718399fSFrançois Tigeot 	 * needed, and the 2nd time to fill it.
17345718399fSFrançois Tigeot 	 */
17355718399fSFrançois Tigeot 	if (plane->format_count &&
17365718399fSFrançois Tigeot 	    (plane_resp->count_format_types >= plane->format_count)) {
1737*b5162e19SFrançois Tigeot 		format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1738*b5162e19SFrançois Tigeot 		if (copy_to_user(format_ptr,
17395718399fSFrançois Tigeot 				 plane->format_types,
17405718399fSFrançois Tigeot 				 sizeof(uint32_t) * plane->format_count)) {
1741*b5162e19SFrançois Tigeot 			ret = -EFAULT;
17425718399fSFrançois Tigeot 			goto out;
17435718399fSFrançois Tigeot 		}
17445718399fSFrançois Tigeot 	}
17455718399fSFrançois Tigeot 	plane_resp->count_format_types = plane->format_count;
17465718399fSFrançois Tigeot 
17475718399fSFrançois Tigeot out:
1748af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
17495718399fSFrançois Tigeot 	return ret;
17505718399fSFrançois Tigeot }
17515718399fSFrançois Tigeot 
17525718399fSFrançois Tigeot /**
17535718399fSFrançois Tigeot  * drm_mode_setplane - set up or tear down an plane
17545718399fSFrançois Tigeot  * @dev: DRM device
17555718399fSFrançois Tigeot  * @data: ioctl data*
17565718399fSFrançois Tigeot  * @file_prive: DRM file info
17575718399fSFrançois Tigeot  *
17585718399fSFrançois Tigeot  * LOCKING:
17595718399fSFrançois Tigeot  * Takes mode config lock.
17605718399fSFrançois Tigeot  *
17615718399fSFrançois Tigeot  * Set plane info, including placement, fb, scaling, and other factors.
17625718399fSFrançois Tigeot  * Or pass a NULL fb to disable.
17635718399fSFrançois Tigeot  */
17645718399fSFrançois Tigeot int drm_mode_setplane(struct drm_device *dev, void *data,
17655718399fSFrançois Tigeot 			struct drm_file *file_priv)
17665718399fSFrançois Tigeot {
17675718399fSFrançois Tigeot 	struct drm_mode_set_plane *plane_req = data;
17685718399fSFrançois Tigeot 	struct drm_mode_object *obj;
17695718399fSFrançois Tigeot 	struct drm_plane *plane;
17705718399fSFrançois Tigeot 	struct drm_crtc *crtc;
17715718399fSFrançois Tigeot 	struct drm_framebuffer *fb;
17725718399fSFrançois Tigeot 	int ret = 0;
17735718399fSFrançois Tigeot 	unsigned int fb_width, fb_height;
17745718399fSFrançois Tigeot 	int i;
17755718399fSFrançois Tigeot 
17765718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1777*b5162e19SFrançois Tigeot 		return -EINVAL;
17785718399fSFrançois Tigeot 
1779af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
17805718399fSFrançois Tigeot 
17815718399fSFrançois Tigeot 	/*
17825718399fSFrançois Tigeot 	 * First, find the plane, crtc, and fb objects.  If not available,
17835718399fSFrançois Tigeot 	 * we don't bother to call the driver.
17845718399fSFrançois Tigeot 	 */
17855718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, plane_req->plane_id,
17865718399fSFrançois Tigeot 				   DRM_MODE_OBJECT_PLANE);
17875718399fSFrançois Tigeot 	if (!obj) {
17885718399fSFrançois Tigeot 		DRM_DEBUG_KMS("Unknown plane ID %d\n",
17895718399fSFrançois Tigeot 			      plane_req->plane_id);
1790*b5162e19SFrançois Tigeot 		ret = -ENOENT;
17915718399fSFrançois Tigeot 		goto out;
17925718399fSFrançois Tigeot 	}
17935718399fSFrançois Tigeot 	plane = obj_to_plane(obj);
17945718399fSFrançois Tigeot 
17955718399fSFrançois Tigeot 	/* No fb means shut it down */
17965718399fSFrançois Tigeot 	if (!plane_req->fb_id) {
17975718399fSFrançois Tigeot 		plane->funcs->disable_plane(plane);
17985718399fSFrançois Tigeot 		plane->crtc = NULL;
17995718399fSFrançois Tigeot 		plane->fb = NULL;
18005718399fSFrançois Tigeot 		goto out;
18015718399fSFrançois Tigeot 	}
18025718399fSFrançois Tigeot 
18035718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, plane_req->crtc_id,
18045718399fSFrançois Tigeot 				   DRM_MODE_OBJECT_CRTC);
18055718399fSFrançois Tigeot 	if (!obj) {
18065718399fSFrançois Tigeot 		DRM_DEBUG_KMS("Unknown crtc ID %d\n",
18075718399fSFrançois Tigeot 			      plane_req->crtc_id);
1808*b5162e19SFrançois Tigeot 		ret = -ENOENT;
18095718399fSFrançois Tigeot 		goto out;
18105718399fSFrançois Tigeot 	}
18115718399fSFrançois Tigeot 	crtc = obj_to_crtc(obj);
18125718399fSFrançois Tigeot 
18135718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, plane_req->fb_id,
18145718399fSFrançois Tigeot 				   DRM_MODE_OBJECT_FB);
18155718399fSFrançois Tigeot 	if (!obj) {
18165718399fSFrançois Tigeot 		DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
18175718399fSFrançois Tigeot 			      plane_req->fb_id);
1818*b5162e19SFrançois Tigeot 		ret = -ENOENT;
18195718399fSFrançois Tigeot 		goto out;
18205718399fSFrançois Tigeot 	}
18215718399fSFrançois Tigeot 	fb = obj_to_fb(obj);
18225718399fSFrançois Tigeot 
18235718399fSFrançois Tigeot 	/* Check whether this plane supports the fb pixel format. */
18245718399fSFrançois Tigeot 	for (i = 0; i < plane->format_count; i++)
18255718399fSFrançois Tigeot 		if (fb->pixel_format == plane->format_types[i])
18265718399fSFrançois Tigeot 			break;
18275718399fSFrançois Tigeot 	if (i == plane->format_count) {
18285718399fSFrançois Tigeot 		DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1829*b5162e19SFrançois Tigeot 		ret = -EINVAL;
18305718399fSFrançois Tigeot 		goto out;
18315718399fSFrançois Tigeot 	}
18325718399fSFrançois Tigeot 
18335718399fSFrançois Tigeot 	fb_width = fb->width << 16;
18345718399fSFrançois Tigeot 	fb_height = fb->height << 16;
18355718399fSFrançois Tigeot 
18365718399fSFrançois Tigeot 	/* Make sure source coordinates are inside the fb. */
18375718399fSFrançois Tigeot 	if (plane_req->src_w > fb_width ||
18385718399fSFrançois Tigeot 	    plane_req->src_x > fb_width - plane_req->src_w ||
18395718399fSFrançois Tigeot 	    plane_req->src_h > fb_height ||
18405718399fSFrançois Tigeot 	    plane_req->src_y > fb_height - plane_req->src_h) {
18415718399fSFrançois Tigeot 		DRM_DEBUG_KMS("Invalid source coordinates "
18425718399fSFrançois Tigeot 			      "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
18435718399fSFrançois Tigeot 			      plane_req->src_w >> 16,
18445718399fSFrançois Tigeot 			      ((plane_req->src_w & 0xffff) * 15625) >> 10,
18455718399fSFrançois Tigeot 			      plane_req->src_h >> 16,
18465718399fSFrançois Tigeot 			      ((plane_req->src_h & 0xffff) * 15625) >> 10,
18475718399fSFrançois Tigeot 			      plane_req->src_x >> 16,
18485718399fSFrançois Tigeot 			      ((plane_req->src_x & 0xffff) * 15625) >> 10,
18495718399fSFrançois Tigeot 			      plane_req->src_y >> 16,
18505718399fSFrançois Tigeot 			      ((plane_req->src_y & 0xffff) * 15625) >> 10);
1851*b5162e19SFrançois Tigeot 		ret = -ENOSPC;
18525718399fSFrançois Tigeot 		goto out;
18535718399fSFrançois Tigeot 	}
18545718399fSFrançois Tigeot 
18555718399fSFrançois Tigeot 	/* Give drivers some help against integer overflows */
18565718399fSFrançois Tigeot 	if (plane_req->crtc_w > INT_MAX ||
18575718399fSFrançois Tigeot 	    plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
18585718399fSFrançois Tigeot 	    plane_req->crtc_h > INT_MAX ||
18595718399fSFrançois Tigeot 	    plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
18605718399fSFrançois Tigeot 		DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
18615718399fSFrançois Tigeot 			      plane_req->crtc_w, plane_req->crtc_h,
18625718399fSFrançois Tigeot 			      plane_req->crtc_x, plane_req->crtc_y);
1863*b5162e19SFrançois Tigeot 		ret = -ERANGE;
18645718399fSFrançois Tigeot 		goto out;
18655718399fSFrançois Tigeot 	}
18665718399fSFrançois Tigeot 
1867*b5162e19SFrançois Tigeot 	ret = plane->funcs->update_plane(plane, crtc, fb,
18685718399fSFrançois Tigeot 					 plane_req->crtc_x, plane_req->crtc_y,
18695718399fSFrançois Tigeot 					 plane_req->crtc_w, plane_req->crtc_h,
18705718399fSFrançois Tigeot 					 plane_req->src_x, plane_req->src_y,
18715718399fSFrançois Tigeot 					 plane_req->src_w, plane_req->src_h);
18725718399fSFrançois Tigeot 	if (!ret) {
18735718399fSFrançois Tigeot 		plane->crtc = crtc;
18745718399fSFrançois Tigeot 		plane->fb = fb;
18755718399fSFrançois Tigeot 	}
18765718399fSFrançois Tigeot 
18775718399fSFrançois Tigeot out:
1878af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
18795718399fSFrançois Tigeot 
18805718399fSFrançois Tigeot 	return ret;
18815718399fSFrançois Tigeot }
18825718399fSFrançois Tigeot 
18835718399fSFrançois Tigeot /**
18845718399fSFrançois Tigeot  * drm_mode_setcrtc - set CRTC configuration
18855718399fSFrançois Tigeot  * @inode: inode from the ioctl
18865718399fSFrançois Tigeot  * @filp: file * from the ioctl
18875718399fSFrançois Tigeot  * @cmd: cmd from ioctl
18885718399fSFrançois Tigeot  * @arg: arg from ioctl
18895718399fSFrançois Tigeot  *
18905718399fSFrançois Tigeot  * LOCKING:
18915718399fSFrançois Tigeot  * Takes mode config lock.
18925718399fSFrançois Tigeot  *
18935718399fSFrançois Tigeot  * Build a new CRTC configuration based on user request.
18945718399fSFrançois Tigeot  *
18955718399fSFrançois Tigeot  * Called by the user via ioctl.
18965718399fSFrançois Tigeot  *
18975718399fSFrançois Tigeot  * RETURNS:
18985718399fSFrançois Tigeot  * Zero on success, errno on failure.
18995718399fSFrançois Tigeot  */
19005718399fSFrançois Tigeot int drm_mode_setcrtc(struct drm_device *dev, void *data,
19015718399fSFrançois Tigeot 		     struct drm_file *file_priv)
19025718399fSFrançois Tigeot {
19035718399fSFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
19045718399fSFrançois Tigeot 	struct drm_mode_crtc *crtc_req = data;
19055718399fSFrançois Tigeot 	struct drm_mode_object *obj;
19065718399fSFrançois Tigeot 	struct drm_crtc *crtc;
19075718399fSFrançois Tigeot 	struct drm_connector **connector_set = NULL, *connector;
19085718399fSFrançois Tigeot 	struct drm_framebuffer *fb = NULL;
19095718399fSFrançois Tigeot 	struct drm_display_mode *mode = NULL;
19105718399fSFrançois Tigeot 	struct drm_mode_set set;
1911*b5162e19SFrançois Tigeot 	uint32_t __user *set_connectors_ptr;
1912*b5162e19SFrançois Tigeot 	int ret;
19135718399fSFrançois Tigeot 	int i;
19145718399fSFrançois Tigeot 
19155718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1916*b5162e19SFrançois Tigeot 		return -EINVAL;
19175718399fSFrançois Tigeot 
19185718399fSFrançois Tigeot 	/* For some reason crtc x/y offsets are signed internally. */
19195718399fSFrançois Tigeot 	if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1920*b5162e19SFrançois Tigeot 		return -ERANGE;
19215718399fSFrançois Tigeot 
1922af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
19235718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, crtc_req->crtc_id,
19245718399fSFrançois Tigeot 				   DRM_MODE_OBJECT_CRTC);
19255718399fSFrançois Tigeot 	if (!obj) {
19265718399fSFrançois Tigeot 		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1927*b5162e19SFrançois Tigeot 		ret = -EINVAL;
19285718399fSFrançois Tigeot 		goto out;
19295718399fSFrançois Tigeot 	}
19305718399fSFrançois Tigeot 	crtc = obj_to_crtc(obj);
19315718399fSFrançois Tigeot 	DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
19325718399fSFrançois Tigeot 
19335718399fSFrançois Tigeot 	if (crtc_req->mode_valid) {
1934*b5162e19SFrançois Tigeot 		int hdisplay, vdisplay;
19355718399fSFrançois Tigeot 		/* If we have a mode we need a framebuffer. */
19365718399fSFrançois Tigeot 		/* If we pass -1, set the mode with the currently bound fb */
19375718399fSFrançois Tigeot 		if (crtc_req->fb_id == -1) {
19385718399fSFrançois Tigeot 			if (!crtc->fb) {
19395718399fSFrançois Tigeot 				DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
19405718399fSFrançois Tigeot 				ret = -EINVAL;
19415718399fSFrançois Tigeot 				goto out;
19425718399fSFrançois Tigeot 			}
19435718399fSFrançois Tigeot 			fb = crtc->fb;
19445718399fSFrançois Tigeot 		} else {
19455718399fSFrançois Tigeot 			obj = drm_mode_object_find(dev, crtc_req->fb_id,
19465718399fSFrançois Tigeot 						   DRM_MODE_OBJECT_FB);
19475718399fSFrançois Tigeot 			if (!obj) {
19485718399fSFrançois Tigeot 				DRM_DEBUG_KMS("Unknown FB ID%d\n",
19495718399fSFrançois Tigeot 						crtc_req->fb_id);
1950*b5162e19SFrançois Tigeot 				ret = -EINVAL;
19515718399fSFrançois Tigeot 				goto out;
19525718399fSFrançois Tigeot 			}
19535718399fSFrançois Tigeot 			fb = obj_to_fb(obj);
19545718399fSFrançois Tigeot 		}
19555718399fSFrançois Tigeot 
19565718399fSFrançois Tigeot 		mode = drm_mode_create(dev);
19575718399fSFrançois Tigeot 		if (!mode) {
1958*b5162e19SFrançois Tigeot 			ret = -ENOMEM;
19595718399fSFrançois Tigeot 			goto out;
19605718399fSFrançois Tigeot 		}
19615718399fSFrançois Tigeot 
19625718399fSFrançois Tigeot 		ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
19635718399fSFrançois Tigeot 		if (ret) {
19645718399fSFrançois Tigeot 			DRM_DEBUG_KMS("Invalid mode\n");
19655718399fSFrançois Tigeot 			goto out;
19665718399fSFrançois Tigeot 		}
19675718399fSFrançois Tigeot 
19685718399fSFrançois Tigeot 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
19695718399fSFrançois Tigeot 
1970*b5162e19SFrançois Tigeot 		hdisplay = mode->hdisplay;
1971*b5162e19SFrançois Tigeot 		vdisplay = mode->vdisplay;
1972*b5162e19SFrançois Tigeot 
1973*b5162e19SFrançois Tigeot 		if (crtc->invert_dimensions)
1974*b5162e19SFrançois Tigeot 			swap(hdisplay, vdisplay);
1975*b5162e19SFrançois Tigeot 
1976*b5162e19SFrançois Tigeot 		if (hdisplay > fb->width ||
1977*b5162e19SFrançois Tigeot 		    vdisplay > fb->height ||
1978*b5162e19SFrançois Tigeot 		    crtc_req->x > fb->width - hdisplay ||
1979*b5162e19SFrançois Tigeot 		    crtc_req->y > fb->height - vdisplay) {
1980*b5162e19SFrançois Tigeot 			DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1981*b5162e19SFrançois Tigeot 				      fb->width, fb->height,
1982*b5162e19SFrançois Tigeot 				      hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1983*b5162e19SFrançois Tigeot 				      crtc->invert_dimensions ? " (inverted)" : "");
1984*b5162e19SFrançois Tigeot 			ret = -ENOSPC;
19855718399fSFrançois Tigeot 			goto out;
19865718399fSFrançois Tigeot 		}
19875718399fSFrançois Tigeot 	}
19885718399fSFrançois Tigeot 
19895718399fSFrançois Tigeot 	if (crtc_req->count_connectors == 0 && mode) {
19905718399fSFrançois Tigeot 		DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1991*b5162e19SFrançois Tigeot 		ret = -EINVAL;
19925718399fSFrançois Tigeot 		goto out;
19935718399fSFrançois Tigeot 	}
19945718399fSFrançois Tigeot 
19955718399fSFrançois Tigeot 	if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
19965718399fSFrançois Tigeot 		DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
19975718399fSFrançois Tigeot 			  crtc_req->count_connectors);
1998*b5162e19SFrançois Tigeot 		ret = -EINVAL;
19995718399fSFrançois Tigeot 		goto out;
20005718399fSFrançois Tigeot 	}
20015718399fSFrançois Tigeot 
20025718399fSFrançois Tigeot 	if (crtc_req->count_connectors > 0) {
20035718399fSFrançois Tigeot 		u32 out_id;
20045718399fSFrançois Tigeot 
20055718399fSFrançois Tigeot 		/* Avoid unbounded kernel memory allocation */
20065718399fSFrançois Tigeot 		if (crtc_req->count_connectors > config->num_connector) {
2007*b5162e19SFrançois Tigeot 			ret = -EINVAL;
20085718399fSFrançois Tigeot 			goto out;
20095718399fSFrançois Tigeot 		}
20105718399fSFrançois Tigeot 
20115718399fSFrançois Tigeot 		connector_set = kmalloc(crtc_req->count_connectors *
20125718399fSFrançois Tigeot 		    sizeof(struct drm_connector *), DRM_MEM_KMS, M_WAITOK);
2013*b5162e19SFrançois Tigeot 		if (!connector_set) {
2014*b5162e19SFrançois Tigeot 			ret = -ENOMEM;
2015*b5162e19SFrançois Tigeot 			goto out;
2016*b5162e19SFrançois Tigeot 		}
20175718399fSFrançois Tigeot 
20185718399fSFrançois Tigeot 		for (i = 0; i < crtc_req->count_connectors; i++) {
2019*b5162e19SFrançois Tigeot 			set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2020*b5162e19SFrançois Tigeot 			if (get_user(out_id, &set_connectors_ptr[i])) {
2021*b5162e19SFrançois Tigeot 				ret = -EFAULT;
20225718399fSFrançois Tigeot 				goto out;
20235718399fSFrançois Tigeot 			}
20245718399fSFrançois Tigeot 
20255718399fSFrançois Tigeot 			obj = drm_mode_object_find(dev, out_id,
20265718399fSFrançois Tigeot 						   DRM_MODE_OBJECT_CONNECTOR);
20275718399fSFrançois Tigeot 			if (!obj) {
20285718399fSFrançois Tigeot 				DRM_DEBUG_KMS("Connector id %d unknown\n",
20295718399fSFrançois Tigeot 						out_id);
2030*b5162e19SFrançois Tigeot 				ret = -EINVAL;
20315718399fSFrançois Tigeot 				goto out;
20325718399fSFrançois Tigeot 			}
20335718399fSFrançois Tigeot 			connector = obj_to_connector(obj);
20345718399fSFrançois Tigeot 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
20355718399fSFrançois Tigeot 					connector->base.id,
20365718399fSFrançois Tigeot 					drm_get_connector_name(connector));
20375718399fSFrançois Tigeot 
20385718399fSFrançois Tigeot 			connector_set[i] = connector;
20395718399fSFrançois Tigeot 		}
20405718399fSFrançois Tigeot 	}
20415718399fSFrançois Tigeot 
20425718399fSFrançois Tigeot 	set.crtc = crtc;
20435718399fSFrançois Tigeot 	set.x = crtc_req->x;
20445718399fSFrançois Tigeot 	set.y = crtc_req->y;
20455718399fSFrançois Tigeot 	set.mode = mode;
20465718399fSFrançois Tigeot 	set.connectors = connector_set;
20475718399fSFrançois Tigeot 	set.num_connectors = crtc_req->count_connectors;
20485718399fSFrançois Tigeot 	set.fb = fb;
20495718399fSFrançois Tigeot 	ret = crtc->funcs->set_config(&set);
20505718399fSFrançois Tigeot 
20515718399fSFrançois Tigeot out:
20525718399fSFrançois Tigeot 	drm_free(connector_set, DRM_MEM_KMS);
20535718399fSFrançois Tigeot 	drm_mode_destroy(dev, mode);
2054af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
20555718399fSFrançois Tigeot 	return ret;
20565718399fSFrançois Tigeot }
20575718399fSFrançois Tigeot 
20585718399fSFrançois Tigeot int drm_mode_cursor_ioctl(struct drm_device *dev,
20595718399fSFrançois Tigeot 			void *data, struct drm_file *file_priv)
20605718399fSFrançois Tigeot {
20615718399fSFrançois Tigeot 	struct drm_mode_cursor *req = data;
20625718399fSFrançois Tigeot 	struct drm_mode_object *obj;
20635718399fSFrançois Tigeot 	struct drm_crtc *crtc;
20645718399fSFrançois Tigeot 	int ret = 0;
20655718399fSFrançois Tigeot 
20665718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2067*b5162e19SFrançois Tigeot 		return -EINVAL;
20685718399fSFrançois Tigeot 
2069*b5162e19SFrançois Tigeot 	if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2070*b5162e19SFrançois Tigeot 		return -EINVAL;
20715718399fSFrançois Tigeot 
2072af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
20735718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
20745718399fSFrançois Tigeot 	if (!obj) {
20755718399fSFrançois Tigeot 		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2076*b5162e19SFrançois Tigeot 		ret = -EINVAL;
20775718399fSFrançois Tigeot 		goto out;
20785718399fSFrançois Tigeot 	}
20795718399fSFrançois Tigeot 	crtc = obj_to_crtc(obj);
20805718399fSFrançois Tigeot 
20815718399fSFrançois Tigeot 	if (req->flags & DRM_MODE_CURSOR_BO) {
20825718399fSFrançois Tigeot 		if (!crtc->funcs->cursor_set) {
2083*b5162e19SFrançois Tigeot 			ret = -ENXIO;
20845718399fSFrançois Tigeot 			goto out;
20855718399fSFrançois Tigeot 		}
20865718399fSFrançois Tigeot 		/* Turns off the cursor if handle is 0 */
2087*b5162e19SFrançois Tigeot 		ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
20885718399fSFrançois Tigeot 					      req->width, req->height);
20895718399fSFrançois Tigeot 	}
20905718399fSFrançois Tigeot 
20915718399fSFrançois Tigeot 	if (req->flags & DRM_MODE_CURSOR_MOVE) {
20925718399fSFrançois Tigeot 		if (crtc->funcs->cursor_move) {
20935718399fSFrançois Tigeot 			ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
20945718399fSFrançois Tigeot 		} else {
2095*b5162e19SFrançois Tigeot 			ret = -EFAULT;
20965718399fSFrançois Tigeot 			goto out;
20975718399fSFrançois Tigeot 		}
20985718399fSFrançois Tigeot 	}
20995718399fSFrançois Tigeot out:
2100af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
21015718399fSFrançois Tigeot 	return ret;
21025718399fSFrançois Tigeot }
21035718399fSFrançois Tigeot 
21045718399fSFrançois Tigeot /* Original addfb only supported RGB formats, so figure out which one */
21055718399fSFrançois Tigeot uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
21065718399fSFrançois Tigeot {
21075718399fSFrançois Tigeot 	uint32_t fmt;
21085718399fSFrançois Tigeot 
21095718399fSFrançois Tigeot 	switch (bpp) {
21105718399fSFrançois Tigeot 	case 8:
21115718399fSFrançois Tigeot 		fmt = DRM_FORMAT_RGB332;
21125718399fSFrançois Tigeot 		break;
21135718399fSFrançois Tigeot 	case 16:
21145718399fSFrançois Tigeot 		if (depth == 15)
21155718399fSFrançois Tigeot 			fmt = DRM_FORMAT_XRGB1555;
21165718399fSFrançois Tigeot 		else
21175718399fSFrançois Tigeot 			fmt = DRM_FORMAT_RGB565;
21185718399fSFrançois Tigeot 		break;
21195718399fSFrançois Tigeot 	case 24:
21205718399fSFrançois Tigeot 		fmt = DRM_FORMAT_RGB888;
21215718399fSFrançois Tigeot 		break;
21225718399fSFrançois Tigeot 	case 32:
21235718399fSFrançois Tigeot 		if (depth == 24)
21245718399fSFrançois Tigeot 			fmt = DRM_FORMAT_XRGB8888;
21255718399fSFrançois Tigeot 		else if (depth == 30)
21265718399fSFrançois Tigeot 			fmt = DRM_FORMAT_XRGB2101010;
21275718399fSFrançois Tigeot 		else
21285718399fSFrançois Tigeot 			fmt = DRM_FORMAT_ARGB8888;
21295718399fSFrançois Tigeot 		break;
21305718399fSFrançois Tigeot 	default:
2131*b5162e19SFrançois Tigeot 		DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
21325718399fSFrançois Tigeot 		fmt = DRM_FORMAT_XRGB8888;
21335718399fSFrançois Tigeot 		break;
21345718399fSFrançois Tigeot 	}
21355718399fSFrançois Tigeot 
21365718399fSFrançois Tigeot 	return fmt;
21375718399fSFrançois Tigeot }
2138*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_legacy_fb_format);
21395718399fSFrançois Tigeot 
21405718399fSFrançois Tigeot /**
21415718399fSFrançois Tigeot  * drm_mode_addfb - add an FB to the graphics configuration
21425718399fSFrançois Tigeot  * @inode: inode from the ioctl
21435718399fSFrançois Tigeot  * @filp: file * from the ioctl
21445718399fSFrançois Tigeot  * @cmd: cmd from ioctl
21455718399fSFrançois Tigeot  * @arg: arg from ioctl
21465718399fSFrançois Tigeot  *
21475718399fSFrançois Tigeot  * LOCKING:
21485718399fSFrançois Tigeot  * Takes mode config lock.
21495718399fSFrançois Tigeot  *
21505718399fSFrançois Tigeot  * Add a new FB to the specified CRTC, given a user request.
21515718399fSFrançois Tigeot  *
21525718399fSFrançois Tigeot  * Called by the user via ioctl.
21535718399fSFrançois Tigeot  *
21545718399fSFrançois Tigeot  * RETURNS:
21555718399fSFrançois Tigeot  * Zero on success, errno on failure.
21565718399fSFrançois Tigeot  */
21575718399fSFrançois Tigeot int drm_mode_addfb(struct drm_device *dev,
21585718399fSFrançois Tigeot 		   void *data, struct drm_file *file_priv)
21595718399fSFrançois Tigeot {
21605718399fSFrançois Tigeot 	struct drm_mode_fb_cmd *or = data;
21615718399fSFrançois Tigeot 	struct drm_mode_fb_cmd2 r = {};
21625718399fSFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
21635718399fSFrançois Tigeot 	struct drm_framebuffer *fb;
21645718399fSFrançois Tigeot 	int ret = 0;
21655718399fSFrançois Tigeot 
21665718399fSFrançois Tigeot 	/* Use new struct with format internally */
21675718399fSFrançois Tigeot 	r.fb_id = or->fb_id;
21685718399fSFrançois Tigeot 	r.width = or->width;
21695718399fSFrançois Tigeot 	r.height = or->height;
21705718399fSFrançois Tigeot 	r.pitches[0] = or->pitch;
21715718399fSFrançois Tigeot 	r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
21725718399fSFrançois Tigeot 	r.handles[0] = or->handle;
21735718399fSFrançois Tigeot 
21745718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2175*b5162e19SFrançois Tigeot 		return -EINVAL;
21765718399fSFrançois Tigeot 
21775718399fSFrançois Tigeot 	if ((config->min_width > r.width) || (r.width > config->max_width))
2178*b5162e19SFrançois Tigeot 		return -EINVAL;
2179*b5162e19SFrançois Tigeot 
21805718399fSFrançois Tigeot 	if ((config->min_height > r.height) || (r.height > config->max_height))
2181*b5162e19SFrançois Tigeot 		return -EINVAL;
21825718399fSFrançois Tigeot 
2183af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
21845718399fSFrançois Tigeot 
2185*b5162e19SFrançois Tigeot 	/* TODO check buffer is sufficiently large */
2186*b5162e19SFrançois Tigeot 	/* TODO setup destructor callback */
2187*b5162e19SFrançois Tigeot 
21885718399fSFrançois Tigeot 	ret = -dev->mode_config.funcs->fb_create(dev, file_priv, &r, &fb);
21895718399fSFrançois Tigeot 	if (ret != 0) {
21905718399fSFrançois Tigeot 		DRM_ERROR("could not create framebuffer, error %d\n", ret);
21915718399fSFrançois Tigeot 		goto out;
21925718399fSFrançois Tigeot 	}
21935718399fSFrançois Tigeot 
21945718399fSFrançois Tigeot 	or->fb_id = fb->base.id;
21955718399fSFrançois Tigeot 	list_add(&fb->filp_head, &file_priv->fbs);
21965718399fSFrançois Tigeot 	DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
21975718399fSFrançois Tigeot 
21985718399fSFrançois Tigeot out:
2199af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
22005718399fSFrançois Tigeot 	return ret;
22015718399fSFrançois Tigeot }
22025718399fSFrançois Tigeot 
2203*b5162e19SFrançois Tigeot static int format_check(const struct drm_mode_fb_cmd2 *r)
22045718399fSFrançois Tigeot {
22055718399fSFrançois Tigeot 	uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
22065718399fSFrançois Tigeot 
22075718399fSFrançois Tigeot 	switch (format) {
22085718399fSFrançois Tigeot 	case DRM_FORMAT_C8:
22095718399fSFrançois Tigeot 	case DRM_FORMAT_RGB332:
22105718399fSFrançois Tigeot 	case DRM_FORMAT_BGR233:
22115718399fSFrançois Tigeot 	case DRM_FORMAT_XRGB4444:
22125718399fSFrançois Tigeot 	case DRM_FORMAT_XBGR4444:
22135718399fSFrançois Tigeot 	case DRM_FORMAT_RGBX4444:
22145718399fSFrançois Tigeot 	case DRM_FORMAT_BGRX4444:
22155718399fSFrançois Tigeot 	case DRM_FORMAT_ARGB4444:
22165718399fSFrançois Tigeot 	case DRM_FORMAT_ABGR4444:
22175718399fSFrançois Tigeot 	case DRM_FORMAT_RGBA4444:
22185718399fSFrançois Tigeot 	case DRM_FORMAT_BGRA4444:
22195718399fSFrançois Tigeot 	case DRM_FORMAT_XRGB1555:
22205718399fSFrançois Tigeot 	case DRM_FORMAT_XBGR1555:
22215718399fSFrançois Tigeot 	case DRM_FORMAT_RGBX5551:
22225718399fSFrançois Tigeot 	case DRM_FORMAT_BGRX5551:
22235718399fSFrançois Tigeot 	case DRM_FORMAT_ARGB1555:
22245718399fSFrançois Tigeot 	case DRM_FORMAT_ABGR1555:
22255718399fSFrançois Tigeot 	case DRM_FORMAT_RGBA5551:
22265718399fSFrançois Tigeot 	case DRM_FORMAT_BGRA5551:
22275718399fSFrançois Tigeot 	case DRM_FORMAT_RGB565:
22285718399fSFrançois Tigeot 	case DRM_FORMAT_BGR565:
22295718399fSFrançois Tigeot 	case DRM_FORMAT_RGB888:
22305718399fSFrançois Tigeot 	case DRM_FORMAT_BGR888:
22315718399fSFrançois Tigeot 	case DRM_FORMAT_XRGB8888:
22325718399fSFrançois Tigeot 	case DRM_FORMAT_XBGR8888:
22335718399fSFrançois Tigeot 	case DRM_FORMAT_RGBX8888:
22345718399fSFrançois Tigeot 	case DRM_FORMAT_BGRX8888:
22355718399fSFrançois Tigeot 	case DRM_FORMAT_ARGB8888:
22365718399fSFrançois Tigeot 	case DRM_FORMAT_ABGR8888:
22375718399fSFrançois Tigeot 	case DRM_FORMAT_RGBA8888:
22385718399fSFrançois Tigeot 	case DRM_FORMAT_BGRA8888:
22395718399fSFrançois Tigeot 	case DRM_FORMAT_XRGB2101010:
22405718399fSFrançois Tigeot 	case DRM_FORMAT_XBGR2101010:
22415718399fSFrançois Tigeot 	case DRM_FORMAT_RGBX1010102:
22425718399fSFrançois Tigeot 	case DRM_FORMAT_BGRX1010102:
22435718399fSFrançois Tigeot 	case DRM_FORMAT_ARGB2101010:
22445718399fSFrançois Tigeot 	case DRM_FORMAT_ABGR2101010:
22455718399fSFrançois Tigeot 	case DRM_FORMAT_RGBA1010102:
22465718399fSFrançois Tigeot 	case DRM_FORMAT_BGRA1010102:
22475718399fSFrançois Tigeot 	case DRM_FORMAT_YUYV:
22485718399fSFrançois Tigeot 	case DRM_FORMAT_YVYU:
22495718399fSFrançois Tigeot 	case DRM_FORMAT_UYVY:
22505718399fSFrançois Tigeot 	case DRM_FORMAT_VYUY:
22515718399fSFrançois Tigeot 	case DRM_FORMAT_AYUV:
22525718399fSFrançois Tigeot 	case DRM_FORMAT_NV12:
22535718399fSFrançois Tigeot 	case DRM_FORMAT_NV21:
22545718399fSFrançois Tigeot 	case DRM_FORMAT_NV16:
22555718399fSFrançois Tigeot 	case DRM_FORMAT_NV61:
2256*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV24:
2257*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV42:
22585718399fSFrançois Tigeot 	case DRM_FORMAT_YUV410:
22595718399fSFrançois Tigeot 	case DRM_FORMAT_YVU410:
22605718399fSFrançois Tigeot 	case DRM_FORMAT_YUV411:
22615718399fSFrançois Tigeot 	case DRM_FORMAT_YVU411:
22625718399fSFrançois Tigeot 	case DRM_FORMAT_YUV420:
22635718399fSFrançois Tigeot 	case DRM_FORMAT_YVU420:
22645718399fSFrançois Tigeot 	case DRM_FORMAT_YUV422:
22655718399fSFrançois Tigeot 	case DRM_FORMAT_YVU422:
22665718399fSFrançois Tigeot 	case DRM_FORMAT_YUV444:
22675718399fSFrançois Tigeot 	case DRM_FORMAT_YVU444:
22685718399fSFrançois Tigeot 		return 0;
22695718399fSFrançois Tigeot 	default:
2270*b5162e19SFrançois Tigeot 		return -EINVAL;
22715718399fSFrançois Tigeot 	}
22725718399fSFrançois Tigeot }
22735718399fSFrançois Tigeot 
2274*b5162e19SFrançois Tigeot static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2275*b5162e19SFrançois Tigeot {
2276*b5162e19SFrançois Tigeot 	int ret, hsub, vsub, num_planes, i;
2277*b5162e19SFrançois Tigeot 
2278*b5162e19SFrançois Tigeot 	ret = format_check(r);
2279*b5162e19SFrançois Tigeot 	if (ret) {
2280*b5162e19SFrançois Tigeot 		DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
2281*b5162e19SFrançois Tigeot 		return ret;
2282*b5162e19SFrançois Tigeot 	}
2283*b5162e19SFrançois Tigeot 
2284*b5162e19SFrançois Tigeot 	hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2285*b5162e19SFrançois Tigeot 	vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2286*b5162e19SFrançois Tigeot 	num_planes = drm_format_num_planes(r->pixel_format);
2287*b5162e19SFrançois Tigeot 
2288*b5162e19SFrançois Tigeot 	if (r->width == 0 || r->width % hsub) {
2289*b5162e19SFrançois Tigeot 		DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2290*b5162e19SFrançois Tigeot 		return -EINVAL;
2291*b5162e19SFrançois Tigeot 	}
2292*b5162e19SFrançois Tigeot 
2293*b5162e19SFrançois Tigeot 	if (r->height == 0 || r->height % vsub) {
2294*b5162e19SFrançois Tigeot 		DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2295*b5162e19SFrançois Tigeot 		return -EINVAL;
2296*b5162e19SFrançois Tigeot 	}
2297*b5162e19SFrançois Tigeot 
2298*b5162e19SFrançois Tigeot 	for (i = 0; i < num_planes; i++) {
2299*b5162e19SFrançois Tigeot 		unsigned int width = r->width / (i != 0 ? hsub : 1);
2300*b5162e19SFrançois Tigeot 		unsigned int height = r->height / (i != 0 ? vsub : 1);
2301*b5162e19SFrançois Tigeot 		unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2302*b5162e19SFrançois Tigeot 
2303*b5162e19SFrançois Tigeot 		if (!r->handles[i]) {
2304*b5162e19SFrançois Tigeot 			DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2305*b5162e19SFrançois Tigeot 			return -EINVAL;
2306*b5162e19SFrançois Tigeot 		}
2307*b5162e19SFrançois Tigeot 
2308*b5162e19SFrançois Tigeot 		if ((uint64_t) width * cpp > UINT_MAX)
2309*b5162e19SFrançois Tigeot 			return -ERANGE;
2310*b5162e19SFrançois Tigeot 
2311*b5162e19SFrançois Tigeot 		if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2312*b5162e19SFrançois Tigeot 			return -ERANGE;
2313*b5162e19SFrançois Tigeot 
2314*b5162e19SFrançois Tigeot 		if (r->pitches[i] < width * cpp) {
2315*b5162e19SFrançois Tigeot 			DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2316*b5162e19SFrançois Tigeot 			return -EINVAL;
2317*b5162e19SFrançois Tigeot 		}
2318*b5162e19SFrançois Tigeot 	}
2319*b5162e19SFrançois Tigeot 
2320*b5162e19SFrançois Tigeot 	return 0;
2321*b5162e19SFrançois Tigeot }
2322*b5162e19SFrançois Tigeot 
23235718399fSFrançois Tigeot /**
23245718399fSFrançois Tigeot  * drm_mode_addfb2 - add an FB to the graphics configuration
23255718399fSFrançois Tigeot  * @inode: inode from the ioctl
23265718399fSFrançois Tigeot  * @filp: file * from the ioctl
23275718399fSFrançois Tigeot  * @cmd: cmd from ioctl
23285718399fSFrançois Tigeot  * @arg: arg from ioctl
23295718399fSFrançois Tigeot  *
23305718399fSFrançois Tigeot  * LOCKING:
23315718399fSFrançois Tigeot  * Takes mode config lock.
23325718399fSFrançois Tigeot  *
23335718399fSFrançois Tigeot  * Add a new FB to the specified CRTC, given a user request with format.
23345718399fSFrançois Tigeot  *
23355718399fSFrançois Tigeot  * Called by the user via ioctl.
23365718399fSFrançois Tigeot  *
23375718399fSFrançois Tigeot  * RETURNS:
23385718399fSFrançois Tigeot  * Zero on success, errno on failure.
23395718399fSFrançois Tigeot  */
23405718399fSFrançois Tigeot int drm_mode_addfb2(struct drm_device *dev,
23415718399fSFrançois Tigeot 		    void *data, struct drm_file *file_priv)
23425718399fSFrançois Tigeot {
23435718399fSFrançois Tigeot 	struct drm_mode_fb_cmd2 *r = data;
23445718399fSFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
23455718399fSFrançois Tigeot 	struct drm_framebuffer *fb;
2346*b5162e19SFrançois Tigeot 	int ret;
23475718399fSFrançois Tigeot 
23485718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2349*b5162e19SFrançois Tigeot 		return -EINVAL;
2350*b5162e19SFrançois Tigeot 
2351*b5162e19SFrançois Tigeot 	if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2352*b5162e19SFrançois Tigeot 		DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2353*b5162e19SFrançois Tigeot 		return -EINVAL;
2354*b5162e19SFrançois Tigeot 	}
23555718399fSFrançois Tigeot 
23565718399fSFrançois Tigeot 	if ((config->min_width > r->width) || (r->width > config->max_width)) {
2357*b5162e19SFrançois Tigeot 		DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
23585718399fSFrançois Tigeot 			  r->width, config->min_width, config->max_width);
2359*b5162e19SFrançois Tigeot 		return -EINVAL;
23605718399fSFrançois Tigeot 	}
23615718399fSFrançois Tigeot 	if ((config->min_height > r->height) || (r->height > config->max_height)) {
2362*b5162e19SFrançois Tigeot 		DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
23635718399fSFrançois Tigeot 			  r->height, config->min_height, config->max_height);
2364*b5162e19SFrançois Tigeot 		return -EINVAL;
23655718399fSFrançois Tigeot 	}
23665718399fSFrançois Tigeot 
2367*b5162e19SFrançois Tigeot 	ret = framebuffer_check(r);
2368*b5162e19SFrançois Tigeot 	if (ret)
23695718399fSFrançois Tigeot 		return ret;
23705718399fSFrançois Tigeot 
2371af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
23725718399fSFrançois Tigeot 
23735718399fSFrançois Tigeot 	ret = -dev->mode_config.funcs->fb_create(dev, file_priv, r, &fb);
23745718399fSFrançois Tigeot 	if (ret != 0) {
23755718399fSFrançois Tigeot 		DRM_ERROR("could not create framebuffer, error %d\n", ret);
2376*b5162e19SFrançois Tigeot 		ret = PTR_ERR(fb);
23775718399fSFrançois Tigeot 		goto out;
23785718399fSFrançois Tigeot 	}
23795718399fSFrançois Tigeot 
23805718399fSFrançois Tigeot 	r->fb_id = fb->base.id;
23815718399fSFrançois Tigeot 	list_add(&fb->filp_head, &file_priv->fbs);
23825718399fSFrançois Tigeot 	DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
23835718399fSFrançois Tigeot 
23845718399fSFrançois Tigeot out:
2385af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
2386*b5162e19SFrançois Tigeot 	return ret;
23875718399fSFrançois Tigeot }
23885718399fSFrançois Tigeot 
23895718399fSFrançois Tigeot /**
23905718399fSFrançois Tigeot  * drm_mode_rmfb - remove an FB from the configuration
23915718399fSFrançois Tigeot  * @inode: inode from the ioctl
23925718399fSFrançois Tigeot  * @filp: file * from the ioctl
23935718399fSFrançois Tigeot  * @cmd: cmd from ioctl
23945718399fSFrançois Tigeot  * @arg: arg from ioctl
23955718399fSFrançois Tigeot  *
23965718399fSFrançois Tigeot  * LOCKING:
23975718399fSFrançois Tigeot  * Takes mode config lock.
23985718399fSFrançois Tigeot  *
23995718399fSFrançois Tigeot  * Remove the FB specified by the user.
24005718399fSFrançois Tigeot  *
24015718399fSFrançois Tigeot  * Called by the user via ioctl.
24025718399fSFrançois Tigeot  *
24035718399fSFrançois Tigeot  * RETURNS:
24045718399fSFrançois Tigeot  * Zero on success, errno on failure.
24055718399fSFrançois Tigeot  */
24065718399fSFrançois Tigeot int drm_mode_rmfb(struct drm_device *dev,
24075718399fSFrançois Tigeot 		   void *data, struct drm_file *file_priv)
24085718399fSFrançois Tigeot {
24095718399fSFrançois Tigeot 	struct drm_mode_object *obj;
24105718399fSFrançois Tigeot 	struct drm_framebuffer *fb = NULL;
24115718399fSFrançois Tigeot 	struct drm_framebuffer *fbl = NULL;
24125718399fSFrançois Tigeot 	uint32_t *id = data;
24135718399fSFrançois Tigeot 	int ret = 0;
24145718399fSFrançois Tigeot 	int found = 0;
24155718399fSFrançois Tigeot 
24165718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2417*b5162e19SFrançois Tigeot 		return -EINVAL;
24185718399fSFrançois Tigeot 
2419af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
24205718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
24215718399fSFrançois Tigeot 	/* TODO check that we really get a framebuffer back. */
24225718399fSFrançois Tigeot 	if (!obj) {
2423*b5162e19SFrançois Tigeot 		ret = -EINVAL;
24245718399fSFrançois Tigeot 		goto out;
24255718399fSFrançois Tigeot 	}
24265718399fSFrançois Tigeot 	fb = obj_to_fb(obj);
24275718399fSFrançois Tigeot 
24285718399fSFrançois Tigeot 	list_for_each_entry(fbl, &file_priv->fbs, filp_head)
24295718399fSFrançois Tigeot 		if (fb == fbl)
24305718399fSFrançois Tigeot 			found = 1;
24315718399fSFrançois Tigeot 
24325718399fSFrançois Tigeot 	if (!found) {
2433*b5162e19SFrançois Tigeot 		ret = -EINVAL;
24345718399fSFrançois Tigeot 		goto out;
24355718399fSFrançois Tigeot 	}
24365718399fSFrançois Tigeot 
2437*b5162e19SFrançois Tigeot 	drm_framebuffer_remove(fb);
24385718399fSFrançois Tigeot 
24395718399fSFrançois Tigeot out:
2440af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
24415718399fSFrançois Tigeot 	return ret;
24425718399fSFrançois Tigeot }
24435718399fSFrançois Tigeot 
24445718399fSFrançois Tigeot /**
24455718399fSFrançois Tigeot  * drm_mode_getfb - get FB info
24465718399fSFrançois Tigeot  * @inode: inode from the ioctl
24475718399fSFrançois Tigeot  * @filp: file * from the ioctl
24485718399fSFrançois Tigeot  * @cmd: cmd from ioctl
24495718399fSFrançois Tigeot  * @arg: arg from ioctl
24505718399fSFrançois Tigeot  *
24515718399fSFrançois Tigeot  * LOCKING:
24525718399fSFrançois Tigeot  * Takes mode config lock.
24535718399fSFrançois Tigeot  *
24545718399fSFrançois Tigeot  * Lookup the FB given its ID and return info about it.
24555718399fSFrançois Tigeot  *
24565718399fSFrançois Tigeot  * Called by the user via ioctl.
24575718399fSFrançois Tigeot  *
24585718399fSFrançois Tigeot  * RETURNS:
24595718399fSFrançois Tigeot  * Zero on success, errno on failure.
24605718399fSFrançois Tigeot  */
24615718399fSFrançois Tigeot int drm_mode_getfb(struct drm_device *dev,
24625718399fSFrançois Tigeot 		   void *data, struct drm_file *file_priv)
24635718399fSFrançois Tigeot {
24645718399fSFrançois Tigeot 	struct drm_mode_fb_cmd *r = data;
24655718399fSFrançois Tigeot 	struct drm_mode_object *obj;
24665718399fSFrançois Tigeot 	struct drm_framebuffer *fb;
24675718399fSFrançois Tigeot 	int ret = 0;
24685718399fSFrançois Tigeot 
24695718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2470*b5162e19SFrançois Tigeot 		return -EINVAL;
24715718399fSFrançois Tigeot 
2472af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
24735718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
24745718399fSFrançois Tigeot 	if (!obj) {
2475*b5162e19SFrançois Tigeot 		ret = -EINVAL;
24765718399fSFrançois Tigeot 		goto out;
24775718399fSFrançois Tigeot 	}
24785718399fSFrançois Tigeot 	fb = obj_to_fb(obj);
24795718399fSFrançois Tigeot 
24805718399fSFrançois Tigeot 	r->height = fb->height;
24815718399fSFrançois Tigeot 	r->width = fb->width;
24825718399fSFrançois Tigeot 	r->depth = fb->depth;
24835718399fSFrançois Tigeot 	r->bpp = fb->bits_per_pixel;
24845718399fSFrançois Tigeot 	r->pitch = fb->pitches[0];
24855718399fSFrançois Tigeot 	fb->funcs->create_handle(fb, file_priv, &r->handle);
24865718399fSFrançois Tigeot 
24875718399fSFrançois Tigeot out:
2488af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
24895718399fSFrançois Tigeot 	return ret;
24905718399fSFrançois Tigeot }
24915718399fSFrançois Tigeot 
24925718399fSFrançois Tigeot int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
24935718399fSFrançois Tigeot 			   void *data, struct drm_file *file_priv)
24945718399fSFrançois Tigeot {
24955718399fSFrançois Tigeot 	struct drm_clip_rect __user *clips_ptr;
24965718399fSFrançois Tigeot 	struct drm_clip_rect *clips = NULL;
24975718399fSFrançois Tigeot 	struct drm_mode_fb_dirty_cmd *r = data;
24985718399fSFrançois Tigeot 	struct drm_mode_object *obj;
24995718399fSFrançois Tigeot 	struct drm_framebuffer *fb;
25005718399fSFrançois Tigeot 	unsigned flags;
25015718399fSFrançois Tigeot 	int num_clips;
2502*b5162e19SFrançois Tigeot 	int ret;
25035718399fSFrançois Tigeot 
25045718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2505*b5162e19SFrançois Tigeot 		return -EINVAL;
25065718399fSFrançois Tigeot 
2507af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
25085718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
25095718399fSFrançois Tigeot 	if (!obj) {
2510*b5162e19SFrançois Tigeot 		ret = -EINVAL;
25115718399fSFrançois Tigeot 		goto out_err1;
25125718399fSFrançois Tigeot 	}
25135718399fSFrançois Tigeot 	fb = obj_to_fb(obj);
25145718399fSFrançois Tigeot 
25155718399fSFrançois Tigeot 	num_clips = r->num_clips;
2516*b5162e19SFrançois Tigeot 	clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
25175718399fSFrançois Tigeot 
25185718399fSFrançois Tigeot 	if (!num_clips != !clips_ptr) {
2519*b5162e19SFrançois Tigeot 		ret = -EINVAL;
25205718399fSFrançois Tigeot 		goto out_err1;
25215718399fSFrançois Tigeot 	}
25225718399fSFrançois Tigeot 
25235718399fSFrançois Tigeot 	flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
25245718399fSFrançois Tigeot 
25255718399fSFrançois Tigeot 	/* If userspace annotates copy, clips must come in pairs */
25265718399fSFrançois Tigeot 	if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2527*b5162e19SFrançois Tigeot 		ret = -EINVAL;
25285718399fSFrançois Tigeot 		goto out_err1;
25295718399fSFrançois Tigeot 	}
25305718399fSFrançois Tigeot 
25315718399fSFrançois Tigeot 	if (num_clips && clips_ptr) {
25325718399fSFrançois Tigeot 		if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2533*b5162e19SFrançois Tigeot 			ret = -EINVAL;
25345718399fSFrançois Tigeot 			goto out_err1;
25355718399fSFrançois Tigeot 		}
25365718399fSFrançois Tigeot 		clips = kmalloc(num_clips * sizeof(*clips), DRM_MEM_KMS,
25375718399fSFrançois Tigeot 		    M_WAITOK | M_ZERO);
2538*b5162e19SFrançois Tigeot 		if (!clips) {
2539*b5162e19SFrançois Tigeot 			ret = -ENOMEM;
2540*b5162e19SFrançois Tigeot 			goto out_err1;
2541*b5162e19SFrançois Tigeot 		}
25425718399fSFrançois Tigeot 
2543*b5162e19SFrançois Tigeot 		ret = copy_from_user(clips, clips_ptr,
2544*b5162e19SFrançois Tigeot 				     num_clips * sizeof(*clips));
2545*b5162e19SFrançois Tigeot 		if (ret) {
2546*b5162e19SFrançois Tigeot 			ret = -EFAULT;
25475718399fSFrançois Tigeot 			goto out_err2;
25485718399fSFrançois Tigeot 		}
2549*b5162e19SFrançois Tigeot 	}
25505718399fSFrançois Tigeot 
25515718399fSFrançois Tigeot 	if (fb->funcs->dirty) {
2552*b5162e19SFrançois Tigeot 		ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
25535718399fSFrançois Tigeot 				       clips, num_clips);
25545718399fSFrançois Tigeot 	} else {
2555*b5162e19SFrançois Tigeot 		ret = -ENOSYS;
25565718399fSFrançois Tigeot 		goto out_err2;
25575718399fSFrançois Tigeot 	}
25585718399fSFrançois Tigeot 
25595718399fSFrançois Tigeot out_err2:
25605718399fSFrançois Tigeot 	drm_free(clips, DRM_MEM_KMS);
25615718399fSFrançois Tigeot out_err1:
2562af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
25635718399fSFrançois Tigeot 	return ret;
25645718399fSFrançois Tigeot }
25655718399fSFrançois Tigeot 
25665718399fSFrançois Tigeot 
25675718399fSFrançois Tigeot /**
25685718399fSFrançois Tigeot  * drm_fb_release - remove and free the FBs on this file
25695718399fSFrançois Tigeot  * @filp: file * from the ioctl
25705718399fSFrançois Tigeot  *
25715718399fSFrançois Tigeot  * LOCKING:
25725718399fSFrançois Tigeot  * Takes mode config lock.
25735718399fSFrançois Tigeot  *
25745718399fSFrançois Tigeot  * Destroy all the FBs associated with @filp.
25755718399fSFrançois Tigeot  *
25765718399fSFrançois Tigeot  * Called by the user via ioctl.
25775718399fSFrançois Tigeot  *
25785718399fSFrançois Tigeot  * RETURNS:
25795718399fSFrançois Tigeot  * Zero on success, errno on failure.
25805718399fSFrançois Tigeot  */
25815718399fSFrançois Tigeot void drm_fb_release(struct drm_file *priv)
25825718399fSFrançois Tigeot {
25835718399fSFrançois Tigeot #if 1
25845718399fSFrançois Tigeot 	struct drm_device *dev = priv->dev;
25855718399fSFrançois Tigeot #else
25865718399fSFrançois Tigeot 	struct drm_device *dev = priv->minor->dev;
25875718399fSFrançois Tigeot #endif
25885718399fSFrançois Tigeot 	struct drm_framebuffer *fb, *tfb;
25895718399fSFrançois Tigeot 
2590af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
25915718399fSFrançois Tigeot 	list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2592*b5162e19SFrançois Tigeot 		drm_framebuffer_remove(fb);
25935718399fSFrançois Tigeot 	}
2594af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
25955718399fSFrançois Tigeot }
25965718399fSFrançois Tigeot 
25975718399fSFrançois Tigeot /**
25985718399fSFrançois Tigeot  * drm_mode_attachmode - add a mode to the user mode list
25995718399fSFrançois Tigeot  * @dev: DRM device
26005718399fSFrançois Tigeot  * @connector: connector to add the mode to
26015718399fSFrançois Tigeot  * @mode: mode to add
26025718399fSFrançois Tigeot  *
26035718399fSFrançois Tigeot  * Add @mode to @connector's user mode list.
26045718399fSFrançois Tigeot  */
26055718399fSFrançois Tigeot static void drm_mode_attachmode(struct drm_device *dev,
26065718399fSFrançois Tigeot 				struct drm_connector *connector,
26075718399fSFrançois Tigeot 				struct drm_display_mode *mode)
26085718399fSFrançois Tigeot {
26095718399fSFrançois Tigeot 	list_add_tail(&mode->head, &connector->user_modes);
26105718399fSFrançois Tigeot }
26115718399fSFrançois Tigeot 
26125718399fSFrançois Tigeot int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
26135718399fSFrançois Tigeot 			     const struct drm_display_mode *mode)
26145718399fSFrançois Tigeot {
26155718399fSFrançois Tigeot 	struct drm_connector *connector;
26165718399fSFrançois Tigeot 	int ret = 0;
26175718399fSFrançois Tigeot 	struct drm_display_mode *dup_mode, *next;
261828828b89SFrançois Tigeot 	LINUX_LIST_HEAD(list);
26195718399fSFrançois Tigeot 
26205718399fSFrançois Tigeot 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
26215718399fSFrançois Tigeot 		if (!connector->encoder)
26225718399fSFrançois Tigeot 			continue;
26235718399fSFrançois Tigeot 		if (connector->encoder->crtc == crtc) {
26245718399fSFrançois Tigeot 			dup_mode = drm_mode_duplicate(dev, mode);
26255718399fSFrançois Tigeot 			if (!dup_mode) {
2626*b5162e19SFrançois Tigeot 				ret = -ENOMEM;
26275718399fSFrançois Tigeot 				goto out;
26285718399fSFrançois Tigeot 			}
26295718399fSFrançois Tigeot 			list_add_tail(&dup_mode->head, &list);
26305718399fSFrançois Tigeot 		}
26315718399fSFrançois Tigeot 	}
26325718399fSFrançois Tigeot 
26335718399fSFrançois Tigeot 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
26345718399fSFrançois Tigeot 		if (!connector->encoder)
26355718399fSFrançois Tigeot 			continue;
26365718399fSFrançois Tigeot 		if (connector->encoder->crtc == crtc)
26375718399fSFrançois Tigeot 			list_move_tail(list.next, &connector->user_modes);
26385718399fSFrançois Tigeot 	}
26395718399fSFrançois Tigeot 
2640*b5162e19SFrançois Tigeot 	WARN_ON(!list_empty(&list));
26415718399fSFrançois Tigeot 
26425718399fSFrançois Tigeot  out:
26435718399fSFrançois Tigeot 	list_for_each_entry_safe(dup_mode, next, &list, head)
26445718399fSFrançois Tigeot 		drm_mode_destroy(dev, dup_mode);
26455718399fSFrançois Tigeot 
26465718399fSFrançois Tigeot 	return ret;
26475718399fSFrançois Tigeot }
2648*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_attachmode_crtc);
26495718399fSFrançois Tigeot 
26505718399fSFrançois Tigeot static int drm_mode_detachmode(struct drm_device *dev,
26515718399fSFrançois Tigeot 			       struct drm_connector *connector,
26525718399fSFrançois Tigeot 			       struct drm_display_mode *mode)
26535718399fSFrançois Tigeot {
26545718399fSFrançois Tigeot 	int found = 0;
26555718399fSFrançois Tigeot 	int ret = 0;
26565718399fSFrançois Tigeot 	struct drm_display_mode *match_mode, *t;
26575718399fSFrançois Tigeot 
26585718399fSFrançois Tigeot 	list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
26595718399fSFrançois Tigeot 		if (drm_mode_equal(match_mode, mode)) {
26605718399fSFrançois Tigeot 			list_del(&match_mode->head);
26615718399fSFrançois Tigeot 			drm_mode_destroy(dev, match_mode);
26625718399fSFrançois Tigeot 			found = 1;
26635718399fSFrançois Tigeot 			break;
26645718399fSFrançois Tigeot 		}
26655718399fSFrançois Tigeot 	}
26665718399fSFrançois Tigeot 
26675718399fSFrançois Tigeot 	if (!found)
26685718399fSFrançois Tigeot 		ret = -EINVAL;
26695718399fSFrançois Tigeot 
26705718399fSFrançois Tigeot 	return ret;
26715718399fSFrançois Tigeot }
26725718399fSFrançois Tigeot 
26735718399fSFrançois Tigeot int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
26745718399fSFrançois Tigeot {
26755718399fSFrançois Tigeot 	struct drm_connector *connector;
26765718399fSFrançois Tigeot 
26775718399fSFrançois Tigeot 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
26785718399fSFrançois Tigeot 		drm_mode_detachmode(dev, connector, mode);
26795718399fSFrançois Tigeot 	}
26805718399fSFrançois Tigeot 	return 0;
26815718399fSFrançois Tigeot }
2682*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_detachmode_crtc);
26835718399fSFrançois Tigeot 
26845718399fSFrançois Tigeot /**
26855718399fSFrançois Tigeot  * drm_fb_attachmode - Attach a user mode to an connector
26865718399fSFrançois Tigeot  * @inode: inode from the ioctl
26875718399fSFrançois Tigeot  * @filp: file * from the ioctl
26885718399fSFrançois Tigeot  * @cmd: cmd from ioctl
26895718399fSFrançois Tigeot  * @arg: arg from ioctl
26905718399fSFrançois Tigeot  *
26915718399fSFrançois Tigeot  * This attaches a user specified mode to an connector.
26925718399fSFrançois Tigeot  * Called by the user via ioctl.
26935718399fSFrançois Tigeot  *
26945718399fSFrançois Tigeot  * RETURNS:
26955718399fSFrançois Tigeot  * Zero on success, errno on failure.
26965718399fSFrançois Tigeot  */
26975718399fSFrançois Tigeot int drm_mode_attachmode_ioctl(struct drm_device *dev,
26985718399fSFrançois Tigeot 			      void *data, struct drm_file *file_priv)
26995718399fSFrançois Tigeot {
27005718399fSFrançois Tigeot 	struct drm_mode_mode_cmd *mode_cmd = data;
27015718399fSFrançois Tigeot 	struct drm_connector *connector;
27025718399fSFrançois Tigeot 	struct drm_display_mode *mode;
27035718399fSFrançois Tigeot 	struct drm_mode_object *obj;
27045718399fSFrançois Tigeot 	struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2705*b5162e19SFrançois Tigeot 	int ret;
27065718399fSFrançois Tigeot 
27075718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
27085718399fSFrançois Tigeot 		return -EINVAL;
27095718399fSFrançois Tigeot 
2710af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
27115718399fSFrançois Tigeot 
27125718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
27135718399fSFrançois Tigeot 	if (!obj) {
27145718399fSFrançois Tigeot 		ret = -EINVAL;
27155718399fSFrançois Tigeot 		goto out;
27165718399fSFrançois Tigeot 	}
27175718399fSFrançois Tigeot 	connector = obj_to_connector(obj);
27185718399fSFrançois Tigeot 
27195718399fSFrançois Tigeot 	mode = drm_mode_create(dev);
27205718399fSFrançois Tigeot 	if (!mode) {
27215718399fSFrançois Tigeot 		ret = -ENOMEM;
27225718399fSFrançois Tigeot 		goto out;
27235718399fSFrançois Tigeot 	}
27245718399fSFrançois Tigeot 
27255718399fSFrançois Tigeot 	ret = drm_crtc_convert_umode(mode, umode);
27265718399fSFrançois Tigeot 	if (ret) {
27275718399fSFrançois Tigeot 		DRM_DEBUG_KMS("Invalid mode\n");
27285718399fSFrançois Tigeot 		drm_mode_destroy(dev, mode);
27295718399fSFrançois Tigeot 		goto out;
27305718399fSFrançois Tigeot 	}
27315718399fSFrançois Tigeot 
27325718399fSFrançois Tigeot 	drm_mode_attachmode(dev, connector, mode);
27335718399fSFrançois Tigeot out:
2734af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
27355718399fSFrançois Tigeot 	return ret;
27365718399fSFrançois Tigeot }
27375718399fSFrançois Tigeot 
27385718399fSFrançois Tigeot 
27395718399fSFrançois Tigeot /**
27405718399fSFrançois Tigeot  * drm_fb_detachmode - Detach a user specified mode from an connector
27415718399fSFrançois Tigeot  * @inode: inode from the ioctl
27425718399fSFrançois Tigeot  * @filp: file * from the ioctl
27435718399fSFrançois Tigeot  * @cmd: cmd from ioctl
27445718399fSFrançois Tigeot  * @arg: arg from ioctl
27455718399fSFrançois Tigeot  *
27465718399fSFrançois Tigeot  * Called by the user via ioctl.
27475718399fSFrançois Tigeot  *
27485718399fSFrançois Tigeot  * RETURNS:
27495718399fSFrançois Tigeot  * Zero on success, errno on failure.
27505718399fSFrançois Tigeot  */
27515718399fSFrançois Tigeot int drm_mode_detachmode_ioctl(struct drm_device *dev,
27525718399fSFrançois Tigeot 			      void *data, struct drm_file *file_priv)
27535718399fSFrançois Tigeot {
27545718399fSFrançois Tigeot 	struct drm_mode_object *obj;
27555718399fSFrançois Tigeot 	struct drm_mode_mode_cmd *mode_cmd = data;
27565718399fSFrançois Tigeot 	struct drm_connector *connector;
27575718399fSFrançois Tigeot 	struct drm_display_mode mode;
27585718399fSFrançois Tigeot 	struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2759*b5162e19SFrançois Tigeot 	int ret;
27605718399fSFrançois Tigeot 
27615718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
27625718399fSFrançois Tigeot 		return -EINVAL;
27635718399fSFrançois Tigeot 
2764af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
27655718399fSFrançois Tigeot 
27665718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
27675718399fSFrançois Tigeot 	if (!obj) {
27685718399fSFrançois Tigeot 		ret = -EINVAL;
27695718399fSFrançois Tigeot 		goto out;
27705718399fSFrançois Tigeot 	}
27715718399fSFrançois Tigeot 	connector = obj_to_connector(obj);
27725718399fSFrançois Tigeot 
27735718399fSFrançois Tigeot 	ret = drm_crtc_convert_umode(&mode, umode);
27745718399fSFrançois Tigeot 	if (ret) {
27755718399fSFrançois Tigeot 		DRM_DEBUG_KMS("Invalid mode\n");
27765718399fSFrançois Tigeot 		goto out;
27775718399fSFrançois Tigeot 	}
27785718399fSFrançois Tigeot 
27795718399fSFrançois Tigeot 	ret = drm_mode_detachmode(dev, connector, &mode);
27805718399fSFrançois Tigeot out:
2781af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
27825718399fSFrançois Tigeot 	return ret;
27835718399fSFrançois Tigeot }
27845718399fSFrançois Tigeot 
27855718399fSFrançois Tigeot struct drm_property *drm_property_create(struct drm_device *dev, int flags,
27865718399fSFrançois Tigeot 					 const char *name, int num_values)
27875718399fSFrançois Tigeot {
27885718399fSFrançois Tigeot 	struct drm_property *property = NULL;
27895718399fSFrançois Tigeot 	int ret;
27905718399fSFrançois Tigeot 
27915718399fSFrançois Tigeot 	property = kmalloc(sizeof(struct drm_property), DRM_MEM_KMS,
27925718399fSFrançois Tigeot 	    M_WAITOK | M_ZERO);
2793*b5162e19SFrançois Tigeot 	if (!property)
2794*b5162e19SFrançois Tigeot 		return NULL;
27955718399fSFrançois Tigeot 
27965718399fSFrançois Tigeot 	if (num_values) {
27975718399fSFrançois Tigeot 		property->values = kmalloc(sizeof(uint64_t)*num_values, DRM_MEM_KMS,
27985718399fSFrançois Tigeot 		    M_WAITOK | M_ZERO);
2799*b5162e19SFrançois Tigeot 		if (!property->values)
2800*b5162e19SFrançois Tigeot 			goto fail;
28015718399fSFrançois Tigeot 	}
28025718399fSFrançois Tigeot 
28035718399fSFrançois Tigeot 	ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
28045718399fSFrançois Tigeot 	if (ret)
28055718399fSFrançois Tigeot 		goto fail;
2806*b5162e19SFrançois Tigeot 
28075718399fSFrançois Tigeot 	property->flags = flags;
28085718399fSFrançois Tigeot 	property->num_values = num_values;
28095718399fSFrançois Tigeot 	INIT_LIST_HEAD(&property->enum_blob_list);
28105718399fSFrançois Tigeot 
28115718399fSFrançois Tigeot 	if (name) {
28125718399fSFrançois Tigeot 		strncpy(property->name, name, DRM_PROP_NAME_LEN);
28135718399fSFrançois Tigeot 		property->name[DRM_PROP_NAME_LEN-1] = '\0';
28145718399fSFrançois Tigeot 	}
28155718399fSFrançois Tigeot 
28165718399fSFrançois Tigeot 	list_add_tail(&property->head, &dev->mode_config.property_list);
28175718399fSFrançois Tigeot 	return property;
28185718399fSFrançois Tigeot fail:
28195718399fSFrançois Tigeot 	drm_free(property->values, DRM_MEM_KMS);
28205718399fSFrançois Tigeot 	drm_free(property, DRM_MEM_KMS);
2821*b5162e19SFrançois Tigeot 	return NULL;
28225718399fSFrançois Tigeot }
2823*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_property_create);
28245718399fSFrançois Tigeot 
28255718399fSFrançois Tigeot struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
28265718399fSFrançois Tigeot 					 const char *name,
28275718399fSFrançois Tigeot 					 const struct drm_prop_enum_list *props,
28285718399fSFrançois Tigeot 					 int num_values)
28295718399fSFrançois Tigeot {
28305718399fSFrançois Tigeot 	struct drm_property *property;
28315718399fSFrançois Tigeot 	int i, ret;
28325718399fSFrançois Tigeot 
28335718399fSFrançois Tigeot 	flags |= DRM_MODE_PROP_ENUM;
28345718399fSFrançois Tigeot 
28355718399fSFrançois Tigeot 	property = drm_property_create(dev, flags, name, num_values);
28365718399fSFrançois Tigeot 	if (!property)
28375718399fSFrançois Tigeot 		return NULL;
28385718399fSFrançois Tigeot 
28395718399fSFrançois Tigeot 	for (i = 0; i < num_values; i++) {
28405718399fSFrançois Tigeot 		ret = drm_property_add_enum(property, i,
28415718399fSFrançois Tigeot 				      props[i].type,
28425718399fSFrançois Tigeot 				      props[i].name);
28435718399fSFrançois Tigeot 		if (ret) {
28445718399fSFrançois Tigeot 			drm_property_destroy(dev, property);
28455718399fSFrançois Tigeot 			return NULL;
28465718399fSFrançois Tigeot 		}
28475718399fSFrançois Tigeot 	}
28485718399fSFrançois Tigeot 
28495718399fSFrançois Tigeot 	return property;
28505718399fSFrançois Tigeot }
2851*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_property_create_enum);
2852*b5162e19SFrançois Tigeot 
2853*b5162e19SFrançois Tigeot struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2854*b5162e19SFrançois Tigeot 					 int flags, const char *name,
2855*b5162e19SFrançois Tigeot 					 const struct drm_prop_enum_list *props,
2856*b5162e19SFrançois Tigeot 					 int num_values)
2857*b5162e19SFrançois Tigeot {
2858*b5162e19SFrançois Tigeot 	struct drm_property *property;
2859*b5162e19SFrançois Tigeot 	int i, ret;
2860*b5162e19SFrançois Tigeot 
2861*b5162e19SFrançois Tigeot 	flags |= DRM_MODE_PROP_BITMASK;
2862*b5162e19SFrançois Tigeot 
2863*b5162e19SFrançois Tigeot 	property = drm_property_create(dev, flags, name, num_values);
2864*b5162e19SFrançois Tigeot 	if (!property)
2865*b5162e19SFrançois Tigeot 		return NULL;
2866*b5162e19SFrançois Tigeot 
2867*b5162e19SFrançois Tigeot 	for (i = 0; i < num_values; i++) {
2868*b5162e19SFrançois Tigeot 		ret = drm_property_add_enum(property, i,
2869*b5162e19SFrançois Tigeot 				      props[i].type,
2870*b5162e19SFrançois Tigeot 				      props[i].name);
2871*b5162e19SFrançois Tigeot 		if (ret) {
2872*b5162e19SFrançois Tigeot 			drm_property_destroy(dev, property);
2873*b5162e19SFrançois Tigeot 			return NULL;
2874*b5162e19SFrançois Tigeot 		}
2875*b5162e19SFrançois Tigeot 	}
2876*b5162e19SFrançois Tigeot 
2877*b5162e19SFrançois Tigeot 	return property;
2878*b5162e19SFrançois Tigeot }
2879*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_property_create_bitmask);
28805718399fSFrançois Tigeot 
28815718399fSFrançois Tigeot struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
28825718399fSFrançois Tigeot 					 const char *name,
28835718399fSFrançois Tigeot 					 uint64_t min, uint64_t max)
28845718399fSFrançois Tigeot {
28855718399fSFrançois Tigeot 	struct drm_property *property;
28865718399fSFrançois Tigeot 
28875718399fSFrançois Tigeot 	flags |= DRM_MODE_PROP_RANGE;
28885718399fSFrançois Tigeot 
28895718399fSFrançois Tigeot 	property = drm_property_create(dev, flags, name, 2);
28905718399fSFrançois Tigeot 	if (!property)
28915718399fSFrançois Tigeot 		return NULL;
28925718399fSFrançois Tigeot 
28935718399fSFrançois Tigeot 	property->values[0] = min;
28945718399fSFrançois Tigeot 	property->values[1] = max;
28955718399fSFrançois Tigeot 
28965718399fSFrançois Tigeot 	return property;
28975718399fSFrançois Tigeot }
2898*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_property_create_range);
28995718399fSFrançois Tigeot 
29005718399fSFrançois Tigeot int drm_property_add_enum(struct drm_property *property, int index,
29015718399fSFrançois Tigeot 			  uint64_t value, const char *name)
29025718399fSFrançois Tigeot {
29035718399fSFrançois Tigeot 	struct drm_property_enum *prop_enum;
29045718399fSFrançois Tigeot 
2905*b5162e19SFrançois Tigeot 	if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2906*b5162e19SFrançois Tigeot 		return -EINVAL;
2907*b5162e19SFrançois Tigeot 
2908*b5162e19SFrançois Tigeot 	/*
2909*b5162e19SFrançois Tigeot 	 * Bitmask enum properties have the additional constraint of values
2910*b5162e19SFrançois Tigeot 	 * from 0 to 63
2911*b5162e19SFrançois Tigeot 	 */
2912*b5162e19SFrançois Tigeot 	if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
29135718399fSFrançois Tigeot 		return -EINVAL;
29145718399fSFrançois Tigeot 
29155718399fSFrançois Tigeot 	if (!list_empty(&property->enum_blob_list)) {
29165718399fSFrançois Tigeot 		list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
29175718399fSFrançois Tigeot 			if (prop_enum->value == value) {
29185718399fSFrançois Tigeot 				strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
29195718399fSFrançois Tigeot 				prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
29205718399fSFrançois Tigeot 				return 0;
29215718399fSFrançois Tigeot 			}
29225718399fSFrançois Tigeot 		}
29235718399fSFrançois Tigeot 	}
29245718399fSFrançois Tigeot 
29255718399fSFrançois Tigeot 	prop_enum = kmalloc(sizeof(struct drm_property_enum), DRM_MEM_KMS,
29265718399fSFrançois Tigeot 	    M_WAITOK | M_ZERO);
2927*b5162e19SFrançois Tigeot 	if (!prop_enum)
2928*b5162e19SFrançois Tigeot 		return -ENOMEM;
29295718399fSFrançois Tigeot 
29305718399fSFrançois Tigeot 	strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
29315718399fSFrançois Tigeot 	prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
29325718399fSFrançois Tigeot 	prop_enum->value = value;
29335718399fSFrançois Tigeot 
29345718399fSFrançois Tigeot 	property->values[index] = value;
29355718399fSFrançois Tigeot 	list_add_tail(&prop_enum->head, &property->enum_blob_list);
29365718399fSFrançois Tigeot 	return 0;
29375718399fSFrançois Tigeot }
2938*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_property_add_enum);
29395718399fSFrançois Tigeot 
29405718399fSFrançois Tigeot void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
29415718399fSFrançois Tigeot {
29425718399fSFrançois Tigeot 	struct drm_property_enum *prop_enum, *pt;
29435718399fSFrançois Tigeot 
29445718399fSFrançois Tigeot 	list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
29455718399fSFrançois Tigeot 		list_del(&prop_enum->head);
29465718399fSFrançois Tigeot 		drm_free(prop_enum, DRM_MEM_KMS);
29475718399fSFrançois Tigeot 	}
29485718399fSFrançois Tigeot 
29495718399fSFrançois Tigeot 	if (property->num_values)
29505718399fSFrançois Tigeot 		drm_free(property->values, DRM_MEM_KMS);
29515718399fSFrançois Tigeot 	drm_mode_object_put(dev, &property->base);
29525718399fSFrançois Tigeot 	list_del(&property->head);
29535718399fSFrançois Tigeot 	drm_free(property, DRM_MEM_KMS);
29545718399fSFrançois Tigeot }
2955*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_property_destroy);
29565718399fSFrançois Tigeot 
2957*b5162e19SFrançois Tigeot void drm_object_attach_property(struct drm_mode_object *obj,
2958*b5162e19SFrançois Tigeot 				struct drm_property *property,
2959*b5162e19SFrançois Tigeot 				uint64_t init_val)
2960*b5162e19SFrançois Tigeot {
2961*b5162e19SFrançois Tigeot 	int count = obj->properties->count;
2962*b5162e19SFrançois Tigeot 
2963*b5162e19SFrançois Tigeot 	if (count == DRM_OBJECT_MAX_PROPERTY) {
2964*b5162e19SFrançois Tigeot 		WARN(1, "Failed to attach object property (type: 0x%x). Please "
2965*b5162e19SFrançois Tigeot 			"increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2966*b5162e19SFrançois Tigeot 			"you see this message on the same object type.\n",
2967*b5162e19SFrançois Tigeot 			obj->type);
2968*b5162e19SFrançois Tigeot 		return;
2969*b5162e19SFrançois Tigeot 	}
2970*b5162e19SFrançois Tigeot 
2971*b5162e19SFrançois Tigeot 	obj->properties->ids[count] = property->base.id;
2972*b5162e19SFrançois Tigeot 	obj->properties->values[count] = init_val;
2973*b5162e19SFrançois Tigeot 	obj->properties->count++;
2974*b5162e19SFrançois Tigeot }
2975*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_object_attach_property);
2976*b5162e19SFrançois Tigeot 
2977*b5162e19SFrançois Tigeot int drm_object_property_set_value(struct drm_mode_object *obj,
2978*b5162e19SFrançois Tigeot 				  struct drm_property *property, uint64_t val)
29795718399fSFrançois Tigeot {
29805718399fSFrançois Tigeot 	int i;
29815718399fSFrançois Tigeot 
2982*b5162e19SFrançois Tigeot 	for (i = 0; i < obj->properties->count; i++) {
2983*b5162e19SFrançois Tigeot 		if (obj->properties->ids[i] == property->base.id) {
2984*b5162e19SFrançois Tigeot 			obj->properties->values[i] = val;
29855718399fSFrançois Tigeot 			return 0;
29865718399fSFrançois Tigeot 		}
29875718399fSFrançois Tigeot 	}
29885718399fSFrançois Tigeot 
29895718399fSFrançois Tigeot 	return -EINVAL;
29905718399fSFrançois Tigeot }
2991*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_object_property_set_value);
29925718399fSFrançois Tigeot 
2993*b5162e19SFrançois Tigeot int drm_object_property_get_value(struct drm_mode_object *obj,
29945718399fSFrançois Tigeot 				  struct drm_property *property, uint64_t *val)
29955718399fSFrançois Tigeot {
29965718399fSFrançois Tigeot 	int i;
29975718399fSFrançois Tigeot 
2998*b5162e19SFrançois Tigeot 	for (i = 0; i < obj->properties->count; i++) {
2999*b5162e19SFrançois Tigeot 		if (obj->properties->ids[i] == property->base.id) {
3000*b5162e19SFrançois Tigeot 			*val = obj->properties->values[i];
3001*b5162e19SFrançois Tigeot 			return 0;
30025718399fSFrançois Tigeot 		}
30035718399fSFrançois Tigeot 	}
30045718399fSFrançois Tigeot 
30055718399fSFrançois Tigeot 	return -EINVAL;
30065718399fSFrançois Tigeot }
3007*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_object_property_get_value);
30085718399fSFrançois Tigeot 
30095718399fSFrançois Tigeot int drm_mode_getproperty_ioctl(struct drm_device *dev,
30105718399fSFrançois Tigeot 			       void *data, struct drm_file *file_priv)
30115718399fSFrançois Tigeot {
30125718399fSFrançois Tigeot 	struct drm_mode_object *obj;
30135718399fSFrançois Tigeot 	struct drm_mode_get_property *out_resp = data;
30145718399fSFrançois Tigeot 	struct drm_property *property;
30155718399fSFrançois Tigeot 	int enum_count = 0;
30165718399fSFrançois Tigeot 	int blob_count = 0;
30175718399fSFrançois Tigeot 	int value_count = 0;
30185718399fSFrançois Tigeot 	int ret = 0, i;
30195718399fSFrançois Tigeot 	int copied;
30205718399fSFrançois Tigeot 	struct drm_property_enum *prop_enum;
30215718399fSFrançois Tigeot 	struct drm_mode_property_enum __user *enum_ptr;
30225718399fSFrançois Tigeot 	struct drm_property_blob *prop_blob;
3023*b5162e19SFrançois Tigeot 	uint32_t __user *blob_id_ptr;
3024*b5162e19SFrançois Tigeot 	uint64_t __user *values_ptr;
3025*b5162e19SFrançois Tigeot 	uint32_t __user *blob_length_ptr;
30265718399fSFrançois Tigeot 
30275718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
30285718399fSFrançois Tigeot 		return -EINVAL;
30295718399fSFrançois Tigeot 
3030af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
30315718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
30325718399fSFrançois Tigeot 	if (!obj) {
30335718399fSFrançois Tigeot 		ret = -EINVAL;
30345718399fSFrançois Tigeot 		goto done;
30355718399fSFrançois Tigeot 	}
30365718399fSFrançois Tigeot 	property = obj_to_property(obj);
30375718399fSFrançois Tigeot 
3038*b5162e19SFrançois Tigeot 	if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
30395718399fSFrançois Tigeot 		list_for_each_entry(prop_enum, &property->enum_blob_list, head)
30405718399fSFrançois Tigeot 			enum_count++;
30415718399fSFrançois Tigeot 	} else if (property->flags & DRM_MODE_PROP_BLOB) {
30425718399fSFrançois Tigeot 		list_for_each_entry(prop_blob, &property->enum_blob_list, head)
30435718399fSFrançois Tigeot 			blob_count++;
30445718399fSFrançois Tigeot 	}
30455718399fSFrançois Tigeot 
30465718399fSFrançois Tigeot 	value_count = property->num_values;
30475718399fSFrançois Tigeot 
30485718399fSFrançois Tigeot 	strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
30495718399fSFrançois Tigeot 	out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
30505718399fSFrançois Tigeot 	out_resp->flags = property->flags;
30515718399fSFrançois Tigeot 
30525718399fSFrançois Tigeot 	if ((out_resp->count_values >= value_count) && value_count) {
3053*b5162e19SFrançois Tigeot 		values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
30545718399fSFrançois Tigeot 		for (i = 0; i < value_count; i++) {
3055*b5162e19SFrançois Tigeot 			if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
30565718399fSFrançois Tigeot 				ret = -EFAULT;
30575718399fSFrançois Tigeot 				goto done;
30585718399fSFrançois Tigeot 			}
30595718399fSFrançois Tigeot 		}
30605718399fSFrançois Tigeot 	}
30615718399fSFrançois Tigeot 	out_resp->count_values = value_count;
30625718399fSFrançois Tigeot 
3063*b5162e19SFrançois Tigeot 	if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
30645718399fSFrançois Tigeot 		if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
30655718399fSFrançois Tigeot 			copied = 0;
3066*b5162e19SFrançois Tigeot 			enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
30675718399fSFrançois Tigeot 			list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
30685718399fSFrançois Tigeot 
3069*b5162e19SFrançois Tigeot 				if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
30705718399fSFrançois Tigeot 					ret = -EFAULT;
30715718399fSFrançois Tigeot 					goto done;
30725718399fSFrançois Tigeot 				}
30735718399fSFrançois Tigeot 
3074*b5162e19SFrançois Tigeot 				if (copy_to_user(&enum_ptr[copied].name,
3075*b5162e19SFrançois Tigeot 						 &prop_enum->name, DRM_PROP_NAME_LEN)) {
30765718399fSFrançois Tigeot 					ret = -EFAULT;
30775718399fSFrançois Tigeot 					goto done;
30785718399fSFrançois Tigeot 				}
30795718399fSFrançois Tigeot 				copied++;
30805718399fSFrançois Tigeot 			}
30815718399fSFrançois Tigeot 		}
30825718399fSFrançois Tigeot 		out_resp->count_enum_blobs = enum_count;
30835718399fSFrançois Tigeot 	}
30845718399fSFrançois Tigeot 
30855718399fSFrançois Tigeot 	if (property->flags & DRM_MODE_PROP_BLOB) {
30865718399fSFrançois Tigeot 		if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
30875718399fSFrançois Tigeot 			copied = 0;
3088*b5162e19SFrançois Tigeot 			blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3089*b5162e19SFrançois Tigeot 			blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
30905718399fSFrançois Tigeot 
30915718399fSFrançois Tigeot 			list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3092*b5162e19SFrançois Tigeot 				if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
30935718399fSFrançois Tigeot 					ret = -EFAULT;
30945718399fSFrançois Tigeot 					goto done;
30955718399fSFrançois Tigeot 				}
30965718399fSFrançois Tigeot 
3097*b5162e19SFrançois Tigeot 				if (put_user(prop_blob->length, blob_length_ptr + copied)) {
30985718399fSFrançois Tigeot 					ret = -EFAULT;
30995718399fSFrançois Tigeot 					goto done;
31005718399fSFrançois Tigeot 				}
31015718399fSFrançois Tigeot 
31025718399fSFrançois Tigeot 				copied++;
31035718399fSFrançois Tigeot 			}
31045718399fSFrançois Tigeot 		}
31055718399fSFrançois Tigeot 		out_resp->count_enum_blobs = blob_count;
31065718399fSFrançois Tigeot 	}
31075718399fSFrançois Tigeot done:
3108af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
31095718399fSFrançois Tigeot 	return ret;
31105718399fSFrançois Tigeot }
31115718399fSFrançois Tigeot 
31125718399fSFrançois Tigeot static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
31135718399fSFrançois Tigeot 							  void *data)
31145718399fSFrançois Tigeot {
31155718399fSFrançois Tigeot 	struct drm_property_blob *blob;
31165718399fSFrançois Tigeot 	int ret;
31175718399fSFrançois Tigeot 
31185718399fSFrançois Tigeot 	if (!length || !data)
31195718399fSFrançois Tigeot 		return NULL;
31205718399fSFrançois Tigeot 
31215718399fSFrançois Tigeot 	blob = kmalloc(sizeof(struct drm_property_blob) + length, DRM_MEM_KMS,
31225718399fSFrançois Tigeot 	    M_WAITOK | M_ZERO);
3123*b5162e19SFrançois Tigeot 	if (!blob)
3124*b5162e19SFrançois Tigeot 		return NULL;
31255718399fSFrançois Tigeot 
31265718399fSFrançois Tigeot 	ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
31275718399fSFrançois Tigeot 	if (ret) {
31285718399fSFrançois Tigeot 		drm_free(blob, DRM_MEM_KMS);
3129*b5162e19SFrançois Tigeot 		return NULL;
31305718399fSFrançois Tigeot 	}
31315718399fSFrançois Tigeot 
31325718399fSFrançois Tigeot 	blob->length = length;
31335718399fSFrançois Tigeot 
31345718399fSFrançois Tigeot 	memcpy(blob->data, data, length);
31355718399fSFrançois Tigeot 
31365718399fSFrançois Tigeot 	list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
31375718399fSFrançois Tigeot 	return blob;
31385718399fSFrançois Tigeot }
31395718399fSFrançois Tigeot 
31405718399fSFrançois Tigeot static void drm_property_destroy_blob(struct drm_device *dev,
31415718399fSFrançois Tigeot 			       struct drm_property_blob *blob)
31425718399fSFrançois Tigeot {
31435718399fSFrançois Tigeot 	drm_mode_object_put(dev, &blob->base);
31445718399fSFrançois Tigeot 	list_del(&blob->head);
31455718399fSFrançois Tigeot 	drm_free(blob, DRM_MEM_KMS);
31465718399fSFrançois Tigeot }
31475718399fSFrançois Tigeot 
31485718399fSFrançois Tigeot int drm_mode_getblob_ioctl(struct drm_device *dev,
31495718399fSFrançois Tigeot 			   void *data, struct drm_file *file_priv)
31505718399fSFrançois Tigeot {
31515718399fSFrançois Tigeot 	struct drm_mode_object *obj;
31525718399fSFrançois Tigeot 	struct drm_mode_get_blob *out_resp = data;
31535718399fSFrançois Tigeot 	struct drm_property_blob *blob;
31545718399fSFrançois Tigeot 	int ret = 0;
3155*b5162e19SFrançois Tigeot 	void __user *blob_ptr;
31565718399fSFrançois Tigeot 
31575718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
31585718399fSFrançois Tigeot 		return -EINVAL;
31595718399fSFrançois Tigeot 
3160af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
31615718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
31625718399fSFrançois Tigeot 	if (!obj) {
31635718399fSFrançois Tigeot 		ret = -EINVAL;
31645718399fSFrançois Tigeot 		goto done;
31655718399fSFrançois Tigeot 	}
31665718399fSFrançois Tigeot 	blob = obj_to_blob(obj);
31675718399fSFrançois Tigeot 
31685718399fSFrançois Tigeot 	if (out_resp->length == blob->length) {
3169*b5162e19SFrançois Tigeot 		blob_ptr = (void __user *)(unsigned long)out_resp->data;
3170*b5162e19SFrançois Tigeot 		if (copy_to_user(blob_ptr, blob->data, blob->length)){
31715718399fSFrançois Tigeot 			ret = -EFAULT;
31725718399fSFrançois Tigeot 			goto done;
31735718399fSFrançois Tigeot 		}
31745718399fSFrançois Tigeot 	}
31755718399fSFrançois Tigeot 	out_resp->length = blob->length;
31765718399fSFrançois Tigeot 
31775718399fSFrançois Tigeot done:
3178af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
31795718399fSFrançois Tigeot 	return ret;
31805718399fSFrançois Tigeot }
31815718399fSFrançois Tigeot 
31825718399fSFrançois Tigeot int drm_mode_connector_update_edid_property(struct drm_connector *connector,
31835718399fSFrançois Tigeot 					    struct edid *edid)
31845718399fSFrançois Tigeot {
31855718399fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
3186*b5162e19SFrançois Tigeot 	int ret, size;
31875718399fSFrançois Tigeot 
31885718399fSFrançois Tigeot 	if (connector->edid_blob_ptr)
31895718399fSFrançois Tigeot 		drm_property_destroy_blob(dev, connector->edid_blob_ptr);
31905718399fSFrançois Tigeot 
31915718399fSFrançois Tigeot 	/* Delete edid, when there is none. */
31925718399fSFrançois Tigeot 	if (!edid) {
31935718399fSFrançois Tigeot 		connector->edid_blob_ptr = NULL;
3194*b5162e19SFrançois Tigeot 		ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
31955718399fSFrançois Tigeot 		return ret;
31965718399fSFrançois Tigeot 	}
31975718399fSFrançois Tigeot 
31985718399fSFrançois Tigeot 	size = EDID_LENGTH * (1 + edid->extensions);
31995718399fSFrançois Tigeot 	connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
32005718399fSFrançois Tigeot 							    size, edid);
3201*b5162e19SFrançois Tigeot 	if (!connector->edid_blob_ptr)
3202*b5162e19SFrançois Tigeot 		return -EINVAL;
32035718399fSFrançois Tigeot 
3204*b5162e19SFrançois Tigeot 	ret = drm_object_property_set_value(&connector->base,
32055718399fSFrançois Tigeot 					       dev->mode_config.edid_property,
32065718399fSFrançois Tigeot 					       connector->edid_blob_ptr->base.id);
32075718399fSFrançois Tigeot 
32085718399fSFrançois Tigeot 	return ret;
32095718399fSFrançois Tigeot }
3210*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3211*b5162e19SFrançois Tigeot 
3212*b5162e19SFrançois Tigeot static bool drm_property_change_is_valid(struct drm_property *property,
3213*b5162e19SFrançois Tigeot 					 uint64_t value)
3214*b5162e19SFrançois Tigeot {
3215*b5162e19SFrançois Tigeot 	if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3216*b5162e19SFrançois Tigeot 		return false;
3217*b5162e19SFrançois Tigeot 	if (property->flags & DRM_MODE_PROP_RANGE) {
3218*b5162e19SFrançois Tigeot 		if (value < property->values[0] || value > property->values[1])
3219*b5162e19SFrançois Tigeot 			return false;
3220*b5162e19SFrançois Tigeot 		return true;
3221*b5162e19SFrançois Tigeot 	} else if (property->flags & DRM_MODE_PROP_BITMASK) {
3222*b5162e19SFrançois Tigeot 		int i;
3223*b5162e19SFrançois Tigeot 		uint64_t valid_mask = 0;
3224*b5162e19SFrançois Tigeot 		for (i = 0; i < property->num_values; i++)
3225*b5162e19SFrançois Tigeot 			valid_mask |= (1ULL << property->values[i]);
3226*b5162e19SFrançois Tigeot 		return !(value & ~valid_mask);
3227*b5162e19SFrançois Tigeot 	} else if (property->flags & DRM_MODE_PROP_BLOB) {
3228*b5162e19SFrançois Tigeot 		/* Only the driver knows */
3229*b5162e19SFrançois Tigeot 		return true;
3230*b5162e19SFrançois Tigeot 	} else {
3231*b5162e19SFrançois Tigeot 		int i;
3232*b5162e19SFrançois Tigeot 		for (i = 0; i < property->num_values; i++)
3233*b5162e19SFrançois Tigeot 			if (property->values[i] == value)
3234*b5162e19SFrançois Tigeot 				return true;
3235*b5162e19SFrançois Tigeot 		return false;
3236*b5162e19SFrançois Tigeot 	}
3237*b5162e19SFrançois Tigeot }
32385718399fSFrançois Tigeot 
32395718399fSFrançois Tigeot int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
32405718399fSFrançois Tigeot 				       void *data, struct drm_file *file_priv)
32415718399fSFrançois Tigeot {
3242*b5162e19SFrançois Tigeot 	struct drm_mode_connector_set_property *conn_set_prop = data;
3243*b5162e19SFrançois Tigeot 	struct drm_mode_obj_set_property obj_set_prop = {
3244*b5162e19SFrançois Tigeot 		.value = conn_set_prop->value,
3245*b5162e19SFrançois Tigeot 		.prop_id = conn_set_prop->prop_id,
3246*b5162e19SFrançois Tigeot 		.obj_id = conn_set_prop->connector_id,
3247*b5162e19SFrançois Tigeot 		.obj_type = DRM_MODE_OBJECT_CONNECTOR
3248*b5162e19SFrançois Tigeot 	};
3249*b5162e19SFrançois Tigeot 
3250*b5162e19SFrançois Tigeot 	/* It does all the locking and checking we need */
3251*b5162e19SFrançois Tigeot 	return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3252*b5162e19SFrançois Tigeot }
3253*b5162e19SFrançois Tigeot 
3254*b5162e19SFrançois Tigeot static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3255*b5162e19SFrançois Tigeot 					   struct drm_property *property,
3256*b5162e19SFrançois Tigeot 					   uint64_t value)
3257*b5162e19SFrançois Tigeot {
3258*b5162e19SFrançois Tigeot 	int ret = -EINVAL;
3259*b5162e19SFrançois Tigeot 	struct drm_connector *connector = obj_to_connector(obj);
3260*b5162e19SFrançois Tigeot 
3261*b5162e19SFrançois Tigeot 	/* Do DPMS ourselves */
3262*b5162e19SFrançois Tigeot 	if (property == connector->dev->mode_config.dpms_property) {
3263*b5162e19SFrançois Tigeot 		if (connector->funcs->dpms)
3264*b5162e19SFrançois Tigeot 			(*connector->funcs->dpms)(connector, (int)value);
3265*b5162e19SFrançois Tigeot 		ret = 0;
3266*b5162e19SFrançois Tigeot 	} else if (connector->funcs->set_property)
3267*b5162e19SFrançois Tigeot 		ret = connector->funcs->set_property(connector, property, value);
3268*b5162e19SFrançois Tigeot 
3269*b5162e19SFrançois Tigeot 	/* store the property value if successful */
3270*b5162e19SFrançois Tigeot 	if (!ret)
3271*b5162e19SFrançois Tigeot 		drm_object_property_set_value(&connector->base, property, value);
3272*b5162e19SFrançois Tigeot 	return ret;
3273*b5162e19SFrançois Tigeot }
3274*b5162e19SFrançois Tigeot 
3275*b5162e19SFrançois Tigeot static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3276*b5162e19SFrançois Tigeot 				      struct drm_property *property,
3277*b5162e19SFrançois Tigeot 				      uint64_t value)
3278*b5162e19SFrançois Tigeot {
3279*b5162e19SFrançois Tigeot 	int ret = -EINVAL;
3280*b5162e19SFrançois Tigeot 	struct drm_crtc *crtc = obj_to_crtc(obj);
3281*b5162e19SFrançois Tigeot 
3282*b5162e19SFrançois Tigeot 	if (crtc->funcs->set_property)
3283*b5162e19SFrançois Tigeot 		ret = crtc->funcs->set_property(crtc, property, value);
3284*b5162e19SFrançois Tigeot 	if (!ret)
3285*b5162e19SFrançois Tigeot 		drm_object_property_set_value(obj, property, value);
3286*b5162e19SFrançois Tigeot 
3287*b5162e19SFrançois Tigeot 	return ret;
3288*b5162e19SFrançois Tigeot }
3289*b5162e19SFrançois Tigeot 
3290*b5162e19SFrançois Tigeot static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3291*b5162e19SFrançois Tigeot 				      struct drm_property *property,
3292*b5162e19SFrançois Tigeot 				      uint64_t value)
3293*b5162e19SFrançois Tigeot {
3294*b5162e19SFrançois Tigeot 	int ret = -EINVAL;
3295*b5162e19SFrançois Tigeot 	struct drm_plane *plane = obj_to_plane(obj);
3296*b5162e19SFrançois Tigeot 
3297*b5162e19SFrançois Tigeot 	if (plane->funcs->set_property)
3298*b5162e19SFrançois Tigeot 		ret = plane->funcs->set_property(plane, property, value);
3299*b5162e19SFrançois Tigeot 	if (!ret)
3300*b5162e19SFrançois Tigeot 		drm_object_property_set_value(obj, property, value);
3301*b5162e19SFrançois Tigeot 
3302*b5162e19SFrançois Tigeot 	return ret;
3303*b5162e19SFrançois Tigeot }
3304*b5162e19SFrançois Tigeot 
3305*b5162e19SFrançois Tigeot int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3306*b5162e19SFrançois Tigeot 				      struct drm_file *file_priv)
3307*b5162e19SFrançois Tigeot {
3308*b5162e19SFrançois Tigeot 	struct drm_mode_obj_get_properties *arg = data;
33095718399fSFrançois Tigeot 	struct drm_mode_object *obj;
3310*b5162e19SFrançois Tigeot 	int ret = 0;
3311*b5162e19SFrançois Tigeot 	int i;
3312*b5162e19SFrançois Tigeot 	int copied = 0;
3313*b5162e19SFrançois Tigeot 	int props_count = 0;
3314*b5162e19SFrançois Tigeot 	uint32_t __user *props_ptr;
3315*b5162e19SFrançois Tigeot 	uint64_t __user *prop_values_ptr;
3316*b5162e19SFrançois Tigeot 
3317*b5162e19SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3318*b5162e19SFrançois Tigeot 		return -EINVAL;
3319*b5162e19SFrançois Tigeot 
3320*b5162e19SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
3321*b5162e19SFrançois Tigeot 
3322*b5162e19SFrançois Tigeot 	obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3323*b5162e19SFrançois Tigeot 	if (!obj) {
3324*b5162e19SFrançois Tigeot 		ret = -EINVAL;
3325*b5162e19SFrançois Tigeot 		goto out;
3326*b5162e19SFrançois Tigeot 	}
3327*b5162e19SFrançois Tigeot 	if (!obj->properties) {
3328*b5162e19SFrançois Tigeot 		ret = -EINVAL;
3329*b5162e19SFrançois Tigeot 		goto out;
3330*b5162e19SFrançois Tigeot 	}
3331*b5162e19SFrançois Tigeot 
3332*b5162e19SFrançois Tigeot 	props_count = obj->properties->count;
3333*b5162e19SFrançois Tigeot 
3334*b5162e19SFrançois Tigeot 	/* This ioctl is called twice, once to determine how much space is
3335*b5162e19SFrançois Tigeot 	 * needed, and the 2nd time to fill it. */
3336*b5162e19SFrançois Tigeot 	if ((arg->count_props >= props_count) && props_count) {
3337*b5162e19SFrançois Tigeot 		copied = 0;
3338*b5162e19SFrançois Tigeot 		props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3339*b5162e19SFrançois Tigeot 		prop_values_ptr = (uint64_t __user *)(unsigned long)
3340*b5162e19SFrançois Tigeot 				  (arg->prop_values_ptr);
3341*b5162e19SFrançois Tigeot 		for (i = 0; i < props_count; i++) {
3342*b5162e19SFrançois Tigeot 			if (put_user(obj->properties->ids[i],
3343*b5162e19SFrançois Tigeot 				     props_ptr + copied)) {
3344*b5162e19SFrançois Tigeot 				ret = -EFAULT;
3345*b5162e19SFrançois Tigeot 				goto out;
3346*b5162e19SFrançois Tigeot 			}
3347*b5162e19SFrançois Tigeot 			if (put_user(obj->properties->values[i],
3348*b5162e19SFrançois Tigeot 				     prop_values_ptr + copied)) {
3349*b5162e19SFrançois Tigeot 				ret = -EFAULT;
3350*b5162e19SFrançois Tigeot 				goto out;
3351*b5162e19SFrançois Tigeot 			}
3352*b5162e19SFrançois Tigeot 			copied++;
3353*b5162e19SFrançois Tigeot 		}
3354*b5162e19SFrançois Tigeot 	}
3355*b5162e19SFrançois Tigeot 	arg->count_props = props_count;
3356*b5162e19SFrançois Tigeot out:
3357*b5162e19SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
3358*b5162e19SFrançois Tigeot 	return ret;
3359*b5162e19SFrançois Tigeot }
3360*b5162e19SFrançois Tigeot 
3361*b5162e19SFrançois Tigeot int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3362*b5162e19SFrançois Tigeot 				    struct drm_file *file_priv)
3363*b5162e19SFrançois Tigeot {
3364*b5162e19SFrançois Tigeot 	struct drm_mode_obj_set_property *arg = data;
3365*b5162e19SFrançois Tigeot 	struct drm_mode_object *arg_obj;
3366*b5162e19SFrançois Tigeot 	struct drm_mode_object *prop_obj;
33675718399fSFrançois Tigeot 	struct drm_property *property;
33685718399fSFrançois Tigeot 	int ret = -EINVAL;
33695718399fSFrançois Tigeot 	int i;
33705718399fSFrançois Tigeot 
33715718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
33725718399fSFrançois Tigeot 		return -EINVAL;
33735718399fSFrançois Tigeot 
3374af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
33755718399fSFrançois Tigeot 
3376*b5162e19SFrançois Tigeot 	arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3377*b5162e19SFrançois Tigeot 	if (!arg_obj)
33785718399fSFrançois Tigeot 		goto out;
3379*b5162e19SFrançois Tigeot 	if (!arg_obj->properties)
3380*b5162e19SFrançois Tigeot 		goto out;
33815718399fSFrançois Tigeot 
3382*b5162e19SFrançois Tigeot 	for (i = 0; i < arg_obj->properties->count; i++)
3383*b5162e19SFrançois Tigeot 		if (arg_obj->properties->ids[i] == arg->prop_id)
3384*b5162e19SFrançois Tigeot 			break;
3385*b5162e19SFrançois Tigeot 
3386*b5162e19SFrançois Tigeot 	if (i == arg_obj->properties->count)
3387*b5162e19SFrançois Tigeot 		goto out;
3388*b5162e19SFrançois Tigeot 
3389*b5162e19SFrançois Tigeot 	prop_obj = drm_mode_object_find(dev, arg->prop_id,
3390*b5162e19SFrançois Tigeot 					DRM_MODE_OBJECT_PROPERTY);
3391*b5162e19SFrançois Tigeot 	if (!prop_obj)
3392*b5162e19SFrançois Tigeot 		goto out;
3393*b5162e19SFrançois Tigeot 	property = obj_to_property(prop_obj);
3394*b5162e19SFrançois Tigeot 
3395*b5162e19SFrançois Tigeot 	if (!drm_property_change_is_valid(property, arg->value))
3396*b5162e19SFrançois Tigeot 		goto out;
3397*b5162e19SFrançois Tigeot 
3398*b5162e19SFrançois Tigeot 	switch (arg_obj->type) {
3399*b5162e19SFrançois Tigeot 	case DRM_MODE_OBJECT_CONNECTOR:
3400*b5162e19SFrançois Tigeot 		ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3401*b5162e19SFrançois Tigeot 						      arg->value);
3402*b5162e19SFrançois Tigeot 		break;
3403*b5162e19SFrançois Tigeot 	case DRM_MODE_OBJECT_CRTC:
3404*b5162e19SFrançois Tigeot 		ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3405*b5162e19SFrançois Tigeot 		break;
3406*b5162e19SFrançois Tigeot 	case DRM_MODE_OBJECT_PLANE:
3407*b5162e19SFrançois Tigeot 		ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
34085718399fSFrançois Tigeot 		break;
34095718399fSFrançois Tigeot 	}
34105718399fSFrançois Tigeot 
34115718399fSFrançois Tigeot out:
3412af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
34135718399fSFrançois Tigeot 	return ret;
34145718399fSFrançois Tigeot }
34155718399fSFrançois Tigeot 
34165718399fSFrançois Tigeot int drm_mode_connector_attach_encoder(struct drm_connector *connector,
34175718399fSFrançois Tigeot 				      struct drm_encoder *encoder)
34185718399fSFrançois Tigeot {
34195718399fSFrançois Tigeot 	int i;
34205718399fSFrançois Tigeot 
34215718399fSFrançois Tigeot 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
34225718399fSFrançois Tigeot 		if (connector->encoder_ids[i] == 0) {
34235718399fSFrançois Tigeot 			connector->encoder_ids[i] = encoder->base.id;
34245718399fSFrançois Tigeot 			return 0;
34255718399fSFrançois Tigeot 		}
34265718399fSFrançois Tigeot 	}
34275718399fSFrançois Tigeot 	return -ENOMEM;
34285718399fSFrançois Tigeot }
3429*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
34305718399fSFrançois Tigeot 
34315718399fSFrançois Tigeot void drm_mode_connector_detach_encoder(struct drm_connector *connector,
34325718399fSFrançois Tigeot 				    struct drm_encoder *encoder)
34335718399fSFrançois Tigeot {
34345718399fSFrançois Tigeot 	int i;
34355718399fSFrançois Tigeot 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
34365718399fSFrançois Tigeot 		if (connector->encoder_ids[i] == encoder->base.id) {
34375718399fSFrançois Tigeot 			connector->encoder_ids[i] = 0;
34385718399fSFrançois Tigeot 			if (connector->encoder == encoder)
34395718399fSFrançois Tigeot 				connector->encoder = NULL;
34405718399fSFrançois Tigeot 			break;
34415718399fSFrançois Tigeot 		}
34425718399fSFrançois Tigeot 	}
34435718399fSFrançois Tigeot }
3444*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
34455718399fSFrançois Tigeot 
34465718399fSFrançois Tigeot int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
34475718399fSFrançois Tigeot 				  int gamma_size)
34485718399fSFrançois Tigeot {
34495718399fSFrançois Tigeot 	crtc->gamma_size = gamma_size;
34505718399fSFrançois Tigeot 
34515718399fSFrançois Tigeot 	crtc->gamma_store = kmalloc(gamma_size * sizeof(uint16_t) * 3,
34525718399fSFrançois Tigeot 	    DRM_MEM_KMS, M_WAITOK | M_ZERO);
3453*b5162e19SFrançois Tigeot 	if (!crtc->gamma_store) {
3454*b5162e19SFrançois Tigeot 		crtc->gamma_size = 0;
3455*b5162e19SFrançois Tigeot 		return -ENOMEM;
3456*b5162e19SFrançois Tigeot 	}
34575718399fSFrançois Tigeot 
34585718399fSFrançois Tigeot 	return 0;
34595718399fSFrançois Tigeot }
3460*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
34615718399fSFrançois Tigeot 
34625718399fSFrançois Tigeot int drm_mode_gamma_set_ioctl(struct drm_device *dev,
34635718399fSFrançois Tigeot 			     void *data, struct drm_file *file_priv)
34645718399fSFrançois Tigeot {
34655718399fSFrançois Tigeot 	struct drm_mode_crtc_lut *crtc_lut = data;
34665718399fSFrançois Tigeot 	struct drm_mode_object *obj;
34675718399fSFrançois Tigeot 	struct drm_crtc *crtc;
34685718399fSFrançois Tigeot 	void *r_base, *g_base, *b_base;
34695718399fSFrançois Tigeot 	int size;
34705718399fSFrançois Tigeot 	int ret = 0;
34715718399fSFrançois Tigeot 
34725718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
34735718399fSFrançois Tigeot 		return -EINVAL;
34745718399fSFrançois Tigeot 
3475af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
34765718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
34775718399fSFrançois Tigeot 	if (!obj) {
34785718399fSFrançois Tigeot 		ret = -EINVAL;
34795718399fSFrançois Tigeot 		goto out;
34805718399fSFrançois Tigeot 	}
34815718399fSFrançois Tigeot 	crtc = obj_to_crtc(obj);
34825718399fSFrançois Tigeot 
3483*b5162e19SFrançois Tigeot 	if (crtc->funcs->gamma_set == NULL) {
3484*b5162e19SFrançois Tigeot 		ret = -ENOSYS;
3485*b5162e19SFrançois Tigeot 		goto out;
3486*b5162e19SFrançois Tigeot 	}
3487*b5162e19SFrançois Tigeot 
34885718399fSFrançois Tigeot 	/* memcpy into gamma store */
34895718399fSFrançois Tigeot 	if (crtc_lut->gamma_size != crtc->gamma_size) {
34905718399fSFrançois Tigeot 		ret = -EINVAL;
34915718399fSFrançois Tigeot 		goto out;
34925718399fSFrançois Tigeot 	}
34935718399fSFrançois Tigeot 
34945718399fSFrançois Tigeot 	size = crtc_lut->gamma_size * (sizeof(uint16_t));
34955718399fSFrançois Tigeot 	r_base = crtc->gamma_store;
3496*b5162e19SFrançois Tigeot 	if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
34975718399fSFrançois Tigeot 		ret = -EFAULT;
34985718399fSFrançois Tigeot 		goto out;
34995718399fSFrançois Tigeot 	}
35005718399fSFrançois Tigeot 
35015718399fSFrançois Tigeot 	g_base = (char *)r_base + size;
3502*b5162e19SFrançois Tigeot 	if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
35035718399fSFrançois Tigeot 		ret = -EFAULT;
35045718399fSFrançois Tigeot 		goto out;
35055718399fSFrançois Tigeot 	}
35065718399fSFrançois Tigeot 
35075718399fSFrançois Tigeot 	b_base = (char *)g_base + size;
3508*b5162e19SFrançois Tigeot 	if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
35095718399fSFrançois Tigeot 		ret = -EFAULT;
35105718399fSFrançois Tigeot 		goto out;
35115718399fSFrançois Tigeot 	}
35125718399fSFrançois Tigeot 
35135718399fSFrançois Tigeot 	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
35145718399fSFrançois Tigeot 
35155718399fSFrançois Tigeot out:
3516af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
35175718399fSFrançois Tigeot 	return ret;
35185718399fSFrançois Tigeot 
35195718399fSFrançois Tigeot }
35205718399fSFrançois Tigeot 
35215718399fSFrançois Tigeot int drm_mode_gamma_get_ioctl(struct drm_device *dev,
35225718399fSFrançois Tigeot 			     void *data, struct drm_file *file_priv)
35235718399fSFrançois Tigeot {
35245718399fSFrançois Tigeot 	struct drm_mode_crtc_lut *crtc_lut = data;
35255718399fSFrançois Tigeot 	struct drm_mode_object *obj;
35265718399fSFrançois Tigeot 	struct drm_crtc *crtc;
35275718399fSFrançois Tigeot 	void *r_base, *g_base, *b_base;
35285718399fSFrançois Tigeot 	int size;
35295718399fSFrançois Tigeot 	int ret = 0;
35305718399fSFrançois Tigeot 
35315718399fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
35325718399fSFrançois Tigeot 		return -EINVAL;
35335718399fSFrançois Tigeot 
3534af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
35355718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
35365718399fSFrançois Tigeot 	if (!obj) {
35375718399fSFrançois Tigeot 		ret = -EINVAL;
35385718399fSFrançois Tigeot 		goto out;
35395718399fSFrançois Tigeot 	}
35405718399fSFrançois Tigeot 	crtc = obj_to_crtc(obj);
35415718399fSFrançois Tigeot 
35425718399fSFrançois Tigeot 	/* memcpy into gamma store */
35435718399fSFrançois Tigeot 	if (crtc_lut->gamma_size != crtc->gamma_size) {
35445718399fSFrançois Tigeot 		ret = -EINVAL;
35455718399fSFrançois Tigeot 		goto out;
35465718399fSFrançois Tigeot 	}
35475718399fSFrançois Tigeot 
35485718399fSFrançois Tigeot 	size = crtc_lut->gamma_size * (sizeof(uint16_t));
35495718399fSFrançois Tigeot 	r_base = crtc->gamma_store;
3550*b5162e19SFrançois Tigeot 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
35515718399fSFrançois Tigeot 		ret = -EFAULT;
35525718399fSFrançois Tigeot 		goto out;
35535718399fSFrançois Tigeot 	}
35545718399fSFrançois Tigeot 
35555718399fSFrançois Tigeot 	g_base = (char *)r_base + size;
3556*b5162e19SFrançois Tigeot 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
35575718399fSFrançois Tigeot 		ret = -EFAULT;
35585718399fSFrançois Tigeot 		goto out;
35595718399fSFrançois Tigeot 	}
35605718399fSFrançois Tigeot 
35615718399fSFrançois Tigeot 	b_base = (char *)g_base + size;
3562*b5162e19SFrançois Tigeot 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
35635718399fSFrançois Tigeot 		ret = -EFAULT;
35645718399fSFrançois Tigeot 		goto out;
35655718399fSFrançois Tigeot 	}
35665718399fSFrançois Tigeot out:
3567af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
35685718399fSFrançois Tigeot 	return ret;
35695718399fSFrançois Tigeot }
35705718399fSFrançois Tigeot 
35715718399fSFrançois Tigeot static void
35725718399fSFrançois Tigeot drm_kms_free(void *arg)
35735718399fSFrançois Tigeot {
35745718399fSFrançois Tigeot 
35755718399fSFrançois Tigeot 	drm_free(arg, DRM_MEM_KMS);
35765718399fSFrançois Tigeot }
35775718399fSFrançois Tigeot 
3578*b5162e19SFrançois Tigeot int drm_mode_page_flip_ioctl(struct drm_device *dev,
3579*b5162e19SFrançois Tigeot 			     void *data, struct drm_file *file_priv)
35805718399fSFrançois Tigeot {
35815718399fSFrançois Tigeot 	struct drm_mode_crtc_page_flip *page_flip = data;
35825718399fSFrançois Tigeot 	struct drm_mode_object *obj;
35835718399fSFrançois Tigeot 	struct drm_crtc *crtc;
35845718399fSFrançois Tigeot 	struct drm_framebuffer *fb;
35855718399fSFrançois Tigeot 	struct drm_pending_vblank_event *e = NULL;
3586*b5162e19SFrançois Tigeot 	int hdisplay, vdisplay;
3587*b5162e19SFrançois Tigeot 	int ret = -EINVAL;
35885718399fSFrançois Tigeot 
35895718399fSFrançois Tigeot 	if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
35905718399fSFrançois Tigeot 	    page_flip->reserved != 0)
3591*b5162e19SFrançois Tigeot 		return -EINVAL;
35925718399fSFrançois Tigeot 
3593af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
35945718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
35955718399fSFrançois Tigeot 	if (!obj)
35965718399fSFrançois Tigeot 		goto out;
35975718399fSFrançois Tigeot 	crtc = obj_to_crtc(obj);
35985718399fSFrançois Tigeot 
35995718399fSFrançois Tigeot 	if (crtc->fb == NULL) {
36005718399fSFrançois Tigeot 		/* The framebuffer is currently unbound, presumably
36015718399fSFrançois Tigeot 		 * due to a hotplug event, that userspace has not
36025718399fSFrançois Tigeot 		 * yet discovered.
36035718399fSFrançois Tigeot 		 */
3604*b5162e19SFrançois Tigeot 		ret = -EBUSY;
36055718399fSFrançois Tigeot 		goto out;
36065718399fSFrançois Tigeot 	}
36075718399fSFrançois Tigeot 
36085718399fSFrançois Tigeot 	if (crtc->funcs->page_flip == NULL)
36095718399fSFrançois Tigeot 		goto out;
36105718399fSFrançois Tigeot 
36115718399fSFrançois Tigeot 	obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
36125718399fSFrançois Tigeot 	if (!obj)
36135718399fSFrançois Tigeot 		goto out;
36145718399fSFrançois Tigeot 	fb = obj_to_fb(obj);
36155718399fSFrançois Tigeot 
3616*b5162e19SFrançois Tigeot 	hdisplay = crtc->mode.hdisplay;
3617*b5162e19SFrançois Tigeot 	vdisplay = crtc->mode.vdisplay;
3618*b5162e19SFrançois Tigeot 
3619*b5162e19SFrançois Tigeot 	if (crtc->invert_dimensions)
3620*b5162e19SFrançois Tigeot 		swap(hdisplay, vdisplay);
3621*b5162e19SFrançois Tigeot 
3622*b5162e19SFrançois Tigeot 	if (hdisplay > fb->width ||
3623*b5162e19SFrançois Tigeot 	    vdisplay > fb->height ||
3624*b5162e19SFrançois Tigeot 	    crtc->x > fb->width - hdisplay ||
3625*b5162e19SFrançois Tigeot 	    crtc->y > fb->height - vdisplay) {
3626*b5162e19SFrançois Tigeot 		DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3627*b5162e19SFrançois Tigeot 			      fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3628*b5162e19SFrançois Tigeot 			      crtc->invert_dimensions ? " (inverted)" : "");
3629*b5162e19SFrançois Tigeot 		ret = -ENOSPC;
36305718399fSFrançois Tigeot 		goto out;
36315718399fSFrançois Tigeot 	}
36325718399fSFrançois Tigeot 
36335718399fSFrançois Tigeot 	if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3634*b5162e19SFrançois Tigeot 		ret = -ENOMEM;
36355718399fSFrançois Tigeot 		lockmgr(&dev->event_lock, LK_EXCLUSIVE);
36365718399fSFrançois Tigeot 		if (file_priv->event_space < sizeof e->event) {
36375718399fSFrançois Tigeot 			lockmgr(&dev->event_lock, LK_RELEASE);
36385718399fSFrançois Tigeot 			goto out;
36395718399fSFrançois Tigeot 		}
36405718399fSFrançois Tigeot 		file_priv->event_space -= sizeof e->event;
36415718399fSFrançois Tigeot 		lockmgr(&dev->event_lock, LK_RELEASE);
36425718399fSFrançois Tigeot 
36435718399fSFrançois Tigeot 		e = kmalloc(sizeof *e, DRM_MEM_KMS, M_WAITOK | M_ZERO);
3644*b5162e19SFrançois Tigeot 		if (e == NULL) {
3645*b5162e19SFrançois Tigeot 			lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3646*b5162e19SFrançois Tigeot 			file_priv->event_space += sizeof e->event;
3647*b5162e19SFrançois Tigeot 			lockmgr(&dev->event_lock, LK_RELEASE);
3648*b5162e19SFrançois Tigeot 			goto out;
3649*b5162e19SFrançois Tigeot 		}
36505718399fSFrançois Tigeot 
36515718399fSFrançois Tigeot 		e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
36525718399fSFrançois Tigeot 		e->event.base.length = sizeof e->event;
36535718399fSFrançois Tigeot 		e->event.user_data = page_flip->user_data;
36545718399fSFrançois Tigeot 		e->base.event = &e->event.base;
36555718399fSFrançois Tigeot 		e->base.file_priv = file_priv;
36565718399fSFrançois Tigeot 		e->base.destroy =
36575718399fSFrançois Tigeot 			(void (*) (struct drm_pending_event *))drm_kms_free;
36585718399fSFrançois Tigeot 	}
36595718399fSFrançois Tigeot 
3660*b5162e19SFrançois Tigeot 	ret = crtc->funcs->page_flip(crtc, fb, e);
3661*b5162e19SFrançois Tigeot 	if (ret) {
36625718399fSFrançois Tigeot 		if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
36635718399fSFrançois Tigeot 			lockmgr(&dev->event_lock, LK_EXCLUSIVE);
36645718399fSFrançois Tigeot 			file_priv->event_space += sizeof e->event;
36655718399fSFrançois Tigeot 			lockmgr(&dev->event_lock, LK_RELEASE);
36665718399fSFrançois Tigeot 			drm_free(e, DRM_MEM_KMS);
36675718399fSFrançois Tigeot 		}
36685718399fSFrançois Tigeot 	}
36695718399fSFrançois Tigeot 
36705718399fSFrançois Tigeot out:
3671af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
3672*b5162e19SFrançois Tigeot 	return ret;
36735718399fSFrançois Tigeot }
36745718399fSFrançois Tigeot 
36755718399fSFrançois Tigeot void drm_mode_config_reset(struct drm_device *dev)
36765718399fSFrançois Tigeot {
36775718399fSFrançois Tigeot 	struct drm_crtc *crtc;
36785718399fSFrançois Tigeot 	struct drm_encoder *encoder;
36795718399fSFrançois Tigeot 	struct drm_connector *connector;
36805718399fSFrançois Tigeot 
36815718399fSFrançois Tigeot 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
36825718399fSFrançois Tigeot 		if (crtc->funcs->reset)
36835718399fSFrançois Tigeot 			crtc->funcs->reset(crtc);
36845718399fSFrançois Tigeot 
36855718399fSFrançois Tigeot 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
36865718399fSFrançois Tigeot 		if (encoder->funcs->reset)
36875718399fSFrançois Tigeot 			encoder->funcs->reset(encoder);
36885718399fSFrançois Tigeot 
3689*b5162e19SFrançois Tigeot 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3690*b5162e19SFrançois Tigeot 		connector->status = connector_status_unknown;
3691*b5162e19SFrançois Tigeot 
36925718399fSFrançois Tigeot 		if (connector->funcs->reset)
36935718399fSFrançois Tigeot 			connector->funcs->reset(connector);
36945718399fSFrançois Tigeot 	}
3695*b5162e19SFrançois Tigeot }
3696*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_mode_config_reset);
36975718399fSFrançois Tigeot 
36985718399fSFrançois Tigeot int drm_mode_create_dumb_ioctl(struct drm_device *dev,
36995718399fSFrançois Tigeot 			       void *data, struct drm_file *file_priv)
37005718399fSFrançois Tigeot {
37015718399fSFrançois Tigeot 	struct drm_mode_create_dumb *args = data;
37025718399fSFrançois Tigeot 
37035718399fSFrançois Tigeot 	if (!dev->driver->dumb_create)
3704*b5162e19SFrançois Tigeot 		return -ENOSYS;
37055718399fSFrançois Tigeot 	return dev->driver->dumb_create(file_priv, dev, args);
37065718399fSFrançois Tigeot }
37075718399fSFrançois Tigeot 
37085718399fSFrançois Tigeot int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
37095718399fSFrançois Tigeot 			     void *data, struct drm_file *file_priv)
37105718399fSFrançois Tigeot {
37115718399fSFrançois Tigeot 	struct drm_mode_map_dumb *args = data;
37125718399fSFrançois Tigeot 
37135718399fSFrançois Tigeot 	/* call driver ioctl to get mmap offset */
37145718399fSFrançois Tigeot 	if (!dev->driver->dumb_map_offset)
3715*b5162e19SFrançois Tigeot 		return -ENOSYS;
37165718399fSFrançois Tigeot 
37175718399fSFrançois Tigeot 	return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
37185718399fSFrançois Tigeot }
37195718399fSFrançois Tigeot 
37205718399fSFrançois Tigeot int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
37215718399fSFrançois Tigeot 				void *data, struct drm_file *file_priv)
37225718399fSFrançois Tigeot {
37235718399fSFrançois Tigeot 	struct drm_mode_destroy_dumb *args = data;
37245718399fSFrançois Tigeot 
37255718399fSFrançois Tigeot 	if (!dev->driver->dumb_destroy)
3726*b5162e19SFrançois Tigeot 		return -ENOSYS;
37275718399fSFrançois Tigeot 
37285718399fSFrançois Tigeot 	return dev->driver->dumb_destroy(file_priv, dev, args->handle);
37295718399fSFrançois Tigeot }
37305718399fSFrançois Tigeot 
37315718399fSFrançois Tigeot /*
37325718399fSFrançois Tigeot  * Just need to support RGB formats here for compat with code that doesn't
37335718399fSFrançois Tigeot  * use pixel formats directly yet.
37345718399fSFrançois Tigeot  */
37355718399fSFrançois Tigeot void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
37365718399fSFrançois Tigeot 			  int *bpp)
37375718399fSFrançois Tigeot {
37385718399fSFrançois Tigeot 	switch (format) {
37395718399fSFrançois Tigeot 	case DRM_FORMAT_RGB332:
37405718399fSFrançois Tigeot 	case DRM_FORMAT_BGR233:
37415718399fSFrançois Tigeot 		*depth = 8;
37425718399fSFrançois Tigeot 		*bpp = 8;
37435718399fSFrançois Tigeot 		break;
37445718399fSFrançois Tigeot 	case DRM_FORMAT_XRGB1555:
37455718399fSFrançois Tigeot 	case DRM_FORMAT_XBGR1555:
37465718399fSFrançois Tigeot 	case DRM_FORMAT_RGBX5551:
37475718399fSFrançois Tigeot 	case DRM_FORMAT_BGRX5551:
37485718399fSFrançois Tigeot 	case DRM_FORMAT_ARGB1555:
37495718399fSFrançois Tigeot 	case DRM_FORMAT_ABGR1555:
37505718399fSFrançois Tigeot 	case DRM_FORMAT_RGBA5551:
37515718399fSFrançois Tigeot 	case DRM_FORMAT_BGRA5551:
37525718399fSFrançois Tigeot 		*depth = 15;
37535718399fSFrançois Tigeot 		*bpp = 16;
37545718399fSFrançois Tigeot 		break;
37555718399fSFrançois Tigeot 	case DRM_FORMAT_RGB565:
37565718399fSFrançois Tigeot 	case DRM_FORMAT_BGR565:
37575718399fSFrançois Tigeot 		*depth = 16;
37585718399fSFrançois Tigeot 		*bpp = 16;
37595718399fSFrançois Tigeot 		break;
37605718399fSFrançois Tigeot 	case DRM_FORMAT_RGB888:
37615718399fSFrançois Tigeot 	case DRM_FORMAT_BGR888:
37625718399fSFrançois Tigeot 		*depth = 24;
37635718399fSFrançois Tigeot 		*bpp = 24;
37645718399fSFrançois Tigeot 		break;
37655718399fSFrançois Tigeot 	case DRM_FORMAT_XRGB8888:
37665718399fSFrançois Tigeot 	case DRM_FORMAT_XBGR8888:
37675718399fSFrançois Tigeot 	case DRM_FORMAT_RGBX8888:
37685718399fSFrançois Tigeot 	case DRM_FORMAT_BGRX8888:
37695718399fSFrançois Tigeot 		*depth = 24;
37705718399fSFrançois Tigeot 		*bpp = 32;
37715718399fSFrançois Tigeot 		break;
37725718399fSFrançois Tigeot 	case DRM_FORMAT_XRGB2101010:
37735718399fSFrançois Tigeot 	case DRM_FORMAT_XBGR2101010:
37745718399fSFrançois Tigeot 	case DRM_FORMAT_RGBX1010102:
37755718399fSFrançois Tigeot 	case DRM_FORMAT_BGRX1010102:
37765718399fSFrançois Tigeot 	case DRM_FORMAT_ARGB2101010:
37775718399fSFrançois Tigeot 	case DRM_FORMAT_ABGR2101010:
37785718399fSFrançois Tigeot 	case DRM_FORMAT_RGBA1010102:
37795718399fSFrançois Tigeot 	case DRM_FORMAT_BGRA1010102:
37805718399fSFrançois Tigeot 		*depth = 30;
37815718399fSFrançois Tigeot 		*bpp = 32;
37825718399fSFrançois Tigeot 		break;
37835718399fSFrançois Tigeot 	case DRM_FORMAT_ARGB8888:
37845718399fSFrançois Tigeot 	case DRM_FORMAT_ABGR8888:
37855718399fSFrançois Tigeot 	case DRM_FORMAT_RGBA8888:
37865718399fSFrançois Tigeot 	case DRM_FORMAT_BGRA8888:
37875718399fSFrançois Tigeot 		*depth = 32;
37885718399fSFrançois Tigeot 		*bpp = 32;
37895718399fSFrançois Tigeot 		break;
37905718399fSFrançois Tigeot 	default:
37915718399fSFrançois Tigeot 		DRM_DEBUG_KMS("unsupported pixel format\n");
37925718399fSFrançois Tigeot 		*depth = 0;
37935718399fSFrançois Tigeot 		*bpp = 0;
37945718399fSFrançois Tigeot 		break;
37955718399fSFrançois Tigeot 	}
37965718399fSFrançois Tigeot }
3797*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3798*b5162e19SFrançois Tigeot 
3799*b5162e19SFrançois Tigeot /**
3800*b5162e19SFrançois Tigeot  * drm_format_num_planes - get the number of planes for format
3801*b5162e19SFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
3802*b5162e19SFrançois Tigeot  *
3803*b5162e19SFrançois Tigeot  * RETURNS:
3804*b5162e19SFrançois Tigeot  * The number of planes used by the specified pixel format.
3805*b5162e19SFrançois Tigeot  */
3806*b5162e19SFrançois Tigeot int drm_format_num_planes(uint32_t format)
3807*b5162e19SFrançois Tigeot {
3808*b5162e19SFrançois Tigeot 	switch (format) {
3809*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV410:
3810*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU410:
3811*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV411:
3812*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU411:
3813*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV420:
3814*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU420:
3815*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV422:
3816*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU422:
3817*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV444:
3818*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU444:
3819*b5162e19SFrançois Tigeot 		return 3;
3820*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV12:
3821*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV21:
3822*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV16:
3823*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV61:
3824*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV24:
3825*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV42:
3826*b5162e19SFrançois Tigeot 		return 2;
3827*b5162e19SFrançois Tigeot 	default:
3828*b5162e19SFrançois Tigeot 		return 1;
3829*b5162e19SFrançois Tigeot 	}
3830*b5162e19SFrançois Tigeot }
3831*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_format_num_planes);
3832*b5162e19SFrançois Tigeot 
3833*b5162e19SFrançois Tigeot /**
3834*b5162e19SFrançois Tigeot  * drm_format_plane_cpp - determine the bytes per pixel value
3835*b5162e19SFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
3836*b5162e19SFrançois Tigeot  * @plane: plane index
3837*b5162e19SFrançois Tigeot  *
3838*b5162e19SFrançois Tigeot  * RETURNS:
3839*b5162e19SFrançois Tigeot  * The bytes per pixel value for the specified plane.
3840*b5162e19SFrançois Tigeot  */
3841*b5162e19SFrançois Tigeot int drm_format_plane_cpp(uint32_t format, int plane)
3842*b5162e19SFrançois Tigeot {
3843*b5162e19SFrançois Tigeot 	unsigned int depth;
3844*b5162e19SFrançois Tigeot 	int bpp;
3845*b5162e19SFrançois Tigeot 
3846*b5162e19SFrançois Tigeot 	if (plane >= drm_format_num_planes(format))
3847*b5162e19SFrançois Tigeot 		return 0;
3848*b5162e19SFrançois Tigeot 
3849*b5162e19SFrançois Tigeot 	switch (format) {
3850*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUYV:
3851*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVYU:
3852*b5162e19SFrançois Tigeot 	case DRM_FORMAT_UYVY:
3853*b5162e19SFrançois Tigeot 	case DRM_FORMAT_VYUY:
3854*b5162e19SFrançois Tigeot 		return 2;
3855*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV12:
3856*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV21:
3857*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV16:
3858*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV61:
3859*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV24:
3860*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV42:
3861*b5162e19SFrançois Tigeot 		return plane ? 2 : 1;
3862*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV410:
3863*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU410:
3864*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV411:
3865*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU411:
3866*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV420:
3867*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU420:
3868*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV422:
3869*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU422:
3870*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV444:
3871*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU444:
3872*b5162e19SFrançois Tigeot 		return 1;
3873*b5162e19SFrançois Tigeot 	default:
3874*b5162e19SFrançois Tigeot 		drm_fb_get_bpp_depth(format, &depth, &bpp);
3875*b5162e19SFrançois Tigeot 		return bpp >> 3;
3876*b5162e19SFrançois Tigeot 	}
3877*b5162e19SFrançois Tigeot }
3878*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_format_plane_cpp);
3879*b5162e19SFrançois Tigeot 
3880*b5162e19SFrançois Tigeot /**
3881*b5162e19SFrançois Tigeot  * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3882*b5162e19SFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
3883*b5162e19SFrançois Tigeot  *
3884*b5162e19SFrançois Tigeot  * RETURNS:
3885*b5162e19SFrançois Tigeot  * The horizontal chroma subsampling factor for the
3886*b5162e19SFrançois Tigeot  * specified pixel format.
3887*b5162e19SFrançois Tigeot  */
3888*b5162e19SFrançois Tigeot int drm_format_horz_chroma_subsampling(uint32_t format)
3889*b5162e19SFrançois Tigeot {
3890*b5162e19SFrançois Tigeot 	switch (format) {
3891*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV411:
3892*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU411:
3893*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV410:
3894*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU410:
3895*b5162e19SFrançois Tigeot 		return 4;
3896*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUYV:
3897*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVYU:
3898*b5162e19SFrançois Tigeot 	case DRM_FORMAT_UYVY:
3899*b5162e19SFrançois Tigeot 	case DRM_FORMAT_VYUY:
3900*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV12:
3901*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV21:
3902*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV16:
3903*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV61:
3904*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV422:
3905*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU422:
3906*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV420:
3907*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU420:
3908*b5162e19SFrançois Tigeot 		return 2;
3909*b5162e19SFrançois Tigeot 	default:
3910*b5162e19SFrançois Tigeot 		return 1;
3911*b5162e19SFrançois Tigeot 	}
3912*b5162e19SFrançois Tigeot }
3913*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3914*b5162e19SFrançois Tigeot 
3915*b5162e19SFrançois Tigeot /**
3916*b5162e19SFrançois Tigeot  * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3917*b5162e19SFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
3918*b5162e19SFrançois Tigeot  *
3919*b5162e19SFrançois Tigeot  * RETURNS:
3920*b5162e19SFrançois Tigeot  * The vertical chroma subsampling factor for the
3921*b5162e19SFrançois Tigeot  * specified pixel format.
3922*b5162e19SFrançois Tigeot  */
3923*b5162e19SFrançois Tigeot int drm_format_vert_chroma_subsampling(uint32_t format)
3924*b5162e19SFrançois Tigeot {
3925*b5162e19SFrançois Tigeot 	switch (format) {
3926*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV410:
3927*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU410:
3928*b5162e19SFrançois Tigeot 		return 4;
3929*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YUV420:
3930*b5162e19SFrançois Tigeot 	case DRM_FORMAT_YVU420:
3931*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV12:
3932*b5162e19SFrançois Tigeot 	case DRM_FORMAT_NV21:
3933*b5162e19SFrançois Tigeot 		return 2;
3934*b5162e19SFrançois Tigeot 	default:
3935*b5162e19SFrançois Tigeot 		return 1;
3936*b5162e19SFrançois Tigeot 	}
3937*b5162e19SFrançois Tigeot }
3938*b5162e19SFrançois Tigeot EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
3939