]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/msm/msm_fbdev.c
Merge tag 'drm-misc-next-2017-03-06' of git://anongit.freedesktop.org/git/drm-misc...
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / msm / msm_fbdev.c
CommitLineData
c8afe684
RC
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"
8f67da33
HL
22#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);
c8afe684
RC
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
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,
00d44063 42 DRM_FB_HELPER_DEFAULT_OPS,
c8afe684
RC
43
44 /* Note: to properly handle manual update displays, we wrap the
45 * basic fbdev ops which write to the framebuffer
46 */
778014f0
AT
47 .fb_read = drm_fb_helper_sys_read,
48 .fb_write = drm_fb_helper_sys_write,
49 .fb_fillrect = drm_fb_helper_sys_fillrect,
50 .fb_copyarea = drm_fb_helper_sys_copyarea,
51 .fb_imageblit = drm_fb_helper_sys_imageblit,
8f67da33 52 .fb_mmap = msm_fbdev_mmap,
c8afe684
RC
53};
54
8f67da33
HL
55static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
56{
57 struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par;
58 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
59 struct drm_gem_object *drm_obj = fbdev->bo;
8f67da33
HL
60 int ret = 0;
61
8f67da33 62 ret = drm_gem_mmap_obj(drm_obj, drm_obj->size, vma);
8f67da33
HL
63 if (ret) {
64 pr_err("%s:drm_gem_mmap_obj fail\n", __func__);
65 return ret;
66 }
67
68 return msm_gem_mmap_obj(drm_obj, vma);
69}
70
c8afe684
RC
71static int msm_fbdev_create(struct drm_fb_helper *helper,
72 struct drm_fb_helper_surface_size *sizes)
73{
74 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
75 struct drm_device *dev = helper->dev;
76 struct drm_framebuffer *fb = NULL;
77 struct fb_info *fbi = NULL;
78 struct drm_mode_fb_cmd2 mode_cmd = {0};
78babc16 79 uint64_t paddr;
c8afe684
RC
80 int ret, size;
81
c8afe684
RC
82 DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
83 sizes->surface_height, sizes->surface_bpp,
84 sizes->fb_width, sizes->fb_height);
85
86 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
87 sizes->surface_depth);
88
89 mode_cmd.width = sizes->surface_width;
90 mode_cmd.height = sizes->surface_height;
91
92 mode_cmd.pitches[0] = align_pitch(
93 mode_cmd.width, sizes->surface_bpp);
94
95 /* allocate backing bo */
96 size = mode_cmd.pitches[0] * mode_cmd.height;
97 DBG("allocating %d bytes for fb %d", size, dev->primary->index);
98 mutex_lock(&dev->struct_mutex);
072f1f91
RC
99 fbdev->bo = msm_gem_new(dev, size, MSM_BO_SCANOUT |
100 MSM_BO_WC | MSM_BO_STOLEN);
c8afe684
RC
101 mutex_unlock(&dev->struct_mutex);
102 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 */
dee2eed2 115 drm_gem_object_unreference_unlocked(fbdev->bo);
c8afe684
RC
116 ret = PTR_ERR(fb);
117 goto fail;
118 }
119
120 mutex_lock(&dev->struct_mutex);
121
8f67da33
HL
122 /*
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 */
127 ret = msm_gem_get_iova_locked(fbdev->bo, 0, &paddr);
128 if (ret) {
129 dev_err(dev->dev, "failed to get buffer obj iova: %d\n", ret);
0d9509d2 130 goto fail_unlock;
8f67da33 131 }
c8afe684 132
778014f0
AT
133 fbi = drm_fb_helper_alloc_fbi(helper);
134 if (IS_ERR(fbi)) {
c8afe684 135 dev_err(dev->dev, "failed to allocate fb info\n");
778014f0 136 ret = PTR_ERR(fbi);
c8afe684
RC
137 goto fail_unlock;
138 }
139
140 DBG("fbi=%p, dev=%p", fbi, dev);
141
142 fbdev->fb = fb;
143 helper->fb = fb;
c8afe684
RC
144
145 fbi->par = helper;
146 fbi->flags = FBINFO_DEFAULT;
147 fbi->fbops = &msm_fb_ops;
148
149 strcpy(fbi->fix.id, "msm");
150
b00c600e 151 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
c8afe684
RC
152 drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
153
154 dev->mode_config.fb_base = paddr;
155
18f23049 156 fbi->screen_base = msm_gem_get_vaddr_locked(fbdev->bo);
69a834c2
RC
157 if (IS_ERR(fbi->screen_base)) {
158 ret = PTR_ERR(fbi->screen_base);
159 goto fail_unlock;
160 }
c8afe684
RC
161 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) {
f5c5d57a 177 if (fb)
c8afe684 178 drm_framebuffer_remove(fb);
c8afe684
RC
179 }
180
181 return ret;
182}
183
3a493879 184static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
c8afe684
RC
185 .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;
8f67da33 194 int ret;
c8afe684
RC
195
196 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
197 if (!fbdev)
198 goto fail;
199
200 helper = &fbdev->base;
201
10a23102 202 drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
c8afe684 203
e4563f6b 204 ret = drm_fb_helper_init(dev, helper, priv->num_connectors);
c8afe684
RC
205 if (ret) {
206 dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
207 goto fail;
208 }
209
01934c2a
TR
210 ret = drm_fb_helper_single_add_all_connectors(helper);
211 if (ret)
212 goto fini;
c8afe684 213
01934c2a
TR
214 ret = drm_fb_helper_initial_config(helper, 32);
215 if (ret)
216 goto fini;
c8afe684
RC
217
218 priv->fbdev = helper;
219
220 return helper;
221
01934c2a
TR
222fini:
223 drm_fb_helper_fini(helper);
c8afe684
RC
224fail:
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;
c8afe684
RC
234
235 DBG();
236
778014f0 237 drm_fb_helper_unregister_fbi(helper);
c8afe684
RC
238
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) {
18f23049 245 msm_gem_put_vaddr(fbdev->bo);
c8afe684
RC
246 drm_framebuffer_remove(fbdev->fb);
247 }
248
249 kfree(fbdev);
250
251 priv->fbdev = NULL;
252}