1 /* $NetBSD: drm_gem_framebuffer_helper.c,v 1.3 2021/12/19 09:49:17 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.3 2021/12/19 09:49:17 riastradh Exp $"); 34 35 #include <linux/err.h> 36 #include <linux/slab.h> 37 38 #include <drm/drm_print.h> 39 #include <drm/drm_fourcc.h> 40 #include <drm/drm_framebuffer.h> 41 #include <drm/drm_gem.h> 42 #include <drm/drm_gem_framebuffer_helper.h> 43 #include <drm/drm_modeset_helper.h> 44 45 #include <uapi/drm/drm_mode.h> 46 47 /* 48 * drm_gem_fb_destroy(fb) 49 * 50 * Release the objects in, clean up and, kfree a framebuffer 51 * allocated with drm_gem_fb_create_with_funcs. 52 * 53 * Fit for use as struct drm_framebuffer_funcs::destroy. Caller 54 * must guarantee that the struct drm_framebuffer is allocated 55 * with kmalloc. 56 */ 57 void 58 drm_gem_fb_destroy(struct drm_framebuffer *fb) 59 { 60 unsigned plane; 61 62 for (plane = 0; plane < __arraycount(fb->obj); plane++) 63 drm_gem_object_put_unlocked(fb->obj[plane]); 64 drm_framebuffer_cleanup(fb); 65 kfree(fb); 66 } 67 68 /* 69 * drm_gem_fb_create_handle(fb, file, handlep) 70 * 71 * Create a GEM handle for the object of the first plane (plane=0) 72 * of fb in the specified drm file namespace, and store it in 73 * *handlep. 74 * 75 * Returns 0 on success, negative error on failure. 76 */ 77 int 78 drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file, 79 unsigned *handlep) 80 { 81 82 return drm_gem_handle_create(file, fb->obj[0], handlep); 83 } 84 85 /* 86 * drm_gem_fb_create_with_funcs(dev, file, mode_cmd, funcs) 87 * 88 * Create a framebuffer in the specified drm device from the given 89 * mode command, resolving mode_cmd's handles in the specified drm 90 * file. 91 * 92 * Returns pointer on success, ERR_PTR on failure. 93 * 94 * ENOENT missing handle 95 * EINVAL wrong size object, invalid mode format 96 */ 97 struct drm_framebuffer * 98 drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file, 99 const struct drm_mode_fb_cmd2 *mode_cmd, 100 const struct drm_framebuffer_funcs *funcs) 101 { 102 struct drm_framebuffer *fb; 103 unsigned plane; 104 int ret; 105 106 /* Allocate a framebuffer object with kmalloc. */ 107 fb = kmalloc(sizeof(*fb), GFP_KERNEL); 108 if (fb == NULL) { 109 ret = -ENOMEM; 110 goto fail0; 111 } 112 113 /* 114 * Fill framebuffer parameters from mode_cmd. If they're not 115 * valid, fail with EINVAL. 116 */ 117 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd); 118 if (fb->format == NULL) { 119 ret = -EINVAL; 120 goto fail1; 121 } 122 123 /* Get the object for each plane. */ 124 for (plane = 0; plane < fb->format->num_planes; plane++) { 125 unsigned vsub = (plane > 0 ? fb->format->vsub : 1); /* XXX ? */ 126 unsigned hsub = (plane > 0 ? fb->format->hsub : 1); /* XXX ? */ 127 unsigned handle = mode_cmd->handles[plane]; 128 unsigned size; 129 130 /* Look up the object for this plane. */ 131 fb->obj[plane] = drm_gem_object_lookup(file, handle); 132 if (fb->obj[plane] == NULL) { 133 ret = -ENOENT; 134 goto fail2; 135 } 136 137 /* Confirm the object is large enough to handle it. */ 138 size = (mode_cmd->height/vsub - 1)*mode_cmd->pitches[plane] 139 + (mode_cmd->width/hsub)*fb->format->cpp[plane] 140 + mode_cmd->offsets[plane]; 141 if (fb->obj[plane]->size < size) { 142 ret = -EINVAL; 143 plane++; /* free this one too */ 144 goto fail2; 145 } 146 } 147 148 /* Initialize the framebuffer. */ 149 ret = drm_framebuffer_init(dev, fb, funcs); 150 if (ret) 151 goto fail2; 152 153 /* Success! */ 154 return fb; 155 156 fail2: while (plane --> 0) 157 drm_gem_object_put_unlocked(fb->obj[plane]); 158 fail1: kmem_free(fb, sizeof(*fb)); 159 fail0: KASSERT(ret); 160 return ERR_PTR(ret); 161 } 162