xref: /dflybsd-src/sys/dev/drm/drm_plane.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
11dedbd3bSFrançois Tigeot /*
21dedbd3bSFrançois Tigeot  * Copyright (c) 2016 Intel Corporation
31dedbd3bSFrançois Tigeot  *
41dedbd3bSFrançois Tigeot  * Permission to use, copy, modify, distribute, and sell this software and its
51dedbd3bSFrançois Tigeot  * documentation for any purpose is hereby granted without fee, provided that
61dedbd3bSFrançois Tigeot  * the above copyright notice appear in all copies and that both that copyright
71dedbd3bSFrançois Tigeot  * notice and this permission notice appear in supporting documentation, and
81dedbd3bSFrançois Tigeot  * that the name of the copyright holders not be used in advertising or
91dedbd3bSFrançois Tigeot  * publicity pertaining to distribution of the software without specific,
101dedbd3bSFrançois Tigeot  * written prior permission.  The copyright holders make no representations
111dedbd3bSFrançois Tigeot  * about the suitability of this software for any purpose.  It is provided "as
121dedbd3bSFrançois Tigeot  * is" without express or implied warranty.
131dedbd3bSFrançois Tigeot  *
141dedbd3bSFrançois Tigeot  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
151dedbd3bSFrançois Tigeot  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
161dedbd3bSFrançois Tigeot  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
171dedbd3bSFrançois Tigeot  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
181dedbd3bSFrançois Tigeot  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
191dedbd3bSFrançois Tigeot  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
201dedbd3bSFrançois Tigeot  * OF THIS SOFTWARE.
211dedbd3bSFrançois Tigeot  */
221dedbd3bSFrançois Tigeot 
231dedbd3bSFrançois Tigeot #include <drm/drmP.h>
241dedbd3bSFrançois Tigeot #include <drm/drm_plane.h>
251dedbd3bSFrançois Tigeot 
261dedbd3bSFrançois Tigeot #include "drm_crtc_internal.h"
271dedbd3bSFrançois Tigeot 
281dedbd3bSFrançois Tigeot /**
291dedbd3bSFrançois Tigeot  * DOC: overview
301dedbd3bSFrançois Tigeot  *
311dedbd3bSFrançois Tigeot  * A plane represents an image source that can be blended with or overlayed on
321dedbd3bSFrançois Tigeot  * top of a CRTC during the scanout process. Planes take their input data from a
331dedbd3bSFrançois Tigeot  * &drm_framebuffer object. The plane itself specifies the cropping and scaling
341dedbd3bSFrançois Tigeot  * of that image, and where it is placed on the visible are of a display
351dedbd3bSFrançois Tigeot  * pipeline, represented by &drm_crtc. A plane can also have additional
361dedbd3bSFrançois Tigeot  * properties that specify how the pixels are positioned and blended, like
371dedbd3bSFrançois Tigeot  * rotation or Z-position. All these properties are stored in &drm_plane_state.
381dedbd3bSFrançois Tigeot  *
391dedbd3bSFrançois Tigeot  * To create a plane, a KMS drivers allocates and zeroes an instances of
40a85cb24fSFrançois Tigeot  * &struct drm_plane (possibly as part of a larger structure) and registers it
411dedbd3bSFrançois Tigeot  * with a call to drm_universal_plane_init().
421dedbd3bSFrançois Tigeot  *
431dedbd3bSFrançois Tigeot  * Cursor and overlay planes are optional. All drivers should provide one
441dedbd3bSFrançois Tigeot  * primary plane per CRTC to avoid surprising userspace too much. See enum
45a85cb24fSFrançois Tigeot  * drm_plane_type for a more in-depth discussion of these special uapi-relevant
461dedbd3bSFrançois Tigeot  * plane types. Special planes are associated with their CRTC by calling
471dedbd3bSFrançois Tigeot  * drm_crtc_init_with_planes().
481dedbd3bSFrançois Tigeot  *
491dedbd3bSFrançois Tigeot  * The type of a plane is exposed in the immutable "type" enumeration property,
501dedbd3bSFrançois Tigeot  * which has one of the following values: "Overlay", "Primary", "Cursor".
511dedbd3bSFrançois Tigeot  */
521dedbd3bSFrançois Tigeot 
drm_num_planes(struct drm_device * dev)531dedbd3bSFrançois Tigeot static unsigned int drm_num_planes(struct drm_device *dev)
541dedbd3bSFrançois Tigeot {
551dedbd3bSFrançois Tigeot 	unsigned int num = 0;
561dedbd3bSFrançois Tigeot 	struct drm_plane *tmp;
571dedbd3bSFrançois Tigeot 
581dedbd3bSFrançois Tigeot 	drm_for_each_plane(tmp, dev) {
591dedbd3bSFrançois Tigeot 		num++;
601dedbd3bSFrançois Tigeot 	}
611dedbd3bSFrançois Tigeot 
621dedbd3bSFrançois Tigeot 	return num;
631dedbd3bSFrançois Tigeot }
641dedbd3bSFrançois Tigeot 
65*3f2dd94aSFrançois Tigeot static inline u32 *
formats_ptr(struct drm_format_modifier_blob * blob)66*3f2dd94aSFrançois Tigeot formats_ptr(struct drm_format_modifier_blob *blob)
67*3f2dd94aSFrançois Tigeot {
68*3f2dd94aSFrançois Tigeot 	return (u32 *)(((char *)blob) + blob->formats_offset);
69*3f2dd94aSFrançois Tigeot }
70*3f2dd94aSFrançois Tigeot 
71*3f2dd94aSFrançois Tigeot static inline struct drm_format_modifier *
modifiers_ptr(struct drm_format_modifier_blob * blob)72*3f2dd94aSFrançois Tigeot modifiers_ptr(struct drm_format_modifier_blob *blob)
73*3f2dd94aSFrançois Tigeot {
74*3f2dd94aSFrançois Tigeot 	return (struct drm_format_modifier *)(((char *)blob) + blob->modifiers_offset);
75*3f2dd94aSFrançois Tigeot }
76*3f2dd94aSFrançois Tigeot 
create_in_format_blob(struct drm_device * dev,struct drm_plane * plane)77*3f2dd94aSFrançois Tigeot static int create_in_format_blob(struct drm_device *dev, struct drm_plane *plane)
78*3f2dd94aSFrançois Tigeot {
79*3f2dd94aSFrançois Tigeot 	const struct drm_mode_config *config = &dev->mode_config;
80*3f2dd94aSFrançois Tigeot 	struct drm_property_blob *blob;
81*3f2dd94aSFrançois Tigeot 	struct drm_format_modifier *mod;
82*3f2dd94aSFrançois Tigeot 	size_t blob_size, formats_size, modifiers_size;
83*3f2dd94aSFrançois Tigeot 	struct drm_format_modifier_blob *blob_data;
84*3f2dd94aSFrançois Tigeot 	unsigned int i, j;
85*3f2dd94aSFrançois Tigeot 
86*3f2dd94aSFrançois Tigeot 	formats_size = sizeof(__u32) * plane->format_count;
87*3f2dd94aSFrançois Tigeot 	if (WARN_ON(!formats_size)) {
88*3f2dd94aSFrançois Tigeot 		/* 0 formats are never expected */
89*3f2dd94aSFrançois Tigeot 		return 0;
90*3f2dd94aSFrançois Tigeot 	}
91*3f2dd94aSFrançois Tigeot 
92*3f2dd94aSFrançois Tigeot 	modifiers_size =
93*3f2dd94aSFrançois Tigeot 		sizeof(struct drm_format_modifier) * plane->modifier_count;
94*3f2dd94aSFrançois Tigeot 
95*3f2dd94aSFrançois Tigeot 	blob_size = sizeof(struct drm_format_modifier_blob);
96*3f2dd94aSFrançois Tigeot 	/* Modifiers offset is a pointer to a struct with a 64 bit field so it
97*3f2dd94aSFrançois Tigeot 	 * should be naturally aligned to 8B.
98*3f2dd94aSFrançois Tigeot 	 */
99*3f2dd94aSFrançois Tigeot 	BUILD_BUG_ON(sizeof(struct drm_format_modifier_blob) % 8);
100*3f2dd94aSFrançois Tigeot 	blob_size += ALIGN(formats_size, 8);
101*3f2dd94aSFrançois Tigeot 	blob_size += modifiers_size;
102*3f2dd94aSFrançois Tigeot 
103*3f2dd94aSFrançois Tigeot 	blob = drm_property_create_blob(dev, blob_size, NULL);
104*3f2dd94aSFrançois Tigeot 	if (IS_ERR(blob))
105*3f2dd94aSFrançois Tigeot 		return -1;
106*3f2dd94aSFrançois Tigeot 
107*3f2dd94aSFrançois Tigeot 	blob_data = (struct drm_format_modifier_blob *)blob->data;
108*3f2dd94aSFrançois Tigeot 	blob_data->version = FORMAT_BLOB_CURRENT;
109*3f2dd94aSFrançois Tigeot 	blob_data->count_formats = plane->format_count;
110*3f2dd94aSFrançois Tigeot 	blob_data->formats_offset = sizeof(struct drm_format_modifier_blob);
111*3f2dd94aSFrançois Tigeot 	blob_data->count_modifiers = plane->modifier_count;
112*3f2dd94aSFrançois Tigeot 
113*3f2dd94aSFrançois Tigeot 	blob_data->modifiers_offset =
114*3f2dd94aSFrançois Tigeot 		ALIGN(blob_data->formats_offset + formats_size, 8);
115*3f2dd94aSFrançois Tigeot 
116*3f2dd94aSFrançois Tigeot 	memcpy(formats_ptr(blob_data), plane->format_types, formats_size);
117*3f2dd94aSFrançois Tigeot 
118*3f2dd94aSFrançois Tigeot 	/* If we can't determine support, just bail */
119*3f2dd94aSFrançois Tigeot 	if (!plane->funcs->format_mod_supported)
120*3f2dd94aSFrançois Tigeot 		goto done;
121*3f2dd94aSFrançois Tigeot 
122*3f2dd94aSFrançois Tigeot 	mod = modifiers_ptr(blob_data);
123*3f2dd94aSFrançois Tigeot 	for (i = 0; i < plane->modifier_count; i++) {
124*3f2dd94aSFrançois Tigeot 		for (j = 0; j < plane->format_count; j++) {
125*3f2dd94aSFrançois Tigeot 			if (plane->funcs->format_mod_supported(plane,
126*3f2dd94aSFrançois Tigeot 							       plane->format_types[j],
127*3f2dd94aSFrançois Tigeot 							       plane->modifiers[i])) {
128*3f2dd94aSFrançois Tigeot 
129*3f2dd94aSFrançois Tigeot 				mod->formats |= 1ULL << j;
130*3f2dd94aSFrançois Tigeot 			}
131*3f2dd94aSFrançois Tigeot 		}
132*3f2dd94aSFrançois Tigeot 
133*3f2dd94aSFrançois Tigeot 		mod->modifier = plane->modifiers[i];
134*3f2dd94aSFrançois Tigeot 		mod->offset = 0;
135*3f2dd94aSFrançois Tigeot 		mod->pad = 0;
136*3f2dd94aSFrançois Tigeot 		mod++;
137*3f2dd94aSFrançois Tigeot 	}
138*3f2dd94aSFrançois Tigeot 
139*3f2dd94aSFrançois Tigeot done:
140*3f2dd94aSFrançois Tigeot 	drm_object_attach_property(&plane->base, config->modifiers_property,
141*3f2dd94aSFrançois Tigeot 				   blob->base.id);
142*3f2dd94aSFrançois Tigeot 
143*3f2dd94aSFrançois Tigeot 	return 0;
144*3f2dd94aSFrançois Tigeot }
145*3f2dd94aSFrançois Tigeot 
1461dedbd3bSFrançois Tigeot /**
1471dedbd3bSFrançois Tigeot  * drm_universal_plane_init - Initialize a new universal plane object
1481dedbd3bSFrançois Tigeot  * @dev: DRM device
1491dedbd3bSFrançois Tigeot  * @plane: plane object to init
1501dedbd3bSFrançois Tigeot  * @possible_crtcs: bitmask of possible CRTCs
1511dedbd3bSFrançois Tigeot  * @funcs: callbacks for the new plane
1521dedbd3bSFrançois Tigeot  * @formats: array of supported formats (DRM_FORMAT\_\*)
1531dedbd3bSFrançois Tigeot  * @format_count: number of elements in @formats
154*3f2dd94aSFrançois Tigeot  * @format_modifiers: array of struct drm_format modifiers terminated by
155*3f2dd94aSFrançois Tigeot  *                    DRM_FORMAT_MOD_INVALID
1561dedbd3bSFrançois Tigeot  * @type: type of plane (overlay, primary, cursor)
1571dedbd3bSFrançois Tigeot  * @name: printf style format string for the plane name, or NULL for default name
1581dedbd3bSFrançois Tigeot  *
1591dedbd3bSFrançois Tigeot  * Initializes a plane object of type @type.
1601dedbd3bSFrançois Tigeot  *
1611dedbd3bSFrançois Tigeot  * Returns:
1621dedbd3bSFrançois Tigeot  * Zero on success, error code on failure.
1631dedbd3bSFrançois Tigeot  */
drm_universal_plane_init(struct drm_device * dev,struct drm_plane * plane,uint32_t possible_crtcs,const struct drm_plane_funcs * funcs,const uint32_t * formats,unsigned int format_count,const uint64_t * format_modifiers,enum drm_plane_type type,const char * name,...)1641dedbd3bSFrançois Tigeot int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1654be47400SFrançois Tigeot 			     uint32_t possible_crtcs,
1661dedbd3bSFrançois Tigeot 			     const struct drm_plane_funcs *funcs,
1671dedbd3bSFrançois Tigeot 			     const uint32_t *formats, unsigned int format_count,
168*3f2dd94aSFrançois Tigeot 			     const uint64_t *format_modifiers,
1691dedbd3bSFrançois Tigeot 			     enum drm_plane_type type,
1701dedbd3bSFrançois Tigeot 			     const char *name, ...)
1711dedbd3bSFrançois Tigeot {
1721dedbd3bSFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
173*3f2dd94aSFrançois Tigeot 	unsigned int format_modifier_count = 0;
1741dedbd3bSFrançois Tigeot 	int ret;
1751dedbd3bSFrançois Tigeot 
176a85cb24fSFrançois Tigeot 	ret = drm_mode_object_add(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1771dedbd3bSFrançois Tigeot 	if (ret)
1781dedbd3bSFrançois Tigeot 		return ret;
1791dedbd3bSFrançois Tigeot 
1801dedbd3bSFrançois Tigeot 	drm_modeset_lock_init(&plane->mutex);
1811dedbd3bSFrançois Tigeot 
1821dedbd3bSFrançois Tigeot 	plane->base.properties = &plane->properties;
1831dedbd3bSFrançois Tigeot 	plane->dev = dev;
1841dedbd3bSFrançois Tigeot 	plane->funcs = funcs;
1851dedbd3bSFrançois Tigeot 	plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1861dedbd3bSFrançois Tigeot 					    GFP_KERNEL);
1871dedbd3bSFrançois Tigeot 	if (!plane->format_types) {
1881dedbd3bSFrançois Tigeot 		DRM_DEBUG_KMS("out of memory when allocating plane\n");
1891dedbd3bSFrançois Tigeot 		drm_mode_object_unregister(dev, &plane->base);
1901dedbd3bSFrançois Tigeot 		return -ENOMEM;
1911dedbd3bSFrançois Tigeot 	}
1921dedbd3bSFrançois Tigeot 
193*3f2dd94aSFrançois Tigeot 	/*
194*3f2dd94aSFrançois Tigeot 	 * First driver to need more than 64 formats needs to fix this. Each
195*3f2dd94aSFrançois Tigeot 	 * format is encoded as a bit and the current code only supports a u64.
196*3f2dd94aSFrançois Tigeot 	 */
197*3f2dd94aSFrançois Tigeot 	if (WARN_ON(format_count > 64))
198*3f2dd94aSFrançois Tigeot 		return -EINVAL;
199*3f2dd94aSFrançois Tigeot 
200*3f2dd94aSFrançois Tigeot 	if (format_modifiers) {
201*3f2dd94aSFrançois Tigeot 		const uint64_t *temp_modifiers = format_modifiers;
202*3f2dd94aSFrançois Tigeot 		while (*temp_modifiers++ != DRM_FORMAT_MOD_INVALID)
203*3f2dd94aSFrançois Tigeot 			format_modifier_count++;
204*3f2dd94aSFrançois Tigeot 	}
205*3f2dd94aSFrançois Tigeot 
206*3f2dd94aSFrançois Tigeot 	plane->modifier_count = format_modifier_count;
207*3f2dd94aSFrançois Tigeot 	plane->modifiers = kmalloc_array(format_modifier_count,
208*3f2dd94aSFrançois Tigeot 					 sizeof(format_modifiers[0]),
209*3f2dd94aSFrançois Tigeot 					 GFP_KERNEL);
210*3f2dd94aSFrançois Tigeot 
211*3f2dd94aSFrançois Tigeot 	if (format_modifier_count && !plane->modifiers) {
212*3f2dd94aSFrançois Tigeot 		DRM_DEBUG_KMS("out of memory when allocating plane\n");
213*3f2dd94aSFrançois Tigeot 		kfree(plane->format_types);
214*3f2dd94aSFrançois Tigeot 		drm_mode_object_unregister(dev, &plane->base);
215*3f2dd94aSFrançois Tigeot 		return -ENOMEM;
216*3f2dd94aSFrançois Tigeot 	}
217*3f2dd94aSFrançois Tigeot 
2181dedbd3bSFrançois Tigeot 	if (name) {
2191dedbd3bSFrançois Tigeot 		va_list ap;
2201dedbd3bSFrançois Tigeot 
2211dedbd3bSFrançois Tigeot 		va_start(ap, name);
2221dedbd3bSFrançois Tigeot 		plane->name = kvasprintf(GFP_KERNEL, name, ap);
2231dedbd3bSFrançois Tigeot 		va_end(ap);
2241dedbd3bSFrançois Tigeot 	} else {
2251dedbd3bSFrançois Tigeot 		plane->name = kasprintf(GFP_KERNEL, "plane-%d",
2261dedbd3bSFrançois Tigeot 					drm_num_planes(dev));
2271dedbd3bSFrançois Tigeot 	}
2281dedbd3bSFrançois Tigeot 	if (!plane->name) {
2291dedbd3bSFrançois Tigeot 		kfree(plane->format_types);
230*3f2dd94aSFrançois Tigeot 		kfree(plane->modifiers);
2311dedbd3bSFrançois Tigeot 		drm_mode_object_unregister(dev, &plane->base);
2321dedbd3bSFrançois Tigeot 		return -ENOMEM;
2331dedbd3bSFrançois Tigeot 	}
2341dedbd3bSFrançois Tigeot 
2351dedbd3bSFrançois Tigeot 	memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
2361dedbd3bSFrançois Tigeot 	plane->format_count = format_count;
237*3f2dd94aSFrançois Tigeot 	memcpy(plane->modifiers, format_modifiers,
238*3f2dd94aSFrançois Tigeot 	       format_modifier_count * sizeof(format_modifiers[0]));
2391dedbd3bSFrançois Tigeot 	plane->possible_crtcs = possible_crtcs;
2401dedbd3bSFrançois Tigeot 	plane->type = type;
2411dedbd3bSFrançois Tigeot 
2421dedbd3bSFrançois Tigeot 	list_add_tail(&plane->head, &config->plane_list);
2431dedbd3bSFrançois Tigeot 	plane->index = config->num_total_plane++;
2441dedbd3bSFrançois Tigeot 
2451dedbd3bSFrançois Tigeot 	drm_object_attach_property(&plane->base,
2461dedbd3bSFrançois Tigeot 				   config->plane_type_property,
2471dedbd3bSFrançois Tigeot 				   plane->type);
2481dedbd3bSFrançois Tigeot 
2491dedbd3bSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
2501dedbd3bSFrançois Tigeot 		drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
2514be47400SFrançois Tigeot 		drm_object_attach_property(&plane->base, config->prop_in_fence_fd, -1);
2521dedbd3bSFrançois Tigeot 		drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
2531dedbd3bSFrançois Tigeot 		drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
2541dedbd3bSFrançois Tigeot 		drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
2551dedbd3bSFrançois Tigeot 		drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
2561dedbd3bSFrançois Tigeot 		drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
2571dedbd3bSFrançois Tigeot 		drm_object_attach_property(&plane->base, config->prop_src_x, 0);
2581dedbd3bSFrançois Tigeot 		drm_object_attach_property(&plane->base, config->prop_src_y, 0);
2591dedbd3bSFrançois Tigeot 		drm_object_attach_property(&plane->base, config->prop_src_w, 0);
2601dedbd3bSFrançois Tigeot 		drm_object_attach_property(&plane->base, config->prop_src_h, 0);
2611dedbd3bSFrançois Tigeot 	}
2621dedbd3bSFrançois Tigeot 
263*3f2dd94aSFrançois Tigeot 	if (config->allow_fb_modifiers)
264*3f2dd94aSFrançois Tigeot 		create_in_format_blob(dev, plane);
265*3f2dd94aSFrançois Tigeot 
2661dedbd3bSFrançois Tigeot 	return 0;
2671dedbd3bSFrançois Tigeot }
2681dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_universal_plane_init);
2691dedbd3bSFrançois Tigeot 
drm_plane_register_all(struct drm_device * dev)2701dedbd3bSFrançois Tigeot int drm_plane_register_all(struct drm_device *dev)
2711dedbd3bSFrançois Tigeot {
2721dedbd3bSFrançois Tigeot 	struct drm_plane *plane;
2731dedbd3bSFrançois Tigeot 	int ret = 0;
2741dedbd3bSFrançois Tigeot 
2751dedbd3bSFrançois Tigeot 	drm_for_each_plane(plane, dev) {
2761dedbd3bSFrançois Tigeot 		if (plane->funcs->late_register)
2771dedbd3bSFrançois Tigeot 			ret = plane->funcs->late_register(plane);
2781dedbd3bSFrançois Tigeot 		if (ret)
2791dedbd3bSFrançois Tigeot 			return ret;
2801dedbd3bSFrançois Tigeot 	}
2811dedbd3bSFrançois Tigeot 
2821dedbd3bSFrançois Tigeot 	return 0;
2831dedbd3bSFrançois Tigeot }
2841dedbd3bSFrançois Tigeot 
drm_plane_unregister_all(struct drm_device * dev)2851dedbd3bSFrançois Tigeot void drm_plane_unregister_all(struct drm_device *dev)
2861dedbd3bSFrançois Tigeot {
2871dedbd3bSFrançois Tigeot 	struct drm_plane *plane;
2881dedbd3bSFrançois Tigeot 
2891dedbd3bSFrançois Tigeot 	drm_for_each_plane(plane, dev) {
2901dedbd3bSFrançois Tigeot 		if (plane->funcs->early_unregister)
2911dedbd3bSFrançois Tigeot 			plane->funcs->early_unregister(plane);
2921dedbd3bSFrançois Tigeot 	}
2931dedbd3bSFrançois Tigeot }
2941dedbd3bSFrançois Tigeot 
2951dedbd3bSFrançois Tigeot /**
2961dedbd3bSFrançois Tigeot  * drm_plane_init - Initialize a legacy plane
2971dedbd3bSFrançois Tigeot  * @dev: DRM device
2981dedbd3bSFrançois Tigeot  * @plane: plane object to init
2991dedbd3bSFrançois Tigeot  * @possible_crtcs: bitmask of possible CRTCs
3001dedbd3bSFrançois Tigeot  * @funcs: callbacks for the new plane
3011dedbd3bSFrançois Tigeot  * @formats: array of supported formats (DRM_FORMAT\_\*)
3021dedbd3bSFrançois Tigeot  * @format_count: number of elements in @formats
3031dedbd3bSFrançois Tigeot  * @is_primary: plane type (primary vs overlay)
3041dedbd3bSFrançois Tigeot  *
3051dedbd3bSFrançois Tigeot  * Legacy API to initialize a DRM plane.
3061dedbd3bSFrançois Tigeot  *
3071dedbd3bSFrançois Tigeot  * New drivers should call drm_universal_plane_init() instead.
3081dedbd3bSFrançois Tigeot  *
3091dedbd3bSFrançois Tigeot  * Returns:
3101dedbd3bSFrançois Tigeot  * Zero on success, error code on failure.
3111dedbd3bSFrançois Tigeot  */
drm_plane_init(struct drm_device * dev,struct drm_plane * plane,uint32_t possible_crtcs,const struct drm_plane_funcs * funcs,const uint32_t * formats,unsigned int format_count,bool is_primary)3121dedbd3bSFrançois Tigeot int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
3134be47400SFrançois Tigeot 		   uint32_t possible_crtcs,
3141dedbd3bSFrançois Tigeot 		   const struct drm_plane_funcs *funcs,
3151dedbd3bSFrançois Tigeot 		   const uint32_t *formats, unsigned int format_count,
3161dedbd3bSFrançois Tigeot 		   bool is_primary)
3171dedbd3bSFrançois Tigeot {
3181dedbd3bSFrançois Tigeot 	enum drm_plane_type type;
3191dedbd3bSFrançois Tigeot 
3201dedbd3bSFrançois Tigeot 	type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
3211dedbd3bSFrançois Tigeot 	return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
322*3f2dd94aSFrançois Tigeot 					formats, format_count,
323*3f2dd94aSFrançois Tigeot 					NULL, type, NULL);
3241dedbd3bSFrançois Tigeot }
3251dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_plane_init);
3261dedbd3bSFrançois Tigeot 
3271dedbd3bSFrançois Tigeot /**
3281dedbd3bSFrançois Tigeot  * drm_plane_cleanup - Clean up the core plane usage
3291dedbd3bSFrançois Tigeot  * @plane: plane to cleanup
3301dedbd3bSFrançois Tigeot  *
3311dedbd3bSFrançois Tigeot  * This function cleans up @plane and removes it from the DRM mode setting
3321dedbd3bSFrançois Tigeot  * core. Note that the function does *not* free the plane structure itself,
3331dedbd3bSFrançois Tigeot  * this is the responsibility of the caller.
3341dedbd3bSFrançois Tigeot  */
drm_plane_cleanup(struct drm_plane * plane)3351dedbd3bSFrançois Tigeot void drm_plane_cleanup(struct drm_plane *plane)
3361dedbd3bSFrançois Tigeot {
3371dedbd3bSFrançois Tigeot 	struct drm_device *dev = plane->dev;
3381dedbd3bSFrançois Tigeot 
3394be47400SFrançois Tigeot 	drm_modeset_lock_fini(&plane->mutex);
3404be47400SFrançois Tigeot 
3411dedbd3bSFrançois Tigeot 	kfree(plane->format_types);
342*3f2dd94aSFrançois Tigeot 	kfree(plane->modifiers);
3431dedbd3bSFrançois Tigeot 	drm_mode_object_unregister(dev, &plane->base);
3441dedbd3bSFrançois Tigeot 
3451dedbd3bSFrançois Tigeot 	BUG_ON(list_empty(&plane->head));
3461dedbd3bSFrançois Tigeot 
3471dedbd3bSFrançois Tigeot 	/* Note that the plane_list is considered to be static; should we
3481dedbd3bSFrançois Tigeot 	 * remove the drm_plane at runtime we would have to decrement all
3491dedbd3bSFrançois Tigeot 	 * the indices on the drm_plane after us in the plane_list.
3501dedbd3bSFrançois Tigeot 	 */
3511dedbd3bSFrançois Tigeot 
3521dedbd3bSFrançois Tigeot 	list_del(&plane->head);
3531dedbd3bSFrançois Tigeot 	dev->mode_config.num_total_plane--;
3541dedbd3bSFrançois Tigeot 
3551dedbd3bSFrançois Tigeot 	WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
3561dedbd3bSFrançois Tigeot 	if (plane->state && plane->funcs->atomic_destroy_state)
3571dedbd3bSFrançois Tigeot 		plane->funcs->atomic_destroy_state(plane, plane->state);
3581dedbd3bSFrançois Tigeot 
3591dedbd3bSFrançois Tigeot 	kfree(plane->name);
3601dedbd3bSFrançois Tigeot 
3611dedbd3bSFrançois Tigeot 	memset(plane, 0, sizeof(*plane));
3621dedbd3bSFrançois Tigeot }
3631dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_plane_cleanup);
3641dedbd3bSFrançois Tigeot 
3651dedbd3bSFrançois Tigeot /**
3661dedbd3bSFrançois Tigeot  * drm_plane_from_index - find the registered plane at an index
3671dedbd3bSFrançois Tigeot  * @dev: DRM device
3681dedbd3bSFrançois Tigeot  * @idx: index of registered plane to find for
3691dedbd3bSFrançois Tigeot  *
3701dedbd3bSFrançois Tigeot  * Given a plane index, return the registered plane from DRM device's
371a85cb24fSFrançois Tigeot  * list of planes with matching index. This is the inverse of drm_plane_index().
3721dedbd3bSFrançois Tigeot  */
3731dedbd3bSFrançois Tigeot struct drm_plane *
drm_plane_from_index(struct drm_device * dev,int idx)3741dedbd3bSFrançois Tigeot drm_plane_from_index(struct drm_device *dev, int idx)
3751dedbd3bSFrançois Tigeot {
3761dedbd3bSFrançois Tigeot 	struct drm_plane *plane;
3771dedbd3bSFrançois Tigeot 
3781dedbd3bSFrançois Tigeot 	drm_for_each_plane(plane, dev)
3791dedbd3bSFrançois Tigeot 		if (idx == plane->index)
3801dedbd3bSFrançois Tigeot 			return plane;
3811dedbd3bSFrançois Tigeot 
3821dedbd3bSFrançois Tigeot 	return NULL;
3831dedbd3bSFrançois Tigeot }
3841dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_plane_from_index);
3851dedbd3bSFrançois Tigeot 
3861dedbd3bSFrançois Tigeot /**
3871dedbd3bSFrançois Tigeot  * drm_plane_force_disable - Forcibly disable a plane
3881dedbd3bSFrançois Tigeot  * @plane: plane to disable
3891dedbd3bSFrançois Tigeot  *
3901dedbd3bSFrançois Tigeot  * Forces the plane to be disabled.
3911dedbd3bSFrançois Tigeot  *
3921dedbd3bSFrançois Tigeot  * Used when the plane's current framebuffer is destroyed,
3931dedbd3bSFrançois Tigeot  * and when restoring fbdev mode.
394a85cb24fSFrançois Tigeot  *
395a85cb24fSFrançois Tigeot  * Note that this function is not suitable for atomic drivers, since it doesn't
396a85cb24fSFrançois Tigeot  * wire through the lock acquisition context properly and hence can't handle
397a85cb24fSFrançois Tigeot  * retries or driver private locks. You probably want to use
398a85cb24fSFrançois Tigeot  * drm_atomic_helper_disable_plane() or
399a85cb24fSFrançois Tigeot  * drm_atomic_helper_disable_planes_on_crtc() instead.
4001dedbd3bSFrançois Tigeot  */
drm_plane_force_disable(struct drm_plane * plane)4011dedbd3bSFrançois Tigeot void drm_plane_force_disable(struct drm_plane *plane)
4021dedbd3bSFrançois Tigeot {
4031dedbd3bSFrançois Tigeot 	int ret;
4041dedbd3bSFrançois Tigeot 
4051dedbd3bSFrançois Tigeot 	if (!plane->fb)
4061dedbd3bSFrançois Tigeot 		return;
4071dedbd3bSFrançois Tigeot 
408a85cb24fSFrançois Tigeot 	WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
409a85cb24fSFrançois Tigeot 
4101dedbd3bSFrançois Tigeot 	plane->old_fb = plane->fb;
411a85cb24fSFrançois Tigeot 	ret = plane->funcs->disable_plane(plane, NULL);
4121dedbd3bSFrançois Tigeot 	if (ret) {
4131dedbd3bSFrançois Tigeot 		DRM_ERROR("failed to disable plane with busy fb\n");
4141dedbd3bSFrançois Tigeot 		plane->old_fb = NULL;
4151dedbd3bSFrançois Tigeot 		return;
4161dedbd3bSFrançois Tigeot 	}
4171dedbd3bSFrançois Tigeot 	/* disconnect the plane from the fb and crtc: */
418a85cb24fSFrançois Tigeot 	drm_framebuffer_put(plane->old_fb);
4191dedbd3bSFrançois Tigeot 	plane->old_fb = NULL;
4201dedbd3bSFrançois Tigeot 	plane->fb = NULL;
4211dedbd3bSFrançois Tigeot 	plane->crtc = NULL;
4221dedbd3bSFrançois Tigeot }
4231dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_plane_force_disable);
4241dedbd3bSFrançois Tigeot 
4251dedbd3bSFrançois Tigeot /**
4261dedbd3bSFrançois Tigeot  * drm_mode_plane_set_obj_prop - set the value of a property
4271dedbd3bSFrançois Tigeot  * @plane: drm plane object to set property value for
4281dedbd3bSFrançois Tigeot  * @property: property to set
4291dedbd3bSFrançois Tigeot  * @value: value the property should be set to
4301dedbd3bSFrançois Tigeot  *
4311dedbd3bSFrançois Tigeot  * This functions sets a given property on a given plane object. This function
4321dedbd3bSFrançois Tigeot  * calls the driver's ->set_property callback and changes the software state of
4331dedbd3bSFrançois Tigeot  * the property if the callback succeeds.
4341dedbd3bSFrançois Tigeot  *
4351dedbd3bSFrançois Tigeot  * Returns:
4361dedbd3bSFrançois Tigeot  * Zero on success, error code on failure.
4371dedbd3bSFrançois Tigeot  */
drm_mode_plane_set_obj_prop(struct drm_plane * plane,struct drm_property * property,uint64_t value)4381dedbd3bSFrançois Tigeot int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4391dedbd3bSFrançois Tigeot 				struct drm_property *property,
4401dedbd3bSFrançois Tigeot 				uint64_t value)
4411dedbd3bSFrançois Tigeot {
4421dedbd3bSFrançois Tigeot 	int ret = -EINVAL;
4431dedbd3bSFrançois Tigeot 	struct drm_mode_object *obj = &plane->base;
4441dedbd3bSFrançois Tigeot 
4451dedbd3bSFrançois Tigeot 	if (plane->funcs->set_property)
4461dedbd3bSFrançois Tigeot 		ret = plane->funcs->set_property(plane, property, value);
4471dedbd3bSFrançois Tigeot 	if (!ret)
4481dedbd3bSFrançois Tigeot 		drm_object_property_set_value(obj, property, value);
4491dedbd3bSFrançois Tigeot 
4501dedbd3bSFrançois Tigeot 	return ret;
4511dedbd3bSFrançois Tigeot }
4521dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4531dedbd3bSFrançois Tigeot 
drm_mode_getplane_res(struct drm_device * dev,void * data,struct drm_file * file_priv)4541dedbd3bSFrançois Tigeot int drm_mode_getplane_res(struct drm_device *dev, void *data,
4551dedbd3bSFrançois Tigeot 			  struct drm_file *file_priv)
4561dedbd3bSFrançois Tigeot {
4571dedbd3bSFrançois Tigeot 	struct drm_mode_get_plane_res *plane_resp = data;
4581dedbd3bSFrançois Tigeot 	struct drm_mode_config *config;
4591dedbd3bSFrançois Tigeot 	struct drm_plane *plane;
4601dedbd3bSFrançois Tigeot 	uint32_t __user *plane_ptr;
461*3f2dd94aSFrançois Tigeot 	int count = 0;
4621dedbd3bSFrançois Tigeot 
4631dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
4641dedbd3bSFrançois Tigeot 		return -EINVAL;
4651dedbd3bSFrançois Tigeot 
4661dedbd3bSFrançois Tigeot 	config = &dev->mode_config;
467*3f2dd94aSFrançois Tigeot 	plane_ptr = u64_to_user_ptr(plane_resp->plane_id_ptr);
4681dedbd3bSFrançois Tigeot 
4691dedbd3bSFrançois Tigeot 	/*
4701dedbd3bSFrançois Tigeot 	 * This ioctl is called twice, once to determine how much space is
4711dedbd3bSFrançois Tigeot 	 * needed, and the 2nd time to fill it.
4721dedbd3bSFrançois Tigeot 	 */
4731dedbd3bSFrançois Tigeot 	drm_for_each_plane(plane, dev) {
4741dedbd3bSFrançois Tigeot 		/*
4751dedbd3bSFrançois Tigeot 		 * Unless userspace set the 'universal planes'
4761dedbd3bSFrançois Tigeot 		 * capability bit, only advertise overlays.
4771dedbd3bSFrançois Tigeot 		 */
4781dedbd3bSFrançois Tigeot 		if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
4791dedbd3bSFrançois Tigeot 		    !file_priv->universal_planes)
4801dedbd3bSFrançois Tigeot 			continue;
4811dedbd3bSFrançois Tigeot 
482*3f2dd94aSFrançois Tigeot 		if (drm_lease_held(file_priv, plane->base.id)) {
483*3f2dd94aSFrançois Tigeot 			if (count < plane_resp->count_planes &&
484*3f2dd94aSFrançois Tigeot 			    put_user(plane->base.id, plane_ptr + count))
4851dedbd3bSFrançois Tigeot 				return -EFAULT;
486*3f2dd94aSFrançois Tigeot 			count++;
4871dedbd3bSFrançois Tigeot 		}
4881dedbd3bSFrançois Tigeot 	}
489*3f2dd94aSFrançois Tigeot 	plane_resp->count_planes = count;
4901dedbd3bSFrançois Tigeot 
4911dedbd3bSFrançois Tigeot 	return 0;
4921dedbd3bSFrançois Tigeot }
4931dedbd3bSFrançois Tigeot 
drm_mode_getplane(struct drm_device * dev,void * data,struct drm_file * file_priv)4941dedbd3bSFrançois Tigeot int drm_mode_getplane(struct drm_device *dev, void *data,
4951dedbd3bSFrançois Tigeot 		      struct drm_file *file_priv)
4961dedbd3bSFrançois Tigeot {
4971dedbd3bSFrançois Tigeot 	struct drm_mode_get_plane *plane_resp = data;
4981dedbd3bSFrançois Tigeot 	struct drm_plane *plane;
4991dedbd3bSFrançois Tigeot 	uint32_t __user *format_ptr;
5001dedbd3bSFrançois Tigeot 
5011dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
5021dedbd3bSFrançois Tigeot 		return -EINVAL;
5031dedbd3bSFrançois Tigeot 
504*3f2dd94aSFrançois Tigeot 	plane = drm_plane_find(dev, file_priv, plane_resp->plane_id);
5051dedbd3bSFrançois Tigeot 	if (!plane)
5061dedbd3bSFrançois Tigeot 		return -ENOENT;
5071dedbd3bSFrançois Tigeot 
5081dedbd3bSFrançois Tigeot 	drm_modeset_lock(&plane->mutex, NULL);
509*3f2dd94aSFrançois Tigeot 	if (plane->state && plane->state->crtc && drm_lease_held(file_priv, plane->state->crtc->base.id))
510a85cb24fSFrançois Tigeot 		plane_resp->crtc_id = plane->state->crtc->base.id;
511*3f2dd94aSFrançois Tigeot 	else if (!plane->state && plane->crtc && drm_lease_held(file_priv, plane->crtc->base.id))
5121dedbd3bSFrançois Tigeot 		plane_resp->crtc_id = plane->crtc->base.id;
5131dedbd3bSFrançois Tigeot 	else
5141dedbd3bSFrançois Tigeot 		plane_resp->crtc_id = 0;
5151dedbd3bSFrançois Tigeot 
516a85cb24fSFrançois Tigeot 	if (plane->state && plane->state->fb)
517a85cb24fSFrançois Tigeot 		plane_resp->fb_id = plane->state->fb->base.id;
518a85cb24fSFrançois Tigeot 	else if (!plane->state && plane->fb)
5191dedbd3bSFrançois Tigeot 		plane_resp->fb_id = plane->fb->base.id;
5201dedbd3bSFrançois Tigeot 	else
5211dedbd3bSFrançois Tigeot 		plane_resp->fb_id = 0;
5221dedbd3bSFrançois Tigeot 	drm_modeset_unlock(&plane->mutex);
5231dedbd3bSFrançois Tigeot 
5241dedbd3bSFrançois Tigeot 	plane_resp->plane_id = plane->base.id;
525*3f2dd94aSFrançois Tigeot 	plane_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,
526*3f2dd94aSFrançois Tigeot 							    plane->possible_crtcs);
527*3f2dd94aSFrançois Tigeot 
5281dedbd3bSFrançois Tigeot 	plane_resp->gamma_size = 0;
5291dedbd3bSFrançois Tigeot 
5301dedbd3bSFrançois Tigeot 	/*
5311dedbd3bSFrançois Tigeot 	 * This ioctl is called twice, once to determine how much space is
5321dedbd3bSFrançois Tigeot 	 * needed, and the 2nd time to fill it.
5331dedbd3bSFrançois Tigeot 	 */
5341dedbd3bSFrançois Tigeot 	if (plane->format_count &&
5351dedbd3bSFrançois Tigeot 	    (plane_resp->count_format_types >= plane->format_count)) {
5361dedbd3bSFrançois Tigeot 		format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
5371dedbd3bSFrançois Tigeot 		if (copy_to_user(format_ptr,
5381dedbd3bSFrançois Tigeot 				 plane->format_types,
5391dedbd3bSFrançois Tigeot 				 sizeof(uint32_t) * plane->format_count)) {
5401dedbd3bSFrançois Tigeot 			return -EFAULT;
5411dedbd3bSFrançois Tigeot 		}
5421dedbd3bSFrançois Tigeot 	}
5431dedbd3bSFrançois Tigeot 	plane_resp->count_format_types = plane->format_count;
5441dedbd3bSFrançois Tigeot 
5451dedbd3bSFrançois Tigeot 	return 0;
5461dedbd3bSFrançois Tigeot }
5471dedbd3bSFrançois Tigeot 
drm_plane_check_pixel_format(const struct drm_plane * plane,u32 format)5481dedbd3bSFrançois Tigeot int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
5491dedbd3bSFrançois Tigeot {
5501dedbd3bSFrançois Tigeot 	unsigned int i;
5511dedbd3bSFrançois Tigeot 
5521dedbd3bSFrançois Tigeot 	for (i = 0; i < plane->format_count; i++) {
5531dedbd3bSFrançois Tigeot 		if (format == plane->format_types[i])
5541dedbd3bSFrançois Tigeot 			return 0;
5551dedbd3bSFrançois Tigeot 	}
5561dedbd3bSFrançois Tigeot 
5571dedbd3bSFrançois Tigeot 	return -EINVAL;
5581dedbd3bSFrançois Tigeot }
5591dedbd3bSFrançois Tigeot 
5601dedbd3bSFrançois Tigeot /*
561*3f2dd94aSFrançois Tigeot  * __setplane_internal - setplane handler for internal callers
5621dedbd3bSFrançois Tigeot  *
563*3f2dd94aSFrançois Tigeot  * This function will take a reference on the new fb for the plane
564*3f2dd94aSFrançois Tigeot  * on success.
5651dedbd3bSFrançois Tigeot  *
5661dedbd3bSFrançois Tigeot  * src_{x,y,w,h} are provided in 16.16 fixed point format
5671dedbd3bSFrançois Tigeot  */
__setplane_internal(struct drm_plane * plane,struct drm_crtc * crtc,struct drm_framebuffer * fb,int32_t crtc_x,int32_t crtc_y,uint32_t crtc_w,uint32_t crtc_h,uint32_t src_x,uint32_t src_y,uint32_t src_w,uint32_t src_h,struct drm_modeset_acquire_ctx * ctx)5681dedbd3bSFrançois Tigeot static int __setplane_internal(struct drm_plane *plane,
5691dedbd3bSFrançois Tigeot 			       struct drm_crtc *crtc,
5701dedbd3bSFrançois Tigeot 			       struct drm_framebuffer *fb,
5711dedbd3bSFrançois Tigeot 			       int32_t crtc_x, int32_t crtc_y,
5721dedbd3bSFrançois Tigeot 			       uint32_t crtc_w, uint32_t crtc_h,
5731dedbd3bSFrançois Tigeot 			       /* src_{x,y,w,h} values are 16.16 fixed point */
5741dedbd3bSFrançois Tigeot 			       uint32_t src_x, uint32_t src_y,
575a85cb24fSFrançois Tigeot 			       uint32_t src_w, uint32_t src_h,
576a85cb24fSFrançois Tigeot 			       struct drm_modeset_acquire_ctx *ctx)
5771dedbd3bSFrançois Tigeot {
5781dedbd3bSFrançois Tigeot 	int ret = 0;
5791dedbd3bSFrançois Tigeot 
5801dedbd3bSFrançois Tigeot 	/* No fb means shut it down */
5811dedbd3bSFrançois Tigeot 	if (!fb) {
5821dedbd3bSFrançois Tigeot 		plane->old_fb = plane->fb;
583a85cb24fSFrançois Tigeot 		ret = plane->funcs->disable_plane(plane, ctx);
5841dedbd3bSFrançois Tigeot 		if (!ret) {
5851dedbd3bSFrançois Tigeot 			plane->crtc = NULL;
5861dedbd3bSFrançois Tigeot 			plane->fb = NULL;
5871dedbd3bSFrançois Tigeot 		} else {
5881dedbd3bSFrançois Tigeot 			plane->old_fb = NULL;
5891dedbd3bSFrançois Tigeot 		}
5901dedbd3bSFrançois Tigeot 		goto out;
5911dedbd3bSFrançois Tigeot 	}
5921dedbd3bSFrançois Tigeot 
5931dedbd3bSFrançois Tigeot 	/* Check whether this plane is usable on this CRTC */
5941dedbd3bSFrançois Tigeot 	if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
5951dedbd3bSFrançois Tigeot 		DRM_DEBUG_KMS("Invalid crtc for plane\n");
5961dedbd3bSFrançois Tigeot 		ret = -EINVAL;
5971dedbd3bSFrançois Tigeot 		goto out;
5981dedbd3bSFrançois Tigeot 	}
5991dedbd3bSFrançois Tigeot 
6001dedbd3bSFrançois Tigeot 	/* Check whether this plane supports the fb pixel format. */
601a85cb24fSFrançois Tigeot 	ret = drm_plane_check_pixel_format(plane, fb->format->format);
6021dedbd3bSFrançois Tigeot 	if (ret) {
6034be47400SFrançois Tigeot 		struct drm_format_name_buf format_name;
6044be47400SFrançois Tigeot 		DRM_DEBUG_KMS("Invalid pixel format %s\n",
605a85cb24fSFrançois Tigeot 		              drm_get_format_name(fb->format->format,
6064be47400SFrançois Tigeot 		                                  &format_name));
6071dedbd3bSFrançois Tigeot 		goto out;
6081dedbd3bSFrançois Tigeot 	}
6091dedbd3bSFrançois Tigeot 
6101dedbd3bSFrançois Tigeot 	/* Give drivers some help against integer overflows */
6111dedbd3bSFrançois Tigeot 	if (crtc_w > INT_MAX ||
6121dedbd3bSFrançois Tigeot 	    crtc_x > INT_MAX - (int32_t) crtc_w ||
6131dedbd3bSFrançois Tigeot 	    crtc_h > INT_MAX ||
6141dedbd3bSFrançois Tigeot 	    crtc_y > INT_MAX - (int32_t) crtc_h) {
6151dedbd3bSFrançois Tigeot 		DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
6161dedbd3bSFrançois Tigeot 			      crtc_w, crtc_h, crtc_x, crtc_y);
6171dedbd3bSFrançois Tigeot 		ret = -ERANGE;
6181dedbd3bSFrançois Tigeot 		goto out;
6191dedbd3bSFrançois Tigeot 	}
6201dedbd3bSFrançois Tigeot 
6211dedbd3bSFrançois Tigeot 	ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, fb);
6221dedbd3bSFrançois Tigeot 	if (ret)
6231dedbd3bSFrançois Tigeot 		goto out;
6241dedbd3bSFrançois Tigeot 
6251dedbd3bSFrançois Tigeot 	plane->old_fb = plane->fb;
6261dedbd3bSFrançois Tigeot 	ret = plane->funcs->update_plane(plane, crtc, fb,
6271dedbd3bSFrançois Tigeot 					 crtc_x, crtc_y, crtc_w, crtc_h,
628a85cb24fSFrançois Tigeot 					 src_x, src_y, src_w, src_h, ctx);
6291dedbd3bSFrançois Tigeot 	if (!ret) {
6301dedbd3bSFrançois Tigeot 		plane->crtc = crtc;
6311dedbd3bSFrançois Tigeot 		plane->fb = fb;
632*3f2dd94aSFrançois Tigeot 		drm_framebuffer_get(plane->fb);
6331dedbd3bSFrançois Tigeot 	} else {
6341dedbd3bSFrançois Tigeot 		plane->old_fb = NULL;
6351dedbd3bSFrançois Tigeot 	}
6361dedbd3bSFrançois Tigeot 
6371dedbd3bSFrançois Tigeot out:
6381dedbd3bSFrançois Tigeot 	if (plane->old_fb)
639a85cb24fSFrançois Tigeot 		drm_framebuffer_put(plane->old_fb);
6401dedbd3bSFrançois Tigeot 	plane->old_fb = NULL;
6411dedbd3bSFrançois Tigeot 
6421dedbd3bSFrançois Tigeot 	return ret;
6431dedbd3bSFrançois Tigeot }
6441dedbd3bSFrançois Tigeot 
setplane_internal(struct drm_plane * plane,struct drm_crtc * crtc,struct drm_framebuffer * fb,int32_t crtc_x,int32_t crtc_y,uint32_t crtc_w,uint32_t crtc_h,uint32_t src_x,uint32_t src_y,uint32_t src_w,uint32_t src_h)6451dedbd3bSFrançois Tigeot static int setplane_internal(struct drm_plane *plane,
6461dedbd3bSFrançois Tigeot 			     struct drm_crtc *crtc,
6471dedbd3bSFrançois Tigeot 			     struct drm_framebuffer *fb,
6481dedbd3bSFrançois Tigeot 			     int32_t crtc_x, int32_t crtc_y,
6491dedbd3bSFrançois Tigeot 			     uint32_t crtc_w, uint32_t crtc_h,
6501dedbd3bSFrançois Tigeot 			     /* src_{x,y,w,h} values are 16.16 fixed point */
6511dedbd3bSFrançois Tigeot 			     uint32_t src_x, uint32_t src_y,
6521dedbd3bSFrançois Tigeot 			     uint32_t src_w, uint32_t src_h)
6531dedbd3bSFrançois Tigeot {
654a85cb24fSFrançois Tigeot 	struct drm_modeset_acquire_ctx ctx;
6551dedbd3bSFrançois Tigeot 	int ret;
6561dedbd3bSFrançois Tigeot 
657*3f2dd94aSFrançois Tigeot 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
658a85cb24fSFrançois Tigeot retry:
659a85cb24fSFrançois Tigeot 	ret = drm_modeset_lock_all_ctx(plane->dev, &ctx);
660a85cb24fSFrançois Tigeot 	if (ret)
661a85cb24fSFrançois Tigeot 		goto fail;
6621dedbd3bSFrançois Tigeot 	ret = __setplane_internal(plane, crtc, fb,
6631dedbd3bSFrançois Tigeot 				  crtc_x, crtc_y, crtc_w, crtc_h,
664a85cb24fSFrançois Tigeot 				  src_x, src_y, src_w, src_h, &ctx);
665a85cb24fSFrançois Tigeot 
666a85cb24fSFrançois Tigeot fail:
667a85cb24fSFrançois Tigeot 	if (ret == -EDEADLK) {
668*3f2dd94aSFrançois Tigeot 		ret = drm_modeset_backoff(&ctx);
669*3f2dd94aSFrançois Tigeot 		if (!ret)
670a85cb24fSFrançois Tigeot 			goto retry;
671a85cb24fSFrançois Tigeot 	}
672a85cb24fSFrançois Tigeot 	drm_modeset_drop_locks(&ctx);
673a85cb24fSFrançois Tigeot 	drm_modeset_acquire_fini(&ctx);
6741dedbd3bSFrançois Tigeot 
6751dedbd3bSFrançois Tigeot 	return ret;
6761dedbd3bSFrançois Tigeot }
6771dedbd3bSFrançois Tigeot 
drm_mode_setplane(struct drm_device * dev,void * data,struct drm_file * file_priv)6781dedbd3bSFrançois Tigeot int drm_mode_setplane(struct drm_device *dev, void *data,
6791dedbd3bSFrançois Tigeot 		      struct drm_file *file_priv)
6801dedbd3bSFrançois Tigeot {
6811dedbd3bSFrançois Tigeot 	struct drm_mode_set_plane *plane_req = data;
6821dedbd3bSFrançois Tigeot 	struct drm_plane *plane;
6831dedbd3bSFrançois Tigeot 	struct drm_crtc *crtc = NULL;
6841dedbd3bSFrançois Tigeot 	struct drm_framebuffer *fb = NULL;
685*3f2dd94aSFrançois Tigeot 	int ret;
6861dedbd3bSFrançois Tigeot 
6871dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
6881dedbd3bSFrançois Tigeot 		return -EINVAL;
6891dedbd3bSFrançois Tigeot 
6901dedbd3bSFrançois Tigeot 	/*
6911dedbd3bSFrançois Tigeot 	 * First, find the plane, crtc, and fb objects.  If not available,
6921dedbd3bSFrançois Tigeot 	 * we don't bother to call the driver.
6931dedbd3bSFrançois Tigeot 	 */
694*3f2dd94aSFrançois Tigeot 	plane = drm_plane_find(dev, file_priv, plane_req->plane_id);
6951dedbd3bSFrançois Tigeot 	if (!plane) {
6961dedbd3bSFrançois Tigeot 		DRM_DEBUG_KMS("Unknown plane ID %d\n",
6971dedbd3bSFrançois Tigeot 			      plane_req->plane_id);
6981dedbd3bSFrançois Tigeot 		return -ENOENT;
6991dedbd3bSFrançois Tigeot 	}
7001dedbd3bSFrançois Tigeot 
7011dedbd3bSFrançois Tigeot 	if (plane_req->fb_id) {
702*3f2dd94aSFrançois Tigeot 		fb = drm_framebuffer_lookup(dev, file_priv, plane_req->fb_id);
7031dedbd3bSFrançois Tigeot 		if (!fb) {
7041dedbd3bSFrançois Tigeot 			DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
7051dedbd3bSFrançois Tigeot 				      plane_req->fb_id);
7061dedbd3bSFrançois Tigeot 			return -ENOENT;
7071dedbd3bSFrançois Tigeot 		}
7081dedbd3bSFrançois Tigeot 
709*3f2dd94aSFrançois Tigeot 		crtc = drm_crtc_find(dev, file_priv, plane_req->crtc_id);
7101dedbd3bSFrançois Tigeot 		if (!crtc) {
711a85cb24fSFrançois Tigeot 			drm_framebuffer_put(fb);
7121dedbd3bSFrançois Tigeot 			DRM_DEBUG_KMS("Unknown crtc ID %d\n",
7131dedbd3bSFrançois Tigeot 				      plane_req->crtc_id);
7141dedbd3bSFrançois Tigeot 			return -ENOENT;
7151dedbd3bSFrançois Tigeot 		}
7161dedbd3bSFrançois Tigeot 	}
7171dedbd3bSFrançois Tigeot 
718*3f2dd94aSFrançois Tigeot 	ret = setplane_internal(plane, crtc, fb,
7191dedbd3bSFrançois Tigeot 				plane_req->crtc_x, plane_req->crtc_y,
7201dedbd3bSFrançois Tigeot 				plane_req->crtc_w, plane_req->crtc_h,
7211dedbd3bSFrançois Tigeot 				plane_req->src_x, plane_req->src_y,
7221dedbd3bSFrançois Tigeot 				plane_req->src_w, plane_req->src_h);
723*3f2dd94aSFrançois Tigeot 
724*3f2dd94aSFrançois Tigeot 	if (fb)
725*3f2dd94aSFrançois Tigeot 		drm_framebuffer_put(fb);
726*3f2dd94aSFrançois Tigeot 
727*3f2dd94aSFrançois Tigeot 	return ret;
7281dedbd3bSFrançois Tigeot }
7291dedbd3bSFrançois Tigeot 
drm_mode_cursor_universal(struct drm_crtc * crtc,struct drm_mode_cursor2 * req,struct drm_file * file_priv,struct drm_modeset_acquire_ctx * ctx)7301dedbd3bSFrançois Tigeot static int drm_mode_cursor_universal(struct drm_crtc *crtc,
7311dedbd3bSFrançois Tigeot 				     struct drm_mode_cursor2 *req,
732a85cb24fSFrançois Tigeot 				     struct drm_file *file_priv,
733a85cb24fSFrançois Tigeot 				     struct drm_modeset_acquire_ctx *ctx)
7341dedbd3bSFrançois Tigeot {
7351dedbd3bSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
7361dedbd3bSFrançois Tigeot 	struct drm_framebuffer *fb = NULL;
7371dedbd3bSFrançois Tigeot 	struct drm_mode_fb_cmd2 fbreq = {
7381dedbd3bSFrançois Tigeot 		.width = req->width,
7391dedbd3bSFrançois Tigeot 		.height = req->height,
7401dedbd3bSFrançois Tigeot 		.pixel_format = DRM_FORMAT_ARGB8888,
7411dedbd3bSFrançois Tigeot 		.pitches = { req->width * 4 },
7421dedbd3bSFrançois Tigeot 		.handles = { req->handle },
7431dedbd3bSFrançois Tigeot 	};
7441dedbd3bSFrançois Tigeot 	int32_t crtc_x, crtc_y;
7451dedbd3bSFrançois Tigeot 	uint32_t crtc_w = 0, crtc_h = 0;
7461dedbd3bSFrançois Tigeot 	uint32_t src_w = 0, src_h = 0;
7471dedbd3bSFrançois Tigeot 	int ret = 0;
7481dedbd3bSFrançois Tigeot 
7491dedbd3bSFrançois Tigeot 	BUG_ON(!crtc->cursor);
7501dedbd3bSFrançois Tigeot 	WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
7511dedbd3bSFrançois Tigeot 
7521dedbd3bSFrançois Tigeot 	/*
7531dedbd3bSFrançois Tigeot 	 * Obtain fb we'll be using (either new or existing) and take an extra
7541dedbd3bSFrançois Tigeot 	 * reference to it if fb != null.  setplane will take care of dropping
7551dedbd3bSFrançois Tigeot 	 * the reference if the plane update fails.
7561dedbd3bSFrançois Tigeot 	 */
7571dedbd3bSFrançois Tigeot 	if (req->flags & DRM_MODE_CURSOR_BO) {
7581dedbd3bSFrançois Tigeot 		if (req->handle) {
7591dedbd3bSFrançois Tigeot 			fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
7601dedbd3bSFrançois Tigeot 			if (IS_ERR(fb)) {
7611dedbd3bSFrançois Tigeot 				DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
7621dedbd3bSFrançois Tigeot 				return PTR_ERR(fb);
7631dedbd3bSFrançois Tigeot 			}
7641dedbd3bSFrançois Tigeot 			fb->hot_x = req->hot_x;
7651dedbd3bSFrançois Tigeot 			fb->hot_y = req->hot_y;
7661dedbd3bSFrançois Tigeot 		} else {
7671dedbd3bSFrançois Tigeot 			fb = NULL;
7681dedbd3bSFrançois Tigeot 		}
7691dedbd3bSFrançois Tigeot 	} else {
7701dedbd3bSFrançois Tigeot 		fb = crtc->cursor->fb;
7711dedbd3bSFrançois Tigeot 		if (fb)
772a85cb24fSFrançois Tigeot 			drm_framebuffer_get(fb);
7731dedbd3bSFrançois Tigeot 	}
7741dedbd3bSFrançois Tigeot 
7751dedbd3bSFrançois Tigeot 	if (req->flags & DRM_MODE_CURSOR_MOVE) {
7761dedbd3bSFrançois Tigeot 		crtc_x = req->x;
7771dedbd3bSFrançois Tigeot 		crtc_y = req->y;
7781dedbd3bSFrançois Tigeot 	} else {
7791dedbd3bSFrançois Tigeot 		crtc_x = crtc->cursor_x;
7801dedbd3bSFrançois Tigeot 		crtc_y = crtc->cursor_y;
7811dedbd3bSFrançois Tigeot 	}
7821dedbd3bSFrançois Tigeot 
7831dedbd3bSFrançois Tigeot 	if (fb) {
7841dedbd3bSFrançois Tigeot 		crtc_w = fb->width;
7851dedbd3bSFrançois Tigeot 		crtc_h = fb->height;
7861dedbd3bSFrançois Tigeot 		src_w = fb->width << 16;
7871dedbd3bSFrançois Tigeot 		src_h = fb->height << 16;
7881dedbd3bSFrançois Tigeot 	}
7891dedbd3bSFrançois Tigeot 
7901dedbd3bSFrançois Tigeot 	ret = __setplane_internal(crtc->cursor, crtc, fb,
7911dedbd3bSFrançois Tigeot 				  crtc_x, crtc_y, crtc_w, crtc_h,
792a85cb24fSFrançois Tigeot 				  0, 0, src_w, src_h, ctx);
7931dedbd3bSFrançois Tigeot 
794*3f2dd94aSFrançois Tigeot 	if (fb)
795*3f2dd94aSFrançois Tigeot 		drm_framebuffer_put(fb);
796*3f2dd94aSFrançois Tigeot 
7971dedbd3bSFrançois Tigeot 	/* Update successful; save new cursor position, if necessary */
7981dedbd3bSFrançois Tigeot 	if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
7991dedbd3bSFrançois Tigeot 		crtc->cursor_x = req->x;
8001dedbd3bSFrançois Tigeot 		crtc->cursor_y = req->y;
8011dedbd3bSFrançois Tigeot 	}
8021dedbd3bSFrançois Tigeot 
8031dedbd3bSFrançois Tigeot 	return ret;
8041dedbd3bSFrançois Tigeot }
8051dedbd3bSFrançois Tigeot 
drm_mode_cursor_common(struct drm_device * dev,struct drm_mode_cursor2 * req,struct drm_file * file_priv)8061dedbd3bSFrançois Tigeot static int drm_mode_cursor_common(struct drm_device *dev,
8071dedbd3bSFrançois Tigeot 				  struct drm_mode_cursor2 *req,
8081dedbd3bSFrançois Tigeot 				  struct drm_file *file_priv)
8091dedbd3bSFrançois Tigeot {
8101dedbd3bSFrançois Tigeot 	struct drm_crtc *crtc;
811a85cb24fSFrançois Tigeot 	struct drm_modeset_acquire_ctx ctx;
8121dedbd3bSFrançois Tigeot 	int ret = 0;
8131dedbd3bSFrançois Tigeot 
8141dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
8151dedbd3bSFrançois Tigeot 		return -EINVAL;
8161dedbd3bSFrançois Tigeot 
8171dedbd3bSFrançois Tigeot 	if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
8181dedbd3bSFrançois Tigeot 		return -EINVAL;
8191dedbd3bSFrançois Tigeot 
820*3f2dd94aSFrançois Tigeot 	crtc = drm_crtc_find(dev, file_priv, req->crtc_id);
8211dedbd3bSFrançois Tigeot 	if (!crtc) {
8221dedbd3bSFrançois Tigeot 		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
8231dedbd3bSFrançois Tigeot 		return -ENOENT;
8241dedbd3bSFrançois Tigeot 	}
8251dedbd3bSFrançois Tigeot 
826*3f2dd94aSFrançois Tigeot 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
827a85cb24fSFrançois Tigeot retry:
828a85cb24fSFrançois Tigeot 	ret = drm_modeset_lock(&crtc->mutex, &ctx);
829a85cb24fSFrançois Tigeot 	if (ret)
830a85cb24fSFrançois Tigeot 		goto out;
8311dedbd3bSFrançois Tigeot 	/*
8321dedbd3bSFrançois Tigeot 	 * If this crtc has a universal cursor plane, call that plane's update
8331dedbd3bSFrançois Tigeot 	 * handler rather than using legacy cursor handlers.
8341dedbd3bSFrançois Tigeot 	 */
8351dedbd3bSFrançois Tigeot 	if (crtc->cursor) {
836a85cb24fSFrançois Tigeot 		ret = drm_modeset_lock(&crtc->cursor->mutex, &ctx);
837a85cb24fSFrançois Tigeot 		if (ret)
838a85cb24fSFrançois Tigeot 			goto out;
839a85cb24fSFrançois Tigeot 
840a85cb24fSFrançois Tigeot 		ret = drm_mode_cursor_universal(crtc, req, file_priv, &ctx);
8411dedbd3bSFrançois Tigeot 		goto out;
8421dedbd3bSFrançois Tigeot 	}
8431dedbd3bSFrançois Tigeot 
8441dedbd3bSFrançois Tigeot 	if (req->flags & DRM_MODE_CURSOR_BO) {
8451dedbd3bSFrançois Tigeot 		if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
8461dedbd3bSFrançois Tigeot 			ret = -ENXIO;
8471dedbd3bSFrançois Tigeot 			goto out;
8481dedbd3bSFrançois Tigeot 		}
8491dedbd3bSFrançois Tigeot 		/* Turns off the cursor if handle is 0 */
8501dedbd3bSFrançois Tigeot 		if (crtc->funcs->cursor_set2)
8511dedbd3bSFrançois Tigeot 			ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
8521dedbd3bSFrançois Tigeot 						      req->width, req->height, req->hot_x, req->hot_y);
8531dedbd3bSFrançois Tigeot 		else
8541dedbd3bSFrançois Tigeot 			ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
8551dedbd3bSFrançois Tigeot 						      req->width, req->height);
8561dedbd3bSFrançois Tigeot 	}
8571dedbd3bSFrançois Tigeot 
8581dedbd3bSFrançois Tigeot 	if (req->flags & DRM_MODE_CURSOR_MOVE) {
8591dedbd3bSFrançois Tigeot 		if (crtc->funcs->cursor_move) {
8601dedbd3bSFrançois Tigeot 			ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
8611dedbd3bSFrançois Tigeot 		} else {
8621dedbd3bSFrançois Tigeot 			ret = -EFAULT;
8631dedbd3bSFrançois Tigeot 			goto out;
8641dedbd3bSFrançois Tigeot 		}
8651dedbd3bSFrançois Tigeot 	}
8661dedbd3bSFrançois Tigeot out:
867a85cb24fSFrançois Tigeot 	if (ret == -EDEADLK) {
868*3f2dd94aSFrançois Tigeot 		ret = drm_modeset_backoff(&ctx);
869*3f2dd94aSFrançois Tigeot 		if (!ret)
870a85cb24fSFrançois Tigeot 			goto retry;
871a85cb24fSFrançois Tigeot 	}
872a85cb24fSFrançois Tigeot 
873a85cb24fSFrançois Tigeot 	drm_modeset_drop_locks(&ctx);
874a85cb24fSFrançois Tigeot 	drm_modeset_acquire_fini(&ctx);
8751dedbd3bSFrançois Tigeot 
8761dedbd3bSFrançois Tigeot 	return ret;
8771dedbd3bSFrançois Tigeot 
8781dedbd3bSFrançois Tigeot }
8791dedbd3bSFrançois Tigeot 
8801dedbd3bSFrançois Tigeot 
drm_mode_cursor_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)8811dedbd3bSFrançois Tigeot int drm_mode_cursor_ioctl(struct drm_device *dev,
8821dedbd3bSFrançois Tigeot 			  void *data, struct drm_file *file_priv)
8831dedbd3bSFrançois Tigeot {
8841dedbd3bSFrançois Tigeot 	struct drm_mode_cursor *req = data;
8851dedbd3bSFrançois Tigeot 	struct drm_mode_cursor2 new_req;
8861dedbd3bSFrançois Tigeot 
8871dedbd3bSFrançois Tigeot 	memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
8881dedbd3bSFrançois Tigeot 	new_req.hot_x = new_req.hot_y = 0;
8891dedbd3bSFrançois Tigeot 
8901dedbd3bSFrançois Tigeot 	return drm_mode_cursor_common(dev, &new_req, file_priv);
8911dedbd3bSFrançois Tigeot }
8921dedbd3bSFrançois Tigeot 
8931dedbd3bSFrançois Tigeot /*
8941dedbd3bSFrançois Tigeot  * Set the cursor configuration based on user request. This implements the 2nd
8951dedbd3bSFrançois Tigeot  * version of the cursor ioctl, which allows userspace to additionally specify
8961dedbd3bSFrançois Tigeot  * the hotspot of the pointer.
8971dedbd3bSFrançois Tigeot  */
drm_mode_cursor2_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)8981dedbd3bSFrançois Tigeot int drm_mode_cursor2_ioctl(struct drm_device *dev,
8991dedbd3bSFrançois Tigeot 			   void *data, struct drm_file *file_priv)
9001dedbd3bSFrançois Tigeot {
9011dedbd3bSFrançois Tigeot 	struct drm_mode_cursor2 *req = data;
9021dedbd3bSFrançois Tigeot 
9031dedbd3bSFrançois Tigeot 	return drm_mode_cursor_common(dev, req, file_priv);
9041dedbd3bSFrançois Tigeot }
9051dedbd3bSFrançois Tigeot 
drm_mode_page_flip_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)9061dedbd3bSFrançois Tigeot int drm_mode_page_flip_ioctl(struct drm_device *dev,
9071dedbd3bSFrançois Tigeot 			     void *data, struct drm_file *file_priv)
9081dedbd3bSFrançois Tigeot {
9091dedbd3bSFrançois Tigeot 	struct drm_mode_crtc_page_flip_target *page_flip = data;
9101dedbd3bSFrançois Tigeot 	struct drm_crtc *crtc;
9111dedbd3bSFrançois Tigeot 	struct drm_framebuffer *fb = NULL;
9121dedbd3bSFrançois Tigeot 	struct drm_pending_vblank_event *e = NULL;
9131dedbd3bSFrançois Tigeot 	u32 target_vblank = page_flip->sequence;
914a85cb24fSFrançois Tigeot 	struct drm_modeset_acquire_ctx ctx;
9151dedbd3bSFrançois Tigeot 	int ret = -EINVAL;
9161dedbd3bSFrançois Tigeot 
9171dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
9181dedbd3bSFrançois Tigeot 		return -EINVAL;
9191dedbd3bSFrançois Tigeot 
9201dedbd3bSFrançois Tigeot 	if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
9211dedbd3bSFrançois Tigeot 		return -EINVAL;
9221dedbd3bSFrançois Tigeot 
9231dedbd3bSFrançois Tigeot 	if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
9241dedbd3bSFrançois Tigeot 		return -EINVAL;
9251dedbd3bSFrançois Tigeot 
9261dedbd3bSFrançois Tigeot 	/* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
9271dedbd3bSFrançois Tigeot 	 * can be specified
9281dedbd3bSFrançois Tigeot 	 */
9291dedbd3bSFrançois Tigeot 	if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
9301dedbd3bSFrançois Tigeot 		return -EINVAL;
9311dedbd3bSFrançois Tigeot 
9321dedbd3bSFrançois Tigeot 	if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
9331dedbd3bSFrançois Tigeot 		return -EINVAL;
9341dedbd3bSFrançois Tigeot 
935*3f2dd94aSFrançois Tigeot 	crtc = drm_crtc_find(dev, file_priv, page_flip->crtc_id);
9361dedbd3bSFrançois Tigeot 	if (!crtc)
9371dedbd3bSFrançois Tigeot 		return -ENOENT;
9381dedbd3bSFrançois Tigeot 
9391dedbd3bSFrançois Tigeot 	if (crtc->funcs->page_flip_target) {
9401dedbd3bSFrançois Tigeot 		u32 current_vblank;
9411dedbd3bSFrançois Tigeot 		int r;
9421dedbd3bSFrançois Tigeot 
9431dedbd3bSFrançois Tigeot 		r = drm_crtc_vblank_get(crtc);
9441dedbd3bSFrançois Tigeot 		if (r)
9451dedbd3bSFrançois Tigeot 			return r;
9461dedbd3bSFrançois Tigeot 
9471dedbd3bSFrançois Tigeot 		current_vblank = drm_crtc_vblank_count(crtc);
9481dedbd3bSFrançois Tigeot 
9491dedbd3bSFrançois Tigeot 		switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
9501dedbd3bSFrançois Tigeot 		case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
9511dedbd3bSFrançois Tigeot 			if ((int)(target_vblank - current_vblank) > 1) {
9521dedbd3bSFrançois Tigeot 				DRM_DEBUG("Invalid absolute flip target %u, "
9531dedbd3bSFrançois Tigeot 					  "must be <= %u\n", target_vblank,
9541dedbd3bSFrançois Tigeot 					  current_vblank + 1);
9551dedbd3bSFrançois Tigeot 				drm_crtc_vblank_put(crtc);
9561dedbd3bSFrançois Tigeot 				return -EINVAL;
9571dedbd3bSFrançois Tigeot 			}
9581dedbd3bSFrançois Tigeot 			break;
9591dedbd3bSFrançois Tigeot 		case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
9601dedbd3bSFrançois Tigeot 			if (target_vblank != 0 && target_vblank != 1) {
9611dedbd3bSFrançois Tigeot 				DRM_DEBUG("Invalid relative flip target %u, "
9621dedbd3bSFrançois Tigeot 					  "must be 0 or 1\n", target_vblank);
9631dedbd3bSFrançois Tigeot 				drm_crtc_vblank_put(crtc);
9641dedbd3bSFrançois Tigeot 				return -EINVAL;
9651dedbd3bSFrançois Tigeot 			}
9661dedbd3bSFrançois Tigeot 			target_vblank += current_vblank;
9671dedbd3bSFrançois Tigeot 			break;
9681dedbd3bSFrançois Tigeot 		default:
9691dedbd3bSFrançois Tigeot 			target_vblank = current_vblank +
9701dedbd3bSFrançois Tigeot 				!(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
9711dedbd3bSFrançois Tigeot 			break;
9721dedbd3bSFrançois Tigeot 		}
9731dedbd3bSFrançois Tigeot 	} else if (crtc->funcs->page_flip == NULL ||
9741dedbd3bSFrançois Tigeot 		   (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
9751dedbd3bSFrançois Tigeot 		return -EINVAL;
9761dedbd3bSFrançois Tigeot 	}
9771dedbd3bSFrançois Tigeot 
978*3f2dd94aSFrançois Tigeot 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
979a85cb24fSFrançois Tigeot retry:
980a85cb24fSFrançois Tigeot 	ret = drm_modeset_lock(&crtc->mutex, &ctx);
981a85cb24fSFrançois Tigeot 	if (ret)
982a85cb24fSFrançois Tigeot 		goto out;
983a85cb24fSFrançois Tigeot 	ret = drm_modeset_lock(&crtc->primary->mutex, &ctx);
984a85cb24fSFrançois Tigeot 	if (ret)
985a85cb24fSFrançois Tigeot 		goto out;
986a85cb24fSFrançois Tigeot 
9871dedbd3bSFrançois Tigeot 	if (crtc->primary->fb == NULL) {
9881dedbd3bSFrançois Tigeot 		/* The framebuffer is currently unbound, presumably
9891dedbd3bSFrançois Tigeot 		 * due to a hotplug event, that userspace has not
9901dedbd3bSFrançois Tigeot 		 * yet discovered.
9911dedbd3bSFrançois Tigeot 		 */
9921dedbd3bSFrançois Tigeot 		ret = -EBUSY;
9931dedbd3bSFrançois Tigeot 		goto out;
9941dedbd3bSFrançois Tigeot 	}
9951dedbd3bSFrançois Tigeot 
996*3f2dd94aSFrançois Tigeot 	fb = drm_framebuffer_lookup(dev, file_priv, page_flip->fb_id);
9971dedbd3bSFrançois Tigeot 	if (!fb) {
9981dedbd3bSFrançois Tigeot 		ret = -ENOENT;
9991dedbd3bSFrançois Tigeot 		goto out;
10001dedbd3bSFrançois Tigeot 	}
10011dedbd3bSFrançois Tigeot 
10021dedbd3bSFrançois Tigeot 	if (crtc->state) {
10031dedbd3bSFrançois Tigeot 		const struct drm_plane_state *state = crtc->primary->state;
10041dedbd3bSFrançois Tigeot 
10051dedbd3bSFrançois Tigeot 		ret = drm_framebuffer_check_src_coords(state->src_x,
10061dedbd3bSFrançois Tigeot 						       state->src_y,
10071dedbd3bSFrançois Tigeot 						       state->src_w,
10081dedbd3bSFrançois Tigeot 						       state->src_h,
10091dedbd3bSFrançois Tigeot 						       fb);
10101dedbd3bSFrançois Tigeot 	} else {
10111dedbd3bSFrançois Tigeot 		ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
10121dedbd3bSFrançois Tigeot 	}
10131dedbd3bSFrançois Tigeot 	if (ret)
10141dedbd3bSFrançois Tigeot 		goto out;
10151dedbd3bSFrançois Tigeot 
1016a85cb24fSFrançois Tigeot 	if (crtc->primary->fb->format != fb->format) {
10171dedbd3bSFrançois Tigeot 		DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
10181dedbd3bSFrançois Tigeot 		ret = -EINVAL;
10191dedbd3bSFrançois Tigeot 		goto out;
10201dedbd3bSFrançois Tigeot 	}
10211dedbd3bSFrançois Tigeot 
10221dedbd3bSFrançois Tigeot 	if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
10231dedbd3bSFrançois Tigeot 		e = kzalloc(sizeof *e, GFP_KERNEL);
10241dedbd3bSFrançois Tigeot 		if (!e) {
10251dedbd3bSFrançois Tigeot 			ret = -ENOMEM;
10261dedbd3bSFrançois Tigeot 			goto out;
10271dedbd3bSFrançois Tigeot 		}
10281dedbd3bSFrançois Tigeot 		e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
10291dedbd3bSFrançois Tigeot 		e->event.base.length = sizeof(e->event);
1030*3f2dd94aSFrançois Tigeot 		e->event.vbl.user_data = page_flip->user_data;
1031*3f2dd94aSFrançois Tigeot 		e->event.vbl.crtc_id = crtc->base.id;
10321dedbd3bSFrançois Tigeot 		ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
10331dedbd3bSFrançois Tigeot 		if (ret) {
10341dedbd3bSFrançois Tigeot 			kfree(e);
1035a85cb24fSFrançois Tigeot 			e = NULL;
10361dedbd3bSFrançois Tigeot 			goto out;
10371dedbd3bSFrançois Tigeot 		}
10381dedbd3bSFrançois Tigeot 	}
10391dedbd3bSFrançois Tigeot 
10401dedbd3bSFrançois Tigeot 	crtc->primary->old_fb = crtc->primary->fb;
10411dedbd3bSFrançois Tigeot 	if (crtc->funcs->page_flip_target)
10421dedbd3bSFrançois Tigeot 		ret = crtc->funcs->page_flip_target(crtc, fb, e,
10431dedbd3bSFrançois Tigeot 						    page_flip->flags,
1044a85cb24fSFrançois Tigeot 						    target_vblank,
1045a85cb24fSFrançois Tigeot 						    &ctx);
10461dedbd3bSFrançois Tigeot 	else
1047a85cb24fSFrançois Tigeot 		ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags,
1048a85cb24fSFrançois Tigeot 					     &ctx);
10491dedbd3bSFrançois Tigeot 	if (ret) {
10501dedbd3bSFrançois Tigeot 		if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
10511dedbd3bSFrançois Tigeot 			drm_event_cancel_free(dev, &e->base);
10521dedbd3bSFrançois Tigeot 		/* Keep the old fb, don't unref it. */
10531dedbd3bSFrançois Tigeot 		crtc->primary->old_fb = NULL;
10541dedbd3bSFrançois Tigeot 	} else {
10551dedbd3bSFrançois Tigeot 		crtc->primary->fb = fb;
10561dedbd3bSFrançois Tigeot 		/* Unref only the old framebuffer. */
10571dedbd3bSFrançois Tigeot 		fb = NULL;
10581dedbd3bSFrançois Tigeot 	}
10591dedbd3bSFrançois Tigeot 
10601dedbd3bSFrançois Tigeot out:
1061a85cb24fSFrançois Tigeot 	if (fb)
1062a85cb24fSFrançois Tigeot 		drm_framebuffer_put(fb);
1063a85cb24fSFrançois Tigeot 	if (crtc->primary->old_fb)
1064a85cb24fSFrançois Tigeot 		drm_framebuffer_put(crtc->primary->old_fb);
1065a85cb24fSFrançois Tigeot 	crtc->primary->old_fb = NULL;
1066a85cb24fSFrançois Tigeot 
1067a85cb24fSFrançois Tigeot 	if (ret == -EDEADLK) {
1068*3f2dd94aSFrançois Tigeot 		ret = drm_modeset_backoff(&ctx);
1069*3f2dd94aSFrançois Tigeot 		if (!ret)
1070a85cb24fSFrançois Tigeot 			goto retry;
1071a85cb24fSFrançois Tigeot 	}
1072a85cb24fSFrançois Tigeot 
1073a85cb24fSFrançois Tigeot 	drm_modeset_drop_locks(&ctx);
1074a85cb24fSFrançois Tigeot 	drm_modeset_acquire_fini(&ctx);
1075a85cb24fSFrançois Tigeot 
10761dedbd3bSFrançois Tigeot 	if (ret && crtc->funcs->page_flip_target)
10771dedbd3bSFrançois Tigeot 		drm_crtc_vblank_put(crtc);
10781dedbd3bSFrançois Tigeot 
10791dedbd3bSFrançois Tigeot 	return ret;
10801dedbd3bSFrançois Tigeot }
1081