1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012-2013 Avionic Design GmbH 4 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved. 5 * 6 * Based on the KMS/FB DMA helpers 7 * Copyright (C) 2012 Analog Devices Inc. 8 */ 9 10 #include <linux/console.h> 11 12 #include <drm/drm_fourcc.h> 13 #include <drm/drm_framebuffer.h> 14 #include <drm/drm_gem_framebuffer_helper.h> 15 #include <drm/drm_modeset_helper.h> 16 17 #include "drm.h" 18 #include "gem.h" 19 20 #ifdef CONFIG_DRM_FBDEV_EMULATION 21 static inline struct tegra_fbdev *to_tegra_fbdev(struct drm_fb_helper *helper) 22 { 23 return container_of(helper, struct tegra_fbdev, base); 24 } 25 #endif 26 27 struct tegra_bo *tegra_fb_get_plane(struct drm_framebuffer *framebuffer, 28 unsigned int index) 29 { 30 return to_tegra_bo(drm_gem_fb_get_obj(framebuffer, index)); 31 } 32 33 bool tegra_fb_is_bottom_up(struct drm_framebuffer *framebuffer) 34 { 35 struct tegra_bo *bo = tegra_fb_get_plane(framebuffer, 0); 36 37 if (bo->flags & TEGRA_BO_BOTTOM_UP) 38 return true; 39 40 return false; 41 } 42 43 int tegra_fb_get_tiling(struct drm_framebuffer *framebuffer, 44 struct tegra_bo_tiling *tiling) 45 { 46 uint64_t modifier = framebuffer->modifier; 47 48 if (fourcc_mod_is_vendor(modifier, NVIDIA)) { 49 if ((modifier & DRM_FORMAT_MOD_NVIDIA_SECTOR_LAYOUT) == 0) 50 tiling->sector_layout = TEGRA_BO_SECTOR_LAYOUT_TEGRA; 51 else 52 tiling->sector_layout = TEGRA_BO_SECTOR_LAYOUT_GPU; 53 54 modifier &= ~DRM_FORMAT_MOD_NVIDIA_SECTOR_LAYOUT; 55 } 56 57 switch (modifier) { 58 case DRM_FORMAT_MOD_LINEAR: 59 tiling->mode = TEGRA_BO_TILING_MODE_PITCH; 60 tiling->value = 0; 61 break; 62 63 case DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED: 64 tiling->mode = TEGRA_BO_TILING_MODE_TILED; 65 tiling->value = 0; 66 break; 67 68 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0): 69 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK; 70 tiling->value = 0; 71 break; 72 73 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1): 74 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK; 75 tiling->value = 1; 76 break; 77 78 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2): 79 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK; 80 tiling->value = 2; 81 break; 82 83 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3): 84 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK; 85 tiling->value = 3; 86 break; 87 88 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4): 89 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK; 90 tiling->value = 4; 91 break; 92 93 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5): 94 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK; 95 tiling->value = 5; 96 break; 97 98 default: 99 DRM_DEBUG_KMS("unknown format modifier: %llx\n", modifier); 100 return -EINVAL; 101 } 102 103 return 0; 104 } 105 106 static const struct drm_framebuffer_funcs tegra_fb_funcs = { 107 .destroy = drm_gem_fb_destroy, 108 .create_handle = drm_gem_fb_create_handle, 109 }; 110 111 static struct drm_framebuffer *tegra_fb_alloc(struct drm_device *drm, 112 const struct drm_mode_fb_cmd2 *mode_cmd, 113 struct tegra_bo **planes, 114 unsigned int num_planes) 115 { 116 struct drm_framebuffer *fb; 117 unsigned int i; 118 int err; 119 120 fb = kzalloc(sizeof(*fb), GFP_KERNEL); 121 if (!fb) 122 return ERR_PTR(-ENOMEM); 123 124 drm_helper_mode_fill_fb_struct(drm, fb, mode_cmd); 125 126 for (i = 0; i < fb->format->num_planes; i++) 127 fb->obj[i] = &planes[i]->gem; 128 129 err = drm_framebuffer_init(drm, fb, &tegra_fb_funcs); 130 if (err < 0) { 131 dev_err(drm->dev, "failed to initialize framebuffer: %d\n", 132 err); 133 kfree(fb); 134 return ERR_PTR(err); 135 } 136 137 return fb; 138 } 139 140 struct drm_framebuffer *tegra_fb_create(struct drm_device *drm, 141 struct drm_file *file, 142 const struct drm_mode_fb_cmd2 *cmd) 143 { 144 const struct drm_format_info *info = drm_get_format_info(drm, cmd); 145 struct tegra_bo *planes[4]; 146 struct drm_gem_object *gem; 147 struct drm_framebuffer *fb; 148 unsigned int i; 149 int err; 150 151 for (i = 0; i < info->num_planes; i++) { 152 unsigned int width = cmd->width / (i ? info->hsub : 1); 153 unsigned int height = cmd->height / (i ? info->vsub : 1); 154 unsigned int size, bpp; 155 156 gem = drm_gem_object_lookup(file, cmd->handles[i]); 157 if (!gem) { 158 err = -ENXIO; 159 goto unreference; 160 } 161 162 bpp = info->cpp[i]; 163 164 size = (height - 1) * cmd->pitches[i] + 165 width * bpp + cmd->offsets[i]; 166 167 if (gem->size < size) { 168 err = -EINVAL; 169 goto unreference; 170 } 171 172 planes[i] = to_tegra_bo(gem); 173 } 174 175 fb = tegra_fb_alloc(drm, cmd, planes, i); 176 if (IS_ERR(fb)) { 177 err = PTR_ERR(fb); 178 goto unreference; 179 } 180 181 return fb; 182 183 unreference: 184 while (i--) 185 drm_gem_object_put(&planes[i]->gem); 186 187 return ERR_PTR(err); 188 } 189 190 #ifdef CONFIG_DRM_FBDEV_EMULATION 191 static int tegra_fb_mmap(struct fb_info *info, struct vm_area_struct *vma) 192 { 193 struct drm_fb_helper *helper = info->par; 194 struct tegra_bo *bo; 195 int err; 196 197 bo = tegra_fb_get_plane(helper->fb, 0); 198 199 err = drm_gem_mmap_obj(&bo->gem, bo->gem.size, vma); 200 if (err < 0) 201 return err; 202 203 return __tegra_gem_mmap(&bo->gem, vma); 204 } 205 206 static const struct fb_ops tegra_fb_ops = { 207 .owner = THIS_MODULE, 208 DRM_FB_HELPER_DEFAULT_OPS, 209 .fb_read = drm_fb_helper_sys_read, 210 .fb_write = drm_fb_helper_sys_write, 211 .fb_fillrect = drm_fb_helper_sys_fillrect, 212 .fb_copyarea = drm_fb_helper_sys_copyarea, 213 .fb_imageblit = drm_fb_helper_sys_imageblit, 214 .fb_mmap = tegra_fb_mmap, 215 }; 216 217 static int tegra_fbdev_probe(struct drm_fb_helper *helper, 218 struct drm_fb_helper_surface_size *sizes) 219 { 220 struct tegra_fbdev *fbdev = to_tegra_fbdev(helper); 221 struct tegra_drm *tegra = helper->dev->dev_private; 222 struct drm_device *drm = helper->dev; 223 struct drm_mode_fb_cmd2 cmd = { 0 }; 224 unsigned int bytes_per_pixel; 225 struct drm_framebuffer *fb; 226 unsigned long offset; 227 struct fb_info *info; 228 struct tegra_bo *bo; 229 size_t size; 230 int err; 231 232 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8); 233 234 cmd.width = sizes->surface_width; 235 cmd.height = sizes->surface_height; 236 cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel, 237 tegra->pitch_align); 238 239 cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, 240 sizes->surface_depth); 241 242 size = cmd.pitches[0] * cmd.height; 243 244 bo = tegra_bo_create(drm, size, 0); 245 if (IS_ERR(bo)) 246 return PTR_ERR(bo); 247 248 info = drm_fb_helper_alloc_info(helper); 249 if (IS_ERR(info)) { 250 dev_err(drm->dev, "failed to allocate framebuffer info\n"); 251 drm_gem_object_put(&bo->gem); 252 return PTR_ERR(info); 253 } 254 255 fbdev->fb = tegra_fb_alloc(drm, &cmd, &bo, 1); 256 if (IS_ERR(fbdev->fb)) { 257 err = PTR_ERR(fbdev->fb); 258 dev_err(drm->dev, "failed to allocate DRM framebuffer: %d\n", 259 err); 260 drm_gem_object_put(&bo->gem); 261 return PTR_ERR(fbdev->fb); 262 } 263 264 fb = fbdev->fb; 265 helper->fb = fb; 266 helper->info = info; 267 268 info->fbops = &tegra_fb_ops; 269 270 drm_fb_helper_fill_info(info, helper, sizes); 271 272 offset = info->var.xoffset * bytes_per_pixel + 273 info->var.yoffset * fb->pitches[0]; 274 275 if (bo->pages) { 276 bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP, 277 pgprot_writecombine(PAGE_KERNEL)); 278 if (!bo->vaddr) { 279 dev_err(drm->dev, "failed to vmap() framebuffer\n"); 280 err = -ENOMEM; 281 goto destroy; 282 } 283 } 284 285 info->screen_base = (void __iomem *)bo->vaddr + offset; 286 info->screen_size = size; 287 info->fix.smem_start = (unsigned long)(bo->iova + offset); 288 info->fix.smem_len = size; 289 290 return 0; 291 292 destroy: 293 drm_framebuffer_remove(fb); 294 return err; 295 } 296 297 static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = { 298 .fb_probe = tegra_fbdev_probe, 299 }; 300 301 static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm) 302 { 303 struct tegra_fbdev *fbdev; 304 305 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL); 306 if (!fbdev) { 307 dev_err(drm->dev, "failed to allocate DRM fbdev\n"); 308 return ERR_PTR(-ENOMEM); 309 } 310 311 drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs); 312 313 return fbdev; 314 } 315 316 static void tegra_fbdev_free(struct tegra_fbdev *fbdev) 317 { 318 kfree(fbdev); 319 } 320 321 static int tegra_fbdev_init(struct tegra_fbdev *fbdev, 322 unsigned int preferred_bpp, 323 unsigned int num_crtc, 324 unsigned int max_connectors) 325 { 326 struct drm_device *drm = fbdev->base.dev; 327 int err; 328 329 err = drm_fb_helper_init(drm, &fbdev->base); 330 if (err < 0) { 331 dev_err(drm->dev, "failed to initialize DRM FB helper: %d\n", 332 err); 333 return err; 334 } 335 336 err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp); 337 if (err < 0) { 338 dev_err(drm->dev, "failed to set initial configuration: %d\n", 339 err); 340 goto fini; 341 } 342 343 return 0; 344 345 fini: 346 drm_fb_helper_fini(&fbdev->base); 347 return err; 348 } 349 350 static void tegra_fbdev_exit(struct tegra_fbdev *fbdev) 351 { 352 drm_fb_helper_unregister_info(&fbdev->base); 353 354 if (fbdev->fb) { 355 struct tegra_bo *bo = tegra_fb_get_plane(fbdev->fb, 0); 356 357 /* Undo the special mapping we made in fbdev probe. */ 358 if (bo && bo->pages) { 359 vunmap(bo->vaddr); 360 bo->vaddr = NULL; 361 } 362 363 drm_framebuffer_remove(fbdev->fb); 364 } 365 366 drm_fb_helper_fini(&fbdev->base); 367 tegra_fbdev_free(fbdev); 368 } 369 #endif 370 371 int tegra_drm_fb_prepare(struct drm_device *drm) 372 { 373 #ifdef CONFIG_DRM_FBDEV_EMULATION 374 struct tegra_drm *tegra = drm->dev_private; 375 376 tegra->fbdev = tegra_fbdev_create(drm); 377 if (IS_ERR(tegra->fbdev)) 378 return PTR_ERR(tegra->fbdev); 379 #endif 380 381 return 0; 382 } 383 384 void tegra_drm_fb_free(struct drm_device *drm) 385 { 386 #ifdef CONFIG_DRM_FBDEV_EMULATION 387 struct tegra_drm *tegra = drm->dev_private; 388 389 tegra_fbdev_free(tegra->fbdev); 390 #endif 391 } 392 393 int tegra_drm_fb_init(struct drm_device *drm) 394 { 395 #ifdef CONFIG_DRM_FBDEV_EMULATION 396 struct tegra_drm *tegra = drm->dev_private; 397 int err; 398 399 err = tegra_fbdev_init(tegra->fbdev, 32, drm->mode_config.num_crtc, 400 drm->mode_config.num_connector); 401 if (err < 0) 402 return err; 403 #endif 404 405 return 0; 406 } 407 408 void tegra_drm_fb_exit(struct drm_device *drm) 409 { 410 #ifdef CONFIG_DRM_FBDEV_EMULATION 411 struct tegra_drm *tegra = drm->dev_private; 412 413 tegra_fbdev_exit(tegra->fbdev); 414 #endif 415 } 416