2 * Copyright (C) 2012-2013 Avionic Design GmbH
3 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
5 * Based on the KMS/FB CMA helpers
6 * Copyright (C) 2012 Analog Device Inc.
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.
13 #include <linux/console.h>
18 static inline struct tegra_fb *to_tegra_fb(struct drm_framebuffer *fb)
20 return container_of(fb, struct tegra_fb, base);
23 #ifdef CONFIG_DRM_FBDEV_EMULATION
24 static inline struct tegra_fbdev *to_tegra_fbdev(struct drm_fb_helper *helper)
26 return container_of(helper, struct tegra_fbdev, base);
30 struct tegra_bo *tegra_fb_get_plane(struct drm_framebuffer *framebuffer,
33 struct tegra_fb *fb = to_tegra_fb(framebuffer);
35 if (index >= framebuffer->format->num_planes)
38 return fb->planes[index];
41 bool tegra_fb_is_bottom_up(struct drm_framebuffer *framebuffer)
43 struct tegra_fb *fb = to_tegra_fb(framebuffer);
45 if (fb->planes[0]->flags & TEGRA_BO_BOTTOM_UP)
51 int tegra_fb_get_tiling(struct drm_framebuffer *framebuffer,
52 struct tegra_bo_tiling *tiling)
54 struct tegra_fb *fb = to_tegra_fb(framebuffer);
55 uint64_t modifier = fb->base.modifier;
58 case DRM_FORMAT_MOD_LINEAR:
59 tiling->mode = TEGRA_BO_TILING_MODE_PITCH;
63 case DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED:
64 tiling->mode = TEGRA_BO_TILING_MODE_TILED;
68 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0):
69 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
73 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1):
74 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
78 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2):
79 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
83 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3):
84 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
88 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4):
89 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
93 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5):
94 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
105 static void tegra_fb_destroy(struct drm_framebuffer *framebuffer)
107 struct tegra_fb *fb = to_tegra_fb(framebuffer);
110 for (i = 0; i < fb->num_planes; i++) {
111 struct tegra_bo *bo = fb->planes[i];
117 drm_gem_object_put_unlocked(&bo->gem);
121 drm_framebuffer_cleanup(framebuffer);
126 static int tegra_fb_create_handle(struct drm_framebuffer *framebuffer,
127 struct drm_file *file, unsigned int *handle)
129 struct tegra_fb *fb = to_tegra_fb(framebuffer);
131 return drm_gem_handle_create(file, &fb->planes[0]->gem, handle);
134 static const struct drm_framebuffer_funcs tegra_fb_funcs = {
135 .destroy = tegra_fb_destroy,
136 .create_handle = tegra_fb_create_handle,
139 static struct tegra_fb *tegra_fb_alloc(struct drm_device *drm,
140 const struct drm_mode_fb_cmd2 *mode_cmd,
141 struct tegra_bo **planes,
142 unsigned int num_planes)
148 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
150 return ERR_PTR(-ENOMEM);
152 fb->planes = kzalloc(num_planes * sizeof(*planes), GFP_KERNEL);
155 return ERR_PTR(-ENOMEM);
158 fb->num_planes = num_planes;
160 drm_helper_mode_fill_fb_struct(drm, &fb->base, mode_cmd);
162 for (i = 0; i < fb->num_planes; i++)
163 fb->planes[i] = planes[i];
165 err = drm_framebuffer_init(drm, &fb->base, &tegra_fb_funcs);
167 dev_err(drm->dev, "failed to initialize framebuffer: %d\n",
177 struct drm_framebuffer *tegra_fb_create(struct drm_device *drm,
178 struct drm_file *file,
179 const struct drm_mode_fb_cmd2 *cmd)
181 unsigned int hsub, vsub, i;
182 struct tegra_bo *planes[4];
183 struct drm_gem_object *gem;
187 hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format);
188 vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format);
190 for (i = 0; i < drm_format_num_planes(cmd->pixel_format); i++) {
191 unsigned int width = cmd->width / (i ? hsub : 1);
192 unsigned int height = cmd->height / (i ? vsub : 1);
193 unsigned int size, bpp;
195 gem = drm_gem_object_lookup(file, cmd->handles[i]);
201 bpp = drm_format_plane_cpp(cmd->pixel_format, i);
203 size = (height - 1) * cmd->pitches[i] +
204 width * bpp + cmd->offsets[i];
206 if (gem->size < size) {
211 planes[i] = to_tegra_bo(gem);
214 fb = tegra_fb_alloc(drm, cmd, planes, i);
224 drm_gem_object_put_unlocked(&planes[i]->gem);
229 #ifdef CONFIG_DRM_FBDEV_EMULATION
230 static int tegra_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
232 struct drm_fb_helper *helper = info->par;
236 bo = tegra_fb_get_plane(helper->fb, 0);
238 err = drm_gem_mmap_obj(&bo->gem, bo->gem.size, vma);
242 return __tegra_gem_mmap(&bo->gem, vma);
245 static struct fb_ops tegra_fb_ops = {
246 .owner = THIS_MODULE,
247 DRM_FB_HELPER_DEFAULT_OPS,
248 .fb_fillrect = drm_fb_helper_sys_fillrect,
249 .fb_copyarea = drm_fb_helper_sys_copyarea,
250 .fb_imageblit = drm_fb_helper_sys_imageblit,
251 .fb_mmap = tegra_fb_mmap,
254 static int tegra_fbdev_probe(struct drm_fb_helper *helper,
255 struct drm_fb_helper_surface_size *sizes)
257 struct tegra_fbdev *fbdev = to_tegra_fbdev(helper);
258 struct tegra_drm *tegra = helper->dev->dev_private;
259 struct drm_device *drm = helper->dev;
260 struct drm_mode_fb_cmd2 cmd = { 0 };
261 unsigned int bytes_per_pixel;
262 struct drm_framebuffer *fb;
263 unsigned long offset;
264 struct fb_info *info;
269 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
271 cmd.width = sizes->surface_width;
272 cmd.height = sizes->surface_height;
273 cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel,
276 cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
277 sizes->surface_depth);
279 size = cmd.pitches[0] * cmd.height;
281 bo = tegra_bo_create(drm, size, 0);
285 info = drm_fb_helper_alloc_fbi(helper);
287 dev_err(drm->dev, "failed to allocate framebuffer info\n");
288 drm_gem_object_put_unlocked(&bo->gem);
289 return PTR_ERR(info);
292 fbdev->fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
293 if (IS_ERR(fbdev->fb)) {
294 err = PTR_ERR(fbdev->fb);
295 dev_err(drm->dev, "failed to allocate DRM framebuffer: %d\n",
297 drm_gem_object_put_unlocked(&bo->gem);
298 return PTR_ERR(fbdev->fb);
301 fb = &fbdev->fb->base;
303 helper->fbdev = info;
306 info->flags = FBINFO_FLAG_DEFAULT;
307 info->fbops = &tegra_fb_ops;
309 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
310 drm_fb_helper_fill_var(info, helper, fb->width, fb->height);
312 offset = info->var.xoffset * bytes_per_pixel +
313 info->var.yoffset * fb->pitches[0];
316 bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP,
317 pgprot_writecombine(PAGE_KERNEL));
319 dev_err(drm->dev, "failed to vmap() framebuffer\n");
325 drm->mode_config.fb_base = (resource_size_t)bo->paddr;
326 info->screen_base = (void __iomem *)bo->vaddr + offset;
327 info->screen_size = size;
328 info->fix.smem_start = (unsigned long)(bo->paddr + offset);
329 info->fix.smem_len = size;
334 drm_framebuffer_remove(fb);
338 static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
339 .fb_probe = tegra_fbdev_probe,
342 static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm)
344 struct tegra_fbdev *fbdev;
346 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
348 dev_err(drm->dev, "failed to allocate DRM fbdev\n");
349 return ERR_PTR(-ENOMEM);
352 drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs);
357 static void tegra_fbdev_free(struct tegra_fbdev *fbdev)
362 static int tegra_fbdev_init(struct tegra_fbdev *fbdev,
363 unsigned int preferred_bpp,
364 unsigned int num_crtc,
365 unsigned int max_connectors)
367 struct drm_device *drm = fbdev->base.dev;
370 err = drm_fb_helper_init(drm, &fbdev->base, max_connectors);
372 dev_err(drm->dev, "failed to initialize DRM FB helper: %d\n",
377 err = drm_fb_helper_single_add_all_connectors(&fbdev->base);
379 dev_err(drm->dev, "failed to add connectors: %d\n", err);
383 err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp);
385 dev_err(drm->dev, "failed to set initial configuration: %d\n",
393 drm_fb_helper_fini(&fbdev->base);
397 static void tegra_fbdev_exit(struct tegra_fbdev *fbdev)
399 drm_fb_helper_unregister_fbi(&fbdev->base);
402 drm_framebuffer_remove(&fbdev->fb->base);
404 drm_fb_helper_fini(&fbdev->base);
405 tegra_fbdev_free(fbdev);
409 int tegra_drm_fb_prepare(struct drm_device *drm)
411 #ifdef CONFIG_DRM_FBDEV_EMULATION
412 struct tegra_drm *tegra = drm->dev_private;
414 tegra->fbdev = tegra_fbdev_create(drm);
415 if (IS_ERR(tegra->fbdev))
416 return PTR_ERR(tegra->fbdev);
422 void tegra_drm_fb_free(struct drm_device *drm)
424 #ifdef CONFIG_DRM_FBDEV_EMULATION
425 struct tegra_drm *tegra = drm->dev_private;
427 tegra_fbdev_free(tegra->fbdev);
431 int tegra_drm_fb_init(struct drm_device *drm)
433 #ifdef CONFIG_DRM_FBDEV_EMULATION
434 struct tegra_drm *tegra = drm->dev_private;
437 err = tegra_fbdev_init(tegra->fbdev, 32, drm->mode_config.num_crtc,
438 drm->mode_config.num_connector);
446 void tegra_drm_fb_exit(struct drm_device *drm)
448 #ifdef CONFIG_DRM_FBDEV_EMULATION
449 struct tegra_drm *tegra = drm->dev_private;
451 tegra_fbdev_exit(tegra->fbdev);
455 void tegra_drm_fb_suspend(struct drm_device *drm)
457 #ifdef CONFIG_DRM_FBDEV_EMULATION
458 struct tegra_drm *tegra = drm->dev_private;
461 drm_fb_helper_set_suspend(&tegra->fbdev->base, 1);
466 void tegra_drm_fb_resume(struct drm_device *drm)
468 #ifdef CONFIG_DRM_FBDEV_EMULATION
469 struct tegra_drm *tegra = drm->dev_private;
472 drm_fb_helper_set_suspend(&tegra->fbdev->base, 0);