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/export.h> 9 #include <linux/slab.h> 10 #include <linux/module.h> 11 12 #include <drm/drm_damage_helper.h> 13 #include <drm/drm_drv.h> 14 #include <drm/drm_fourcc.h> 15 #include <drm/drm_framebuffer.h> 16 #include <drm/drm_gem.h> 17 #include <drm/drm_gem_framebuffer_helper.h> 18 #include <drm/drm_modeset_helper.h> 19 20 #include "drm_internal.h" 21 22 MODULE_IMPORT_NS("DMA_BUF"); 23 24 #define AFBC_HEADER_SIZE 16 25 #define AFBC_TH_LAYOUT_ALIGNMENT 8 26 #define AFBC_HDR_ALIGN 64 27 #define AFBC_SUPERBLOCK_PIXELS 256 28 #define AFBC_SUPERBLOCK_ALIGNMENT 128 29 #define AFBC_TH_BODY_START_ALIGNMENT 4096 30 31 /** 32 * DOC: overview 33 * 34 * This library provides helpers for drivers that don't subclass 35 * &drm_framebuffer and use &drm_gem_object for their backing storage. 36 * 37 * Drivers without additional needs to validate framebuffers can simply use 38 * drm_gem_fb_create() and everything is wired up automatically. Other drivers 39 * can use all parts independently. 40 */ 41 42 /** 43 * drm_gem_fb_get_obj() - Get GEM object backing the framebuffer 44 * @fb: Framebuffer 45 * @plane: Plane index 46 * 47 * No additional reference is taken beyond the one that the &drm_frambuffer 48 * already holds. 49 * 50 * Returns: 51 * Pointer to &drm_gem_object for the given framebuffer and plane index or NULL 52 * if it does not exist. 53 */ 54 struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb, 55 unsigned int plane) 56 { 57 struct drm_device *dev = fb->dev; 58 59 if (drm_WARN_ON_ONCE(dev, plane >= ARRAY_SIZE(fb->obj))) 60 return NULL; 61 else if (drm_WARN_ON_ONCE(dev, !fb->obj[plane])) 62 return NULL; 63 64 return fb->obj[plane]; 65 } 66 EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj); 67 68 static int 69 drm_gem_fb_init(struct drm_device *dev, 70 struct drm_framebuffer *fb, 71 const struct drm_format_info *info, 72 const struct drm_mode_fb_cmd2 *mode_cmd, 73 struct drm_gem_object **obj, unsigned int num_planes, 74 const struct drm_framebuffer_funcs *funcs) 75 { 76 unsigned int i; 77 int ret; 78 79 drm_helper_mode_fill_fb_struct(dev, fb, info, mode_cmd); 80 81 for (i = 0; i < num_planes; i++) 82 fb->obj[i] = obj[i]; 83 84 ret = drm_framebuffer_init(dev, fb, funcs); 85 if (ret) 86 drm_err(dev, "Failed to init framebuffer: %d\n", ret); 87 88 return ret; 89 } 90 91 /** 92 * drm_gem_fb_destroy - Free GEM backed framebuffer 93 * @fb: Framebuffer 94 * 95 * Frees a GEM backed framebuffer with its backing buffer(s) and the structure 96 * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy 97 * callback. 98 */ 99 void drm_gem_fb_destroy(struct drm_framebuffer *fb) 100 { 101 unsigned int i; 102 103 for (i = 0; i < fb->format->num_planes; i++) 104 drm_gem_object_put(fb->obj[i]); 105 106 drm_framebuffer_cleanup(fb); 107 kfree(fb); 108 } 109 EXPORT_SYMBOL(drm_gem_fb_destroy); 110 111 /** 112 * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer 113 * @fb: Framebuffer 114 * @file: DRM file to register the handle for 115 * @handle: Pointer to return the created handle 116 * 117 * This function creates a handle for the GEM object backing the framebuffer. 118 * Drivers can use this as their &drm_framebuffer_funcs->create_handle 119 * callback. The GETFB IOCTL calls into this callback. 120 * 121 * Returns: 122 * 0 on success or a negative error code on failure. 123 */ 124 int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file, 125 unsigned int *handle) 126 { 127 return drm_gem_handle_create(file, fb->obj[0], handle); 128 } 129 EXPORT_SYMBOL(drm_gem_fb_create_handle); 130 131 /** 132 * drm_gem_fb_init_with_funcs() - Helper function for implementing 133 * &drm_mode_config_funcs.fb_create 134 * callback in cases when the driver 135 * allocates a subclass of 136 * struct drm_framebuffer 137 * @dev: DRM device 138 * @fb: framebuffer object 139 * @file: DRM file that holds the GEM handle(s) backing the framebuffer 140 * @info: pixel format information 141 * @mode_cmd: Metadata from the userspace framebuffer creation request 142 * @funcs: vtable to be used for the new framebuffer object 143 * 144 * This function can be used to set &drm_framebuffer_funcs for drivers that need 145 * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to 146 * change &drm_framebuffer_funcs. The function does buffer size validation. 147 * The buffer size validation is for a general case, though, so users should 148 * pay attention to the checks being appropriate for them or, at least, 149 * non-conflicting. 150 * 151 * Returns: 152 * Zero or a negative error code. 153 */ 154 int drm_gem_fb_init_with_funcs(struct drm_device *dev, 155 struct drm_framebuffer *fb, 156 struct drm_file *file, 157 const struct drm_format_info *info, 158 const struct drm_mode_fb_cmd2 *mode_cmd, 159 const struct drm_framebuffer_funcs *funcs) 160 { 161 struct drm_gem_object *objs[DRM_FORMAT_MAX_PLANES]; 162 unsigned int i; 163 int ret; 164 165 if (drm_drv_uses_atomic_modeset(dev) && 166 !drm_any_plane_has_format(dev, mode_cmd->pixel_format, 167 mode_cmd->modifier[0])) { 168 drm_dbg_kms(dev, "Unsupported pixel format %p4cc / modifier 0x%llx\n", 169 &mode_cmd->pixel_format, mode_cmd->modifier[0]); 170 return -EINVAL; 171 } 172 173 for (i = 0; i < info->num_planes; i++) { 174 unsigned int width = mode_cmd->width / (i ? info->hsub : 1); 175 unsigned int height = mode_cmd->height / (i ? info->vsub : 1); 176 unsigned int min_size; 177 178 objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]); 179 if (!objs[i]) { 180 drm_dbg_kms(dev, "Failed to lookup GEM object\n"); 181 ret = -ENOENT; 182 goto err_gem_object_put; 183 } 184 185 min_size = (height - 1) * mode_cmd->pitches[i] 186 + drm_format_info_min_pitch(info, i, width) 187 + mode_cmd->offsets[i]; 188 189 if (objs[i]->size < min_size) { 190 drm_dbg_kms(dev, 191 "GEM object size (%zu) smaller than minimum size (%u) for plane %d\n", 192 objs[i]->size, min_size, i); 193 drm_gem_object_put(objs[i]); 194 ret = -EINVAL; 195 goto err_gem_object_put; 196 } 197 } 198 199 ret = drm_gem_fb_init(dev, fb, info, mode_cmd, objs, i, funcs); 200 if (ret) 201 goto err_gem_object_put; 202 203 return 0; 204 205 err_gem_object_put: 206 while (i > 0) { 207 --i; 208 drm_gem_object_put(objs[i]); 209 } 210 return ret; 211 } 212 EXPORT_SYMBOL_GPL(drm_gem_fb_init_with_funcs); 213 214 /** 215 * drm_gem_fb_create_with_funcs() - Helper function for the 216 * &drm_mode_config_funcs.fb_create 217 * callback 218 * @dev: DRM device 219 * @file: DRM file that holds the GEM handle(s) backing the framebuffer 220 * @info: pixel format information 221 * @mode_cmd: Metadata from the userspace framebuffer creation request 222 * @funcs: vtable to be used for the new framebuffer object 223 * 224 * This function can be used to set &drm_framebuffer_funcs for drivers that need 225 * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to 226 * change &drm_framebuffer_funcs. The function does buffer size validation. 227 * 228 * Returns: 229 * Pointer to a &drm_framebuffer on success or an error pointer on failure. 230 */ 231 struct drm_framebuffer * 232 drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file, 233 const struct drm_format_info *info, 234 const struct drm_mode_fb_cmd2 *mode_cmd, 235 const struct drm_framebuffer_funcs *funcs) 236 { 237 struct drm_framebuffer *fb; 238 int ret; 239 240 fb = kzalloc(sizeof(*fb), GFP_KERNEL); 241 if (!fb) 242 return ERR_PTR(-ENOMEM); 243 244 ret = drm_gem_fb_init_with_funcs(dev, fb, file, info, mode_cmd, funcs); 245 if (ret) { 246 kfree(fb); 247 return ERR_PTR(ret); 248 } 249 250 return fb; 251 } 252 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs); 253 254 static const struct drm_framebuffer_funcs drm_gem_fb_funcs = { 255 .destroy = drm_gem_fb_destroy, 256 .create_handle = drm_gem_fb_create_handle, 257 }; 258 259 /** 260 * drm_gem_fb_create() - Helper function for the 261 * &drm_mode_config_funcs.fb_create callback 262 * @dev: DRM device 263 * @file: DRM file that holds the GEM handle(s) backing the framebuffer 264 * @info: pixel format information 265 * @mode_cmd: Metadata from the userspace framebuffer creation request 266 * 267 * This function creates a new framebuffer object described by 268 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s) 269 * backing the framebuffer. 270 * 271 * If your hardware has special alignment or pitch requirements these should be 272 * checked before calling this function. The function does buffer size 273 * validation. Use drm_gem_fb_create_with_dirty() if you need framebuffer 274 * flushing. 275 * 276 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback. 277 * The ADDFB2 IOCTL calls into this callback. 278 * 279 * Returns: 280 * Pointer to a &drm_framebuffer on success or an error pointer on failure. 281 */ 282 struct drm_framebuffer * 283 drm_gem_fb_create(struct drm_device *dev, struct drm_file *file, 284 const struct drm_format_info *info, 285 const struct drm_mode_fb_cmd2 *mode_cmd) 286 { 287 return drm_gem_fb_create_with_funcs(dev, file, info, mode_cmd, 288 &drm_gem_fb_funcs); 289 } 290 EXPORT_SYMBOL_GPL(drm_gem_fb_create); 291 292 static const struct drm_framebuffer_funcs drm_gem_fb_funcs_dirtyfb = { 293 .destroy = drm_gem_fb_destroy, 294 .create_handle = drm_gem_fb_create_handle, 295 .dirty = drm_atomic_helper_dirtyfb, 296 }; 297 298 /** 299 * drm_gem_fb_create_with_dirty() - Helper function for the 300 * &drm_mode_config_funcs.fb_create callback 301 * @dev: DRM device 302 * @file: DRM file that holds the GEM handle(s) backing the framebuffer 303 * @info: pixel format information 304 * @mode_cmd: Metadata from the userspace framebuffer creation request 305 * 306 * This function creates a new framebuffer object described by 307 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s) 308 * backing the framebuffer. drm_atomic_helper_dirtyfb() is used for the dirty 309 * callback giving framebuffer flushing through the atomic machinery. Use 310 * drm_gem_fb_create() if you don't need the dirty callback. 311 * The function does buffer size validation. 312 * 313 * Drivers should also call drm_plane_enable_fb_damage_clips() on all planes 314 * to enable userspace to use damage clips also with the ATOMIC IOCTL. 315 * 316 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback. 317 * The ADDFB2 IOCTL calls into this callback. 318 * 319 * Returns: 320 * Pointer to a &drm_framebuffer on success or an error pointer on failure. 321 */ 322 struct drm_framebuffer * 323 drm_gem_fb_create_with_dirty(struct drm_device *dev, struct drm_file *file, 324 const struct drm_format_info *info, 325 const struct drm_mode_fb_cmd2 *mode_cmd) 326 { 327 return drm_gem_fb_create_with_funcs(dev, file, info, mode_cmd, 328 &drm_gem_fb_funcs_dirtyfb); 329 } 330 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_dirty); 331 332 /** 333 * drm_gem_fb_vmap - maps all framebuffer BOs into kernel address space 334 * @fb: the framebuffer 335 * @map: returns the mapping's address for each BO 336 * @data: returns the data address for each BO, can be NULL 337 * 338 * This function maps all buffer objects of the given framebuffer into 339 * kernel address space and stores them in struct iosys_map. If the 340 * mapping operation fails for one of the BOs, the function unmaps the 341 * already established mappings automatically. 342 * 343 * Callers that want to access a BO's stored data should pass @data. 344 * The argument returns the addresses of the data stored in each BO. This 345 * is different from @map if the framebuffer's offsets field is non-zero. 346 * 347 * Both, @map and @data, must each refer to arrays with at least 348 * fb->format->num_planes elements. 349 * 350 * See drm_gem_fb_vunmap() for unmapping. 351 * 352 * Returns: 353 * 0 on success, or a negative errno code otherwise. 354 */ 355 int drm_gem_fb_vmap(struct drm_framebuffer *fb, struct iosys_map *map, 356 struct iosys_map *data) 357 { 358 struct drm_gem_object *obj; 359 unsigned int i; 360 int ret; 361 362 for (i = 0; i < fb->format->num_planes; ++i) { 363 obj = drm_gem_fb_get_obj(fb, i); 364 if (!obj) { 365 ret = -EINVAL; 366 goto err_drm_gem_vunmap; 367 } 368 ret = drm_gem_vmap(obj, &map[i]); 369 if (ret) 370 goto err_drm_gem_vunmap; 371 } 372 373 if (data) { 374 for (i = 0; i < fb->format->num_planes; ++i) { 375 memcpy(&data[i], &map[i], sizeof(data[i])); 376 if (iosys_map_is_null(&data[i])) 377 continue; 378 iosys_map_incr(&data[i], fb->offsets[i]); 379 } 380 } 381 382 return 0; 383 384 err_drm_gem_vunmap: 385 while (i) { 386 --i; 387 obj = drm_gem_fb_get_obj(fb, i); 388 if (!obj) 389 continue; 390 drm_gem_vunmap(obj, &map[i]); 391 } 392 return ret; 393 } 394 EXPORT_SYMBOL(drm_gem_fb_vmap); 395 396 /** 397 * drm_gem_fb_vunmap - unmaps framebuffer BOs from kernel address space 398 * @fb: the framebuffer 399 * @map: mapping addresses as returned by drm_gem_fb_vmap() 400 * 401 * This function unmaps all buffer objects of the given framebuffer. 402 * 403 * See drm_gem_fb_vmap() for more information. 404 */ 405 void drm_gem_fb_vunmap(struct drm_framebuffer *fb, struct iosys_map *map) 406 { 407 unsigned int i = fb->format->num_planes; 408 struct drm_gem_object *obj; 409 410 while (i) { 411 --i; 412 obj = drm_gem_fb_get_obj(fb, i); 413 if (!obj) 414 continue; 415 if (iosys_map_is_null(&map[i])) 416 continue; 417 drm_gem_vunmap(obj, &map[i]); 418 } 419 } 420 EXPORT_SYMBOL(drm_gem_fb_vunmap); 421 422 static void __drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir, 423 unsigned int num_planes) 424 { 425 struct drm_gem_object *obj; 426 int ret; 427 428 while (num_planes) { 429 --num_planes; 430 obj = drm_gem_fb_get_obj(fb, num_planes); 431 if (!obj) 432 continue; 433 if (!drm_gem_is_imported(obj)) 434 continue; 435 ret = dma_buf_end_cpu_access(obj->dma_buf, dir); 436 if (ret) 437 drm_err(fb->dev, "dma_buf_end_cpu_access(%u, %d) failed: %d\n", 438 ret, num_planes, dir); 439 } 440 } 441 442 /** 443 * drm_gem_fb_begin_cpu_access - prepares GEM buffer objects for CPU access 444 * @fb: the framebuffer 445 * @dir: access mode 446 * 447 * Prepares a framebuffer's GEM buffer objects for CPU access. This function 448 * must be called before accessing the BO data within the kernel. For imported 449 * BOs, the function calls dma_buf_begin_cpu_access(). 450 * 451 * See drm_gem_fb_end_cpu_access() for signalling the end of CPU access. 452 * 453 * Returns: 454 * 0 on success, or a negative errno code otherwise. 455 */ 456 int drm_gem_fb_begin_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir) 457 { 458 struct drm_gem_object *obj; 459 unsigned int i; 460 int ret; 461 462 for (i = 0; i < fb->format->num_planes; ++i) { 463 obj = drm_gem_fb_get_obj(fb, i); 464 if (!obj) { 465 ret = -EINVAL; 466 goto err___drm_gem_fb_end_cpu_access; 467 } 468 if (!drm_gem_is_imported(obj)) 469 continue; 470 ret = dma_buf_begin_cpu_access(obj->dma_buf, dir); 471 if (ret) 472 goto err___drm_gem_fb_end_cpu_access; 473 } 474 475 return 0; 476 477 err___drm_gem_fb_end_cpu_access: 478 __drm_gem_fb_end_cpu_access(fb, dir, i); 479 return ret; 480 } 481 EXPORT_SYMBOL(drm_gem_fb_begin_cpu_access); 482 483 /** 484 * drm_gem_fb_end_cpu_access - signals end of CPU access to GEM buffer objects 485 * @fb: the framebuffer 486 * @dir: access mode 487 * 488 * Signals the end of CPU access to the given framebuffer's GEM buffer objects. This 489 * function must be paired with a corresponding call to drm_gem_fb_begin_cpu_access(). 490 * For imported BOs, the function calls dma_buf_end_cpu_access(). 491 * 492 * See also drm_gem_fb_begin_cpu_access(). 493 */ 494 void drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir) 495 { 496 __drm_gem_fb_end_cpu_access(fb, dir, fb->format->num_planes); 497 } 498 EXPORT_SYMBOL(drm_gem_fb_end_cpu_access); 499 500 // TODO Drop this function and replace by drm_format_info_bpp() once all 501 // DRM_FORMAT_* provide proper block info in drivers/gpu/drm/drm_fourcc.c 502 static __u32 drm_gem_afbc_get_bpp(struct drm_device *dev, 503 const struct drm_format_info *info, 504 const struct drm_mode_fb_cmd2 *mode_cmd) 505 { 506 switch (info->format) { 507 case DRM_FORMAT_YUV420_8BIT: 508 return 12; 509 case DRM_FORMAT_YUV420_10BIT: 510 return 15; 511 case DRM_FORMAT_VUY101010: 512 return 30; 513 default: 514 return drm_format_info_bpp(info, 0); 515 } 516 } 517 518 static int drm_gem_afbc_min_size(struct drm_device *dev, 519 const struct drm_format_info *info, 520 const struct drm_mode_fb_cmd2 *mode_cmd, 521 struct drm_afbc_framebuffer *afbc_fb) 522 { 523 __u32 n_blocks, w_alignment, h_alignment, hdr_alignment; 524 /* remove bpp when all users properly encode cpp in drm_format_info */ 525 __u32 bpp; 526 527 switch (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) { 528 case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16: 529 afbc_fb->block_width = 16; 530 afbc_fb->block_height = 16; 531 break; 532 case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8: 533 afbc_fb->block_width = 32; 534 afbc_fb->block_height = 8; 535 break; 536 /* no user exists yet - fall through */ 537 case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4: 538 case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4: 539 default: 540 drm_dbg_kms(dev, "Invalid AFBC_FORMAT_MOD_BLOCK_SIZE: %lld.\n", 541 mode_cmd->modifier[0] 542 & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK); 543 return -EINVAL; 544 } 545 546 /* tiled header afbc */ 547 w_alignment = afbc_fb->block_width; 548 h_alignment = afbc_fb->block_height; 549 hdr_alignment = AFBC_HDR_ALIGN; 550 if (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_TILED) { 551 w_alignment *= AFBC_TH_LAYOUT_ALIGNMENT; 552 h_alignment *= AFBC_TH_LAYOUT_ALIGNMENT; 553 hdr_alignment = AFBC_TH_BODY_START_ALIGNMENT; 554 } 555 556 afbc_fb->aligned_width = ALIGN(mode_cmd->width, w_alignment); 557 afbc_fb->aligned_height = ALIGN(mode_cmd->height, h_alignment); 558 afbc_fb->offset = mode_cmd->offsets[0]; 559 560 bpp = drm_gem_afbc_get_bpp(dev, info, mode_cmd); 561 if (!bpp) { 562 drm_dbg_kms(dev, "Invalid AFBC bpp value: %d\n", bpp); 563 return -EINVAL; 564 } 565 566 n_blocks = (afbc_fb->aligned_width * afbc_fb->aligned_height) 567 / AFBC_SUPERBLOCK_PIXELS; 568 afbc_fb->afbc_size = ALIGN(n_blocks * AFBC_HEADER_SIZE, hdr_alignment); 569 afbc_fb->afbc_size += n_blocks * ALIGN(bpp * AFBC_SUPERBLOCK_PIXELS / 8, 570 AFBC_SUPERBLOCK_ALIGNMENT); 571 572 return 0; 573 } 574 575 /** 576 * drm_gem_fb_afbc_init() - Helper function for drivers using afbc to 577 * fill and validate all the afbc-specific 578 * struct drm_afbc_framebuffer members 579 * 580 * @dev: DRM device 581 * @afbc_fb: afbc-specific framebuffer 582 * @info: pixel format information 583 * @mode_cmd: Metadata from the userspace framebuffer creation request 584 * @afbc_fb: afbc framebuffer 585 * 586 * This function can be used by drivers which support afbc to complete 587 * the preparation of struct drm_afbc_framebuffer. It must be called after 588 * allocating the said struct and calling drm_gem_fb_init_with_funcs(). 589 * It is caller's responsibility to put afbc_fb->base.obj objects in case 590 * the call is unsuccessful. 591 * 592 * Returns: 593 * Zero on success or a negative error value on failure. 594 */ 595 int drm_gem_fb_afbc_init(struct drm_device *dev, 596 const struct drm_format_info *info, 597 const struct drm_mode_fb_cmd2 *mode_cmd, 598 struct drm_afbc_framebuffer *afbc_fb) 599 { 600 struct drm_gem_object **objs; 601 int ret; 602 603 objs = afbc_fb->base.obj; 604 605 ret = drm_gem_afbc_min_size(dev, info, mode_cmd, afbc_fb); 606 if (ret < 0) 607 return ret; 608 609 if (objs[0]->size < afbc_fb->afbc_size) { 610 drm_dbg_kms(dev, "GEM object size (%zu) smaller than minimum afbc size (%u)\n", 611 objs[0]->size, afbc_fb->afbc_size); 612 return -EINVAL; 613 } 614 615 return 0; 616 } 617 EXPORT_SYMBOL_GPL(drm_gem_fb_afbc_init); 618