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_mode_fb_cmd2 *mode_cmd, 72 struct drm_gem_object **obj, unsigned int num_planes, 73 const struct drm_framebuffer_funcs *funcs) 74 { 75 unsigned int i; 76 int ret; 77 78 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd); 79 80 for (i = 0; i < num_planes; i++) 81 fb->obj[i] = obj[i]; 82 83 ret = drm_framebuffer_init(dev, fb, funcs); 84 if (ret) 85 drm_err(dev, "Failed to init framebuffer: %d\n", ret); 86 87 return ret; 88 } 89 90 /** 91 * drm_gem_fb_destroy - Free GEM backed framebuffer 92 * @fb: Framebuffer 93 * 94 * Frees a GEM backed framebuffer with its backing buffer(s) and the structure 95 * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy 96 * callback. 97 */ 98 void drm_gem_fb_destroy(struct drm_framebuffer *fb) 99 { 100 unsigned int i; 101 102 for (i = 0; i < fb->format->num_planes; i++) 103 drm_gem_object_put(fb->obj[i]); 104 105 drm_framebuffer_cleanup(fb); 106 kfree(fb); 107 } 108 EXPORT_SYMBOL(drm_gem_fb_destroy); 109 110 /** 111 * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer 112 * @fb: Framebuffer 113 * @file: DRM file to register the handle for 114 * @handle: Pointer to return the created handle 115 * 116 * This function creates a handle for the GEM object backing the framebuffer. 117 * Drivers can use this as their &drm_framebuffer_funcs->create_handle 118 * callback. The GETFB IOCTL calls into this callback. 119 * 120 * Returns: 121 * 0 on success or a negative error code on failure. 122 */ 123 int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file, 124 unsigned int *handle) 125 { 126 return drm_gem_handle_create(file, fb->obj[0], handle); 127 } 128 EXPORT_SYMBOL(drm_gem_fb_create_handle); 129 130 /** 131 * drm_gem_fb_init_with_funcs() - Helper function for implementing 132 * &drm_mode_config_funcs.fb_create 133 * callback in cases when the driver 134 * allocates a subclass of 135 * struct drm_framebuffer 136 * @dev: DRM device 137 * @fb: framebuffer object 138 * @file: DRM file that holds the GEM handle(s) backing the framebuffer 139 * @mode_cmd: Metadata from the userspace framebuffer creation request 140 * @funcs: vtable to be used for the new framebuffer object 141 * 142 * This function can be used to set &drm_framebuffer_funcs for drivers that need 143 * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to 144 * change &drm_framebuffer_funcs. The function does buffer size validation. 145 * The buffer size validation is for a general case, though, so users should 146 * pay attention to the checks being appropriate for them or, at least, 147 * non-conflicting. 148 * 149 * Returns: 150 * Zero or a negative error code. 151 */ 152 int drm_gem_fb_init_with_funcs(struct drm_device *dev, 153 struct drm_framebuffer *fb, 154 struct drm_file *file, 155 const struct drm_mode_fb_cmd2 *mode_cmd, 156 const struct drm_framebuffer_funcs *funcs) 157 { 158 const struct drm_format_info *info; 159 struct drm_gem_object *objs[DRM_FORMAT_MAX_PLANES]; 160 unsigned int i; 161 int ret; 162 163 info = drm_get_format_info(dev, mode_cmd); 164 if (!info) { 165 drm_dbg_kms(dev, "Failed to get FB format info\n"); 166 return -EINVAL; 167 } 168 169 if (drm_drv_uses_atomic_modeset(dev) && 170 !drm_any_plane_has_format(dev, mode_cmd->pixel_format, 171 mode_cmd->modifier[0])) { 172 drm_dbg_kms(dev, "Unsupported pixel format %p4cc / modifier 0x%llx\n", 173 &mode_cmd->pixel_format, mode_cmd->modifier[0]); 174 return -EINVAL; 175 } 176 177 for (i = 0; i < info->num_planes; i++) { 178 unsigned int width = mode_cmd->width / (i ? info->hsub : 1); 179 unsigned int height = mode_cmd->height / (i ? info->vsub : 1); 180 unsigned int min_size; 181 182 objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]); 183 if (!objs[i]) { 184 drm_dbg_kms(dev, "Failed to lookup GEM object\n"); 185 ret = -ENOENT; 186 goto err_gem_object_put; 187 } 188 189 min_size = (height - 1) * mode_cmd->pitches[i] 190 + drm_format_info_min_pitch(info, i, width) 191 + mode_cmd->offsets[i]; 192 193 if (objs[i]->size < min_size) { 194 drm_dbg_kms(dev, 195 "GEM object size (%zu) smaller than minimum size (%u) for plane %d\n", 196 objs[i]->size, min_size, i); 197 drm_gem_object_put(objs[i]); 198 ret = -EINVAL; 199 goto err_gem_object_put; 200 } 201 } 202 203 ret = drm_gem_fb_init(dev, fb, mode_cmd, objs, i, funcs); 204 if (ret) 205 goto err_gem_object_put; 206 207 return 0; 208 209 err_gem_object_put: 210 while (i > 0) { 211 --i; 212 drm_gem_object_put(objs[i]); 213 } 214 return ret; 215 } 216 EXPORT_SYMBOL_GPL(drm_gem_fb_init_with_funcs); 217 218 /** 219 * drm_gem_fb_create_with_funcs() - Helper function for the 220 * &drm_mode_config_funcs.fb_create 221 * callback 222 * @dev: DRM device 223 * @file: DRM file that holds the GEM handle(s) backing the framebuffer 224 * @mode_cmd: Metadata from the userspace framebuffer creation request 225 * @funcs: vtable to be used for the new framebuffer object 226 * 227 * This function can be used to set &drm_framebuffer_funcs for drivers that need 228 * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to 229 * change &drm_framebuffer_funcs. The function does buffer size validation. 230 * 231 * Returns: 232 * Pointer to a &drm_framebuffer on success or an error pointer on failure. 233 */ 234 struct drm_framebuffer * 235 drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file, 236 const struct drm_mode_fb_cmd2 *mode_cmd, 237 const struct drm_framebuffer_funcs *funcs) 238 { 239 struct drm_framebuffer *fb; 240 int ret; 241 242 fb = kzalloc(sizeof(*fb), GFP_KERNEL); 243 if (!fb) 244 return ERR_PTR(-ENOMEM); 245 246 ret = drm_gem_fb_init_with_funcs(dev, fb, file, mode_cmd, funcs); 247 if (ret) { 248 kfree(fb); 249 return ERR_PTR(ret); 250 } 251 252 return fb; 253 } 254 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs); 255 256 static const struct drm_framebuffer_funcs drm_gem_fb_funcs = { 257 .destroy = drm_gem_fb_destroy, 258 .create_handle = drm_gem_fb_create_handle, 259 }; 260 261 /** 262 * drm_gem_fb_create() - Helper function for the 263 * &drm_mode_config_funcs.fb_create callback 264 * @dev: DRM device 265 * @file: DRM file that holds the GEM handle(s) backing the framebuffer 266 * @mode_cmd: Metadata from the userspace framebuffer creation request 267 * 268 * This function creates a new framebuffer object described by 269 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s) 270 * backing the framebuffer. 271 * 272 * If your hardware has special alignment or pitch requirements these should be 273 * checked before calling this function. The function does buffer size 274 * validation. Use drm_gem_fb_create_with_dirty() if you need framebuffer 275 * flushing. 276 * 277 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback. 278 * The ADDFB2 IOCTL calls into this callback. 279 * 280 * Returns: 281 * Pointer to a &drm_framebuffer on success or an error pointer on failure. 282 */ 283 struct drm_framebuffer * 284 drm_gem_fb_create(struct drm_device *dev, struct drm_file *file, 285 const struct drm_mode_fb_cmd2 *mode_cmd) 286 { 287 return drm_gem_fb_create_with_funcs(dev, file, 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 * @mode_cmd: Metadata from the userspace framebuffer creation request 304 * 305 * This function creates a new framebuffer object described by 306 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s) 307 * backing the framebuffer. drm_atomic_helper_dirtyfb() is used for the dirty 308 * callback giving framebuffer flushing through the atomic machinery. Use 309 * drm_gem_fb_create() if you don't need the dirty callback. 310 * The function does buffer size validation. 311 * 312 * Drivers should also call drm_plane_enable_fb_damage_clips() on all planes 313 * to enable userspace to use damage clips also with the ATOMIC IOCTL. 314 * 315 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback. 316 * The ADDFB2 IOCTL calls into this callback. 317 * 318 * Returns: 319 * Pointer to a &drm_framebuffer on success or an error pointer on failure. 320 */ 321 struct drm_framebuffer * 322 drm_gem_fb_create_with_dirty(struct drm_device *dev, struct drm_file *file, 323 const struct drm_mode_fb_cmd2 *mode_cmd) 324 { 325 return drm_gem_fb_create_with_funcs(dev, file, mode_cmd, 326 &drm_gem_fb_funcs_dirtyfb); 327 } 328 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_dirty); 329 330 /** 331 * drm_gem_fb_vmap - maps all framebuffer BOs into kernel address space 332 * @fb: the framebuffer 333 * @map: returns the mapping's address for each BO 334 * @data: returns the data address for each BO, can be NULL 335 * 336 * This function maps all buffer objects of the given framebuffer into 337 * kernel address space and stores them in struct iosys_map. If the 338 * mapping operation fails for one of the BOs, the function unmaps the 339 * already established mappings automatically. 340 * 341 * Callers that want to access a BO's stored data should pass @data. 342 * The argument returns the addresses of the data stored in each BO. This 343 * is different from @map if the framebuffer's offsets field is non-zero. 344 * 345 * Both, @map and @data, must each refer to arrays with at least 346 * fb->format->num_planes elements. 347 * 348 * See drm_gem_fb_vunmap() for unmapping. 349 * 350 * Returns: 351 * 0 on success, or a negative errno code otherwise. 352 */ 353 int drm_gem_fb_vmap(struct drm_framebuffer *fb, struct iosys_map *map, 354 struct iosys_map *data) 355 { 356 struct drm_gem_object *obj; 357 unsigned int i; 358 int ret; 359 360 for (i = 0; i < fb->format->num_planes; ++i) { 361 obj = drm_gem_fb_get_obj(fb, i); 362 if (!obj) { 363 ret = -EINVAL; 364 goto err_drm_gem_vunmap; 365 } 366 ret = drm_gem_vmap(obj, &map[i]); 367 if (ret) 368 goto err_drm_gem_vunmap; 369 } 370 371 if (data) { 372 for (i = 0; i < fb->format->num_planes; ++i) { 373 memcpy(&data[i], &map[i], sizeof(data[i])); 374 if (iosys_map_is_null(&data[i])) 375 continue; 376 iosys_map_incr(&data[i], fb->offsets[i]); 377 } 378 } 379 380 return 0; 381 382 err_drm_gem_vunmap: 383 while (i) { 384 --i; 385 obj = drm_gem_fb_get_obj(fb, i); 386 if (!obj) 387 continue; 388 drm_gem_vunmap(obj, &map[i]); 389 } 390 return ret; 391 } 392 EXPORT_SYMBOL(drm_gem_fb_vmap); 393 394 /** 395 * drm_gem_fb_vunmap - unmaps framebuffer BOs from kernel address space 396 * @fb: the framebuffer 397 * @map: mapping addresses as returned by drm_gem_fb_vmap() 398 * 399 * This function unmaps all buffer objects of the given framebuffer. 400 * 401 * See drm_gem_fb_vmap() for more information. 402 */ 403 void drm_gem_fb_vunmap(struct drm_framebuffer *fb, struct iosys_map *map) 404 { 405 unsigned int i = fb->format->num_planes; 406 struct drm_gem_object *obj; 407 408 while (i) { 409 --i; 410 obj = drm_gem_fb_get_obj(fb, i); 411 if (!obj) 412 continue; 413 if (iosys_map_is_null(&map[i])) 414 continue; 415 drm_gem_vunmap(obj, &map[i]); 416 } 417 } 418 EXPORT_SYMBOL(drm_gem_fb_vunmap); 419 420 static void __drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir, 421 unsigned int num_planes) 422 { 423 struct drm_gem_object *obj; 424 int ret; 425 426 while (num_planes) { 427 --num_planes; 428 obj = drm_gem_fb_get_obj(fb, num_planes); 429 if (!obj) 430 continue; 431 if (!drm_gem_is_imported(obj)) 432 continue; 433 ret = dma_buf_end_cpu_access(obj->dma_buf, dir); 434 if (ret) 435 drm_err(fb->dev, "dma_buf_end_cpu_access(%u, %d) failed: %d\n", 436 ret, num_planes, dir); 437 } 438 } 439 440 /** 441 * drm_gem_fb_begin_cpu_access - prepares GEM buffer objects for CPU access 442 * @fb: the framebuffer 443 * @dir: access mode 444 * 445 * Prepares a framebuffer's GEM buffer objects for CPU access. This function 446 * must be called before accessing the BO data within the kernel. For imported 447 * BOs, the function calls dma_buf_begin_cpu_access(). 448 * 449 * See drm_gem_fb_end_cpu_access() for signalling the end of CPU access. 450 * 451 * Returns: 452 * 0 on success, or a negative errno code otherwise. 453 */ 454 int drm_gem_fb_begin_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir) 455 { 456 struct drm_gem_object *obj; 457 unsigned int i; 458 int ret; 459 460 for (i = 0; i < fb->format->num_planes; ++i) { 461 obj = drm_gem_fb_get_obj(fb, i); 462 if (!obj) { 463 ret = -EINVAL; 464 goto err___drm_gem_fb_end_cpu_access; 465 } 466 if (!drm_gem_is_imported(obj)) 467 continue; 468 ret = dma_buf_begin_cpu_access(obj->dma_buf, dir); 469 if (ret) 470 goto err___drm_gem_fb_end_cpu_access; 471 } 472 473 return 0; 474 475 err___drm_gem_fb_end_cpu_access: 476 __drm_gem_fb_end_cpu_access(fb, dir, i); 477 return ret; 478 } 479 EXPORT_SYMBOL(drm_gem_fb_begin_cpu_access); 480 481 /** 482 * drm_gem_fb_end_cpu_access - signals end of CPU access to GEM buffer objects 483 * @fb: the framebuffer 484 * @dir: access mode 485 * 486 * Signals the end of CPU access to the given framebuffer's GEM buffer objects. This 487 * function must be paired with a corresponding call to drm_gem_fb_begin_cpu_access(). 488 * For imported BOs, the function calls dma_buf_end_cpu_access(). 489 * 490 * See also drm_gem_fb_begin_cpu_access(). 491 */ 492 void drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir) 493 { 494 __drm_gem_fb_end_cpu_access(fb, dir, fb->format->num_planes); 495 } 496 EXPORT_SYMBOL(drm_gem_fb_end_cpu_access); 497 498 // TODO Drop this function and replace by drm_format_info_bpp() once all 499 // DRM_FORMAT_* provide proper block info in drivers/gpu/drm/drm_fourcc.c 500 static __u32 drm_gem_afbc_get_bpp(struct drm_device *dev, 501 const struct drm_mode_fb_cmd2 *mode_cmd) 502 { 503 const struct drm_format_info *info; 504 505 info = drm_get_format_info(dev, mode_cmd); 506 507 switch (info->format) { 508 case DRM_FORMAT_YUV420_8BIT: 509 return 12; 510 case DRM_FORMAT_YUV420_10BIT: 511 return 15; 512 case DRM_FORMAT_VUY101010: 513 return 30; 514 default: 515 return drm_format_info_bpp(info, 0); 516 } 517 } 518 519 static int drm_gem_afbc_min_size(struct drm_device *dev, 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, 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 * @mode_cmd: Metadata from the userspace framebuffer creation request 583 * @afbc_fb: afbc framebuffer 584 * 585 * This function can be used by drivers which support afbc to complete 586 * the preparation of struct drm_afbc_framebuffer. It must be called after 587 * allocating the said struct and calling drm_gem_fb_init_with_funcs(). 588 * It is caller's responsibility to put afbc_fb->base.obj objects in case 589 * the call is unsuccessful. 590 * 591 * Returns: 592 * Zero on success or a negative error value on failure. 593 */ 594 int drm_gem_fb_afbc_init(struct drm_device *dev, 595 const struct drm_mode_fb_cmd2 *mode_cmd, 596 struct drm_afbc_framebuffer *afbc_fb) 597 { 598 const struct drm_format_info *info; 599 struct drm_gem_object **objs; 600 int ret; 601 602 objs = afbc_fb->base.obj; 603 info = drm_get_format_info(dev, mode_cmd); 604 if (!info) 605 return -EINVAL; 606 607 ret = drm_gem_afbc_min_size(dev, mode_cmd, afbc_fb); 608 if (ret < 0) 609 return ret; 610 611 if (objs[0]->size < afbc_fb->afbc_size) { 612 drm_dbg_kms(dev, "GEM object size (%zu) smaller than minimum afbc size (%u)\n", 613 objs[0]->size, afbc_fb->afbc_size); 614 return -EINVAL; 615 } 616 617 return 0; 618 } 619 EXPORT_SYMBOL_GPL(drm_gem_fb_afbc_init); 620