]>
Commit | Line | Data |
---|---|---|
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 | ||
78f27b1c MY |
18 | #include <drm/drm_crtc.h> |
19 | #include <drm/drm_fb_helper.h> | |
c8afe684 | 20 | |
78f27b1c | 21 | #include "msm_drv.h" |
8f67da33 | 22 | #include "msm_gem.h" |
f59f62d5 | 23 | #include "msm_kms.h" |
8f67da33 HL |
24 | |
25 | extern int msm_gem_mmap_obj(struct drm_gem_object *obj, | |
26 | struct vm_area_struct *vma); | |
27 | static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma); | |
c8afe684 RC |
28 | |
29 | /* | |
30 | * fbdev funcs, to implement legacy fbdev interface on top of drm driver | |
31 | */ | |
32 | ||
33 | #define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base) | |
34 | ||
35 | struct msm_fbdev { | |
36 | struct drm_fb_helper base; | |
37 | struct drm_framebuffer *fb; | |
38 | struct drm_gem_object *bo; | |
39 | }; | |
40 | ||
41 | static struct fb_ops msm_fb_ops = { | |
42 | .owner = THIS_MODULE, | |
00d44063 | 43 | DRM_FB_HELPER_DEFAULT_OPS, |
c8afe684 RC |
44 | |
45 | /* Note: to properly handle manual update displays, we wrap the | |
46 | * basic fbdev ops which write to the framebuffer | |
47 | */ | |
778014f0 AT |
48 | .fb_read = drm_fb_helper_sys_read, |
49 | .fb_write = drm_fb_helper_sys_write, | |
50 | .fb_fillrect = drm_fb_helper_sys_fillrect, | |
51 | .fb_copyarea = drm_fb_helper_sys_copyarea, | |
52 | .fb_imageblit = drm_fb_helper_sys_imageblit, | |
8f67da33 | 53 | .fb_mmap = msm_fbdev_mmap, |
c8afe684 RC |
54 | }; |
55 | ||
8f67da33 HL |
56 | static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma) |
57 | { | |
58 | struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par; | |
59 | struct msm_fbdev *fbdev = to_msm_fbdev(helper); | |
60 | struct drm_gem_object *drm_obj = fbdev->bo; | |
8f67da33 HL |
61 | int ret = 0; |
62 | ||
8f67da33 | 63 | ret = drm_gem_mmap_obj(drm_obj, drm_obj->size, vma); |
8f67da33 HL |
64 | if (ret) { |
65 | pr_err("%s:drm_gem_mmap_obj fail\n", __func__); | |
66 | return ret; | |
67 | } | |
68 | ||
69 | return msm_gem_mmap_obj(drm_obj, vma); | |
70 | } | |
71 | ||
c8afe684 RC |
72 | static int msm_fbdev_create(struct drm_fb_helper *helper, |
73 | struct drm_fb_helper_surface_size *sizes) | |
74 | { | |
75 | struct msm_fbdev *fbdev = to_msm_fbdev(helper); | |
76 | struct drm_device *dev = helper->dev; | |
f59f62d5 | 77 | struct msm_drm_private *priv = dev->dev_private; |
c8afe684 RC |
78 | struct drm_framebuffer *fb = NULL; |
79 | struct fb_info *fbi = NULL; | |
80 | struct drm_mode_fb_cmd2 mode_cmd = {0}; | |
78babc16 | 81 | uint64_t paddr; |
c8afe684 RC |
82 | int ret, size; |
83 | ||
c8afe684 RC |
84 | DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width, |
85 | sizes->surface_height, sizes->surface_bpp, | |
86 | sizes->fb_width, sizes->fb_height); | |
87 | ||
88 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, | |
89 | sizes->surface_depth); | |
90 | ||
91 | mode_cmd.width = sizes->surface_width; | |
92 | mode_cmd.height = sizes->surface_height; | |
93 | ||
94 | mode_cmd.pitches[0] = align_pitch( | |
95 | mode_cmd.width, sizes->surface_bpp); | |
96 | ||
97 | /* allocate backing bo */ | |
98 | size = mode_cmd.pitches[0] * mode_cmd.height; | |
99 | DBG("allocating %d bytes for fb %d", size, dev->primary->index); | |
072f1f91 RC |
100 | fbdev->bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | |
101 | MSM_BO_WC | MSM_BO_STOLEN); | |
c8afe684 RC |
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 | */ | |
0e08270a | 127 | ret = msm_gem_get_iova(fbdev->bo, priv->kms->aspace, &paddr); |
8f67da33 HL |
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; | |
c8afe684 RC |
146 | fbi->fbops = &msm_fb_ops; |
147 | ||
148 | strcpy(fbi->fix.id, "msm"); | |
149 | ||
b00c600e | 150 | drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth); |
c8afe684 RC |
151 | drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height); |
152 | ||
153 | dev->mode_config.fb_base = paddr; | |
154 | ||
0e08270a | 155 | fbi->screen_base = msm_gem_get_vaddr(fbdev->bo); |
69a834c2 RC |
156 | if (IS_ERR(fbi->screen_base)) { |
157 | ret = PTR_ERR(fbi->screen_base); | |
158 | goto fail_unlock; | |
159 | } | |
c8afe684 RC |
160 | fbi->screen_size = fbdev->bo->size; |
161 | fbi->fix.smem_start = paddr; | |
162 | fbi->fix.smem_len = fbdev->bo->size; | |
163 | ||
164 | DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres); | |
165 | DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height); | |
166 | ||
167 | mutex_unlock(&dev->struct_mutex); | |
168 | ||
169 | return 0; | |
170 | ||
171 | fail_unlock: | |
172 | mutex_unlock(&dev->struct_mutex); | |
173 | fail: | |
174 | ||
175 | if (ret) { | |
f5c5d57a | 176 | if (fb) |
c8afe684 | 177 | drm_framebuffer_remove(fb); |
c8afe684 RC |
178 | } |
179 | ||
180 | return ret; | |
181 | } | |
182 | ||
3a493879 | 183 | static const struct drm_fb_helper_funcs msm_fb_helper_funcs = { |
c8afe684 RC |
184 | .fb_probe = msm_fbdev_create, |
185 | }; | |
186 | ||
187 | /* initialize fbdev helper */ | |
188 | struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev) | |
189 | { | |
190 | struct msm_drm_private *priv = dev->dev_private; | |
191 | struct msm_fbdev *fbdev = NULL; | |
192 | struct drm_fb_helper *helper; | |
8f67da33 | 193 | int ret; |
c8afe684 RC |
194 | |
195 | fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL); | |
196 | if (!fbdev) | |
197 | goto fail; | |
198 | ||
199 | helper = &fbdev->base; | |
200 | ||
10a23102 | 201 | drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs); |
c8afe684 | 202 | |
e4563f6b | 203 | ret = drm_fb_helper_init(dev, helper, priv->num_connectors); |
c8afe684 RC |
204 | if (ret) { |
205 | dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret); | |
206 | goto fail; | |
207 | } | |
208 | ||
01934c2a TR |
209 | ret = drm_fb_helper_single_add_all_connectors(helper); |
210 | if (ret) | |
211 | goto fini; | |
c8afe684 | 212 | |
01934c2a TR |
213 | ret = drm_fb_helper_initial_config(helper, 32); |
214 | if (ret) | |
215 | goto fini; | |
c8afe684 RC |
216 | |
217 | priv->fbdev = helper; | |
218 | ||
219 | return helper; | |
220 | ||
01934c2a TR |
221 | fini: |
222 | drm_fb_helper_fini(helper); | |
c8afe684 RC |
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; | |
c8afe684 RC |
233 | |
234 | DBG(); | |
235 | ||
778014f0 | 236 | drm_fb_helper_unregister_fbi(helper); |
c8afe684 RC |
237 | |
238 | drm_fb_helper_fini(helper); | |
239 | ||
240 | fbdev = to_msm_fbdev(priv->fbdev); | |
241 | ||
242 | /* this will free the backing object */ | |
243 | if (fbdev->fb) { | |
18f23049 | 244 | msm_gem_put_vaddr(fbdev->bo); |
c8afe684 RC |
245 | drm_framebuffer_remove(fbdev->fb); |
246 | } | |
247 | ||
248 | kfree(fbdev); | |
249 | ||
250 | priv->fbdev = NULL; | |
251 | } |