blob: ab5bfd2d0ebf2e29897fa56e1df2c77c0f47aeb8 [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
18#include "msm_drv.h"
19
20#include "drm_crtc.h"
21#include "drm_fb_helper.h"
Hai Li8f67da32014-06-18 16:55:27 -040022#include "msm_gem.h"
23
24extern int msm_gem_mmap_obj(struct drm_gem_object *obj,
25 struct vm_area_struct *vma);
26static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma);
Rob Clarkc8afe682013-06-26 12:44:06 -040027
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
34struct msm_fbdev {
35 struct drm_fb_helper base;
36 struct drm_framebuffer *fb;
37 struct drm_gem_object *bo;
38};
39
40static struct fb_ops msm_fb_ops = {
41 .owner = THIS_MODULE,
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 = fb_sys_read,
47 .fb_write = fb_sys_write,
48 .fb_fillrect = sys_fillrect,
49 .fb_copyarea = sys_copyarea,
50 .fb_imageblit = sys_imageblit,
Hai Li8f67da32014-06-18 16:55:27 -040051 .fb_mmap = msm_fbdev_mmap,
Rob Clarkc8afe682013-06-26 12:44:06 -040052
53 .fb_check_var = drm_fb_helper_check_var,
54 .fb_set_par = drm_fb_helper_set_par,
55 .fb_pan_display = drm_fb_helper_pan_display,
56 .fb_blank = drm_fb_helper_blank,
57 .fb_setcmap = drm_fb_helper_setcmap,
58};
59
Hai Li8f67da32014-06-18 16:55:27 -040060static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
61{
62 struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par;
63 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
64 struct drm_gem_object *drm_obj = fbdev->bo;
65 struct drm_device *dev = helper->dev;
66 int ret = 0;
67
68 if (drm_device_is_unplugged(dev))
69 return -ENODEV;
70
71 mutex_lock(&dev->struct_mutex);
72
73 ret = drm_gem_mmap_obj(drm_obj, drm_obj->size, vma);
74
75 mutex_unlock(&dev->struct_mutex);
76
77 if (ret) {
78 pr_err("%s:drm_gem_mmap_obj fail\n", __func__);
79 return ret;
80 }
81
82 return msm_gem_mmap_obj(drm_obj, vma);
83}
84
Rob Clarkc8afe682013-06-26 12:44:06 -040085static int msm_fbdev_create(struct drm_fb_helper *helper,
86 struct drm_fb_helper_surface_size *sizes)
87{
88 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
89 struct drm_device *dev = helper->dev;
90 struct drm_framebuffer *fb = NULL;
91 struct fb_info *fbi = NULL;
92 struct drm_mode_fb_cmd2 mode_cmd = {0};
Matwey V. Kornilov2557e2d2014-06-02 20:17:29 +040093 uint32_t paddr;
Rob Clarkc8afe682013-06-26 12:44:06 -040094 int ret, size;
95
Rob Clarkc8afe682013-06-26 12:44:06 -040096 sizes->surface_bpp = 32;
Rob Clark96673ec2014-04-25 08:50:08 -040097 sizes->surface_depth = 24;
Rob Clarkc8afe682013-06-26 12:44:06 -040098
99 DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
100 sizes->surface_height, sizes->surface_bpp,
101 sizes->fb_width, sizes->fb_height);
102
103 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
104 sizes->surface_depth);
105
106 mode_cmd.width = sizes->surface_width;
107 mode_cmd.height = sizes->surface_height;
108
109 mode_cmd.pitches[0] = align_pitch(
110 mode_cmd.width, sizes->surface_bpp);
111
112 /* allocate backing bo */
113 size = mode_cmd.pitches[0] * mode_cmd.height;
114 DBG("allocating %d bytes for fb %d", size, dev->primary->index);
115 mutex_lock(&dev->struct_mutex);
116 fbdev->bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC);
117 mutex_unlock(&dev->struct_mutex);
118 if (IS_ERR(fbdev->bo)) {
119 ret = PTR_ERR(fbdev->bo);
120 fbdev->bo = NULL;
121 dev_err(dev->dev, "failed to allocate buffer object: %d\n", ret);
122 goto fail;
123 }
124
125 fb = msm_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
126 if (IS_ERR(fb)) {
127 dev_err(dev->dev, "failed to allocate fb\n");
128 /* note: if fb creation failed, we can't rely on fb destroy
129 * to unref the bo:
130 */
131 drm_gem_object_unreference(fbdev->bo);
132 ret = PTR_ERR(fb);
133 goto fail;
134 }
135
136 mutex_lock(&dev->struct_mutex);
137
Hai Li8f67da32014-06-18 16:55:27 -0400138 /*
139 * NOTE: if we can be guaranteed to be able to map buffer
140 * in panic (ie. lock-safe, etc) we could avoid pinning the
141 * buffer now:
142 */
143 ret = msm_gem_get_iova_locked(fbdev->bo, 0, &paddr);
144 if (ret) {
145 dev_err(dev->dev, "failed to get buffer obj iova: %d\n", ret);
Wei Yongjun0d9509d2014-08-14 09:01:40 +0800146 goto fail_unlock;
Hai Li8f67da32014-06-18 16:55:27 -0400147 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400148
149 fbi = framebuffer_alloc(0, dev->dev);
150 if (!fbi) {
151 dev_err(dev->dev, "failed to allocate fb info\n");
152 ret = -ENOMEM;
153 goto fail_unlock;
154 }
155
156 DBG("fbi=%p, dev=%p", fbi, dev);
157
158 fbdev->fb = fb;
159 helper->fb = fb;
160 helper->fbdev = fbi;
161
162 fbi->par = helper;
163 fbi->flags = FBINFO_DEFAULT;
164 fbi->fbops = &msm_fb_ops;
165
166 strcpy(fbi->fix.id, "msm");
167
168 ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
169 if (ret) {
170 ret = -ENOMEM;
171 goto fail_unlock;
172 }
173
174 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
175 drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
176
177 dev->mode_config.fb_base = paddr;
178
179 fbi->screen_base = msm_gem_vaddr_locked(fbdev->bo);
180 fbi->screen_size = fbdev->bo->size;
181 fbi->fix.smem_start = paddr;
182 fbi->fix.smem_len = fbdev->bo->size;
183
184 DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
185 DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
186
187 mutex_unlock(&dev->struct_mutex);
188
189 return 0;
190
191fail_unlock:
192 mutex_unlock(&dev->struct_mutex);
193fail:
194
195 if (ret) {
196 if (fbi)
197 framebuffer_release(fbi);
198 if (fb) {
199 drm_framebuffer_unregister_private(fb);
200 drm_framebuffer_remove(fb);
201 }
202 }
203
204 return ret;
205}
206
207static void msm_crtc_fb_gamma_set(struct drm_crtc *crtc,
208 u16 red, u16 green, u16 blue, int regno)
209{
210 DBG("fbdev: set gamma");
211}
212
213static void msm_crtc_fb_gamma_get(struct drm_crtc *crtc,
214 u16 *red, u16 *green, u16 *blue, int regno)
215{
216 DBG("fbdev: get gamma");
217}
218
Thierry Reding3a493872014-06-27 17:19:23 +0200219static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
Rob Clarkc8afe682013-06-26 12:44:06 -0400220 .gamma_set = msm_crtc_fb_gamma_set,
221 .gamma_get = msm_crtc_fb_gamma_get,
222 .fb_probe = msm_fbdev_create,
223};
224
225/* initialize fbdev helper */
226struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
227{
228 struct msm_drm_private *priv = dev->dev_private;
229 struct msm_fbdev *fbdev = NULL;
230 struct drm_fb_helper *helper;
Hai Li8f67da32014-06-18 16:55:27 -0400231 int ret;
Rob Clarkc8afe682013-06-26 12:44:06 -0400232
233 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
234 if (!fbdev)
235 goto fail;
236
237 helper = &fbdev->base;
238
Thierry Reding10a23102014-06-27 17:19:24 +0200239 drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
Rob Clarkc8afe682013-06-26 12:44:06 -0400240
241 ret = drm_fb_helper_init(dev, helper,
242 priv->num_crtcs, priv->num_connectors);
243 if (ret) {
244 dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
245 goto fail;
246 }
247
248 drm_fb_helper_single_add_all_connectors(helper);
249
250 /* disable all the possible outputs/crtcs before entering KMS mode */
251 drm_helper_disable_unused_functions(dev);
252
253 drm_fb_helper_initial_config(helper, 32);
254
255 priv->fbdev = helper;
256
257 return helper;
258
259fail:
260 kfree(fbdev);
261 return NULL;
262}
263
264void msm_fbdev_free(struct drm_device *dev)
265{
266 struct msm_drm_private *priv = dev->dev_private;
267 struct drm_fb_helper *helper = priv->fbdev;
268 struct msm_fbdev *fbdev;
269 struct fb_info *fbi;
270
271 DBG();
272
273 fbi = helper->fbdev;
274
275 /* only cleanup framebuffer if it is present */
276 if (fbi) {
277 unregister_framebuffer(fbi);
278 framebuffer_release(fbi);
279 }
280
281 drm_fb_helper_fini(helper);
282
283 fbdev = to_msm_fbdev(priv->fbdev);
284
285 /* this will free the backing object */
286 if (fbdev->fb) {
287 drm_framebuffer_unregister_private(fbdev->fb);
288 drm_framebuffer_remove(fbdev->fb);
289 }
290
291 kfree(fbdev);
292
293 priv->fbdev = NULL;
294}