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