xref: /dflybsd-src/sys/dev/drm/drm_mode_config.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
14be47400SFrançois Tigeot /*
24be47400SFrançois Tigeot  * Copyright (c) 2016 Intel Corporation
34be47400SFrançois Tigeot  *
44be47400SFrançois Tigeot  * Permission to use, copy, modify, distribute, and sell this software and its
54be47400SFrançois Tigeot  * documentation for any purpose is hereby granted without fee, provided that
64be47400SFrançois Tigeot  * the above copyright notice appear in all copies and that both that copyright
74be47400SFrançois Tigeot  * notice and this permission notice appear in supporting documentation, and
84be47400SFrançois Tigeot  * that the name of the copyright holders not be used in advertising or
94be47400SFrançois Tigeot  * publicity pertaining to distribution of the software without specific,
104be47400SFrançois Tigeot  * written prior permission.  The copyright holders make no representations
114be47400SFrançois Tigeot  * about the suitability of this software for any purpose.  It is provided "as
124be47400SFrançois Tigeot  * is" without express or implied warranty.
134be47400SFrançois Tigeot  *
144be47400SFrançois Tigeot  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
154be47400SFrançois Tigeot  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
164be47400SFrançois Tigeot  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
174be47400SFrançois Tigeot  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
184be47400SFrançois Tigeot  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
194be47400SFrançois Tigeot  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
204be47400SFrançois Tigeot  * OF THIS SOFTWARE.
214be47400SFrançois Tigeot  */
224be47400SFrançois Tigeot 
23a85cb24fSFrançois Tigeot #include <drm/drm_encoder.h>
244be47400SFrançois Tigeot #include <drm/drm_mode_config.h>
254be47400SFrançois Tigeot #include <drm/drmP.h>
264be47400SFrançois Tigeot 
274be47400SFrançois Tigeot #include "drm_crtc_internal.h"
284be47400SFrançois Tigeot #include "drm_internal.h"
294be47400SFrançois Tigeot 
drm_modeset_register_all(struct drm_device * dev)304be47400SFrançois Tigeot int drm_modeset_register_all(struct drm_device *dev)
314be47400SFrançois Tigeot {
324be47400SFrançois Tigeot 	int ret;
334be47400SFrançois Tigeot 
344be47400SFrançois Tigeot 	ret = drm_plane_register_all(dev);
354be47400SFrançois Tigeot 	if (ret)
364be47400SFrançois Tigeot 		goto err_plane;
374be47400SFrançois Tigeot 
384be47400SFrançois Tigeot 	ret = drm_crtc_register_all(dev);
394be47400SFrançois Tigeot 	if  (ret)
404be47400SFrançois Tigeot 		goto err_crtc;
414be47400SFrançois Tigeot 
424be47400SFrançois Tigeot 	ret = drm_encoder_register_all(dev);
434be47400SFrançois Tigeot 	if (ret)
444be47400SFrançois Tigeot 		goto err_encoder;
454be47400SFrançois Tigeot 
464be47400SFrançois Tigeot 	ret = drm_connector_register_all(dev);
474be47400SFrançois Tigeot 	if (ret)
484be47400SFrançois Tigeot 		goto err_connector;
494be47400SFrançois Tigeot 
504be47400SFrançois Tigeot 	return 0;
514be47400SFrançois Tigeot 
524be47400SFrançois Tigeot err_connector:
534be47400SFrançois Tigeot 	drm_encoder_unregister_all(dev);
544be47400SFrançois Tigeot err_encoder:
554be47400SFrançois Tigeot 	drm_crtc_unregister_all(dev);
564be47400SFrançois Tigeot err_crtc:
574be47400SFrançois Tigeot 	drm_plane_unregister_all(dev);
584be47400SFrançois Tigeot err_plane:
594be47400SFrançois Tigeot 	return ret;
604be47400SFrançois Tigeot }
614be47400SFrançois Tigeot 
drm_modeset_unregister_all(struct drm_device * dev)624be47400SFrançois Tigeot void drm_modeset_unregister_all(struct drm_device *dev)
634be47400SFrançois Tigeot {
644be47400SFrançois Tigeot 	drm_connector_unregister_all(dev);
654be47400SFrançois Tigeot 	drm_encoder_unregister_all(dev);
664be47400SFrançois Tigeot 	drm_crtc_unregister_all(dev);
674be47400SFrançois Tigeot 	drm_plane_unregister_all(dev);
684be47400SFrançois Tigeot }
694be47400SFrançois Tigeot 
704be47400SFrançois Tigeot /**
714be47400SFrançois Tigeot  * drm_mode_getresources - get graphics configuration
724be47400SFrançois Tigeot  * @dev: drm device for the ioctl
734be47400SFrançois Tigeot  * @data: data pointer for the ioctl
744be47400SFrançois Tigeot  * @file_priv: drm file for the ioctl call
754be47400SFrançois Tigeot  *
764be47400SFrançois Tigeot  * Construct a set of configuration description structures and return
774be47400SFrançois Tigeot  * them to the user, including CRTC, connector and framebuffer configuration.
784be47400SFrançois Tigeot  *
794be47400SFrançois Tigeot  * Called by the user via ioctl.
804be47400SFrançois Tigeot  *
814be47400SFrançois Tigeot  * Returns:
824be47400SFrançois Tigeot  * Zero on success, negative errno on failure.
834be47400SFrançois Tigeot  */
drm_mode_getresources(struct drm_device * dev,void * data,struct drm_file * file_priv)844be47400SFrançois Tigeot int drm_mode_getresources(struct drm_device *dev, void *data,
854be47400SFrançois Tigeot 			  struct drm_file *file_priv)
864be47400SFrançois Tigeot {
874be47400SFrançois Tigeot 	struct drm_mode_card_res *card_res = data;
884be47400SFrançois Tigeot 	struct drm_framebuffer *fb;
894be47400SFrançois Tigeot 	struct drm_connector *connector;
904be47400SFrançois Tigeot 	struct drm_crtc *crtc;
914be47400SFrançois Tigeot 	struct drm_encoder *encoder;
92a85cb24fSFrançois Tigeot 	int count, ret = 0;
934be47400SFrançois Tigeot 	uint32_t __user *fb_id;
944be47400SFrançois Tigeot 	uint32_t __user *crtc_id;
954be47400SFrançois Tigeot 	uint32_t __user *connector_id;
964be47400SFrançois Tigeot 	uint32_t __user *encoder_id;
97a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
984be47400SFrançois Tigeot 
994be47400SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1004be47400SFrançois Tigeot 		return -EINVAL;
1014be47400SFrançois Tigeot 
1024be47400SFrançois Tigeot 
1034be47400SFrançois Tigeot 	mutex_lock(&file_priv->fbs_lock);
104a85cb24fSFrançois Tigeot 	count = 0;
105a85cb24fSFrançois Tigeot 	fb_id = u64_to_user_ptr(card_res->fb_id_ptr);
1064be47400SFrançois Tigeot 	list_for_each_entry(fb, &file_priv->fbs, filp_head) {
107a85cb24fSFrançois Tigeot 		if (count < card_res->count_fbs &&
108a85cb24fSFrançois Tigeot 		    put_user(fb->base.id, fb_id + count)) {
1094be47400SFrançois Tigeot 			mutex_unlock(&file_priv->fbs_lock);
1104be47400SFrançois Tigeot 			return -EFAULT;
1114be47400SFrançois Tigeot 		}
112a85cb24fSFrançois Tigeot 		count++;
1134be47400SFrançois Tigeot 	}
114a85cb24fSFrançois Tigeot 	card_res->count_fbs = count;
1154be47400SFrançois Tigeot 	mutex_unlock(&file_priv->fbs_lock);
1164be47400SFrançois Tigeot 
1174be47400SFrançois Tigeot 	card_res->max_height = dev->mode_config.max_height;
1184be47400SFrançois Tigeot 	card_res->min_height = dev->mode_config.min_height;
1194be47400SFrançois Tigeot 	card_res->max_width = dev->mode_config.max_width;
1204be47400SFrançois Tigeot 	card_res->min_width = dev->mode_config.min_width;
1214be47400SFrançois Tigeot 
122a85cb24fSFrançois Tigeot 	count = 0;
123a85cb24fSFrançois Tigeot 	crtc_id = u64_to_user_ptr(card_res->crtc_id_ptr);
1244be47400SFrançois Tigeot 	drm_for_each_crtc(crtc, dev) {
125*3f2dd94aSFrançois Tigeot 		if (drm_lease_held(file_priv, crtc->base.id)) {
126a85cb24fSFrançois Tigeot 			if (count < card_res->count_crtcs &&
127a85cb24fSFrançois Tigeot 			    put_user(crtc->base.id, crtc_id + count))
128a85cb24fSFrançois Tigeot 				return -EFAULT;
129a85cb24fSFrançois Tigeot 			count++;
1304be47400SFrançois Tigeot 		}
131*3f2dd94aSFrançois Tigeot 	}
132a85cb24fSFrançois Tigeot 	card_res->count_crtcs = count;
1334be47400SFrançois Tigeot 
134a85cb24fSFrançois Tigeot 	count = 0;
135a85cb24fSFrançois Tigeot 	encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
1364be47400SFrançois Tigeot 	drm_for_each_encoder(encoder, dev) {
137a85cb24fSFrançois Tigeot 		if (count < card_res->count_encoders &&
138a85cb24fSFrançois Tigeot 		    put_user(encoder->base.id, encoder_id + count))
139a85cb24fSFrançois Tigeot 			return -EFAULT;
140a85cb24fSFrançois Tigeot 		count++;
1414be47400SFrançois Tigeot 	}
142a85cb24fSFrançois Tigeot 	card_res->count_encoders = count;
1434be47400SFrançois Tigeot 
144a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
145a85cb24fSFrançois Tigeot 	count = 0;
146a85cb24fSFrançois Tigeot 	connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
147a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter) {
148*3f2dd94aSFrançois Tigeot 		if (drm_lease_held(file_priv, connector->base.id)) {
149a85cb24fSFrançois Tigeot 			if (count < card_res->count_connectors &&
150a85cb24fSFrançois Tigeot 			    put_user(connector->base.id, connector_id + count)) {
151a85cb24fSFrançois Tigeot 				drm_connector_list_iter_end(&conn_iter);
152a85cb24fSFrançois Tigeot 				return -EFAULT;
1534be47400SFrançois Tigeot 			}
154a85cb24fSFrançois Tigeot 			count++;
1554be47400SFrançois Tigeot 		}
156*3f2dd94aSFrançois Tigeot 	}
157a85cb24fSFrançois Tigeot 	card_res->count_connectors = count;
158a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
1594be47400SFrançois Tigeot 
1604be47400SFrançois Tigeot 	return ret;
1614be47400SFrançois Tigeot }
1624be47400SFrançois Tigeot 
1634be47400SFrançois Tigeot /**
1644be47400SFrançois Tigeot  * drm_mode_config_reset - call ->reset callbacks
1654be47400SFrançois Tigeot  * @dev: drm device
1664be47400SFrançois Tigeot  *
1674be47400SFrançois Tigeot  * This functions calls all the crtc's, encoder's and connector's ->reset
1684be47400SFrançois Tigeot  * callback. Drivers can use this in e.g. their driver load or resume code to
1694be47400SFrançois Tigeot  * reset hardware and software state.
1704be47400SFrançois Tigeot  */
drm_mode_config_reset(struct drm_device * dev)1714be47400SFrançois Tigeot void drm_mode_config_reset(struct drm_device *dev)
1724be47400SFrançois Tigeot {
1734be47400SFrançois Tigeot 	struct drm_crtc *crtc;
1744be47400SFrançois Tigeot 	struct drm_plane *plane;
1754be47400SFrançois Tigeot 	struct drm_encoder *encoder;
1764be47400SFrançois Tigeot 	struct drm_connector *connector;
177a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
1784be47400SFrançois Tigeot 
1794be47400SFrançois Tigeot 	drm_for_each_plane(plane, dev)
1804be47400SFrançois Tigeot 		if (plane->funcs->reset)
1814be47400SFrançois Tigeot 			plane->funcs->reset(plane);
1824be47400SFrançois Tigeot 
1834be47400SFrançois Tigeot 	drm_for_each_crtc(crtc, dev)
1844be47400SFrançois Tigeot 		if (crtc->funcs->reset)
1854be47400SFrançois Tigeot 			crtc->funcs->reset(crtc);
1864be47400SFrançois Tigeot 
1874be47400SFrançois Tigeot 	drm_for_each_encoder(encoder, dev)
1884be47400SFrançois Tigeot 		if (encoder->funcs->reset)
1894be47400SFrançois Tigeot 			encoder->funcs->reset(encoder);
1904be47400SFrançois Tigeot 
191a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
192a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter)
1934be47400SFrançois Tigeot 		if (connector->funcs->reset)
1944be47400SFrançois Tigeot 			connector->funcs->reset(connector);
195a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
1964be47400SFrançois Tigeot }
1974be47400SFrançois Tigeot EXPORT_SYMBOL(drm_mode_config_reset);
1984be47400SFrançois Tigeot 
1994be47400SFrançois Tigeot /*
2004be47400SFrançois Tigeot  * Global properties
2014be47400SFrançois Tigeot  */
2024be47400SFrançois Tigeot static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
2034be47400SFrançois Tigeot 	{ DRM_PLANE_TYPE_OVERLAY, "Overlay" },
2044be47400SFrançois Tigeot 	{ DRM_PLANE_TYPE_PRIMARY, "Primary" },
2054be47400SFrançois Tigeot 	{ DRM_PLANE_TYPE_CURSOR, "Cursor" },
2064be47400SFrançois Tigeot };
2074be47400SFrançois Tigeot 
drm_mode_create_standard_properties(struct drm_device * dev)2084be47400SFrançois Tigeot static int drm_mode_create_standard_properties(struct drm_device *dev)
2094be47400SFrançois Tigeot {
2104be47400SFrançois Tigeot 	struct drm_property *prop;
2114be47400SFrançois Tigeot 	int ret;
2124be47400SFrançois Tigeot 
2134be47400SFrançois Tigeot 	ret = drm_connector_create_standard_properties(dev);
2144be47400SFrançois Tigeot 	if (ret)
2154be47400SFrançois Tigeot 		return ret;
2164be47400SFrançois Tigeot 
2174be47400SFrançois Tigeot 	prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
2184be47400SFrançois Tigeot 					"type", drm_plane_type_enum_list,
2194be47400SFrançois Tigeot 					ARRAY_SIZE(drm_plane_type_enum_list));
2204be47400SFrançois Tigeot 	if (!prop)
2214be47400SFrançois Tigeot 		return -ENOMEM;
2224be47400SFrançois Tigeot 	dev->mode_config.plane_type_property = prop;
2234be47400SFrançois Tigeot 
2244be47400SFrançois Tigeot 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
2254be47400SFrançois Tigeot 			"SRC_X", 0, UINT_MAX);
2264be47400SFrançois Tigeot 	if (!prop)
2274be47400SFrançois Tigeot 		return -ENOMEM;
2284be47400SFrançois Tigeot 	dev->mode_config.prop_src_x = prop;
2294be47400SFrançois Tigeot 
2304be47400SFrançois Tigeot 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
2314be47400SFrançois Tigeot 			"SRC_Y", 0, UINT_MAX);
2324be47400SFrançois Tigeot 	if (!prop)
2334be47400SFrançois Tigeot 		return -ENOMEM;
2344be47400SFrançois Tigeot 	dev->mode_config.prop_src_y = prop;
2354be47400SFrançois Tigeot 
2364be47400SFrançois Tigeot 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
2374be47400SFrançois Tigeot 			"SRC_W", 0, UINT_MAX);
2384be47400SFrançois Tigeot 	if (!prop)
2394be47400SFrançois Tigeot 		return -ENOMEM;
2404be47400SFrançois Tigeot 	dev->mode_config.prop_src_w = prop;
2414be47400SFrançois Tigeot 
2424be47400SFrançois Tigeot 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
2434be47400SFrançois Tigeot 			"SRC_H", 0, UINT_MAX);
2444be47400SFrançois Tigeot 	if (!prop)
2454be47400SFrançois Tigeot 		return -ENOMEM;
2464be47400SFrançois Tigeot 	dev->mode_config.prop_src_h = prop;
2474be47400SFrançois Tigeot 
2484be47400SFrançois Tigeot 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
2494be47400SFrançois Tigeot 			"CRTC_X", INT_MIN, INT_MAX);
2504be47400SFrançois Tigeot 	if (!prop)
2514be47400SFrançois Tigeot 		return -ENOMEM;
2524be47400SFrançois Tigeot 	dev->mode_config.prop_crtc_x = prop;
2534be47400SFrançois Tigeot 
2544be47400SFrançois Tigeot 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
2554be47400SFrançois Tigeot 			"CRTC_Y", INT_MIN, INT_MAX);
2564be47400SFrançois Tigeot 	if (!prop)
2574be47400SFrançois Tigeot 		return -ENOMEM;
2584be47400SFrançois Tigeot 	dev->mode_config.prop_crtc_y = prop;
2594be47400SFrançois Tigeot 
2604be47400SFrançois Tigeot 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
2614be47400SFrançois Tigeot 			"CRTC_W", 0, INT_MAX);
2624be47400SFrançois Tigeot 	if (!prop)
2634be47400SFrançois Tigeot 		return -ENOMEM;
2644be47400SFrançois Tigeot 	dev->mode_config.prop_crtc_w = prop;
2654be47400SFrançois Tigeot 
2664be47400SFrançois Tigeot 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
2674be47400SFrançois Tigeot 			"CRTC_H", 0, INT_MAX);
2684be47400SFrançois Tigeot 	if (!prop)
2694be47400SFrançois Tigeot 		return -ENOMEM;
2704be47400SFrançois Tigeot 	dev->mode_config.prop_crtc_h = prop;
2714be47400SFrançois Tigeot 
2724be47400SFrançois Tigeot 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
2734be47400SFrançois Tigeot 			"FB_ID", DRM_MODE_OBJECT_FB);
2744be47400SFrançois Tigeot 	if (!prop)
2754be47400SFrançois Tigeot 		return -ENOMEM;
2764be47400SFrançois Tigeot 	dev->mode_config.prop_fb_id = prop;
2774be47400SFrançois Tigeot 
2784be47400SFrançois Tigeot 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
2794be47400SFrançois Tigeot 			"IN_FENCE_FD", -1, INT_MAX);
2804be47400SFrançois Tigeot 	if (!prop)
2814be47400SFrançois Tigeot 		return -ENOMEM;
2824be47400SFrançois Tigeot 	dev->mode_config.prop_in_fence_fd = prop;
2834be47400SFrançois Tigeot 
2844be47400SFrançois Tigeot 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
2854be47400SFrançois Tigeot 			"OUT_FENCE_PTR", 0, U64_MAX);
2864be47400SFrançois Tigeot 	if (!prop)
2874be47400SFrançois Tigeot 		return -ENOMEM;
2884be47400SFrançois Tigeot 	dev->mode_config.prop_out_fence_ptr = prop;
2894be47400SFrançois Tigeot 
2904be47400SFrançois Tigeot 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
2914be47400SFrançois Tigeot 			"CRTC_ID", DRM_MODE_OBJECT_CRTC);
2924be47400SFrançois Tigeot 	if (!prop)
2934be47400SFrançois Tigeot 		return -ENOMEM;
2944be47400SFrançois Tigeot 	dev->mode_config.prop_crtc_id = prop;
2954be47400SFrançois Tigeot 
2964be47400SFrançois Tigeot 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
2974be47400SFrançois Tigeot 			"ACTIVE");
2984be47400SFrançois Tigeot 	if (!prop)
2994be47400SFrançois Tigeot 		return -ENOMEM;
3004be47400SFrançois Tigeot 	dev->mode_config.prop_active = prop;
3014be47400SFrançois Tigeot 
3024be47400SFrançois Tigeot 	prop = drm_property_create(dev,
3034be47400SFrançois Tigeot 			DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
3044be47400SFrançois Tigeot 			"MODE_ID", 0);
3054be47400SFrançois Tigeot 	if (!prop)
3064be47400SFrançois Tigeot 		return -ENOMEM;
3074be47400SFrançois Tigeot 	dev->mode_config.prop_mode_id = prop;
3084be47400SFrançois Tigeot 
3094be47400SFrançois Tigeot 	prop = drm_property_create(dev,
3104be47400SFrançois Tigeot 			DRM_MODE_PROP_BLOB,
3114be47400SFrançois Tigeot 			"DEGAMMA_LUT", 0);
3124be47400SFrançois Tigeot 	if (!prop)
3134be47400SFrançois Tigeot 		return -ENOMEM;
3144be47400SFrançois Tigeot 	dev->mode_config.degamma_lut_property = prop;
3154be47400SFrançois Tigeot 
3164be47400SFrançois Tigeot 	prop = drm_property_create_range(dev,
3174be47400SFrançois Tigeot 			DRM_MODE_PROP_IMMUTABLE,
3184be47400SFrançois Tigeot 			"DEGAMMA_LUT_SIZE", 0, UINT_MAX);
3194be47400SFrançois Tigeot 	if (!prop)
3204be47400SFrançois Tigeot 		return -ENOMEM;
3214be47400SFrançois Tigeot 	dev->mode_config.degamma_lut_size_property = prop;
3224be47400SFrançois Tigeot 
3234be47400SFrançois Tigeot 	prop = drm_property_create(dev,
3244be47400SFrançois Tigeot 			DRM_MODE_PROP_BLOB,
3254be47400SFrançois Tigeot 			"CTM", 0);
3264be47400SFrançois Tigeot 	if (!prop)
3274be47400SFrançois Tigeot 		return -ENOMEM;
3284be47400SFrançois Tigeot 	dev->mode_config.ctm_property = prop;
3294be47400SFrançois Tigeot 
3304be47400SFrançois Tigeot 	prop = drm_property_create(dev,
3314be47400SFrançois Tigeot 			DRM_MODE_PROP_BLOB,
3324be47400SFrançois Tigeot 			"GAMMA_LUT", 0);
3334be47400SFrançois Tigeot 	if (!prop)
3344be47400SFrançois Tigeot 		return -ENOMEM;
3354be47400SFrançois Tigeot 	dev->mode_config.gamma_lut_property = prop;
3364be47400SFrançois Tigeot 
3374be47400SFrançois Tigeot 	prop = drm_property_create_range(dev,
3384be47400SFrançois Tigeot 			DRM_MODE_PROP_IMMUTABLE,
3394be47400SFrançois Tigeot 			"GAMMA_LUT_SIZE", 0, UINT_MAX);
3404be47400SFrançois Tigeot 	if (!prop)
3414be47400SFrançois Tigeot 		return -ENOMEM;
3424be47400SFrançois Tigeot 	dev->mode_config.gamma_lut_size_property = prop;
3434be47400SFrançois Tigeot 
344*3f2dd94aSFrançois Tigeot 	prop = drm_property_create(dev,
345*3f2dd94aSFrançois Tigeot 				   DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
346*3f2dd94aSFrançois Tigeot 				   "IN_FORMATS", 0);
347*3f2dd94aSFrançois Tigeot 	if (!prop)
348*3f2dd94aSFrançois Tigeot 		return -ENOMEM;
349*3f2dd94aSFrançois Tigeot 	dev->mode_config.modifiers_property = prop;
350*3f2dd94aSFrançois Tigeot 
3514be47400SFrançois Tigeot 	return 0;
3524be47400SFrançois Tigeot }
3534be47400SFrançois Tigeot 
3544be47400SFrançois Tigeot /**
3554be47400SFrançois Tigeot  * drm_mode_config_init - initialize DRM mode_configuration structure
3564be47400SFrançois Tigeot  * @dev: DRM device
3574be47400SFrançois Tigeot  *
3584be47400SFrançois Tigeot  * Initialize @dev's mode_config structure, used for tracking the graphics
3594be47400SFrançois Tigeot  * configuration of @dev.
3604be47400SFrançois Tigeot  *
3614be47400SFrançois Tigeot  * Since this initializes the modeset locks, no locking is possible. Which is no
3624be47400SFrançois Tigeot  * problem, since this should happen single threaded at init time. It is the
3634be47400SFrançois Tigeot  * driver's problem to ensure this guarantee.
3644be47400SFrançois Tigeot  *
3654be47400SFrançois Tigeot  */
drm_mode_config_init(struct drm_device * dev)3664be47400SFrançois Tigeot void drm_mode_config_init(struct drm_device *dev)
3674be47400SFrançois Tigeot {
3684be47400SFrançois Tigeot 	lockinit(&dev->mode_config.mutex, "drmmcm", 0, LK_CANRECURSE);
3694be47400SFrançois Tigeot 	drm_modeset_lock_init(&dev->mode_config.connection_mutex);
3704be47400SFrançois Tigeot 	lockinit(&dev->mode_config.idr_mutex, "mcfgidr", 0, LK_CANRECURSE);
3714be47400SFrançois Tigeot 	lockinit(&dev->mode_config.fb_lock, "drmfbl", 0, LK_CANRECURSE);
3724be47400SFrançois Tigeot 	lockinit(&dev->mode_config.blob_lock, "drmcbl", 0, LK_CANRECURSE);
3734be47400SFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.fb_list);
3744be47400SFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3754be47400SFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.connector_list);
3764be47400SFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3774be47400SFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.property_list);
3784be47400SFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3794be47400SFrançois Tigeot 	INIT_LIST_HEAD(&dev->mode_config.plane_list);
3804be47400SFrançois Tigeot 	idr_init(&dev->mode_config.crtc_idr);
3814be47400SFrançois Tigeot 	idr_init(&dev->mode_config.tile_idr);
3824be47400SFrançois Tigeot 	ida_init(&dev->mode_config.connector_ida);
383a85cb24fSFrançois Tigeot 	lockinit(&dev->mode_config.connector_list_lock, "dmccll", 0, 0);
3844be47400SFrançois Tigeot 
385*3f2dd94aSFrançois Tigeot 	init_llist_head(&dev->mode_config.connector_free_list);
386*3f2dd94aSFrançois Tigeot 	INIT_WORK(&dev->mode_config.connector_free_work, drm_connector_free_work_fn);
387*3f2dd94aSFrançois Tigeot 
3884be47400SFrançois Tigeot 	drm_mode_create_standard_properties(dev);
3894be47400SFrançois Tigeot 
3904be47400SFrançois Tigeot 	/* Just to be sure */
3914be47400SFrançois Tigeot 	dev->mode_config.num_fb = 0;
3924be47400SFrançois Tigeot 	dev->mode_config.num_connector = 0;
3934be47400SFrançois Tigeot 	dev->mode_config.num_crtc = 0;
3944be47400SFrançois Tigeot 	dev->mode_config.num_encoder = 0;
3954be47400SFrançois Tigeot 	dev->mode_config.num_total_plane = 0;
3964be47400SFrançois Tigeot }
3974be47400SFrançois Tigeot EXPORT_SYMBOL(drm_mode_config_init);
3984be47400SFrançois Tigeot 
3994be47400SFrançois Tigeot /**
4004be47400SFrançois Tigeot  * drm_mode_config_cleanup - free up DRM mode_config info
4014be47400SFrançois Tigeot  * @dev: DRM device
4024be47400SFrançois Tigeot  *
4034be47400SFrançois Tigeot  * Free up all the connectors and CRTCs associated with this DRM device, then
4044be47400SFrançois Tigeot  * free up the framebuffers and associated buffer objects.
4054be47400SFrançois Tigeot  *
4064be47400SFrançois Tigeot  * Note that since this /should/ happen single-threaded at driver/device
4074be47400SFrançois Tigeot  * teardown time, no locking is required. It's the driver's job to ensure that
4084be47400SFrançois Tigeot  * this guarantee actually holds true.
4094be47400SFrançois Tigeot  *
4104be47400SFrançois Tigeot  * FIXME: cleanup any dangling user buffer objects too
4114be47400SFrançois Tigeot  */
drm_mode_config_cleanup(struct drm_device * dev)4124be47400SFrançois Tigeot void drm_mode_config_cleanup(struct drm_device *dev)
4134be47400SFrançois Tigeot {
414a85cb24fSFrançois Tigeot 	struct drm_connector *connector;
415a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
4164be47400SFrançois Tigeot 	struct drm_crtc *crtc, *ct;
4174be47400SFrançois Tigeot 	struct drm_encoder *encoder, *enct;
4184be47400SFrançois Tigeot 	struct drm_framebuffer *fb, *fbt;
4194be47400SFrançois Tigeot 	struct drm_property *property, *pt;
4204be47400SFrançois Tigeot 	struct drm_property_blob *blob, *bt;
4214be47400SFrançois Tigeot 	struct drm_plane *plane, *plt;
4224be47400SFrançois Tigeot 
4234be47400SFrançois Tigeot 	list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4244be47400SFrançois Tigeot 				 head) {
4254be47400SFrançois Tigeot 		encoder->funcs->destroy(encoder);
4264be47400SFrançois Tigeot 	}
4274be47400SFrançois Tigeot 
428a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
429a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter) {
430a85cb24fSFrançois Tigeot 		/* drm_connector_list_iter holds an full reference to the
431a85cb24fSFrançois Tigeot 		 * current connector itself, which means it is inherently safe
432a85cb24fSFrançois Tigeot 		 * against unreferencing the current connector - but not against
433a85cb24fSFrançois Tigeot 		 * deleting it right away. */
434a85cb24fSFrançois Tigeot 		drm_connector_put(connector);
435a85cb24fSFrançois Tigeot 	}
436a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
437*3f2dd94aSFrançois Tigeot 	/* connector_iter drops references in a work item. */
438*3f2dd94aSFrançois Tigeot 	flush_work(&dev->mode_config.connector_free_work);
439a85cb24fSFrançois Tigeot 	if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
440a85cb24fSFrançois Tigeot 		drm_connector_list_iter_begin(dev, &conn_iter);
441a85cb24fSFrançois Tigeot 		drm_for_each_connector_iter(connector, &conn_iter)
442a85cb24fSFrançois Tigeot 			DRM_ERROR("connector %s leaked!\n", connector->name);
443a85cb24fSFrançois Tigeot 		drm_connector_list_iter_end(&conn_iter);
4444be47400SFrançois Tigeot 	}
4454be47400SFrançois Tigeot 
4464be47400SFrançois Tigeot 	list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4474be47400SFrançois Tigeot 				 head) {
4484be47400SFrançois Tigeot 		drm_property_destroy(dev, property);
4494be47400SFrançois Tigeot 	}
4504be47400SFrançois Tigeot 
4514be47400SFrançois Tigeot 	list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4524be47400SFrançois Tigeot 				 head) {
4534be47400SFrançois Tigeot 		plane->funcs->destroy(plane);
4544be47400SFrançois Tigeot 	}
4554be47400SFrançois Tigeot 
4564be47400SFrançois Tigeot 	list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4574be47400SFrançois Tigeot 		crtc->funcs->destroy(crtc);
4584be47400SFrançois Tigeot 	}
4594be47400SFrançois Tigeot 
4604be47400SFrançois Tigeot 	list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4614be47400SFrançois Tigeot 				 head_global) {
462*3f2dd94aSFrançois Tigeot 		drm_property_blob_put(blob);
4634be47400SFrançois Tigeot 	}
4644be47400SFrançois Tigeot 
4654be47400SFrançois Tigeot 	/*
4664be47400SFrançois Tigeot 	 * Single-threaded teardown context, so it's not required to grab the
4674be47400SFrançois Tigeot 	 * fb_lock to protect against concurrent fb_list access. Contrary, it
4684be47400SFrançois Tigeot 	 * would actually deadlock with the drm_framebuffer_cleanup function.
4694be47400SFrançois Tigeot 	 *
4704be47400SFrançois Tigeot 	 * Also, if there are any framebuffers left, that's a driver leak now,
4714be47400SFrançois Tigeot 	 * so politely WARN about this.
4724be47400SFrançois Tigeot 	 */
4734be47400SFrançois Tigeot 	WARN_ON(!list_empty(&dev->mode_config.fb_list));
4744be47400SFrançois Tigeot 	list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4754be47400SFrançois Tigeot 		drm_framebuffer_free(&fb->base.refcount);
4764be47400SFrançois Tigeot 	}
4774be47400SFrançois Tigeot 
4784be47400SFrançois Tigeot 	ida_destroy(&dev->mode_config.connector_ida);
4794be47400SFrançois Tigeot 	idr_destroy(&dev->mode_config.tile_idr);
4804be47400SFrançois Tigeot 	idr_destroy(&dev->mode_config.crtc_idr);
4814be47400SFrançois Tigeot 	drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
4824be47400SFrançois Tigeot }
4834be47400SFrançois Tigeot EXPORT_SYMBOL(drm_mode_config_cleanup);
484