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