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 18 static inline struct tegra_fb *to_tegra_fb(struct drm_framebuffer *fb) 19 { 20 return container_of(fb, struct tegra_fb, base); 21 } 22 23 #ifdef CONFIG_DRM_FBDEV_EMULATION 24 static inline struct tegra_fbdev *to_tegra_fbdev(struct drm_fb_helper *helper) 25 { 26 return container_of(helper, struct tegra_fbdev, base); 27 } 28 #endif 29 30 struct tegra_bo *tegra_fb_get_plane(struct drm_framebuffer *framebuffer, 31 unsigned int index) 32 { 33 struct tegra_fb *fb = to_tegra_fb(framebuffer); 34 35 if (index >= framebuffer->format->num_planes) 36 return NULL; 37 38 return fb->planes[index]; 39 } 40 41 bool tegra_fb_is_bottom_up(struct drm_framebuffer *framebuffer) 42 { 43 struct tegra_fb *fb = to_tegra_fb(framebuffer); 44 45 if (fb->planes[0]->flags & TEGRA_BO_BOTTOM_UP) 46 return true; 47 48 return false; 49 } 50 51 int tegra_fb_get_tiling(struct drm_framebuffer *framebuffer, 52 struct tegra_bo_tiling *tiling) 53 { 54 struct tegra_fb *fb = to_tegra_fb(framebuffer); 55 uint64_t modifier = fb->base.modifier; 56 57 switch (fourcc_mod_tegra_mod(modifier)) { 58 case NV_FORMAT_MOD_TEGRA_TILED: 59 tiling->mode = TEGRA_BO_TILING_MODE_TILED; 60 tiling->value = 0; 61 break; 62 63 case NV_FORMAT_MOD_TEGRA_16BX2_BLOCK(0): 64 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK; 65 tiling->value = fourcc_mod_tegra_param(modifier); 66 if (tiling->value > 5) 67 return -EINVAL; 68 break; 69 70 default: 71 /* TODO: handle YUV formats? */ 72 *tiling = fb->planes[0]->tiling; 73 break; 74 } 75 76 return 0; 77 } 78 79 static void tegra_fb_destroy(struct drm_framebuffer *framebuffer) 80 { 81 struct tegra_fb *fb = to_tegra_fb(framebuffer); 82 unsigned int i; 83 84 for (i = 0; i < fb->num_planes; i++) { 85 struct tegra_bo *bo = fb->planes[i]; 86 87 if (bo) { 88 if (bo->pages) 89 vunmap(bo->vaddr); 90 91 drm_gem_object_unreference_unlocked(&bo->gem); 92 } 93 } 94 95 drm_framebuffer_cleanup(framebuffer); 96 kfree(fb->planes); 97 kfree(fb); 98 } 99 100 static int tegra_fb_create_handle(struct drm_framebuffer *framebuffer, 101 struct drm_file *file, unsigned int *handle) 102 { 103 struct tegra_fb *fb = to_tegra_fb(framebuffer); 104 105 return drm_gem_handle_create(file, &fb->planes[0]->gem, handle); 106 } 107 108 static const struct drm_framebuffer_funcs tegra_fb_funcs = { 109 .destroy = tegra_fb_destroy, 110 .create_handle = tegra_fb_create_handle, 111 }; 112 113 static struct tegra_fb *tegra_fb_alloc(struct drm_device *drm, 114 const struct drm_mode_fb_cmd2 *mode_cmd, 115 struct tegra_bo **planes, 116 unsigned int num_planes) 117 { 118 struct tegra_fb *fb; 119 unsigned int i; 120 int err; 121 122 fb = kzalloc(sizeof(*fb), GFP_KERNEL); 123 if (!fb) 124 return ERR_PTR(-ENOMEM); 125 126 fb->planes = kzalloc(num_planes * sizeof(*planes), GFP_KERNEL); 127 if (!fb->planes) { 128 kfree(fb); 129 return ERR_PTR(-ENOMEM); 130 } 131 132 fb->num_planes = num_planes; 133 134 drm_helper_mode_fill_fb_struct(drm, &fb->base, mode_cmd); 135 136 for (i = 0; i < fb->num_planes; i++) 137 fb->planes[i] = planes[i]; 138 139 err = drm_framebuffer_init(drm, &fb->base, &tegra_fb_funcs); 140 if (err < 0) { 141 dev_err(drm->dev, "failed to initialize framebuffer: %d\n", 142 err); 143 kfree(fb->planes); 144 kfree(fb); 145 return ERR_PTR(err); 146 } 147 148 return fb; 149 } 150 151 struct drm_framebuffer *tegra_fb_create(struct drm_device *drm, 152 struct drm_file *file, 153 const struct drm_mode_fb_cmd2 *cmd) 154 { 155 unsigned int hsub, vsub, i; 156 struct tegra_bo *planes[4]; 157 struct drm_gem_object *gem; 158 struct tegra_fb *fb; 159 int err; 160 161 hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format); 162 vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format); 163 164 for (i = 0; i < drm_format_num_planes(cmd->pixel_format); i++) { 165 unsigned int width = cmd->width / (i ? hsub : 1); 166 unsigned int height = cmd->height / (i ? vsub : 1); 167 unsigned int size, bpp; 168 169 gem = drm_gem_object_lookup(file, cmd->handles[i]); 170 if (!gem) { 171 err = -ENXIO; 172 goto unreference; 173 } 174 175 bpp = drm_format_plane_cpp(cmd->pixel_format, i); 176 177 size = (height - 1) * cmd->pitches[i] + 178 width * bpp + cmd->offsets[i]; 179 180 if (gem->size < size) { 181 err = -EINVAL; 182 goto unreference; 183 } 184 185 planes[i] = to_tegra_bo(gem); 186 } 187 188 fb = tegra_fb_alloc(drm, cmd, planes, i); 189 if (IS_ERR(fb)) { 190 err = PTR_ERR(fb); 191 goto unreference; 192 } 193 194 return &fb->base; 195 196 unreference: 197 while (i--) 198 drm_gem_object_unreference_unlocked(&planes[i]->gem); 199 200 return ERR_PTR(err); 201 } 202 203 #ifdef CONFIG_DRM_FBDEV_EMULATION 204 static struct fb_ops tegra_fb_ops = { 205 .owner = THIS_MODULE, 206 DRM_FB_HELPER_DEFAULT_OPS, 207 .fb_fillrect = drm_fb_helper_sys_fillrect, 208 .fb_copyarea = drm_fb_helper_sys_copyarea, 209 .fb_imageblit = drm_fb_helper_sys_imageblit, 210 }; 211 212 static int tegra_fbdev_probe(struct drm_fb_helper *helper, 213 struct drm_fb_helper_surface_size *sizes) 214 { 215 struct tegra_fbdev *fbdev = to_tegra_fbdev(helper); 216 struct tegra_drm *tegra = helper->dev->dev_private; 217 struct drm_device *drm = helper->dev; 218 struct drm_mode_fb_cmd2 cmd = { 0 }; 219 unsigned int bytes_per_pixel; 220 struct drm_framebuffer *fb; 221 unsigned long offset; 222 struct fb_info *info; 223 struct tegra_bo *bo; 224 size_t size; 225 int err; 226 227 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8); 228 229 cmd.width = sizes->surface_width; 230 cmd.height = sizes->surface_height; 231 cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel, 232 tegra->pitch_align); 233 cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, 234 sizes->surface_depth); 235 236 size = cmd.pitches[0] * cmd.height; 237 238 bo = tegra_bo_create(drm, size, 0); 239 if (IS_ERR(bo)) 240 return PTR_ERR(bo); 241 242 info = drm_fb_helper_alloc_fbi(helper); 243 if (IS_ERR(info)) { 244 dev_err(drm->dev, "failed to allocate framebuffer info\n"); 245 drm_gem_object_unreference_unlocked(&bo->gem); 246 return PTR_ERR(info); 247 } 248 249 fbdev->fb = tegra_fb_alloc(drm, &cmd, &bo, 1); 250 if (IS_ERR(fbdev->fb)) { 251 err = PTR_ERR(fbdev->fb); 252 dev_err(drm->dev, "failed to allocate DRM framebuffer: %d\n", 253 err); 254 drm_gem_object_unreference_unlocked(&bo->gem); 255 return PTR_ERR(fbdev->fb); 256 } 257 258 fb = &fbdev->fb->base; 259 helper->fb = fb; 260 helper->fbdev = info; 261 262 info->par = helper; 263 info->flags = FBINFO_FLAG_DEFAULT; 264 info->fbops = &tegra_fb_ops; 265 266 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth); 267 drm_fb_helper_fill_var(info, helper, fb->width, fb->height); 268 269 offset = info->var.xoffset * bytes_per_pixel + 270 info->var.yoffset * fb->pitches[0]; 271 272 if (bo->pages) { 273 bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP, 274 pgprot_writecombine(PAGE_KERNEL)); 275 if (!bo->vaddr) { 276 dev_err(drm->dev, "failed to vmap() framebuffer\n"); 277 err = -ENOMEM; 278 goto destroy; 279 } 280 } 281 282 drm->mode_config.fb_base = (resource_size_t)bo->paddr; 283 info->screen_base = (void __iomem *)bo->vaddr + offset; 284 info->screen_size = size; 285 info->fix.smem_start = (unsigned long)(bo->paddr + offset); 286 info->fix.smem_len = size; 287 288 return 0; 289 290 destroy: 291 drm_framebuffer_remove(fb); 292 return err; 293 } 294 295 static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = { 296 .fb_probe = tegra_fbdev_probe, 297 }; 298 299 static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm) 300 { 301 struct tegra_fbdev *fbdev; 302 303 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL); 304 if (!fbdev) { 305 dev_err(drm->dev, "failed to allocate DRM fbdev\n"); 306 return ERR_PTR(-ENOMEM); 307 } 308 309 drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs); 310 311 return fbdev; 312 } 313 314 static void tegra_fbdev_free(struct tegra_fbdev *fbdev) 315 { 316 kfree(fbdev); 317 } 318 319 static int tegra_fbdev_init(struct tegra_fbdev *fbdev, 320 unsigned int preferred_bpp, 321 unsigned int num_crtc, 322 unsigned int max_connectors) 323 { 324 struct drm_device *drm = fbdev->base.dev; 325 int err; 326 327 err = drm_fb_helper_init(drm, &fbdev->base, max_connectors); 328 if (err < 0) { 329 dev_err(drm->dev, "failed to initialize DRM FB helper: %d\n", 330 err); 331 return err; 332 } 333 334 err = drm_fb_helper_single_add_all_connectors(&fbdev->base); 335 if (err < 0) { 336 dev_err(drm->dev, "failed to add connectors: %d\n", err); 337 goto fini; 338 } 339 340 err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp); 341 if (err < 0) { 342 dev_err(drm->dev, "failed to set initial configuration: %d\n", 343 err); 344 goto fini; 345 } 346 347 return 0; 348 349 fini: 350 drm_fb_helper_fini(&fbdev->base); 351 return err; 352 } 353 354 static void tegra_fbdev_exit(struct tegra_fbdev *fbdev) 355 { 356 drm_fb_helper_unregister_fbi(&fbdev->base); 357 358 if (fbdev->fb) 359 drm_framebuffer_remove(&fbdev->fb->base); 360 361 drm_fb_helper_fini(&fbdev->base); 362 tegra_fbdev_free(fbdev); 363 } 364 365 void tegra_fbdev_restore_mode(struct tegra_fbdev *fbdev) 366 { 367 if (fbdev) 368 drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev->base); 369 } 370 371 void tegra_fb_output_poll_changed(struct drm_device *drm) 372 { 373 struct tegra_drm *tegra = drm->dev_private; 374 375 if (tegra->fbdev) 376 drm_fb_helper_hotplug_event(&tegra->fbdev->base); 377 } 378 #endif 379 380 int tegra_drm_fb_prepare(struct drm_device *drm) 381 { 382 #ifdef CONFIG_DRM_FBDEV_EMULATION 383 struct tegra_drm *tegra = drm->dev_private; 384 385 tegra->fbdev = tegra_fbdev_create(drm); 386 if (IS_ERR(tegra->fbdev)) 387 return PTR_ERR(tegra->fbdev); 388 #endif 389 390 return 0; 391 } 392 393 void tegra_drm_fb_free(struct drm_device *drm) 394 { 395 #ifdef CONFIG_DRM_FBDEV_EMULATION 396 struct tegra_drm *tegra = drm->dev_private; 397 398 tegra_fbdev_free(tegra->fbdev); 399 #endif 400 } 401 402 int tegra_drm_fb_init(struct drm_device *drm) 403 { 404 #ifdef CONFIG_DRM_FBDEV_EMULATION 405 struct tegra_drm *tegra = drm->dev_private; 406 int err; 407 408 err = tegra_fbdev_init(tegra->fbdev, 32, drm->mode_config.num_crtc, 409 drm->mode_config.num_connector); 410 if (err < 0) 411 return err; 412 #endif 413 414 return 0; 415 } 416 417 void tegra_drm_fb_exit(struct drm_device *drm) 418 { 419 #ifdef CONFIG_DRM_FBDEV_EMULATION 420 struct tegra_drm *tegra = drm->dev_private; 421 422 tegra_fbdev_exit(tegra->fbdev); 423 #endif 424 } 425 426 void tegra_drm_fb_suspend(struct drm_device *drm) 427 { 428 #ifdef CONFIG_DRM_FBDEV_EMULATION 429 struct tegra_drm *tegra = drm->dev_private; 430 431 console_lock(); 432 drm_fb_helper_set_suspend(&tegra->fbdev->base, 1); 433 console_unlock(); 434 #endif 435 } 436 437 void tegra_drm_fb_resume(struct drm_device *drm) 438 { 439 #ifdef CONFIG_DRM_FBDEV_EMULATION 440 struct tegra_drm *tegra = drm->dev_private; 441 442 console_lock(); 443 drm_fb_helper_set_suspend(&tegra->fbdev->base, 0); 444 console_unlock(); 445 #endif 446 } 447