xref: /openbmc/linux/drivers/gpu/drm/msm/msm_fbdev.c (revision e23feb16)
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 
29 struct msm_fbdev {
30 	struct drm_fb_helper base;
31 	struct drm_framebuffer *fb;
32 	struct drm_gem_object *bo;
33 };
34 
35 static 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 
54 static 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 	/* only doing ARGB32 since this is what is needed to alpha-blend
66 	 * with video overlays:
67 	 */
68 	sizes->surface_bpp = 32;
69 	sizes->surface_depth = 32;
70 
71 	DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
72 			sizes->surface_height, sizes->surface_bpp,
73 			sizes->fb_width, sizes->fb_height);
74 
75 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
76 			sizes->surface_depth);
77 
78 	mode_cmd.width = sizes->surface_width;
79 	mode_cmd.height = sizes->surface_height;
80 
81 	mode_cmd.pitches[0] = align_pitch(
82 			mode_cmd.width, sizes->surface_bpp);
83 
84 	/* allocate backing bo */
85 	size = mode_cmd.pitches[0] * mode_cmd.height;
86 	DBG("allocating %d bytes for fb %d", size, dev->primary->index);
87 	mutex_lock(&dev->struct_mutex);
88 	fbdev->bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC);
89 	mutex_unlock(&dev->struct_mutex);
90 	if (IS_ERR(fbdev->bo)) {
91 		ret = PTR_ERR(fbdev->bo);
92 		fbdev->bo = NULL;
93 		dev_err(dev->dev, "failed to allocate buffer object: %d\n", ret);
94 		goto fail;
95 	}
96 
97 	fb = msm_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
98 	if (IS_ERR(fb)) {
99 		dev_err(dev->dev, "failed to allocate fb\n");
100 		/* note: if fb creation failed, we can't rely on fb destroy
101 		 * to unref the bo:
102 		 */
103 		drm_gem_object_unreference(fbdev->bo);
104 		ret = PTR_ERR(fb);
105 		goto fail;
106 	}
107 
108 	mutex_lock(&dev->struct_mutex);
109 
110 	/* TODO implement our own fb_mmap so we don't need this: */
111 	msm_gem_get_iova_locked(fbdev->bo, 0, &paddr);
112 
113 	fbi = framebuffer_alloc(0, dev->dev);
114 	if (!fbi) {
115 		dev_err(dev->dev, "failed to allocate fb info\n");
116 		ret = -ENOMEM;
117 		goto fail_unlock;
118 	}
119 
120 	DBG("fbi=%p, dev=%p", fbi, dev);
121 
122 	fbdev->fb = fb;
123 	helper->fb = fb;
124 	helper->fbdev = fbi;
125 
126 	fbi->par = helper;
127 	fbi->flags = FBINFO_DEFAULT;
128 	fbi->fbops = &msm_fb_ops;
129 
130 	strcpy(fbi->fix.id, "msm");
131 
132 	ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
133 	if (ret) {
134 		ret = -ENOMEM;
135 		goto fail_unlock;
136 	}
137 
138 	drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
139 	drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
140 
141 	dev->mode_config.fb_base = paddr;
142 
143 	fbi->screen_base = msm_gem_vaddr_locked(fbdev->bo);
144 	fbi->screen_size = fbdev->bo->size;
145 	fbi->fix.smem_start = paddr;
146 	fbi->fix.smem_len = fbdev->bo->size;
147 
148 	DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
149 	DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
150 
151 	mutex_unlock(&dev->struct_mutex);
152 
153 	return 0;
154 
155 fail_unlock:
156 	mutex_unlock(&dev->struct_mutex);
157 fail:
158 
159 	if (ret) {
160 		if (fbi)
161 			framebuffer_release(fbi);
162 		if (fb) {
163 			drm_framebuffer_unregister_private(fb);
164 			drm_framebuffer_remove(fb);
165 		}
166 	}
167 
168 	return ret;
169 }
170 
171 static void msm_crtc_fb_gamma_set(struct drm_crtc *crtc,
172 		u16 red, u16 green, u16 blue, int regno)
173 {
174 	DBG("fbdev: set gamma");
175 }
176 
177 static void msm_crtc_fb_gamma_get(struct drm_crtc *crtc,
178 		u16 *red, u16 *green, u16 *blue, int regno)
179 {
180 	DBG("fbdev: get gamma");
181 }
182 
183 static struct drm_fb_helper_funcs msm_fb_helper_funcs = {
184 	.gamma_set = msm_crtc_fb_gamma_set,
185 	.gamma_get = msm_crtc_fb_gamma_get,
186 	.fb_probe = msm_fbdev_create,
187 };
188 
189 /* initialize fbdev helper */
190 struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
191 {
192 	struct msm_drm_private *priv = dev->dev_private;
193 	struct msm_fbdev *fbdev = NULL;
194 	struct drm_fb_helper *helper;
195 	int ret = 0;
196 
197 	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
198 	if (!fbdev)
199 		goto fail;
200 
201 	helper = &fbdev->base;
202 
203 	helper->funcs = &msm_fb_helper_funcs;
204 
205 	ret = drm_fb_helper_init(dev, helper,
206 			priv->num_crtcs, priv->num_connectors);
207 	if (ret) {
208 		dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
209 		goto fail;
210 	}
211 
212 	drm_fb_helper_single_add_all_connectors(helper);
213 
214 	/* disable all the possible outputs/crtcs before entering KMS mode */
215 	drm_helper_disable_unused_functions(dev);
216 
217 	drm_fb_helper_initial_config(helper, 32);
218 
219 	priv->fbdev = helper;
220 
221 	return helper;
222 
223 fail:
224 	kfree(fbdev);
225 	return NULL;
226 }
227 
228 void msm_fbdev_free(struct drm_device *dev)
229 {
230 	struct msm_drm_private *priv = dev->dev_private;
231 	struct drm_fb_helper *helper = priv->fbdev;
232 	struct msm_fbdev *fbdev;
233 	struct fb_info *fbi;
234 
235 	DBG();
236 
237 	fbi = helper->fbdev;
238 
239 	/* only cleanup framebuffer if it is present */
240 	if (fbi) {
241 		unregister_framebuffer(fbi);
242 		framebuffer_release(fbi);
243 	}
244 
245 	drm_fb_helper_fini(helper);
246 
247 	fbdev = to_msm_fbdev(priv->fbdev);
248 
249 	/* this will free the backing object */
250 	if (fbdev->fb) {
251 		drm_framebuffer_unregister_private(fbdev->fb);
252 		drm_framebuffer_remove(fbdev->fb);
253 	}
254 
255 	kfree(fbdev);
256 
257 	priv->fbdev = NULL;
258 }
259