2 * Copyright (C) 2013 Red Hat
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_fb_helper.h>
24 extern int msm_gem_mmap_obj(struct drm_gem_object *obj,
25 struct vm_area_struct *vma);
26 static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma);
29 * fbdev funcs, to implement legacy fbdev interface on top of drm driver
32 #define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base)
35 struct drm_fb_helper base;
36 struct drm_framebuffer *fb;
39 static struct fb_ops msm_fb_ops = {
41 DRM_FB_HELPER_DEFAULT_OPS,
43 /* Note: to properly handle manual update displays, we wrap the
44 * basic fbdev ops which write to the framebuffer
46 .fb_read = drm_fb_helper_sys_read,
47 .fb_write = drm_fb_helper_sys_write,
48 .fb_fillrect = drm_fb_helper_sys_fillrect,
49 .fb_copyarea = drm_fb_helper_sys_copyarea,
50 .fb_imageblit = drm_fb_helper_sys_imageblit,
51 .fb_mmap = msm_fbdev_mmap,
54 static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
56 struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par;
57 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
58 struct drm_gem_object *bo = msm_framebuffer_bo(fbdev->fb, 0);
61 ret = drm_gem_mmap_obj(bo, bo->size, vma);
63 pr_err("%s:drm_gem_mmap_obj fail\n", __func__);
67 return msm_gem_mmap_obj(bo, vma);
70 static int msm_fbdev_create(struct drm_fb_helper *helper,
71 struct drm_fb_helper_surface_size *sizes)
73 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
74 struct drm_device *dev = helper->dev;
75 struct msm_drm_private *priv = dev->dev_private;
76 struct drm_framebuffer *fb = NULL;
77 struct drm_gem_object *bo;
78 struct fb_info *fbi = NULL;
83 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
85 DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
86 sizes->surface_height, sizes->surface_bpp,
87 sizes->fb_width, sizes->fb_height);
89 pitch = align_pitch(sizes->surface_width, sizes->surface_bpp);
90 fb = msm_alloc_stolen_fb(dev, sizes->surface_width,
91 sizes->surface_height, pitch, format);
94 dev_err(dev->dev, "failed to allocate fb\n");
98 bo = msm_framebuffer_bo(fb, 0);
100 mutex_lock(&dev->struct_mutex);
103 * NOTE: if we can be guaranteed to be able to map buffer
104 * in panic (ie. lock-safe, etc) we could avoid pinning the
107 ret = msm_gem_get_iova(bo, priv->kms->aspace, &paddr);
109 dev_err(dev->dev, "failed to get buffer obj iova: %d\n", ret);
113 fbi = drm_fb_helper_alloc_fbi(helper);
115 dev_err(dev->dev, "failed to allocate fb info\n");
120 DBG("fbi=%p, dev=%p", fbi, dev);
126 fbi->fbops = &msm_fb_ops;
128 strcpy(fbi->fix.id, "msm");
130 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
131 drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
133 dev->mode_config.fb_base = paddr;
135 fbi->screen_base = msm_gem_get_vaddr(bo);
136 if (IS_ERR(fbi->screen_base)) {
137 ret = PTR_ERR(fbi->screen_base);
140 fbi->screen_size = bo->size;
141 fbi->fix.smem_start = paddr;
142 fbi->fix.smem_len = bo->size;
144 DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
145 DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
147 mutex_unlock(&dev->struct_mutex);
152 mutex_unlock(&dev->struct_mutex);
153 drm_framebuffer_remove(fb);
157 static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
158 .fb_probe = msm_fbdev_create,
161 /* initialize fbdev helper */
162 struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
164 struct msm_drm_private *priv = dev->dev_private;
165 struct msm_fbdev *fbdev = NULL;
166 struct drm_fb_helper *helper;
169 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
173 helper = &fbdev->base;
175 drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
177 ret = drm_fb_helper_init(dev, helper, priv->num_connectors);
179 dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
183 ret = drm_fb_helper_single_add_all_connectors(helper);
187 ret = drm_fb_helper_initial_config(helper, 32);
191 priv->fbdev = helper;
196 drm_fb_helper_fini(helper);
202 void msm_fbdev_free(struct drm_device *dev)
204 struct msm_drm_private *priv = dev->dev_private;
205 struct drm_fb_helper *helper = priv->fbdev;
206 struct msm_fbdev *fbdev;
210 drm_fb_helper_unregister_fbi(helper);
212 drm_fb_helper_fini(helper);
214 fbdev = to_msm_fbdev(priv->fbdev);
216 /* this will free the backing object */
218 struct drm_gem_object *bo =
219 msm_framebuffer_bo(fbdev->fb, 0);
220 msm_gem_put_vaddr(bo);
221 drm_framebuffer_remove(fbdev->fb);