drm_gem_framebuffer_helper.c revision 1.1 1 /* $NetBSD: drm_gem_framebuffer_helper.c,v 1.1 2021/12/19 00:25:53 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: drm_gem_framebuffer_helper.c,v 1.1 2021/12/19 00:25:53 riastradh Exp $");
34
35 #include <linux/err.h>
36 #include <linux/slab.h>
37
38 #include <drm/drm_framebuffer.h>
39 #include <drm/drm_gem.h>
40
41 #include <uapi/drm/drm_mode.h>
42
43 /*
44 * drm_gem_fb_destroy(fb)
45 *
46 * Release the objects in, clean up and, kfree a framebuffer
47 * allocated with drm_gem_fb_create_with_funcs.
48 *
49 * Fit for use as struct drm_framebuffer_funcs::destroy. Caller
50 * must guarantee that the struct drm_framebuffer is allocated
51 * with kmalloc.
52 */
53 void
54 drm_gem_fb_destroy(struct drm_framebuffer *fb)
55 {
56 unsigned plane;
57
58 for (plane = 0; plane < __arraycount(fb->obj); plane++)
59 drm_gem_object_put_unlocked(fb->obj[plane]);
60 drm_framebuffer_cleanup(fb);
61 kfree(fb);
62 }
63
64 /*
65 * drm_gem_fb_create_handle(fb, file, handlep)
66 *
67 * Create a GEM handle for the object of the first plane (plane=0)
68 * of fb in the specified drm file namespace, and store it in
69 * *handlep.
70 *
71 * Returns 0 on success, negative error on failure.
72 */
73 int
74 drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
75 unsigned *handlep)
76 {
77
78 return drm_gem_handle_create(file, fb->obj[0], handlep);
79 }
80
81 /*
82 * drm_gem_fb_create_with_funcs(dev, file, mode_cmd, funcs)
83 *
84 * Create a framebuffer in the specified drm device from the given
85 * mode command, resolving mode_cmd's handles in the specified drm
86 * file.
87 *
88 * Returns pointer on success, ERR_PTR on failure.
89 *
90 * ENOENT missing handle
91 * EINVAL wrong size object, invalid mode format
92 */
93 struct drm_framebuffer *
94 drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file,
95 const struct drm_mode_fb_cmd2 *mode_cmd,
96 const struct drm_framebuffer_funcs *funcs)
97 {
98 struct drm_framebuffer *fb;
99 unsigned plane;
100 int ret;
101
102 /* Allocate a framebuffer object with kmalloc. */
103 fb = kmalloc(sizeof(*fb), GFP_KERNEL);
104 if (fb == NULL) {
105 ret = -ENOMEM;
106 goto fail0;
107 }
108
109 /*
110 * Fill framebuffer parameters from mode_cmd. If they're not
111 * valid, fail with EINVAL.
112 */
113 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
114 if (fb->format == NULL) {
115 ret = -EINVAL;
116 goto fail1;
117 }
118
119 /* Get the object for each plane. */
120 for (plane = 0; plane < fb->format->num_planes; plane++) {
121 unsigned vsub = (plane > 0 ? info->vsub : 1); /* XXX ? */
122 unsigned hsub = (plane > 0 ? info->hsub : 1); /* XXX ? */
123 unsigned handle = mode_cmd->handles[plane];
124 unsigned size;
125
126 /* Look up the object for this plane. */
127 fb->obj[plane] = drm_gem_object_lookup(file, handle);
128 if (fb->obj[plane] == NULL) {
129 ret = -ENOENT;
130 goto fail2;
131 }
132
133 /* Confirm the object is large enough to handle it. */
134 size = (mode_cmd->height/vsub - 1)*mode_cmd->pitches[plane]
135 + (mode_cmd->width/hsub)*fb->format->cpp[plane]
136 + mode_cmd->offsets[plane];
137 if (fb->obj[plane]->size < size) {
138 ret = -EINVAL;
139 plane++; /* free this one too */
140 goto fail2;
141 }
142 }
143
144 /* Initialize the framebuffer. */
145 ret = drm_framebuffer_init(dev, fb, funcs);
146 if (ret)
147 goto fail2;
148
149 /* Success! */
150 return fb;
151
152 fail2: while (plane --> 0)
153 drm_gem_object_put_unlocked(fb->obj[plane]);
154 fail1: kmem_free(fb, sizeof(*fb));
155 fail0: KASSERT(ret);
156 return ERR_PTR(ret);
157 }
158