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