Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
5 */
6
7#include <drm/drm_crtc.h>
8#include <drm/drm_fb_helper.h>
9#include <drm/drm_fourcc.h>
10
11#include "msm_drv.h"
12#include "msm_gem.h"
13#include "msm_kms.h"
14
15extern int msm_gem_mmap_obj(struct drm_gem_object *obj,
16 struct vm_area_struct *vma);
17static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma);
18
19/*
20 * fbdev funcs, to implement legacy fbdev interface on top of drm driver
21 */
22
23#define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base)
24
25struct msm_fbdev {
26 struct drm_fb_helper base;
27 struct drm_framebuffer *fb;
28};
29
30static const struct fb_ops msm_fb_ops = {
31 .owner = THIS_MODULE,
32 DRM_FB_HELPER_DEFAULT_OPS,
33
34 /* Note: to properly handle manual update displays, we wrap the
35 * basic fbdev ops which write to the framebuffer
36 */
37 .fb_read = drm_fb_helper_sys_read,
38 .fb_write = drm_fb_helper_sys_write,
39 .fb_fillrect = drm_fb_helper_sys_fillrect,
40 .fb_copyarea = drm_fb_helper_sys_copyarea,
41 .fb_imageblit = drm_fb_helper_sys_imageblit,
42 .fb_mmap = msm_fbdev_mmap,
43};
44
45static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
46{
47 struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par;
48 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
49 struct drm_gem_object *bo = msm_framebuffer_bo(fbdev->fb, 0);
50 int ret = 0;
51
52 ret = drm_gem_mmap_obj(bo, bo->size, vma);
53 if (ret) {
54 pr_err("%s:drm_gem_mmap_obj fail\n", __func__);
55 return ret;
56 }
57
58 return msm_gem_mmap_obj(bo, vma);
59}
60
61static int msm_fbdev_create(struct drm_fb_helper *helper,
62 struct drm_fb_helper_surface_size *sizes)
63{
64 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
65 struct drm_device *dev = helper->dev;
66 struct msm_drm_private *priv = dev->dev_private;
67 struct drm_framebuffer *fb = NULL;
68 struct drm_gem_object *bo;
69 struct fb_info *fbi = NULL;
70 uint64_t paddr;
71 uint32_t format;
72 int ret, pitch;
73
74 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
75
76 DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
77 sizes->surface_height, sizes->surface_bpp,
78 sizes->fb_width, sizes->fb_height);
79
80 pitch = align_pitch(sizes->surface_width, sizes->surface_bpp);
81 fb = msm_alloc_stolen_fb(dev, sizes->surface_width,
82 sizes->surface_height, pitch, format);
83
84 if (IS_ERR(fb)) {
85 DRM_DEV_ERROR(dev->dev, "failed to allocate fb\n");
86 return PTR_ERR(fb);
87 }
88
89 bo = msm_framebuffer_bo(fb, 0);
90
91 mutex_lock(&dev->struct_mutex);
92
93 /*
94 * NOTE: if we can be guaranteed to be able to map buffer
95 * in panic (ie. lock-safe, etc) we could avoid pinning the
96 * buffer now:
97 */
98 ret = msm_gem_get_and_pin_iova(bo, priv->kms->aspace, &paddr);
99 if (ret) {
100 DRM_DEV_ERROR(dev->dev, "failed to get buffer obj iova: %d\n", ret);
101 goto fail_unlock;
102 }
103
104 fbi = drm_fb_helper_alloc_fbi(helper);
105 if (IS_ERR(fbi)) {
106 DRM_DEV_ERROR(dev->dev, "failed to allocate fb info\n");
107 ret = PTR_ERR(fbi);
108 goto fail_unlock;
109 }
110
111 DBG("fbi=%p, dev=%p", fbi, dev);
112
113 fbdev->fb = fb;
114 helper->fb = fb;
115
116 fbi->fbops = &msm_fb_ops;
117
118 drm_fb_helper_fill_info(fbi, helper, sizes);
119
120 dev->mode_config.fb_base = paddr;
121
122 fbi->screen_base = msm_gem_get_vaddr(bo);
123 if (IS_ERR(fbi->screen_base)) {
124 ret = PTR_ERR(fbi->screen_base);
125 goto fail_unlock;
126 }
127 fbi->screen_size = bo->size;
128 fbi->fix.smem_start = paddr;
129 fbi->fix.smem_len = bo->size;
130
131 DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
132 DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
133
134 mutex_unlock(&dev->struct_mutex);
135
136 return 0;
137
138fail_unlock:
139 mutex_unlock(&dev->struct_mutex);
140 drm_framebuffer_remove(fb);
141 return ret;
142}
143
144static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
145 .fb_probe = msm_fbdev_create,
146};
147
148/* initialize fbdev helper */
149struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
150{
151 struct msm_drm_private *priv = dev->dev_private;
152 struct msm_fbdev *fbdev = NULL;
153 struct drm_fb_helper *helper;
154 int ret;
155
156 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
157 if (!fbdev)
158 goto fail;
159
160 helper = &fbdev->base;
161
162 drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
163
164 ret = drm_fb_helper_init(dev, helper);
165 if (ret) {
166 DRM_DEV_ERROR(dev->dev, "could not init fbdev: ret=%d\n", ret);
167 goto fail;
168 }
169
170 /* the fw fb could be anywhere in memory */
171 drm_fb_helper_remove_conflicting_framebuffers(NULL, "msm", false);
172
173 ret = drm_fb_helper_initial_config(helper, 32);
174 if (ret)
175 goto fini;
176
177 priv->fbdev = helper;
178
179 return helper;
180
181fini:
182 drm_fb_helper_fini(helper);
183fail:
184 kfree(fbdev);
185 return NULL;
186}
187
188void msm_fbdev_free(struct drm_device *dev)
189{
190 struct msm_drm_private *priv = dev->dev_private;
191 struct drm_fb_helper *helper = priv->fbdev;
192 struct msm_fbdev *fbdev;
193
194 DBG();
195
196 drm_fb_helper_unregister_fbi(helper);
197
198 drm_fb_helper_fini(helper);
199
200 fbdev = to_msm_fbdev(priv->fbdev);
201
202 /* this will free the backing object */
203 if (fbdev->fb) {
204 struct drm_gem_object *bo =
205 msm_framebuffer_bo(fbdev->fb, 0);
206 msm_gem_put_vaddr(bo);
207 drm_framebuffer_remove(fbdev->fb);
208 }
209
210 kfree(fbdev);
211
212 priv->fbdev = NULL;
213}