xref: /netbsd-src/sys/external/bsd/drm2/drm/drm_gem_framebuffer_helper.c (revision f6878be31f721b2de852a4699462ef13f4b76a5b)
1*f6878be3Sriastradh /*	$NetBSD: drm_gem_framebuffer_helper.c,v 1.3 2021/12/19 09:49:17 riastradh Exp $	*/
236085aaeSriastradh 
336085aaeSriastradh /*-
436085aaeSriastradh  * Copyright (c) 2018 The NetBSD Foundation, Inc.
536085aaeSriastradh  * All rights reserved.
636085aaeSriastradh  *
736085aaeSriastradh  * This code is derived from software contributed to The NetBSD Foundation
836085aaeSriastradh  * by Taylor R. Campbell.
936085aaeSriastradh  *
1036085aaeSriastradh  * Redistribution and use in source and binary forms, with or without
1136085aaeSriastradh  * modification, are permitted provided that the following conditions
1236085aaeSriastradh  * are met:
1336085aaeSriastradh  * 1. Redistributions of source code must retain the above copyright
1436085aaeSriastradh  *    notice, this list of conditions and the following disclaimer.
1536085aaeSriastradh  * 2. Redistributions in binary form must reproduce the above copyright
1636085aaeSriastradh  *    notice, this list of conditions and the following disclaimer in the
1736085aaeSriastradh  *    documentation and/or other materials provided with the distribution.
1836085aaeSriastradh  *
1936085aaeSriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2036085aaeSriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2136085aaeSriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2236085aaeSriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2336085aaeSriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2436085aaeSriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2536085aaeSriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2636085aaeSriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2736085aaeSriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2836085aaeSriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2936085aaeSriastradh  * POSSIBILITY OF SUCH DAMAGE.
3036085aaeSriastradh  */
3136085aaeSriastradh 
3236085aaeSriastradh #include <sys/cdefs.h>
33*f6878be3Sriastradh __KERNEL_RCSID(0, "$NetBSD: drm_gem_framebuffer_helper.c,v 1.3 2021/12/19 09:49:17 riastradh Exp $");
3436085aaeSriastradh 
3536085aaeSriastradh #include <linux/err.h>
3636085aaeSriastradh #include <linux/slab.h>
3736085aaeSriastradh 
38*f6878be3Sriastradh #include <drm/drm_print.h>
39*f6878be3Sriastradh #include <drm/drm_fourcc.h>
4036085aaeSriastradh #include <drm/drm_framebuffer.h>
4136085aaeSriastradh #include <drm/drm_gem.h>
426bb5688cSriastradh #include <drm/drm_gem_framebuffer_helper.h>
436bb5688cSriastradh #include <drm/drm_modeset_helper.h>
4436085aaeSriastradh 
4536085aaeSriastradh #include <uapi/drm/drm_mode.h>
4636085aaeSriastradh 
4736085aaeSriastradh /*
4836085aaeSriastradh  * drm_gem_fb_destroy(fb)
4936085aaeSriastradh  *
5036085aaeSriastradh  *	Release the objects in, clean up and, kfree a framebuffer
5136085aaeSriastradh  *	allocated with drm_gem_fb_create_with_funcs.
5236085aaeSriastradh  *
5336085aaeSriastradh  *	Fit for use as struct drm_framebuffer_funcs::destroy.  Caller
5436085aaeSriastradh  *	must guarantee that the struct drm_framebuffer is allocated
5536085aaeSriastradh  *	with kmalloc.
5636085aaeSriastradh  */
5736085aaeSriastradh void
drm_gem_fb_destroy(struct drm_framebuffer * fb)5836085aaeSriastradh drm_gem_fb_destroy(struct drm_framebuffer *fb)
5936085aaeSriastradh {
6036085aaeSriastradh 	unsigned plane;
6136085aaeSriastradh 
6236085aaeSriastradh 	for (plane = 0; plane < __arraycount(fb->obj); plane++)
6336085aaeSriastradh 		drm_gem_object_put_unlocked(fb->obj[plane]);
6436085aaeSriastradh 	drm_framebuffer_cleanup(fb);
6536085aaeSriastradh 	kfree(fb);
6636085aaeSriastradh }
6736085aaeSriastradh 
6836085aaeSriastradh /*
6936085aaeSriastradh  * drm_gem_fb_create_handle(fb, file, handlep)
7036085aaeSriastradh  *
7136085aaeSriastradh  *	Create a GEM handle for the object of the first plane (plane=0)
7236085aaeSriastradh  *	of fb in the specified drm file namespace, and store it in
7336085aaeSriastradh  *	*handlep.
7436085aaeSriastradh  *
7536085aaeSriastradh  *	Returns 0 on success, negative error on failure.
7636085aaeSriastradh  */
7736085aaeSriastradh int
drm_gem_fb_create_handle(struct drm_framebuffer * fb,struct drm_file * file,unsigned * handlep)7836085aaeSriastradh drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
7936085aaeSriastradh     unsigned *handlep)
8036085aaeSriastradh {
8136085aaeSriastradh 
8236085aaeSriastradh 	return drm_gem_handle_create(file, fb->obj[0], handlep);
8336085aaeSriastradh }
8436085aaeSriastradh 
8536085aaeSriastradh /*
8636085aaeSriastradh  * drm_gem_fb_create_with_funcs(dev, file, mode_cmd, funcs)
8736085aaeSriastradh  *
8836085aaeSriastradh  *	Create a framebuffer in the specified drm device from the given
8936085aaeSriastradh  *	mode command, resolving mode_cmd's handles in the specified drm
9036085aaeSriastradh  *	file.
9136085aaeSriastradh  *
9236085aaeSriastradh  *	Returns pointer on success, ERR_PTR on failure.
9336085aaeSriastradh  *
9436085aaeSriastradh  *	ENOENT	missing handle
9536085aaeSriastradh  *	EINVAL	wrong size object, invalid mode format
9636085aaeSriastradh  */
9736085aaeSriastradh struct drm_framebuffer *
drm_gem_fb_create_with_funcs(struct drm_device * dev,struct drm_file * file,const struct drm_mode_fb_cmd2 * mode_cmd,const struct drm_framebuffer_funcs * funcs)9836085aaeSriastradh drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file,
9936085aaeSriastradh     const struct drm_mode_fb_cmd2 *mode_cmd,
10036085aaeSriastradh     const struct drm_framebuffer_funcs *funcs)
10136085aaeSriastradh {
10236085aaeSriastradh 	struct drm_framebuffer *fb;
10336085aaeSriastradh 	unsigned plane;
10436085aaeSriastradh 	int ret;
10536085aaeSriastradh 
10636085aaeSriastradh 	/* Allocate a framebuffer object with kmalloc.  */
10736085aaeSriastradh 	fb = kmalloc(sizeof(*fb), GFP_KERNEL);
10836085aaeSriastradh 	if (fb == NULL) {
10936085aaeSriastradh 		ret = -ENOMEM;
11036085aaeSriastradh 		goto fail0;
11136085aaeSriastradh 	}
11236085aaeSriastradh 
11336085aaeSriastradh 	/*
11436085aaeSriastradh 	 * Fill framebuffer parameters from mode_cmd.  If they're not
11536085aaeSriastradh 	 * valid, fail with EINVAL.
11636085aaeSriastradh 	 */
11736085aaeSriastradh 	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
11836085aaeSriastradh 	if (fb->format == NULL) {
11936085aaeSriastradh 		ret = -EINVAL;
12036085aaeSriastradh 		goto fail1;
12136085aaeSriastradh 	}
12236085aaeSriastradh 
12336085aaeSriastradh 	/* Get the object for each plane.  */
12436085aaeSriastradh 	for (plane = 0; plane < fb->format->num_planes; plane++) {
1256bb5688cSriastradh 		unsigned vsub = (plane > 0 ? fb->format->vsub : 1); /* XXX ? */
1266bb5688cSriastradh 		unsigned hsub = (plane > 0 ? fb->format->hsub : 1); /* XXX ? */
12736085aaeSriastradh 		unsigned handle = mode_cmd->handles[plane];
12836085aaeSriastradh 		unsigned size;
12936085aaeSriastradh 
13036085aaeSriastradh 		/* Look up the object for this plane.  */
13136085aaeSriastradh 		fb->obj[plane] = drm_gem_object_lookup(file, handle);
13236085aaeSriastradh 		if (fb->obj[plane] == NULL) {
13336085aaeSriastradh 			ret = -ENOENT;
13436085aaeSriastradh 			goto fail2;
13536085aaeSriastradh 		}
13636085aaeSriastradh 
13736085aaeSriastradh 		/* Confirm the object is large enough to handle it.  */
13836085aaeSriastradh 		size = (mode_cmd->height/vsub - 1)*mode_cmd->pitches[plane]
13936085aaeSriastradh 		    + (mode_cmd->width/hsub)*fb->format->cpp[plane]
14036085aaeSriastradh 		    + mode_cmd->offsets[plane];
14136085aaeSriastradh 		if (fb->obj[plane]->size < size) {
14236085aaeSriastradh 			ret = -EINVAL;
14336085aaeSriastradh 			plane++; /* free this one too */
14436085aaeSriastradh 			goto fail2;
14536085aaeSriastradh 		}
14636085aaeSriastradh 	}
14736085aaeSriastradh 
14836085aaeSriastradh 	/* Initialize the framebuffer.  */
14936085aaeSriastradh 	ret = drm_framebuffer_init(dev, fb, funcs);
15036085aaeSriastradh 	if (ret)
15136085aaeSriastradh 		goto fail2;
15236085aaeSriastradh 
15336085aaeSriastradh 	/* Success!  */
15436085aaeSriastradh 	return fb;
15536085aaeSriastradh 
15636085aaeSriastradh fail2:	while (plane --> 0)
15736085aaeSriastradh 		drm_gem_object_put_unlocked(fb->obj[plane]);
15836085aaeSriastradh fail1:	kmem_free(fb, sizeof(*fb));
15936085aaeSriastradh fail0:	KASSERT(ret);
16036085aaeSriastradh 	return ERR_PTR(ret);
16136085aaeSriastradh }
162