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>
17 #include <drm/drm_gem_framebuffer_helper.h>
18 #include <drm/drm_modeset_helper.h>
20 #ifdef CONFIG_DRM_FBDEV_EMULATION
21 static inline struct tegra_fbdev *to_tegra_fbdev(struct drm_fb_helper *helper)
23 return container_of(helper, struct tegra_fbdev, base);
27 struct tegra_bo *tegra_fb_get_plane(struct drm_framebuffer *framebuffer,
30 return to_tegra_bo(drm_gem_fb_get_obj(framebuffer, index));
33 bool tegra_fb_is_bottom_up(struct drm_framebuffer *framebuffer)
35 struct tegra_bo *bo = tegra_fb_get_plane(framebuffer, 0);
37 if (bo->flags & TEGRA_BO_BOTTOM_UP)
43 int tegra_fb_get_tiling(struct drm_framebuffer *framebuffer,
44 struct tegra_bo_tiling *tiling)
46 uint64_t modifier = framebuffer->modifier;
49 case DRM_FORMAT_MOD_LINEAR:
50 tiling->mode = TEGRA_BO_TILING_MODE_PITCH;
54 case DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED:
55 tiling->mode = TEGRA_BO_TILING_MODE_TILED;
59 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0):
60 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
64 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1):
65 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
69 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2):
70 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
74 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3):
75 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
79 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4):
80 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
84 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5):
85 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
96 static const struct drm_framebuffer_funcs tegra_fb_funcs = {
97 .destroy = drm_gem_fb_destroy,
98 .create_handle = drm_gem_fb_create_handle,
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)
106 struct drm_framebuffer *fb;
110 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
112 return ERR_PTR(-ENOMEM);
114 drm_helper_mode_fill_fb_struct(drm, fb, mode_cmd);
116 for (i = 0; i < fb->format->num_planes; i++)
117 fb->obj[i] = &planes[i]->gem;
119 err = drm_framebuffer_init(drm, fb, &tegra_fb_funcs);
121 dev_err(drm->dev, "failed to initialize framebuffer: %d\n",
130 struct drm_framebuffer *tegra_fb_create(struct drm_device *drm,
131 struct drm_file *file,
132 const struct drm_mode_fb_cmd2 *cmd)
134 unsigned int hsub, vsub, i;
135 struct tegra_bo *planes[4];
136 struct drm_gem_object *gem;
137 struct drm_framebuffer *fb;
140 hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format);
141 vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format);
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;
148 gem = drm_gem_object_lookup(file, cmd->handles[i]);
154 bpp = drm_format_plane_cpp(cmd->pixel_format, i);
156 size = (height - 1) * cmd->pitches[i] +
157 width * bpp + cmd->offsets[i];
159 if (gem->size < size) {
164 planes[i] = to_tegra_bo(gem);
167 fb = tegra_fb_alloc(drm, cmd, planes, i);
177 drm_gem_object_put_unlocked(&planes[i]->gem);
182 #ifdef CONFIG_DRM_FBDEV_EMULATION
183 static int tegra_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
185 struct drm_fb_helper *helper = info->par;
189 bo = tegra_fb_get_plane(helper->fb, 0);
191 err = drm_gem_mmap_obj(&bo->gem, bo->gem.size, vma);
195 return __tegra_gem_mmap(&bo->gem, vma);
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,
207 static int tegra_fbdev_probe(struct drm_fb_helper *helper,
208 struct drm_fb_helper_surface_size *sizes)
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;
222 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
224 cmd.width = sizes->surface_width;
225 cmd.height = sizes->surface_height;
226 cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel,
229 cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
230 sizes->surface_depth);
232 size = cmd.pitches[0] * cmd.height;
234 bo = tegra_bo_create(drm, size, 0);
238 info = drm_fb_helper_alloc_fbi(helper);
240 dev_err(drm->dev, "failed to allocate framebuffer info\n");
241 drm_gem_object_put_unlocked(&bo->gem);
242 return PTR_ERR(info);
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",
250 drm_gem_object_put_unlocked(&bo->gem);
251 return PTR_ERR(fbdev->fb);
256 helper->fbdev = info;
259 info->fbops = &tegra_fb_ops;
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);
264 offset = info->var.xoffset * bytes_per_pixel +
265 info->var.yoffset * fb->pitches[0];
268 bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP,
269 pgprot_writecombine(PAGE_KERNEL));
271 dev_err(drm->dev, "failed to vmap() framebuffer\n");
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;
286 drm_framebuffer_remove(fb);
290 static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
291 .fb_probe = tegra_fbdev_probe,
294 static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm)
296 struct tegra_fbdev *fbdev;
298 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
300 dev_err(drm->dev, "failed to allocate DRM fbdev\n");
301 return ERR_PTR(-ENOMEM);
304 drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs);
309 static void tegra_fbdev_free(struct tegra_fbdev *fbdev)
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)
319 struct drm_device *drm = fbdev->base.dev;
322 err = drm_fb_helper_init(drm, &fbdev->base, max_connectors);
324 dev_err(drm->dev, "failed to initialize DRM FB helper: %d\n",
329 err = drm_fb_helper_single_add_all_connectors(&fbdev->base);
331 dev_err(drm->dev, "failed to add connectors: %d\n", err);
335 err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp);
337 dev_err(drm->dev, "failed to set initial configuration: %d\n",
345 drm_fb_helper_fini(&fbdev->base);
349 static void tegra_fbdev_exit(struct tegra_fbdev *fbdev)
351 drm_fb_helper_unregister_fbi(&fbdev->base);
354 struct tegra_bo *bo = tegra_fb_get_plane(fbdev->fb, 0);
356 /* Undo the special mapping we made in fbdev probe. */
357 if (bo && bo->pages) {
362 drm_framebuffer_remove(fbdev->fb);
365 drm_fb_helper_fini(&fbdev->base);
366 tegra_fbdev_free(fbdev);
370 int tegra_drm_fb_prepare(struct drm_device *drm)
372 #ifdef CONFIG_DRM_FBDEV_EMULATION
373 struct tegra_drm *tegra = drm->dev_private;
375 tegra->fbdev = tegra_fbdev_create(drm);
376 if (IS_ERR(tegra->fbdev))
377 return PTR_ERR(tegra->fbdev);
383 void tegra_drm_fb_free(struct drm_device *drm)
385 #ifdef CONFIG_DRM_FBDEV_EMULATION
386 struct tegra_drm *tegra = drm->dev_private;
388 tegra_fbdev_free(tegra->fbdev);
392 int tegra_drm_fb_init(struct drm_device *drm)
394 #ifdef CONFIG_DRM_FBDEV_EMULATION
395 struct tegra_drm *tegra = drm->dev_private;
398 err = tegra_fbdev_init(tegra->fbdev, 32, drm->mode_config.num_crtc,
399 drm->mode_config.num_connector);
407 void tegra_drm_fb_exit(struct drm_device *drm)
409 #ifdef CONFIG_DRM_FBDEV_EMULATION
410 struct tegra_drm *tegra = drm->dev_private;
412 tegra_fbdev_exit(tegra->fbdev);