]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/msm/msm_fbdev.c
Merge remote-tracking branch 'tip/auto-latest'
[karo-tx-linux.git] / drivers / gpu / drm / msm / msm_fbdev.c
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 "msm_drv.h"
19
20 #include "drm_crtc.h"
21 #include "drm_fb_helper.h"
22 #include "msm_gem.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         struct drm_gem_object *bo;
38 };
39
40 static 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 = 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         .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
60 static 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         ret = drm_gem_mmap_obj(drm_obj, drm_obj->size, vma);
69         if (ret) {
70                 pr_err("%s:drm_gem_mmap_obj fail\n", __func__);
71                 return ret;
72         }
73
74         return msm_gem_mmap_obj(drm_obj, vma);
75 }
76
77 static int msm_fbdev_create(struct drm_fb_helper *helper,
78                 struct drm_fb_helper_surface_size *sizes)
79 {
80         struct msm_fbdev *fbdev = to_msm_fbdev(helper);
81         struct drm_device *dev = helper->dev;
82         struct drm_framebuffer *fb = NULL;
83         struct fb_info *fbi = NULL;
84         struct drm_mode_fb_cmd2 mode_cmd = {0};
85         uint32_t paddr;
86         int ret, size;
87
88         DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
89                         sizes->surface_height, sizes->surface_bpp,
90                         sizes->fb_width, sizes->fb_height);
91
92         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
93                         sizes->surface_depth);
94
95         mode_cmd.width = sizes->surface_width;
96         mode_cmd.height = sizes->surface_height;
97
98         mode_cmd.pitches[0] = align_pitch(
99                         mode_cmd.width, sizes->surface_bpp);
100
101         /* allocate backing bo */
102         size = mode_cmd.pitches[0] * mode_cmd.height;
103         DBG("allocating %d bytes for fb %d", size, dev->primary->index);
104         mutex_lock(&dev->struct_mutex);
105         fbdev->bo = msm_gem_new(dev, size, MSM_BO_SCANOUT |
106                         MSM_BO_WC | MSM_BO_STOLEN);
107         mutex_unlock(&dev->struct_mutex);
108         if (IS_ERR(fbdev->bo)) {
109                 ret = PTR_ERR(fbdev->bo);
110                 fbdev->bo = NULL;
111                 dev_err(dev->dev, "failed to allocate buffer object: %d\n", ret);
112                 goto fail;
113         }
114
115         fb = msm_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
116         if (IS_ERR(fb)) {
117                 dev_err(dev->dev, "failed to allocate fb\n");
118                 /* note: if fb creation failed, we can't rely on fb destroy
119                  * to unref the bo:
120                  */
121                 drm_gem_object_unreference_unlocked(fbdev->bo);
122                 ret = PTR_ERR(fb);
123                 goto fail;
124         }
125
126         mutex_lock(&dev->struct_mutex);
127
128         /*
129          * NOTE: if we can be guaranteed to be able to map buffer
130          * in panic (ie. lock-safe, etc) we could avoid pinning the
131          * buffer now:
132          */
133         ret = msm_gem_get_iova_locked(fbdev->bo, 0, &paddr);
134         if (ret) {
135                 dev_err(dev->dev, "failed to get buffer obj iova: %d\n", ret);
136                 goto fail_unlock;
137         }
138
139         fbi = drm_fb_helper_alloc_fbi(helper);
140         if (IS_ERR(fbi)) {
141                 dev_err(dev->dev, "failed to allocate fb info\n");
142                 ret = PTR_ERR(fbi);
143                 goto fail_unlock;
144         }
145
146         DBG("fbi=%p, dev=%p", fbi, dev);
147
148         fbdev->fb = fb;
149         helper->fb = fb;
150
151         fbi->par = helper;
152         fbi->flags = FBINFO_DEFAULT;
153         fbi->fbops = &msm_fb_ops;
154
155         strcpy(fbi->fix.id, "msm");
156
157         drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
158         drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
159
160         dev->mode_config.fb_base = paddr;
161
162         fbi->screen_base = msm_gem_vaddr_locked(fbdev->bo);
163         fbi->screen_size = fbdev->bo->size;
164         fbi->fix.smem_start = paddr;
165         fbi->fix.smem_len = fbdev->bo->size;
166
167         DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
168         DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
169
170         mutex_unlock(&dev->struct_mutex);
171
172         return 0;
173
174 fail_unlock:
175         mutex_unlock(&dev->struct_mutex);
176 fail:
177
178         if (ret) {
179                 if (fb) {
180                         drm_framebuffer_unregister_private(fb);
181                         drm_framebuffer_remove(fb);
182                 }
183         }
184
185         return ret;
186 }
187
188 static void msm_crtc_fb_gamma_set(struct drm_crtc *crtc,
189                 u16 red, u16 green, u16 blue, int regno)
190 {
191         DBG("fbdev: set gamma");
192 }
193
194 static void msm_crtc_fb_gamma_get(struct drm_crtc *crtc,
195                 u16 *red, u16 *green, u16 *blue, int regno)
196 {
197         DBG("fbdev: get gamma");
198 }
199
200 static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
201         .gamma_set = msm_crtc_fb_gamma_set,
202         .gamma_get = msm_crtc_fb_gamma_get,
203         .fb_probe = msm_fbdev_create,
204 };
205
206 /* initialize fbdev helper */
207 struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
208 {
209         struct msm_drm_private *priv = dev->dev_private;
210         struct msm_fbdev *fbdev = NULL;
211         struct drm_fb_helper *helper;
212         int ret;
213
214         fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
215         if (!fbdev)
216                 goto fail;
217
218         helper = &fbdev->base;
219
220         drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
221
222         ret = drm_fb_helper_init(dev, helper,
223                         priv->num_crtcs, priv->num_connectors);
224         if (ret) {
225                 dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
226                 goto fail;
227         }
228
229         ret = drm_fb_helper_single_add_all_connectors(helper);
230         if (ret)
231                 goto fini;
232
233         ret = drm_fb_helper_initial_config(helper, 32);
234         if (ret)
235                 goto fini;
236
237         priv->fbdev = helper;
238
239         return helper;
240
241 fini:
242         drm_fb_helper_fini(helper);
243 fail:
244         kfree(fbdev);
245         return NULL;
246 }
247
248 void msm_fbdev_free(struct drm_device *dev)
249 {
250         struct msm_drm_private *priv = dev->dev_private;
251         struct drm_fb_helper *helper = priv->fbdev;
252         struct msm_fbdev *fbdev;
253
254         DBG();
255
256         drm_fb_helper_unregister_fbi(helper);
257         drm_fb_helper_release_fbi(helper);
258
259         drm_fb_helper_fini(helper);
260
261         fbdev = to_msm_fbdev(priv->fbdev);
262
263         /* this will free the backing object */
264         if (fbdev->fb) {
265                 drm_framebuffer_unregister_private(fbdev->fb);
266                 drm_framebuffer_remove(fbdev->fb);
267         }
268
269         kfree(fbdev);
270
271         priv->fbdev = NULL;
272 }