Loading...
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
23/*
24 * fbdev funcs, to implement legacy fbdev interface on top of drm driver
25 */
26
27#define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base)
28
29struct msm_fbdev {
30 struct drm_fb_helper base;
31 struct drm_framebuffer *fb;
32 struct drm_gem_object *bo;
33};
34
35static struct fb_ops msm_fb_ops = {
36 .owner = THIS_MODULE,
37
38 /* Note: to properly handle manual update displays, we wrap the
39 * basic fbdev ops which write to the framebuffer
40 */
41 .fb_read = fb_sys_read,
42 .fb_write = fb_sys_write,
43 .fb_fillrect = sys_fillrect,
44 .fb_copyarea = sys_copyarea,
45 .fb_imageblit = sys_imageblit,
46
47 .fb_check_var = drm_fb_helper_check_var,
48 .fb_set_par = drm_fb_helper_set_par,
49 .fb_pan_display = drm_fb_helper_pan_display,
50 .fb_blank = drm_fb_helper_blank,
51 .fb_setcmap = drm_fb_helper_setcmap,
52};
53
54static int msm_fbdev_create(struct drm_fb_helper *helper,
55 struct drm_fb_helper_surface_size *sizes)
56{
57 struct msm_fbdev *fbdev = to_msm_fbdev(helper);
58 struct drm_device *dev = helper->dev;
59 struct drm_framebuffer *fb = NULL;
60 struct fb_info *fbi = NULL;
61 struct drm_mode_fb_cmd2 mode_cmd = {0};
62 dma_addr_t paddr;
63 int ret, size;
64
65 sizes->surface_bpp = 32;
66 sizes->surface_depth = 24;
67
68 DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
69 sizes->surface_height, sizes->surface_bpp,
70 sizes->fb_width, sizes->fb_height);
71
72 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
73 sizes->surface_depth);
74
75 mode_cmd.width = sizes->surface_width;
76 mode_cmd.height = sizes->surface_height;
77
78 mode_cmd.pitches[0] = align_pitch(
79 mode_cmd.width, sizes->surface_bpp);
80
81 /* allocate backing bo */
82 size = mode_cmd.pitches[0] * mode_cmd.height;
83 DBG("allocating %d bytes for fb %d", size, dev->primary->index);
84 mutex_lock(&dev->struct_mutex);
85 fbdev->bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC);
86 mutex_unlock(&dev->struct_mutex);
87 if (IS_ERR(fbdev->bo)) {
88 ret = PTR_ERR(fbdev->bo);
89 fbdev->bo = NULL;
90 dev_err(dev->dev, "failed to allocate buffer object: %d\n", ret);
91 goto fail;
92 }
93
94 fb = msm_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
95 if (IS_ERR(fb)) {
96 dev_err(dev->dev, "failed to allocate fb\n");
97 /* note: if fb creation failed, we can't rely on fb destroy
98 * to unref the bo:
99 */
100 drm_gem_object_unreference(fbdev->bo);
101 ret = PTR_ERR(fb);
102 goto fail;
103 }
104
105 mutex_lock(&dev->struct_mutex);
106
107 /* TODO implement our own fb_mmap so we don't need this: */
108 msm_gem_get_iova_locked(fbdev->bo, 0, &paddr);
109
110 fbi = framebuffer_alloc(0, dev->dev);
111 if (!fbi) {
112 dev_err(dev->dev, "failed to allocate fb info\n");
113 ret = -ENOMEM;
114 goto fail_unlock;
115 }
116
117 DBG("fbi=%p, dev=%p", fbi, dev);
118
119 fbdev->fb = fb;
120 helper->fb = fb;
121 helper->fbdev = fbi;
122
123 fbi->par = helper;
124 fbi->flags = FBINFO_DEFAULT;
125 fbi->fbops = &msm_fb_ops;
126
127 strcpy(fbi->fix.id, "msm");
128
129 ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
130 if (ret) {
131 ret = -ENOMEM;
132 goto fail_unlock;
133 }
134
135 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
136 drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
137
138 dev->mode_config.fb_base = paddr;
139
140 fbi->screen_base = msm_gem_vaddr_locked(fbdev->bo);
141 fbi->screen_size = fbdev->bo->size;
142 fbi->fix.smem_start = paddr;
143 fbi->fix.smem_len = fbdev->bo->size;
144
145 DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
146 DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
147
148 mutex_unlock(&dev->struct_mutex);
149
150 return 0;
151
152fail_unlock:
153 mutex_unlock(&dev->struct_mutex);
154fail:
155
156 if (ret) {
157 if (fbi)
158 framebuffer_release(fbi);
159 if (fb) {
160 drm_framebuffer_unregister_private(fb);
161 drm_framebuffer_remove(fb);
162 }
163 }
164
165 return ret;
166}
167
168static void msm_crtc_fb_gamma_set(struct drm_crtc *crtc,
169 u16 red, u16 green, u16 blue, int regno)
170{
171 DBG("fbdev: set gamma");
172}
173
174static void msm_crtc_fb_gamma_get(struct drm_crtc *crtc,
175 u16 *red, u16 *green, u16 *blue, int regno)
176{
177 DBG("fbdev: get gamma");
178}
179
180static struct drm_fb_helper_funcs msm_fb_helper_funcs = {
181 .gamma_set = msm_crtc_fb_gamma_set,
182 .gamma_get = msm_crtc_fb_gamma_get,
183 .fb_probe = msm_fbdev_create,
184};
185
186/* initialize fbdev helper */
187struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
188{
189 struct msm_drm_private *priv = dev->dev_private;
190 struct msm_fbdev *fbdev = NULL;
191 struct drm_fb_helper *helper;
192 int ret = 0;
193
194 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
195 if (!fbdev)
196 goto fail;
197
198 helper = &fbdev->base;
199
200 helper->funcs = &msm_fb_helper_funcs;
201
202 ret = drm_fb_helper_init(dev, helper,
203 priv->num_crtcs, priv->num_connectors);
204 if (ret) {
205 dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
206 goto fail;
207 }
208
209 drm_fb_helper_single_add_all_connectors(helper);
210
211 /* disable all the possible outputs/crtcs before entering KMS mode */
212 drm_helper_disable_unused_functions(dev);
213
214 drm_fb_helper_initial_config(helper, 32);
215
216 priv->fbdev = helper;
217
218 return helper;
219
220fail:
221 kfree(fbdev);
222 return NULL;
223}
224
225void msm_fbdev_free(struct drm_device *dev)
226{
227 struct msm_drm_private *priv = dev->dev_private;
228 struct drm_fb_helper *helper = priv->fbdev;
229 struct msm_fbdev *fbdev;
230 struct fb_info *fbi;
231
232 DBG();
233
234 fbi = helper->fbdev;
235
236 /* only cleanup framebuffer if it is present */
237 if (fbi) {
238 unregister_framebuffer(fbi);
239 framebuffer_release(fbi);
240 }
241
242 drm_fb_helper_fini(helper);
243
244 fbdev = to_msm_fbdev(priv->fbdev);
245
246 /* this will free the backing object */
247 if (fbdev->fb) {
248 drm_framebuffer_unregister_private(fbdev->fb);
249 drm_framebuffer_remove(fbdev->fb);
250 }
251
252 kfree(fbdev);
253
254 priv->fbdev = NULL;
255}
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
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);
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};
38
39static 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
54static 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
70static 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 dev_err(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_iova(bo, priv->kms->aspace, &paddr);
108 if (ret) {
109 dev_err(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 dev_err(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->par = helper;
126 fbi->fbops = &msm_fb_ops;
127
128 strcpy(fbi->fix.id, "msm");
129
130 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
131 drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
132
133 dev->mode_config.fb_base = paddr;
134
135 fbi->screen_base = msm_gem_get_vaddr(bo);
136 if (IS_ERR(fbi->screen_base)) {
137 ret = PTR_ERR(fbi->screen_base);
138 goto fail_unlock;
139 }
140 fbi->screen_size = bo->size;
141 fbi->fix.smem_start = paddr;
142 fbi->fix.smem_len = bo->size;
143
144 DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
145 DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
146
147 mutex_unlock(&dev->struct_mutex);
148
149 return 0;
150
151fail_unlock:
152 mutex_unlock(&dev->struct_mutex);
153 drm_framebuffer_remove(fb);
154 return ret;
155}
156
157static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
158 .fb_probe = msm_fbdev_create,
159};
160
161/* initialize fbdev helper */
162struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
163{
164 struct msm_drm_private *priv = dev->dev_private;
165 struct msm_fbdev *fbdev = NULL;
166 struct drm_fb_helper *helper;
167 int ret;
168
169 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
170 if (!fbdev)
171 goto fail;
172
173 helper = &fbdev->base;
174
175 drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
176
177 ret = drm_fb_helper_init(dev, helper, priv->num_connectors);
178 if (ret) {
179 dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
180 goto fail;
181 }
182
183 ret = drm_fb_helper_single_add_all_connectors(helper);
184 if (ret)
185 goto fini;
186
187 ret = drm_fb_helper_initial_config(helper, 32);
188 if (ret)
189 goto fini;
190
191 priv->fbdev = helper;
192
193 return helper;
194
195fini:
196 drm_fb_helper_fini(helper);
197fail:
198 kfree(fbdev);
199 return NULL;
200}
201
202void msm_fbdev_free(struct drm_device *dev)
203{
204 struct msm_drm_private *priv = dev->dev_private;
205 struct drm_fb_helper *helper = priv->fbdev;
206 struct msm_fbdev *fbdev;
207
208 DBG();
209
210 drm_fb_helper_unregister_fbi(helper);
211
212 drm_fb_helper_fini(helper);
213
214 fbdev = to_msm_fbdev(priv->fbdev);
215
216 /* this will free the backing object */
217 if (fbdev->fb) {
218 struct drm_gem_object *bo =
219 msm_framebuffer_bo(fbdev->fb, 0);
220 msm_gem_put_vaddr(bo);
221 drm_framebuffer_remove(fbdev->fb);
222 }
223
224 kfree(fbdev);
225
226 priv->fbdev = NULL;
227}