blob: 4c39ef9dd75da8313d82a92aa96ddfe05cb151de [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Rob Clarkc8afe682013-06-26 12:44:06 -04002/*
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
Rob Clarkc8afe682013-06-26 12:44:06 -04005 */
6
Thomas Zimmermann6848c292021-04-12 15:10:42 +02007#include <drm/drm_aperture.h>
Masahiro Yamada78f27b12017-04-24 13:50:28 +09008#include <drm/drm_crtc.h>
9#include <drm/drm_fb_helper.h>
Sam Ravnborgfeea39a2019-08-04 08:55:51 +020010#include <drm/drm_fourcc.h>
Thomas Zimmermann510410b2021-07-06 10:47:53 +020011#include <drm/drm_prime.h>
Rob Clarkc8afe682013-06-26 12:44:06 -040012
Masahiro Yamada78f27b12017-04-24 13:50:28 +090013#include "msm_drv.h"
Rob Clark8f642372020-10-23 09:51:05 -070014#include "msm_gem.h"
Rob Clarkf59f62d2017-06-13 10:22:37 -040015#include "msm_kms.h"
Hai Li8f67da32014-06-18 16:55:27 -040016
Hai Li8f67da32014-06-18 16:55:27 -040017static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma);
Rob Clarkc8afe682013-06-26 12:44:06 -040018
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;
Rob Clarkc8afe682013-06-26 12:44:06 -040028};
29
Jani Nikulab6ff7532019-12-03 18:38:48 +020030static const struct fb_ops msm_fb_ops = {
Rob Clarkc8afe682013-06-26 12:44:06 -040031 .owner = THIS_MODULE,
Stefan Christ00d44062016-11-14 00:03:24 +010032 DRM_FB_HELPER_DEFAULT_OPS,
Rob Clarkc8afe682013-06-26 12:44:06 -040033
34 /* Note: to properly handle manual update displays, we wrap the
35 * basic fbdev ops which write to the framebuffer
36 */
Archit Taneja778014f2015-07-22 14:58:08 +053037 .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,
Hai Li8f67da32014-06-18 16:55:27 -040042 .fb_mmap = msm_fbdev_mmap,
Rob Clarkc8afe682013-06-26 12:44:06 -040043};
44
Hai Li8f67da32014-06-18 16:55:27 -040045static 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);
Rob Clark38958ba2017-07-11 10:13:29 -040049 struct drm_gem_object *bo = msm_framebuffer_bo(fbdev->fb, 0);
Hai Li8f67da32014-06-18 16:55:27 -040050
Thomas Zimmermann510410b2021-07-06 10:47:53 +020051 return drm_gem_prime_mmap(bo, vma);
Hai Li8f67da32014-06-18 16:55:27 -040052}
53
Rob Clarkc8afe682013-06-26 12:44:06 -040054static int msm_fbdev_create(struct drm_fb_helper *helper,
55 struct drm_fb_helper_surface_size *sizes)
56{
57 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
58 struct drm_device *dev = helper->dev;
Rob Clarkf59f62d2017-06-13 10:22:37 -040059 struct msm_drm_private *priv = dev->dev_private;
Rob Clarkc8afe682013-06-26 12:44:06 -040060 struct drm_framebuffer *fb = NULL;
Rob Clark38958ba2017-07-11 10:13:29 -040061 struct drm_gem_object *bo;
Rob Clarkc8afe682013-06-26 12:44:06 -040062 struct fb_info *fbi = NULL;
Rob Clark78babc12016-11-11 12:06:46 -050063 uint64_t paddr;
Rob Clark466e5602017-07-11 10:40:13 -040064 uint32_t format;
65 int ret, pitch;
66
67 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
Rob Clarkc8afe682013-06-26 12:44:06 -040068
Rob Clarkc8afe682013-06-26 12:44:06 -040069 DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
70 sizes->surface_height, sizes->surface_bpp,
71 sizes->fb_width, sizes->fb_height);
72
Rob Clark466e5602017-07-11 10:40:13 -040073 pitch = align_pitch(sizes->surface_width, sizes->surface_bpp);
74 fb = msm_alloc_stolen_fb(dev, sizes->surface_width,
75 sizes->surface_height, pitch, format);
Rob Clarkc8afe682013-06-26 12:44:06 -040076
Rob Clarkc8afe682013-06-26 12:44:06 -040077 if (IS_ERR(fb)) {
Mamta Shukla6a41da12018-10-20 23:19:26 +053078 DRM_DEV_ERROR(dev->dev, "failed to allocate fb\n");
Emil Velikov789d4c32018-03-28 17:22:16 +010079 return PTR_ERR(fb);
Rob Clarkc8afe682013-06-26 12:44:06 -040080 }
81
Rob Clark38958ba2017-07-11 10:13:29 -040082 bo = msm_framebuffer_bo(fb, 0);
83
Hai Li8f67da32014-06-18 16:55:27 -040084 /*
85 * NOTE: if we can be guaranteed to be able to map buffer
86 * in panic (ie. lock-safe, etc) we could avoid pinning the
87 * buffer now:
88 */
Jordan Crouse9fe041f2018-11-07 15:35:50 -070089 ret = msm_gem_get_and_pin_iova(bo, priv->kms->aspace, &paddr);
Hai Li8f67da32014-06-18 16:55:27 -040090 if (ret) {
Mamta Shukla6a41da12018-10-20 23:19:26 +053091 DRM_DEV_ERROR(dev->dev, "failed to get buffer obj iova: %d\n", ret);
Rob Clarkd8c00a82021-11-09 10:11:01 -080092 goto fail;
Hai Li8f67da32014-06-18 16:55:27 -040093 }
Rob Clarkc8afe682013-06-26 12:44:06 -040094
Archit Taneja778014f2015-07-22 14:58:08 +053095 fbi = drm_fb_helper_alloc_fbi(helper);
96 if (IS_ERR(fbi)) {
Mamta Shukla6a41da12018-10-20 23:19:26 +053097 DRM_DEV_ERROR(dev->dev, "failed to allocate fb info\n");
Archit Taneja778014f2015-07-22 14:58:08 +053098 ret = PTR_ERR(fbi);
Rob Clarkd8c00a82021-11-09 10:11:01 -080099 goto fail;
Rob Clarkc8afe682013-06-26 12:44:06 -0400100 }
101
102 DBG("fbi=%p, dev=%p", fbi, dev);
103
104 fbdev->fb = fb;
105 helper->fb = fb;
Rob Clarkc8afe682013-06-26 12:44:06 -0400106
Rob Clarkc8afe682013-06-26 12:44:06 -0400107 fbi->fbops = &msm_fb_ops;
108
Daniel Vetterd4eae712019-03-26 14:20:00 +0100109 drm_fb_helper_fill_info(fbi, helper, sizes);
Rob Clarkc8afe682013-06-26 12:44:06 -0400110
111 dev->mode_config.fb_base = paddr;
112
Rob Clark38958ba2017-07-11 10:13:29 -0400113 fbi->screen_base = msm_gem_get_vaddr(bo);
Rob Clark69a834c2016-05-24 18:29:38 -0400114 if (IS_ERR(fbi->screen_base)) {
115 ret = PTR_ERR(fbi->screen_base);
Rob Clarkd8c00a82021-11-09 10:11:01 -0800116 goto fail;
Rob Clark69a834c2016-05-24 18:29:38 -0400117 }
Rob Clark38958ba2017-07-11 10:13:29 -0400118 fbi->screen_size = bo->size;
Rob Clarkc8afe682013-06-26 12:44:06 -0400119 fbi->fix.smem_start = paddr;
Rob Clark38958ba2017-07-11 10:13:29 -0400120 fbi->fix.smem_len = bo->size;
Rob Clarkc8afe682013-06-26 12:44:06 -0400121
122 DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
123 DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
124
Rob Clarkc8afe682013-06-26 12:44:06 -0400125 return 0;
126
Rob Clarkd8c00a82021-11-09 10:11:01 -0800127fail:
Emil Velikov789d4c32018-03-28 17:22:16 +0100128 drm_framebuffer_remove(fb);
Rob Clarkc8afe682013-06-26 12:44:06 -0400129 return ret;
130}
131
Thierry Reding3a493872014-06-27 17:19:23 +0200132static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
Rob Clarkc8afe682013-06-26 12:44:06 -0400133 .fb_probe = msm_fbdev_create,
134};
135
136/* initialize fbdev helper */
137struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
138{
139 struct msm_drm_private *priv = dev->dev_private;
140 struct msm_fbdev *fbdev = NULL;
141 struct drm_fb_helper *helper;
Hai Li8f67da32014-06-18 16:55:27 -0400142 int ret;
Rob Clarkc8afe682013-06-26 12:44:06 -0400143
144 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
145 if (!fbdev)
146 goto fail;
147
148 helper = &fbdev->base;
149
Thierry Reding10a23102014-06-27 17:19:24 +0200150 drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
Rob Clarkc8afe682013-06-26 12:44:06 -0400151
Pankaj Bharadiya2dea2d12020-03-05 17:34:28 +0530152 ret = drm_fb_helper_init(dev, helper);
Rob Clarkc8afe682013-06-26 12:44:06 -0400153 if (ret) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530154 DRM_DEV_ERROR(dev->dev, "could not init fbdev: ret=%d\n", ret);
Rob Clarkc8afe682013-06-26 12:44:06 -0400155 goto fail;
156 }
157
Jeffrey Hugo23c25972019-06-28 09:28:31 -0700158 /* the fw fb could be anywhere in memory */
Thomas Zimmermann97c9bfe2021-06-29 15:58:33 +0200159 ret = drm_aperture_remove_framebuffers(false, dev->driver);
Thomas Zimmermann6848c292021-04-12 15:10:42 +0200160 if (ret)
161 goto fini;
Jeffrey Hugo23c25972019-06-28 09:28:31 -0700162
Thierry Reding01934c22014-12-19 11:21:32 +0100163 ret = drm_fb_helper_initial_config(helper, 32);
164 if (ret)
165 goto fini;
Rob Clarkc8afe682013-06-26 12:44:06 -0400166
167 priv->fbdev = helper;
168
169 return helper;
170
Thierry Reding01934c22014-12-19 11:21:32 +0100171fini:
172 drm_fb_helper_fini(helper);
Rob Clarkc8afe682013-06-26 12:44:06 -0400173fail:
174 kfree(fbdev);
175 return NULL;
176}
177
178void msm_fbdev_free(struct drm_device *dev)
179{
180 struct msm_drm_private *priv = dev->dev_private;
181 struct drm_fb_helper *helper = priv->fbdev;
182 struct msm_fbdev *fbdev;
Rob Clarkc8afe682013-06-26 12:44:06 -0400183
184 DBG();
185
Archit Taneja778014f2015-07-22 14:58:08 +0530186 drm_fb_helper_unregister_fbi(helper);
Rob Clarkc8afe682013-06-26 12:44:06 -0400187
188 drm_fb_helper_fini(helper);
189
190 fbdev = to_msm_fbdev(priv->fbdev);
191
192 /* this will free the backing object */
193 if (fbdev->fb) {
Rob Clark38958ba2017-07-11 10:13:29 -0400194 struct drm_gem_object *bo =
195 msm_framebuffer_bo(fbdev->fb, 0);
196 msm_gem_put_vaddr(bo);
Rob Clarkc8afe682013-06-26 12:44:06 -0400197 drm_framebuffer_remove(fbdev->fb);
198 }
199
200 kfree(fbdev);
201
202 priv->fbdev = NULL;
203}