1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
7 #include <drm/drm_crtc.h>
8 #include <drm/drm_fb_helper.h>
9 #include <drm/drm_fourcc.h>
14 extern int msm_gem_mmap_obj(struct drm_gem_object *obj,
15 struct vm_area_struct *vma);
16 static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma);
19 * fbdev funcs, to implement legacy fbdev interface on top of drm driver
22 #define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base)
25 struct drm_fb_helper base;
26 struct drm_framebuffer *fb;
29 static const struct fb_ops msm_fb_ops = {
31 DRM_FB_HELPER_DEFAULT_OPS,
33 /* Note: to properly handle manual update displays, we wrap the
34 * basic fbdev ops which write to the framebuffer
36 .fb_read = drm_fb_helper_sys_read,
37 .fb_write = drm_fb_helper_sys_write,
38 .fb_fillrect = drm_fb_helper_sys_fillrect,
39 .fb_copyarea = drm_fb_helper_sys_copyarea,
40 .fb_imageblit = drm_fb_helper_sys_imageblit,
41 .fb_mmap = msm_fbdev_mmap,
44 static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
46 struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par;
47 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
48 struct drm_gem_object *bo = msm_framebuffer_bo(fbdev->fb, 0);
51 ret = drm_gem_mmap_obj(bo, bo->size, vma);
53 pr_err("%s:drm_gem_mmap_obj fail\n", __func__);
57 return msm_gem_mmap_obj(bo, vma);
60 static int msm_fbdev_create(struct drm_fb_helper *helper,
61 struct drm_fb_helper_surface_size *sizes)
63 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
64 struct drm_device *dev = helper->dev;
65 struct msm_drm_private *priv = dev->dev_private;
66 struct drm_framebuffer *fb = NULL;
67 struct drm_gem_object *bo;
68 struct fb_info *fbi = NULL;
73 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
75 DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
76 sizes->surface_height, sizes->surface_bpp,
77 sizes->fb_width, sizes->fb_height);
79 pitch = align_pitch(sizes->surface_width, sizes->surface_bpp);
80 fb = msm_alloc_stolen_fb(dev, sizes->surface_width,
81 sizes->surface_height, pitch, format);
84 DRM_DEV_ERROR(dev->dev, "failed to allocate fb\n");
88 bo = msm_framebuffer_bo(fb, 0);
90 mutex_lock(&dev->struct_mutex);
93 * NOTE: if we can be guaranteed to be able to map buffer
94 * in panic (ie. lock-safe, etc) we could avoid pinning the
97 ret = msm_gem_get_and_pin_iova(bo, priv->kms->aspace, &paddr);
99 DRM_DEV_ERROR(dev->dev, "failed to get buffer obj iova: %d\n", ret);
103 fbi = drm_fb_helper_alloc_fbi(helper);
105 DRM_DEV_ERROR(dev->dev, "failed to allocate fb info\n");
110 DBG("fbi=%p, dev=%p", fbi, dev);
115 fbi->fbops = &msm_fb_ops;
117 drm_fb_helper_fill_info(fbi, helper, sizes);
119 dev->mode_config.fb_base = paddr;
121 fbi->screen_base = msm_gem_get_vaddr(bo);
122 if (IS_ERR(fbi->screen_base)) {
123 ret = PTR_ERR(fbi->screen_base);
126 fbi->screen_size = bo->size;
127 fbi->fix.smem_start = paddr;
128 fbi->fix.smem_len = bo->size;
130 DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
131 DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
133 mutex_unlock(&dev->struct_mutex);
138 mutex_unlock(&dev->struct_mutex);
139 drm_framebuffer_remove(fb);
143 static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
144 .fb_probe = msm_fbdev_create,
147 /* initialize fbdev helper */
148 struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
150 struct msm_drm_private *priv = dev->dev_private;
151 struct msm_fbdev *fbdev = NULL;
152 struct drm_fb_helper *helper;
155 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
159 helper = &fbdev->base;
161 drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
163 ret = drm_fb_helper_init(dev, helper);
165 DRM_DEV_ERROR(dev->dev, "could not init fbdev: ret=%d\n", ret);
169 /* the fw fb could be anywhere in memory */
170 drm_fb_helper_remove_conflicting_framebuffers(NULL, "msm", false);
172 ret = drm_fb_helper_initial_config(helper, 32);
176 priv->fbdev = helper;
181 drm_fb_helper_fini(helper);
187 void msm_fbdev_free(struct drm_device *dev)
189 struct msm_drm_private *priv = dev->dev_private;
190 struct drm_fb_helper *helper = priv->fbdev;
191 struct msm_fbdev *fbdev;
195 drm_fb_helper_unregister_fbi(helper);
197 drm_fb_helper_fini(helper);
199 fbdev = to_msm_fbdev(priv->fbdev);
201 /* this will free the backing object */
203 struct drm_gem_object *bo =
204 msm_framebuffer_bo(fbdev->fb, 0);
205 msm_gem_put_vaddr(bo);
206 drm_framebuffer_remove(fbdev->fb);