blob: 5ecf4ff9a05982882691f0f76044c38d21790174 [file] [log] [blame]
Rob Clarkc8afe682013-06-26 12:44:06 -04001/*
2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
4 *
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.
8 *
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
12 * more details.
13 *
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/>.
16 */
17
Masahiro Yamada78f27b12017-04-24 13:50:28 +090018#include <drm/drm_crtc.h>
19#include <drm/drm_fb_helper.h>
Rob Clarkc8afe682013-06-26 12:44:06 -040020
Masahiro Yamada78f27b12017-04-24 13:50:28 +090021#include "msm_drv.h"
Hai Li8f67da32014-06-18 16:55:27 -040022#include "msm_gem.h"
Rob Clarkf59f62d2017-06-13 10:22:37 -040023#include "msm_kms.h"
Hai Li8f67da32014-06-18 16:55:27 -040024
25extern int msm_gem_mmap_obj(struct drm_gem_object *obj,
26 struct vm_area_struct *vma);
27static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma);
Rob Clarkc8afe682013-06-26 12:44:06 -040028
29/*
30 * fbdev funcs, to implement legacy fbdev interface on top of drm driver
31 */
32
33#define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base)
34
35struct msm_fbdev {
36 struct drm_fb_helper base;
37 struct drm_framebuffer *fb;
38 struct drm_gem_object *bo;
39};
40
41static struct fb_ops msm_fb_ops = {
42 .owner = THIS_MODULE,
Stefan Christ00d44062016-11-14 00:03:24 +010043 DRM_FB_HELPER_DEFAULT_OPS,
Rob Clarkc8afe682013-06-26 12:44:06 -040044
45 /* Note: to properly handle manual update displays, we wrap the
46 * basic fbdev ops which write to the framebuffer
47 */
Archit Taneja778014f2015-07-22 14:58:08 +053048 .fb_read = drm_fb_helper_sys_read,
49 .fb_write = drm_fb_helper_sys_write,
50 .fb_fillrect = drm_fb_helper_sys_fillrect,
51 .fb_copyarea = drm_fb_helper_sys_copyarea,
52 .fb_imageblit = drm_fb_helper_sys_imageblit,
Hai Li8f67da32014-06-18 16:55:27 -040053 .fb_mmap = msm_fbdev_mmap,
Rob Clarkc8afe682013-06-26 12:44:06 -040054};
55
Hai Li8f67da32014-06-18 16:55:27 -040056static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
57{
58 struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par;
59 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
60 struct drm_gem_object *drm_obj = fbdev->bo;
Hai Li8f67da32014-06-18 16:55:27 -040061 int ret = 0;
62
Hai Li8f67da32014-06-18 16:55:27 -040063 ret = drm_gem_mmap_obj(drm_obj, drm_obj->size, vma);
Hai Li8f67da32014-06-18 16:55:27 -040064 if (ret) {
65 pr_err("%s:drm_gem_mmap_obj fail\n", __func__);
66 return ret;
67 }
68
69 return msm_gem_mmap_obj(drm_obj, vma);
70}
71
Rob Clarkc8afe682013-06-26 12:44:06 -040072static int msm_fbdev_create(struct drm_fb_helper *helper,
73 struct drm_fb_helper_surface_size *sizes)
74{
75 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
76 struct drm_device *dev = helper->dev;
Rob Clarkf59f62d2017-06-13 10:22:37 -040077 struct msm_drm_private *priv = dev->dev_private;
Rob Clarkc8afe682013-06-26 12:44:06 -040078 struct drm_framebuffer *fb = NULL;
79 struct fb_info *fbi = NULL;
80 struct drm_mode_fb_cmd2 mode_cmd = {0};
Rob Clark78babc12016-11-11 12:06:46 -050081 uint64_t paddr;
Rob Clarkc8afe682013-06-26 12:44:06 -040082 int ret, size;
83
Rob Clarkc8afe682013-06-26 12:44:06 -040084 DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
85 sizes->surface_height, sizes->surface_bpp,
86 sizes->fb_width, sizes->fb_height);
87
88 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
89 sizes->surface_depth);
90
91 mode_cmd.width = sizes->surface_width;
92 mode_cmd.height = sizes->surface_height;
93
94 mode_cmd.pitches[0] = align_pitch(
95 mode_cmd.width, sizes->surface_bpp);
96
97 /* allocate backing bo */
98 size = mode_cmd.pitches[0] * mode_cmd.height;
99 DBG("allocating %d bytes for fb %d", size, dev->primary->index);
Rob Clark072f1f92015-03-03 15:04:25 -0500100 fbdev->bo = msm_gem_new(dev, size, MSM_BO_SCANOUT |
101 MSM_BO_WC | MSM_BO_STOLEN);
Rob Clarkc8afe682013-06-26 12:44:06 -0400102 if (IS_ERR(fbdev->bo)) {
103 ret = PTR_ERR(fbdev->bo);
104 fbdev->bo = NULL;
105 dev_err(dev->dev, "failed to allocate buffer object: %d\n", ret);
106 goto fail;
107 }
108
109 fb = msm_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
110 if (IS_ERR(fb)) {
111 dev_err(dev->dev, "failed to allocate fb\n");
112 /* note: if fb creation failed, we can't rely on fb destroy
113 * to unref the bo:
114 */
Daniel Vetterdee2eed2015-11-23 10:32:41 +0100115 drm_gem_object_unreference_unlocked(fbdev->bo);
Rob Clarkc8afe682013-06-26 12:44:06 -0400116 ret = PTR_ERR(fb);
117 goto fail;
118 }
119
120 mutex_lock(&dev->struct_mutex);
121
Hai Li8f67da32014-06-18 16:55:27 -0400122 /*
123 * NOTE: if we can be guaranteed to be able to map buffer
124 * in panic (ie. lock-safe, etc) we could avoid pinning the
125 * buffer now:
126 */
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600127 ret = msm_gem_get_iova(fbdev->bo, priv->kms->aspace, &paddr);
Hai Li8f67da32014-06-18 16:55:27 -0400128 if (ret) {
129 dev_err(dev->dev, "failed to get buffer obj iova: %d\n", ret);
Wei Yongjun0d9509d2014-08-14 09:01:40 +0800130 goto fail_unlock;
Hai Li8f67da32014-06-18 16:55:27 -0400131 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400132
Archit Taneja778014f2015-07-22 14:58:08 +0530133 fbi = drm_fb_helper_alloc_fbi(helper);
134 if (IS_ERR(fbi)) {
Rob Clarkc8afe682013-06-26 12:44:06 -0400135 dev_err(dev->dev, "failed to allocate fb info\n");
Archit Taneja778014f2015-07-22 14:58:08 +0530136 ret = PTR_ERR(fbi);
Rob Clarkc8afe682013-06-26 12:44:06 -0400137 goto fail_unlock;
138 }
139
140 DBG("fbi=%p, dev=%p", fbi, dev);
141
142 fbdev->fb = fb;
143 helper->fb = fb;
Rob Clarkc8afe682013-06-26 12:44:06 -0400144
145 fbi->par = helper;
146 fbi->flags = FBINFO_DEFAULT;
147 fbi->fbops = &msm_fb_ops;
148
149 strcpy(fbi->fix.id, "msm");
150
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200151 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
Rob Clarkc8afe682013-06-26 12:44:06 -0400152 drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
153
154 dev->mode_config.fb_base = paddr;
155
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600156 fbi->screen_base = msm_gem_get_vaddr(fbdev->bo);
Rob Clark69a834c2016-05-24 18:29:38 -0400157 if (IS_ERR(fbi->screen_base)) {
158 ret = PTR_ERR(fbi->screen_base);
159 goto fail_unlock;
160 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400161 fbi->screen_size = fbdev->bo->size;
162 fbi->fix.smem_start = paddr;
163 fbi->fix.smem_len = fbdev->bo->size;
164
165 DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
166 DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
167
168 mutex_unlock(&dev->struct_mutex);
169
170 return 0;
171
172fail_unlock:
173 mutex_unlock(&dev->struct_mutex);
174fail:
175
176 if (ret) {
Daniel Vetterf5c5d572016-12-27 11:49:21 +0100177 if (fb)
Rob Clarkc8afe682013-06-26 12:44:06 -0400178 drm_framebuffer_remove(fb);
Rob Clarkc8afe682013-06-26 12:44:06 -0400179 }
180
181 return ret;
182}
183
Thierry Reding3a493872014-06-27 17:19:23 +0200184static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
Rob Clarkc8afe682013-06-26 12:44:06 -0400185 .fb_probe = msm_fbdev_create,
186};
187
188/* initialize fbdev helper */
189struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
190{
191 struct msm_drm_private *priv = dev->dev_private;
192 struct msm_fbdev *fbdev = NULL;
193 struct drm_fb_helper *helper;
Hai Li8f67da32014-06-18 16:55:27 -0400194 int ret;
Rob Clarkc8afe682013-06-26 12:44:06 -0400195
196 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
197 if (!fbdev)
198 goto fail;
199
200 helper = &fbdev->base;
201
Thierry Reding10a23102014-06-27 17:19:24 +0200202 drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
Rob Clarkc8afe682013-06-26 12:44:06 -0400203
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200204 ret = drm_fb_helper_init(dev, helper, priv->num_connectors);
Rob Clarkc8afe682013-06-26 12:44:06 -0400205 if (ret) {
206 dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
207 goto fail;
208 }
209
Thierry Reding01934c22014-12-19 11:21:32 +0100210 ret = drm_fb_helper_single_add_all_connectors(helper);
211 if (ret)
212 goto fini;
Rob Clarkc8afe682013-06-26 12:44:06 -0400213
Thierry Reding01934c22014-12-19 11:21:32 +0100214 ret = drm_fb_helper_initial_config(helper, 32);
215 if (ret)
216 goto fini;
Rob Clarkc8afe682013-06-26 12:44:06 -0400217
218 priv->fbdev = helper;
219
220 return helper;
221
Thierry Reding01934c22014-12-19 11:21:32 +0100222fini:
223 drm_fb_helper_fini(helper);
Rob Clarkc8afe682013-06-26 12:44:06 -0400224fail:
225 kfree(fbdev);
226 return NULL;
227}
228
229void msm_fbdev_free(struct drm_device *dev)
230{
231 struct msm_drm_private *priv = dev->dev_private;
232 struct drm_fb_helper *helper = priv->fbdev;
233 struct msm_fbdev *fbdev;
Rob Clarkc8afe682013-06-26 12:44:06 -0400234
235 DBG();
236
Archit Taneja778014f2015-07-22 14:58:08 +0530237 drm_fb_helper_unregister_fbi(helper);
Rob Clarkc8afe682013-06-26 12:44:06 -0400238
239 drm_fb_helper_fini(helper);
240
241 fbdev = to_msm_fbdev(priv->fbdev);
242
243 /* this will free the backing object */
244 if (fbdev->fb) {
Rob Clark18f23042016-05-26 16:24:35 -0400245 msm_gem_put_vaddr(fbdev->bo);
Rob Clarkc8afe682013-06-26 12:44:06 -0400246 drm_framebuffer_remove(fbdev->fb);
247 }
248
249 kfree(fbdev);
250
251 priv->fbdev = NULL;
252}