1 /* 2 * drm gem framebuffer helper functions 3 * 4 * Copyright (C) 2017 Noralf Trønnes 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 */ 11 12 #include <linux/dma-buf.h> 13 #include <linux/dma-fence.h> 14 #include <linux/reservation.h> 15 #include <linux/slab.h> 16 17 #include <drm/drmP.h> 18 #include <drm/drm_atomic.h> 19 #include <drm/drm_fb_helper.h> 20 #include <drm/drm_fourcc.h> 21 #include <drm/drm_framebuffer.h> 22 #include <drm/drm_gem.h> 23 #include <drm/drm_gem_framebuffer_helper.h> 24 #include <drm/drm_modeset_helper.h> 25 26 /** 27 * DOC: overview 28 * 29 * This library provides helpers for drivers that don't subclass 30 * &drm_framebuffer and and use &drm_gem_object for their backing storage. 31 * 32 * Drivers without additional needs to validate framebuffers can simply use 33 * drm_gem_fb_create() and everything is wired up automatically. But all 34 * parts can be used individually. 35 */ 36 37 /** 38 * drm_gem_fb_get_obj() - Get GEM object for framebuffer 39 * @fb: The framebuffer 40 * @plane: Which plane 41 * 42 * Returns the GEM object for given framebuffer. 43 */ 44 struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb, 45 unsigned int plane) 46 { 47 if (plane >= 4) 48 return NULL; 49 50 return fb->obj[plane]; 51 } 52 EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj); 53 54 static struct drm_framebuffer * 55 drm_gem_fb_alloc(struct drm_device *dev, 56 const struct drm_mode_fb_cmd2 *mode_cmd, 57 struct drm_gem_object **obj, unsigned int num_planes, 58 const struct drm_framebuffer_funcs *funcs) 59 { 60 struct drm_framebuffer *fb; 61 int ret, i; 62 63 fb = kzalloc(sizeof(*fb), GFP_KERNEL); 64 if (!fb) 65 return ERR_PTR(-ENOMEM); 66 67 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd); 68 69 for (i = 0; i < num_planes; i++) 70 fb->obj[i] = obj[i]; 71 72 ret = drm_framebuffer_init(dev, fb, funcs); 73 if (ret) { 74 DRM_DEV_ERROR(dev->dev, "Failed to init framebuffer: %d\n", 75 ret); 76 kfree(fb); 77 return ERR_PTR(ret); 78 } 79 80 return fb; 81 } 82 83 /** 84 * drm_gem_fb_destroy - Free GEM backed framebuffer 85 * @fb: DRM framebuffer 86 * 87 * Frees a GEM backed framebuffer with its backing buffer(s) and the structure 88 * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy 89 * callback. 90 */ 91 void drm_gem_fb_destroy(struct drm_framebuffer *fb) 92 { 93 int i; 94 95 for (i = 0; i < 4; i++) 96 drm_gem_object_put_unlocked(fb->obj[i]); 97 98 drm_framebuffer_cleanup(fb); 99 kfree(fb); 100 } 101 EXPORT_SYMBOL(drm_gem_fb_destroy); 102 103 /** 104 * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer 105 * @fb: DRM framebuffer 106 * @file: drm file 107 * @handle: handle created 108 * 109 * Drivers can use this as their &drm_framebuffer_funcs->create_handle 110 * callback. 111 * 112 * Returns: 113 * 0 on success or a negative error code on failure. 114 */ 115 int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file, 116 unsigned int *handle) 117 { 118 return drm_gem_handle_create(file, fb->obj[0], handle); 119 } 120 EXPORT_SYMBOL(drm_gem_fb_create_handle); 121 122 /** 123 * drm_gem_fb_create_with_funcs() - helper function for the 124 * &drm_mode_config_funcs.fb_create 125 * callback 126 * @dev: DRM device 127 * @file: drm file for the ioctl call 128 * @mode_cmd: metadata from the userspace fb creation request 129 * @funcs: vtable to be used for the new framebuffer object 130 * 131 * This can be used to set &drm_framebuffer_funcs for drivers that need the 132 * &drm_framebuffer_funcs.dirty callback. Use drm_gem_fb_create() if you don't 133 * need to change &drm_framebuffer_funcs. 134 * The function does buffer size validation. 135 */ 136 struct drm_framebuffer * 137 drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file, 138 const struct drm_mode_fb_cmd2 *mode_cmd, 139 const struct drm_framebuffer_funcs *funcs) 140 { 141 const struct drm_format_info *info; 142 struct drm_gem_object *objs[4]; 143 struct drm_framebuffer *fb; 144 int ret, i; 145 146 info = drm_get_format_info(dev, mode_cmd); 147 if (!info) 148 return ERR_PTR(-EINVAL); 149 150 for (i = 0; i < info->num_planes; i++) { 151 unsigned int width = mode_cmd->width / (i ? info->hsub : 1); 152 unsigned int height = mode_cmd->height / (i ? info->vsub : 1); 153 unsigned int min_size; 154 155 objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]); 156 if (!objs[i]) { 157 DRM_DEV_ERROR(dev->dev, "Failed to lookup GEM\n"); 158 ret = -ENOENT; 159 goto err_gem_object_put; 160 } 161 162 min_size = (height - 1) * mode_cmd->pitches[i] 163 + width * info->cpp[i] 164 + mode_cmd->offsets[i]; 165 166 if (objs[i]->size < min_size) { 167 drm_gem_object_put_unlocked(objs[i]); 168 ret = -EINVAL; 169 goto err_gem_object_put; 170 } 171 } 172 173 fb = drm_gem_fb_alloc(dev, mode_cmd, objs, i, funcs); 174 if (IS_ERR(fb)) { 175 ret = PTR_ERR(fb); 176 goto err_gem_object_put; 177 } 178 179 return fb; 180 181 err_gem_object_put: 182 for (i--; i >= 0; i--) 183 drm_gem_object_put_unlocked(objs[i]); 184 185 return ERR_PTR(ret); 186 } 187 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs); 188 189 static const struct drm_framebuffer_funcs drm_gem_fb_funcs = { 190 .destroy = drm_gem_fb_destroy, 191 .create_handle = drm_gem_fb_create_handle, 192 }; 193 194 /** 195 * drm_gem_fb_create() - &drm_mode_config_funcs.fb_create callback function 196 * @dev: DRM device 197 * @file: drm file for the ioctl call 198 * @mode_cmd: metadata from the userspace fb creation request 199 * 200 * If your hardware has special alignment or pitch requirements these should be 201 * checked before calling this function. The function does buffer size 202 * validation. Use drm_gem_fb_create_with_funcs() if you need to set 203 * &drm_framebuffer_funcs.dirty. 204 */ 205 struct drm_framebuffer * 206 drm_gem_fb_create(struct drm_device *dev, struct drm_file *file, 207 const struct drm_mode_fb_cmd2 *mode_cmd) 208 { 209 return drm_gem_fb_create_with_funcs(dev, file, mode_cmd, 210 &drm_gem_fb_funcs); 211 } 212 EXPORT_SYMBOL_GPL(drm_gem_fb_create); 213 214 /** 215 * drm_gem_fb_prepare_fb() - Prepare gem framebuffer 216 * @plane: Which plane 217 * @state: Plane state attach fence to 218 * 219 * This can be used as the &drm_plane_helper_funcs.prepare_fb hook. 220 * 221 * This function checks if the plane FB has an dma-buf attached, extracts 222 * the exclusive fence and attaches it to plane state for the atomic helper 223 * to wait on. 224 * 225 * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple 226 * gem based framebuffer drivers which have their buffers always pinned in 227 * memory. 228 */ 229 int drm_gem_fb_prepare_fb(struct drm_plane *plane, 230 struct drm_plane_state *state) 231 { 232 struct dma_buf *dma_buf; 233 struct dma_fence *fence; 234 235 if ((plane->state->fb == state->fb) || !state->fb) 236 return 0; 237 238 dma_buf = drm_gem_fb_get_obj(state->fb, 0)->dma_buf; 239 if (dma_buf) { 240 fence = reservation_object_get_excl_rcu(dma_buf->resv); 241 drm_atomic_set_fence_for_plane(state, fence); 242 } 243 244 return 0; 245 } 246 EXPORT_SYMBOL_GPL(drm_gem_fb_prepare_fb); 247 248 /** 249 * drm_gem_fbdev_fb_create - Create a drm_framebuffer for fbdev emulation 250 * @dev: DRM device 251 * @sizes: fbdev size description 252 * @pitch_align: optional pitch alignment 253 * @obj: GEM object backing the framebuffer 254 * @funcs: vtable to be used for the new framebuffer object 255 * 256 * This function creates a framebuffer for use with fbdev emulation. 257 * 258 * Returns: 259 * Pointer to a drm_framebuffer on success or an error pointer on failure. 260 */ 261 struct drm_framebuffer * 262 drm_gem_fbdev_fb_create(struct drm_device *dev, 263 struct drm_fb_helper_surface_size *sizes, 264 unsigned int pitch_align, struct drm_gem_object *obj, 265 const struct drm_framebuffer_funcs *funcs) 266 { 267 struct drm_mode_fb_cmd2 mode_cmd = { 0 }; 268 269 mode_cmd.width = sizes->surface_width; 270 mode_cmd.height = sizes->surface_height; 271 mode_cmd.pitches[0] = sizes->surface_width * 272 DIV_ROUND_UP(sizes->surface_bpp, 8); 273 if (pitch_align) 274 mode_cmd.pitches[0] = roundup(mode_cmd.pitches[0], 275 pitch_align); 276 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, 277 sizes->surface_depth); 278 if (obj->size < mode_cmd.pitches[0] * mode_cmd.height) 279 return ERR_PTR(-EINVAL); 280 281 return drm_gem_fb_alloc(dev, &mode_cmd, &obj, 1, funcs); 282 } 283 EXPORT_SYMBOL(drm_gem_fbdev_fb_create); 284