xref: /openbmc/linux/drivers/gpu/drm/msm/msm_fbdev.c (revision c2cd9d04)
1 /*
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 
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_fb_helper.h>
20 
21 #include "msm_drv.h"
22 #include "msm_kms.h"
23 
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);
27 
28 /*
29  * fbdev funcs, to implement legacy fbdev interface on top of drm driver
30  */
31 
32 #define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base)
33 
34 struct msm_fbdev {
35 	struct drm_fb_helper base;
36 	struct drm_framebuffer *fb;
37 };
38 
39 static struct fb_ops msm_fb_ops = {
40 	.owner = THIS_MODULE,
41 	DRM_FB_HELPER_DEFAULT_OPS,
42 
43 	/* Note: to properly handle manual update displays, we wrap the
44 	 * basic fbdev ops which write to the framebuffer
45 	 */
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,
52 };
53 
54 static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
55 {
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);
59 	int ret = 0;
60 
61 	ret = drm_gem_mmap_obj(bo, bo->size, vma);
62 	if (ret) {
63 		pr_err("%s:drm_gem_mmap_obj fail\n", __func__);
64 		return ret;
65 	}
66 
67 	return msm_gem_mmap_obj(bo, vma);
68 }
69 
70 static int msm_fbdev_create(struct drm_fb_helper *helper,
71 		struct drm_fb_helper_surface_size *sizes)
72 {
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;
79 	uint64_t paddr;
80 	uint32_t format;
81 	int ret, pitch;
82 
83 	format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
84 
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);
88 
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);
92 
93 	if (IS_ERR(fb)) {
94 		DRM_DEV_ERROR(dev->dev, "failed to allocate fb\n");
95 		return PTR_ERR(fb);
96 	}
97 
98 	bo = msm_framebuffer_bo(fb, 0);
99 
100 	mutex_lock(&dev->struct_mutex);
101 
102 	/*
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
105 	 * buffer now:
106 	 */
107 	ret = msm_gem_get_and_pin_iova(bo, priv->kms->aspace, &paddr);
108 	if (ret) {
109 		DRM_DEV_ERROR(dev->dev, "failed to get buffer obj iova: %d\n", ret);
110 		goto fail_unlock;
111 	}
112 
113 	fbi = drm_fb_helper_alloc_fbi(helper);
114 	if (IS_ERR(fbi)) {
115 		DRM_DEV_ERROR(dev->dev, "failed to allocate fb info\n");
116 		ret = PTR_ERR(fbi);
117 		goto fail_unlock;
118 	}
119 
120 	DBG("fbi=%p, dev=%p", fbi, dev);
121 
122 	fbdev->fb = fb;
123 	helper->fb = fb;
124 
125 	fbi->fbops = &msm_fb_ops;
126 
127 	drm_fb_helper_fill_info(fbi, helper, sizes);
128 
129 	dev->mode_config.fb_base = paddr;
130 
131 	fbi->screen_base = msm_gem_get_vaddr(bo);
132 	if (IS_ERR(fbi->screen_base)) {
133 		ret = PTR_ERR(fbi->screen_base);
134 		goto fail_unlock;
135 	}
136 	fbi->screen_size = bo->size;
137 	fbi->fix.smem_start = paddr;
138 	fbi->fix.smem_len = bo->size;
139 
140 	DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
141 	DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
142 
143 	mutex_unlock(&dev->struct_mutex);
144 
145 	return 0;
146 
147 fail_unlock:
148 	mutex_unlock(&dev->struct_mutex);
149 	drm_framebuffer_remove(fb);
150 	return ret;
151 }
152 
153 static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
154 	.fb_probe = msm_fbdev_create,
155 };
156 
157 /* initialize fbdev helper */
158 struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
159 {
160 	struct msm_drm_private *priv = dev->dev_private;
161 	struct msm_fbdev *fbdev = NULL;
162 	struct drm_fb_helper *helper;
163 	int ret;
164 
165 	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
166 	if (!fbdev)
167 		goto fail;
168 
169 	helper = &fbdev->base;
170 
171 	drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
172 
173 	ret = drm_fb_helper_init(dev, helper, priv->num_connectors);
174 	if (ret) {
175 		DRM_DEV_ERROR(dev->dev, "could not init fbdev: ret=%d\n", ret);
176 		goto fail;
177 	}
178 
179 	ret = drm_fb_helper_single_add_all_connectors(helper);
180 	if (ret)
181 		goto fini;
182 
183 	ret = drm_fb_helper_initial_config(helper, 32);
184 	if (ret)
185 		goto fini;
186 
187 	priv->fbdev = helper;
188 
189 	return helper;
190 
191 fini:
192 	drm_fb_helper_fini(helper);
193 fail:
194 	kfree(fbdev);
195 	return NULL;
196 }
197 
198 void msm_fbdev_free(struct drm_device *dev)
199 {
200 	struct msm_drm_private *priv = dev->dev_private;
201 	struct drm_fb_helper *helper = priv->fbdev;
202 	struct msm_fbdev *fbdev;
203 
204 	DBG();
205 
206 	drm_fb_helper_unregister_fbi(helper);
207 
208 	drm_fb_helper_fini(helper);
209 
210 	fbdev = to_msm_fbdev(priv->fbdev);
211 
212 	/* this will free the backing object */
213 	if (fbdev->fb) {
214 		struct drm_gem_object *bo =
215 			msm_framebuffer_bo(fbdev->fb, 0);
216 		msm_gem_put_vaddr(bo);
217 		drm_framebuffer_remove(fbdev->fb);
218 	}
219 
220 	kfree(fbdev);
221 
222 	priv->fbdev = NULL;
223 }
224