1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * drm gem framebuffer helper functions 4 * 5 * Copyright (C) 2017 Noralf Trønnes 6 */ 7 8 #include <linux/slab.h> 9 10 #include <drm/drm_damage_helper.h> 11 #include <drm/drm_fb_helper.h> 12 #include <drm/drm_fourcc.h> 13 #include <drm/drm_framebuffer.h> 14 #include <drm/drm_gem.h> 15 #include <drm/drm_gem_framebuffer_helper.h> 16 #include <drm/drm_modeset_helper.h> 17 18 #define AFBC_HEADER_SIZE 16 19 #define AFBC_TH_LAYOUT_ALIGNMENT 8 20 #define AFBC_HDR_ALIGN 64 21 #define AFBC_SUPERBLOCK_PIXELS 256 22 #define AFBC_SUPERBLOCK_ALIGNMENT 128 23 #define AFBC_TH_BODY_START_ALIGNMENT 4096 24 25 /** 26 * DOC: overview 27 * 28 * This library provides helpers for drivers that don't subclass 29 * &drm_framebuffer and use &drm_gem_object for their backing storage. 30 * 31 * Drivers without additional needs to validate framebuffers can simply use 32 * drm_gem_fb_create() and everything is wired up automatically. Other drivers 33 * can use all parts independently. 34 */ 35 36 /** 37 * drm_gem_fb_get_obj() - Get GEM object backing the framebuffer 38 * @fb: Framebuffer 39 * @plane: Plane index 40 * 41 * No additional reference is taken beyond the one that the &drm_frambuffer 42 * already holds. 43 * 44 * Returns: 45 * Pointer to &drm_gem_object for the given framebuffer and plane index or NULL 46 * if it does not exist. 47 */ 48 struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb, 49 unsigned int plane) 50 { 51 if (plane >= 4) 52 return NULL; 53 54 return fb->obj[plane]; 55 } 56 EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj); 57 58 static int 59 drm_gem_fb_init(struct drm_device *dev, 60 struct drm_framebuffer *fb, 61 const struct drm_mode_fb_cmd2 *mode_cmd, 62 struct drm_gem_object **obj, unsigned int num_planes, 63 const struct drm_framebuffer_funcs *funcs) 64 { 65 int ret, i; 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_err(dev, "Failed to init framebuffer: %d\n", ret); 75 76 return ret; 77 } 78 79 /** 80 * drm_gem_fb_destroy - Free GEM backed framebuffer 81 * @fb: Framebuffer 82 * 83 * Frees a GEM backed framebuffer with its backing buffer(s) and the structure 84 * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy 85 * callback. 86 */ 87 void drm_gem_fb_destroy(struct drm_framebuffer *fb) 88 { 89 int i; 90 91 for (i = 0; i < 4; i++) 92 drm_gem_object_put(fb->obj[i]); 93 94 drm_framebuffer_cleanup(fb); 95 kfree(fb); 96 } 97 EXPORT_SYMBOL(drm_gem_fb_destroy); 98 99 /** 100 * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer 101 * @fb: Framebuffer 102 * @file: DRM file to register the handle for 103 * @handle: Pointer to return the created handle 104 * 105 * This function creates a handle for the GEM object backing the framebuffer. 106 * Drivers can use this as their &drm_framebuffer_funcs->create_handle 107 * callback. The GETFB IOCTL calls into this callback. 108 * 109 * Returns: 110 * 0 on success or a negative error code on failure. 111 */ 112 int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file, 113 unsigned int *handle) 114 { 115 return drm_gem_handle_create(file, fb->obj[0], handle); 116 } 117 EXPORT_SYMBOL(drm_gem_fb_create_handle); 118 119 /** 120 * drm_gem_fb_init_with_funcs() - Helper function for implementing 121 * &drm_mode_config_funcs.fb_create 122 * callback in cases when the driver 123 * allocates a subclass of 124 * struct drm_framebuffer 125 * @dev: DRM device 126 * @fb: framebuffer object 127 * @file: DRM file that holds the GEM handle(s) backing the framebuffer 128 * @mode_cmd: Metadata from the userspace framebuffer creation request 129 * @funcs: vtable to be used for the new framebuffer object 130 * 131 * This function can be used to set &drm_framebuffer_funcs for drivers that need 132 * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to 133 * change &drm_framebuffer_funcs. The function does buffer size validation. 134 * The buffer size validation is for a general case, though, so users should 135 * pay attention to the checks being appropriate for them or, at least, 136 * non-conflicting. 137 * 138 * Returns: 139 * Zero or a negative error code. 140 */ 141 int drm_gem_fb_init_with_funcs(struct drm_device *dev, 142 struct drm_framebuffer *fb, 143 struct drm_file *file, 144 const struct drm_mode_fb_cmd2 *mode_cmd, 145 const struct drm_framebuffer_funcs *funcs) 146 { 147 const struct drm_format_info *info; 148 struct drm_gem_object *objs[4]; 149 int ret, i; 150 151 info = drm_get_format_info(dev, mode_cmd); 152 if (!info) 153 return -EINVAL; 154 155 for (i = 0; i < info->num_planes; i++) { 156 unsigned int width = mode_cmd->width / (i ? info->hsub : 1); 157 unsigned int height = mode_cmd->height / (i ? info->vsub : 1); 158 unsigned int min_size; 159 160 objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]); 161 if (!objs[i]) { 162 drm_dbg_kms(dev, "Failed to lookup GEM object\n"); 163 ret = -ENOENT; 164 goto err_gem_object_put; 165 } 166 167 min_size = (height - 1) * mode_cmd->pitches[i] 168 + drm_format_info_min_pitch(info, i, width) 169 + mode_cmd->offsets[i]; 170 171 if (objs[i]->size < min_size) { 172 drm_gem_object_put(objs[i]); 173 ret = -EINVAL; 174 goto err_gem_object_put; 175 } 176 } 177 178 ret = drm_gem_fb_init(dev, fb, mode_cmd, objs, i, funcs); 179 if (ret) 180 goto err_gem_object_put; 181 182 return 0; 183 184 err_gem_object_put: 185 for (i--; i >= 0; i--) 186 drm_gem_object_put(objs[i]); 187 188 return ret; 189 } 190 EXPORT_SYMBOL_GPL(drm_gem_fb_init_with_funcs); 191 192 /** 193 * drm_gem_fb_create_with_funcs() - Helper function for the 194 * &drm_mode_config_funcs.fb_create 195 * callback 196 * @dev: DRM device 197 * @file: DRM file that holds the GEM handle(s) backing the framebuffer 198 * @mode_cmd: Metadata from the userspace framebuffer creation request 199 * @funcs: vtable to be used for the new framebuffer object 200 * 201 * This function can be used to set &drm_framebuffer_funcs for drivers that need 202 * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to 203 * change &drm_framebuffer_funcs. The function does buffer size validation. 204 * 205 * Returns: 206 * Pointer to a &drm_framebuffer on success or an error pointer on failure. 207 */ 208 struct drm_framebuffer * 209 drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file, 210 const struct drm_mode_fb_cmd2 *mode_cmd, 211 const struct drm_framebuffer_funcs *funcs) 212 { 213 struct drm_framebuffer *fb; 214 int ret; 215 216 fb = kzalloc(sizeof(*fb), GFP_KERNEL); 217 if (!fb) 218 return ERR_PTR(-ENOMEM); 219 220 ret = drm_gem_fb_init_with_funcs(dev, fb, file, mode_cmd, funcs); 221 if (ret) { 222 kfree(fb); 223 return ERR_PTR(ret); 224 } 225 226 return fb; 227 } 228 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs); 229 230 static const struct drm_framebuffer_funcs drm_gem_fb_funcs = { 231 .destroy = drm_gem_fb_destroy, 232 .create_handle = drm_gem_fb_create_handle, 233 }; 234 235 /** 236 * drm_gem_fb_create() - Helper function for the 237 * &drm_mode_config_funcs.fb_create callback 238 * @dev: DRM device 239 * @file: DRM file that holds the GEM handle(s) backing the framebuffer 240 * @mode_cmd: Metadata from the userspace framebuffer creation request 241 * 242 * This function creates a new framebuffer object described by 243 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s) 244 * backing the framebuffer. 245 * 246 * If your hardware has special alignment or pitch requirements these should be 247 * checked before calling this function. The function does buffer size 248 * validation. Use drm_gem_fb_create_with_dirty() if you need framebuffer 249 * flushing. 250 * 251 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback. 252 * The ADDFB2 IOCTL calls into this callback. 253 * 254 * Returns: 255 * Pointer to a &drm_framebuffer on success or an error pointer on failure. 256 */ 257 struct drm_framebuffer * 258 drm_gem_fb_create(struct drm_device *dev, struct drm_file *file, 259 const struct drm_mode_fb_cmd2 *mode_cmd) 260 { 261 return drm_gem_fb_create_with_funcs(dev, file, mode_cmd, 262 &drm_gem_fb_funcs); 263 } 264 EXPORT_SYMBOL_GPL(drm_gem_fb_create); 265 266 static const struct drm_framebuffer_funcs drm_gem_fb_funcs_dirtyfb = { 267 .destroy = drm_gem_fb_destroy, 268 .create_handle = drm_gem_fb_create_handle, 269 .dirty = drm_atomic_helper_dirtyfb, 270 }; 271 272 /** 273 * drm_gem_fb_create_with_dirty() - Helper function for the 274 * &drm_mode_config_funcs.fb_create callback 275 * @dev: DRM device 276 * @file: DRM file that holds the GEM handle(s) backing the framebuffer 277 * @mode_cmd: Metadata from the userspace framebuffer creation request 278 * 279 * This function creates a new framebuffer object described by 280 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s) 281 * backing the framebuffer. drm_atomic_helper_dirtyfb() is used for the dirty 282 * callback giving framebuffer flushing through the atomic machinery. Use 283 * drm_gem_fb_create() if you don't need the dirty callback. 284 * The function does buffer size validation. 285 * 286 * Drivers should also call drm_plane_enable_fb_damage_clips() on all planes 287 * to enable userspace to use damage clips also with the ATOMIC IOCTL. 288 * 289 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback. 290 * The ADDFB2 IOCTL calls into this callback. 291 * 292 * Returns: 293 * Pointer to a &drm_framebuffer on success or an error pointer on failure. 294 */ 295 struct drm_framebuffer * 296 drm_gem_fb_create_with_dirty(struct drm_device *dev, struct drm_file *file, 297 const struct drm_mode_fb_cmd2 *mode_cmd) 298 { 299 return drm_gem_fb_create_with_funcs(dev, file, mode_cmd, 300 &drm_gem_fb_funcs_dirtyfb); 301 } 302 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_dirty); 303 304 static __u32 drm_gem_afbc_get_bpp(struct drm_device *dev, 305 const struct drm_mode_fb_cmd2 *mode_cmd) 306 { 307 const struct drm_format_info *info; 308 309 info = drm_get_format_info(dev, mode_cmd); 310 311 /* use whatever a driver has set */ 312 if (info->cpp[0]) 313 return info->cpp[0] * 8; 314 315 /* guess otherwise */ 316 switch (info->format) { 317 case DRM_FORMAT_YUV420_8BIT: 318 return 12; 319 case DRM_FORMAT_YUV420_10BIT: 320 return 15; 321 case DRM_FORMAT_VUY101010: 322 return 30; 323 default: 324 break; 325 } 326 327 /* all attempts failed */ 328 return 0; 329 } 330 331 static int drm_gem_afbc_min_size(struct drm_device *dev, 332 const struct drm_mode_fb_cmd2 *mode_cmd, 333 struct drm_afbc_framebuffer *afbc_fb) 334 { 335 __u32 n_blocks, w_alignment, h_alignment, hdr_alignment; 336 /* remove bpp when all users properly encode cpp in drm_format_info */ 337 __u32 bpp; 338 339 switch (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) { 340 case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16: 341 afbc_fb->block_width = 16; 342 afbc_fb->block_height = 16; 343 break; 344 case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8: 345 afbc_fb->block_width = 32; 346 afbc_fb->block_height = 8; 347 break; 348 /* no user exists yet - fall through */ 349 case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4: 350 case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4: 351 default: 352 drm_dbg_kms(dev, "Invalid AFBC_FORMAT_MOD_BLOCK_SIZE: %lld.\n", 353 mode_cmd->modifier[0] 354 & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK); 355 return -EINVAL; 356 } 357 358 /* tiled header afbc */ 359 w_alignment = afbc_fb->block_width; 360 h_alignment = afbc_fb->block_height; 361 hdr_alignment = AFBC_HDR_ALIGN; 362 if (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_TILED) { 363 w_alignment *= AFBC_TH_LAYOUT_ALIGNMENT; 364 h_alignment *= AFBC_TH_LAYOUT_ALIGNMENT; 365 hdr_alignment = AFBC_TH_BODY_START_ALIGNMENT; 366 } 367 368 afbc_fb->aligned_width = ALIGN(mode_cmd->width, w_alignment); 369 afbc_fb->aligned_height = ALIGN(mode_cmd->height, h_alignment); 370 afbc_fb->offset = mode_cmd->offsets[0]; 371 372 bpp = drm_gem_afbc_get_bpp(dev, mode_cmd); 373 if (!bpp) { 374 drm_dbg_kms(dev, "Invalid AFBC bpp value: %d\n", bpp); 375 return -EINVAL; 376 } 377 378 n_blocks = (afbc_fb->aligned_width * afbc_fb->aligned_height) 379 / AFBC_SUPERBLOCK_PIXELS; 380 afbc_fb->afbc_size = ALIGN(n_blocks * AFBC_HEADER_SIZE, hdr_alignment); 381 afbc_fb->afbc_size += n_blocks * ALIGN(bpp * AFBC_SUPERBLOCK_PIXELS / 8, 382 AFBC_SUPERBLOCK_ALIGNMENT); 383 384 return 0; 385 } 386 387 /** 388 * drm_gem_fb_afbc_init() - Helper function for drivers using afbc to 389 * fill and validate all the afbc-specific 390 * struct drm_afbc_framebuffer members 391 * 392 * @dev: DRM device 393 * @afbc_fb: afbc-specific framebuffer 394 * @mode_cmd: Metadata from the userspace framebuffer creation request 395 * @afbc_fb: afbc framebuffer 396 * 397 * This function can be used by drivers which support afbc to complete 398 * the preparation of struct drm_afbc_framebuffer. It must be called after 399 * allocating the said struct and calling drm_gem_fb_init_with_funcs(). 400 * It is caller's responsibility to put afbc_fb->base.obj objects in case 401 * the call is unsuccessful. 402 * 403 * Returns: 404 * Zero on success or a negative error value on failure. 405 */ 406 int drm_gem_fb_afbc_init(struct drm_device *dev, 407 const struct drm_mode_fb_cmd2 *mode_cmd, 408 struct drm_afbc_framebuffer *afbc_fb) 409 { 410 const struct drm_format_info *info; 411 struct drm_gem_object **objs; 412 int ret; 413 414 objs = afbc_fb->base.obj; 415 info = drm_get_format_info(dev, mode_cmd); 416 if (!info) 417 return -EINVAL; 418 419 ret = drm_gem_afbc_min_size(dev, mode_cmd, afbc_fb); 420 if (ret < 0) 421 return ret; 422 423 if (objs[0]->size < afbc_fb->afbc_size) 424 return -EINVAL; 425 426 return 0; 427 } 428 EXPORT_SYMBOL_GPL(drm_gem_fb_afbc_init); 429