Merge tag 'drm-misc-next-fixes-2021-09-09' of git://anongit.freedesktop.org/drm/drm...
[linux-2.6-microblaze.git] / drivers / gpu / drm / msm / msm_fbdev.c
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_aperture.h>
8 #include <drm/drm_crtc.h>
9 #include <drm/drm_fb_helper.h>
10 #include <drm/drm_fourcc.h>
11 #include <drm/drm_prime.h>
12
13 #include "msm_drv.h"
14 #include "msm_gem.h"
15 #include "msm_kms.h"
16
17 static 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
25 struct msm_fbdev {
26         struct drm_fb_helper base;
27         struct drm_framebuffer *fb;
28 };
29
30 static 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
45 static 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
51         return drm_gem_prime_mmap(bo, vma);
52 }
53
54 static 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;
59         struct msm_drm_private *priv = dev->dev_private;
60         struct drm_framebuffer *fb = NULL;
61         struct drm_gem_object *bo;
62         struct fb_info *fbi = NULL;
63         uint64_t paddr;
64         uint32_t format;
65         int ret, pitch;
66
67         format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
68
69         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
73         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);
76
77         if (IS_ERR(fb)) {
78                 DRM_DEV_ERROR(dev->dev, "failed to allocate fb\n");
79                 return PTR_ERR(fb);
80         }
81
82         bo = msm_framebuffer_bo(fb, 0);
83
84         mutex_lock(&dev->struct_mutex);
85
86         /*
87          * NOTE: if we can be guaranteed to be able to map buffer
88          * in panic (ie. lock-safe, etc) we could avoid pinning the
89          * buffer now:
90          */
91         ret = msm_gem_get_and_pin_iova(bo, priv->kms->aspace, &paddr);
92         if (ret) {
93                 DRM_DEV_ERROR(dev->dev, "failed to get buffer obj iova: %d\n", ret);
94                 goto fail_unlock;
95         }
96
97         fbi = drm_fb_helper_alloc_fbi(helper);
98         if (IS_ERR(fbi)) {
99                 DRM_DEV_ERROR(dev->dev, "failed to allocate fb info\n");
100                 ret = PTR_ERR(fbi);
101                 goto fail_unlock;
102         }
103
104         DBG("fbi=%p, dev=%p", fbi, dev);
105
106         fbdev->fb = fb;
107         helper->fb = fb;
108
109         fbi->fbops = &msm_fb_ops;
110
111         drm_fb_helper_fill_info(fbi, helper, sizes);
112
113         dev->mode_config.fb_base = paddr;
114
115         fbi->screen_base = msm_gem_get_vaddr(bo);
116         if (IS_ERR(fbi->screen_base)) {
117                 ret = PTR_ERR(fbi->screen_base);
118                 goto fail_unlock;
119         }
120         fbi->screen_size = bo->size;
121         fbi->fix.smem_start = paddr;
122         fbi->fix.smem_len = bo->size;
123
124         DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
125         DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
126
127         mutex_unlock(&dev->struct_mutex);
128
129         return 0;
130
131 fail_unlock:
132         mutex_unlock(&dev->struct_mutex);
133         drm_framebuffer_remove(fb);
134         return ret;
135 }
136
137 static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
138         .fb_probe = msm_fbdev_create,
139 };
140
141 /* initialize fbdev helper */
142 struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
143 {
144         struct msm_drm_private *priv = dev->dev_private;
145         struct msm_fbdev *fbdev = NULL;
146         struct drm_fb_helper *helper;
147         int ret;
148
149         fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
150         if (!fbdev)
151                 goto fail;
152
153         helper = &fbdev->base;
154
155         drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
156
157         ret = drm_fb_helper_init(dev, helper);
158         if (ret) {
159                 DRM_DEV_ERROR(dev->dev, "could not init fbdev: ret=%d\n", ret);
160                 goto fail;
161         }
162
163         /* the fw fb could be anywhere in memory */
164         ret = drm_aperture_remove_framebuffers(false, dev->driver);
165         if (ret)
166                 goto fini;
167
168         ret = drm_fb_helper_initial_config(helper, 32);
169         if (ret)
170                 goto fini;
171
172         priv->fbdev = helper;
173
174         return helper;
175
176 fini:
177         drm_fb_helper_fini(helper);
178 fail:
179         kfree(fbdev);
180         return NULL;
181 }
182
183 void msm_fbdev_free(struct drm_device *dev)
184 {
185         struct msm_drm_private *priv = dev->dev_private;
186         struct drm_fb_helper *helper = priv->fbdev;
187         struct msm_fbdev *fbdev;
188
189         DBG();
190
191         drm_fb_helper_unregister_fbi(helper);
192
193         drm_fb_helper_fini(helper);
194
195         fbdev = to_msm_fbdev(priv->fbdev);
196
197         /* this will free the backing object */
198         if (fbdev->fb) {
199                 struct drm_gem_object *bo =
200                         msm_framebuffer_bo(fbdev->fb, 0);
201                 msm_gem_put_vaddr(bo);
202                 drm_framebuffer_remove(fbdev->fb);
203         }
204
205         kfree(fbdev);
206
207         priv->fbdev = NULL;
208 }