Loading...
Note: File does not exist in v3.1.
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2012-2013 Avionic Design GmbH
4 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
5 *
6 * Based on the KMS/FB DMA helpers
7 * Copyright (C) 2012 Analog Devices Inc.
8 */
9
10#include <linux/console.h>
11#include <linux/fb.h>
12#include <linux/vmalloc.h>
13
14#include <drm/drm_drv.h>
15#include <drm/drm_crtc_helper.h>
16#include <drm/drm_fb_helper.h>
17#include <drm/drm_fourcc.h>
18#include <drm/drm_framebuffer.h>
19#include <drm/drm_gem_framebuffer_helper.h>
20#include <drm/drm_modeset_helper.h>
21
22#include "drm.h"
23#include "gem.h"
24
25static int tegra_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
26{
27 struct drm_fb_helper *helper = info->par;
28 struct tegra_bo *bo;
29 int err;
30
31 bo = tegra_fb_get_plane(helper->fb, 0);
32
33 err = drm_gem_mmap_obj(&bo->gem, bo->gem.size, vma);
34 if (err < 0)
35 return err;
36
37 return __tegra_gem_mmap(&bo->gem, vma);
38}
39
40static void tegra_fbdev_fb_destroy(struct fb_info *info)
41{
42 struct drm_fb_helper *helper = info->par;
43 struct drm_framebuffer *fb = helper->fb;
44 struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
45
46 drm_fb_helper_fini(helper);
47
48 /* Undo the special mapping we made in fbdev probe. */
49 if (bo->pages) {
50 vunmap(bo->vaddr);
51 bo->vaddr = NULL;
52 }
53 drm_framebuffer_remove(fb);
54
55 drm_client_release(&helper->client);
56 drm_fb_helper_unprepare(helper);
57 kfree(helper);
58}
59
60static const struct fb_ops tegra_fb_ops = {
61 .owner = THIS_MODULE,
62 __FB_DEFAULT_DMAMEM_OPS_RDWR,
63 DRM_FB_HELPER_DEFAULT_OPS,
64 __FB_DEFAULT_DMAMEM_OPS_DRAW,
65 .fb_mmap = tegra_fb_mmap,
66 .fb_destroy = tegra_fbdev_fb_destroy,
67};
68
69static const struct drm_fb_helper_funcs tegra_fbdev_helper_funcs = {
70};
71
72int tegra_fbdev_driver_fbdev_probe(struct drm_fb_helper *helper,
73 struct drm_fb_helper_surface_size *sizes)
74{
75 struct tegra_drm *tegra = helper->dev->dev_private;
76 struct drm_device *drm = helper->dev;
77 struct drm_mode_fb_cmd2 cmd = { 0 };
78 unsigned int bytes_per_pixel;
79 struct drm_framebuffer *fb;
80 unsigned long offset;
81 struct fb_info *info;
82 struct tegra_bo *bo;
83 size_t size;
84 int err;
85
86 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
87
88 cmd.width = sizes->surface_width;
89 cmd.height = sizes->surface_height;
90 cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel,
91 tegra->pitch_align);
92
93 cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
94 sizes->surface_depth);
95
96 size = cmd.pitches[0] * cmd.height;
97
98 bo = tegra_bo_create(drm, size, 0);
99 if (IS_ERR(bo))
100 return PTR_ERR(bo);
101
102 info = drm_fb_helper_alloc_info(helper);
103 if (IS_ERR(info)) {
104 dev_err(drm->dev, "failed to allocate framebuffer info\n");
105 drm_gem_object_put(&bo->gem);
106 return PTR_ERR(info);
107 }
108
109 fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
110 if (IS_ERR(fb)) {
111 err = PTR_ERR(fb);
112 dev_err(drm->dev, "failed to allocate DRM framebuffer: %d\n",
113 err);
114 drm_gem_object_put(&bo->gem);
115 return PTR_ERR(fb);
116 }
117
118 helper->funcs = &tegra_fbdev_helper_funcs;
119 helper->fb = fb;
120 helper->info = info;
121
122 info->fbops = &tegra_fb_ops;
123
124 drm_fb_helper_fill_info(info, helper, sizes);
125
126 offset = info->var.xoffset * bytes_per_pixel +
127 info->var.yoffset * fb->pitches[0];
128
129 if (bo->pages) {
130 bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP,
131 pgprot_writecombine(PAGE_KERNEL));
132 if (!bo->vaddr) {
133 dev_err(drm->dev, "failed to vmap() framebuffer\n");
134 err = -ENOMEM;
135 goto destroy;
136 }
137 }
138
139 info->flags |= FBINFO_VIRTFB;
140 info->screen_buffer = bo->vaddr + offset;
141 info->screen_size = size;
142 info->fix.smem_start = (unsigned long)(bo->iova + offset);
143 info->fix.smem_len = size;
144
145 return 0;
146
147destroy:
148 drm_framebuffer_remove(fb);
149 return err;
150}