Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 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" |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 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); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 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 | */ |
Archit Taneja | 778014f | 2015-07-22 14:58:08 +0530 | [diff] [blame] | 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, |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 51 | .fb_mmap = msm_fbdev_mmap, |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 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 | |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 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; |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 65 | int ret = 0; |
| 66 | |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 67 | ret = drm_gem_mmap_obj(drm_obj, drm_obj->size, vma); |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 68 | if (ret) { |
| 69 | pr_err("%s:drm_gem_mmap_obj fail\n", __func__); |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | return msm_gem_mmap_obj(drm_obj, vma); |
| 74 | } |
| 75 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 76 | static int msm_fbdev_create(struct drm_fb_helper *helper, |
| 77 | struct drm_fb_helper_surface_size *sizes) |
| 78 | { |
| 79 | struct msm_fbdev *fbdev = to_msm_fbdev(helper); |
| 80 | struct drm_device *dev = helper->dev; |
| 81 | struct drm_framebuffer *fb = NULL; |
| 82 | struct fb_info *fbi = NULL; |
| 83 | struct drm_mode_fb_cmd2 mode_cmd = {0}; |
Matwey V. Kornilov | 2557e2d | 2014-06-02 20:17:29 +0400 | [diff] [blame] | 84 | uint32_t paddr; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 85 | int ret, size; |
| 86 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 87 | DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width, |
| 88 | sizes->surface_height, sizes->surface_bpp, |
| 89 | sizes->fb_width, sizes->fb_height); |
| 90 | |
| 91 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, |
| 92 | sizes->surface_depth); |
| 93 | |
| 94 | mode_cmd.width = sizes->surface_width; |
| 95 | mode_cmd.height = sizes->surface_height; |
| 96 | |
| 97 | mode_cmd.pitches[0] = align_pitch( |
| 98 | mode_cmd.width, sizes->surface_bpp); |
| 99 | |
| 100 | /* allocate backing bo */ |
| 101 | size = mode_cmd.pitches[0] * mode_cmd.height; |
| 102 | DBG("allocating %d bytes for fb %d", size, dev->primary->index); |
| 103 | mutex_lock(&dev->struct_mutex); |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 104 | fbdev->bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | |
| 105 | MSM_BO_WC | MSM_BO_STOLEN); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 106 | mutex_unlock(&dev->struct_mutex); |
| 107 | if (IS_ERR(fbdev->bo)) { |
| 108 | ret = PTR_ERR(fbdev->bo); |
| 109 | fbdev->bo = NULL; |
| 110 | dev_err(dev->dev, "failed to allocate buffer object: %d\n", ret); |
| 111 | goto fail; |
| 112 | } |
| 113 | |
| 114 | fb = msm_framebuffer_init(dev, &mode_cmd, &fbdev->bo); |
| 115 | if (IS_ERR(fb)) { |
| 116 | dev_err(dev->dev, "failed to allocate fb\n"); |
| 117 | /* note: if fb creation failed, we can't rely on fb destroy |
| 118 | * to unref the bo: |
| 119 | */ |
Daniel Vetter | dee2eed | 2015-11-23 10:32:41 +0100 | [diff] [blame] | 120 | drm_gem_object_unreference_unlocked(fbdev->bo); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 121 | ret = PTR_ERR(fb); |
| 122 | goto fail; |
| 123 | } |
| 124 | |
| 125 | mutex_lock(&dev->struct_mutex); |
| 126 | |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 127 | /* |
| 128 | * NOTE: if we can be guaranteed to be able to map buffer |
| 129 | * in panic (ie. lock-safe, etc) we could avoid pinning the |
| 130 | * buffer now: |
| 131 | */ |
| 132 | ret = msm_gem_get_iova_locked(fbdev->bo, 0, &paddr); |
| 133 | if (ret) { |
| 134 | dev_err(dev->dev, "failed to get buffer obj iova: %d\n", ret); |
Wei Yongjun | 0d9509d | 2014-08-14 09:01:40 +0800 | [diff] [blame] | 135 | goto fail_unlock; |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 136 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 137 | |
Archit Taneja | 778014f | 2015-07-22 14:58:08 +0530 | [diff] [blame] | 138 | fbi = drm_fb_helper_alloc_fbi(helper); |
| 139 | if (IS_ERR(fbi)) { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 140 | dev_err(dev->dev, "failed to allocate fb info\n"); |
Archit Taneja | 778014f | 2015-07-22 14:58:08 +0530 | [diff] [blame] | 141 | ret = PTR_ERR(fbi); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 142 | goto fail_unlock; |
| 143 | } |
| 144 | |
| 145 | DBG("fbi=%p, dev=%p", fbi, dev); |
| 146 | |
| 147 | fbdev->fb = fb; |
| 148 | helper->fb = fb; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 149 | |
| 150 | fbi->par = helper; |
| 151 | fbi->flags = FBINFO_DEFAULT; |
| 152 | fbi->fbops = &msm_fb_ops; |
| 153 | |
| 154 | strcpy(fbi->fix.id, "msm"); |
| 155 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 156 | drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth); |
| 157 | drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height); |
| 158 | |
| 159 | dev->mode_config.fb_base = paddr; |
| 160 | |
| 161 | fbi->screen_base = msm_gem_vaddr_locked(fbdev->bo); |
Rob Clark | 69a834c | 2016-05-24 18:29:38 -0400 | [diff] [blame] | 162 | if (IS_ERR(fbi->screen_base)) { |
| 163 | ret = PTR_ERR(fbi->screen_base); |
| 164 | goto fail_unlock; |
| 165 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 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) { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 182 | if (fb) { |
| 183 | drm_framebuffer_unregister_private(fb); |
| 184 | drm_framebuffer_remove(fb); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
Thierry Reding | 3a49387 | 2014-06-27 17:19:23 +0200 | [diff] [blame] | 191 | static const struct drm_fb_helper_funcs msm_fb_helper_funcs = { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 192 | .fb_probe = msm_fbdev_create, |
| 193 | }; |
| 194 | |
| 195 | /* initialize fbdev helper */ |
| 196 | struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev) |
| 197 | { |
| 198 | struct msm_drm_private *priv = dev->dev_private; |
| 199 | struct msm_fbdev *fbdev = NULL; |
| 200 | struct drm_fb_helper *helper; |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 201 | int ret; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 202 | |
| 203 | fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL); |
| 204 | if (!fbdev) |
| 205 | goto fail; |
| 206 | |
| 207 | helper = &fbdev->base; |
| 208 | |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 209 | drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 210 | |
| 211 | ret = drm_fb_helper_init(dev, helper, |
| 212 | priv->num_crtcs, priv->num_connectors); |
| 213 | if (ret) { |
| 214 | dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret); |
| 215 | goto fail; |
| 216 | } |
| 217 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 218 | ret = drm_fb_helper_single_add_all_connectors(helper); |
| 219 | if (ret) |
| 220 | goto fini; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 221 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 222 | ret = drm_fb_helper_initial_config(helper, 32); |
| 223 | if (ret) |
| 224 | goto fini; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 225 | |
| 226 | priv->fbdev = helper; |
| 227 | |
| 228 | return helper; |
| 229 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 230 | fini: |
| 231 | drm_fb_helper_fini(helper); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 232 | fail: |
| 233 | kfree(fbdev); |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | void msm_fbdev_free(struct drm_device *dev) |
| 238 | { |
| 239 | struct msm_drm_private *priv = dev->dev_private; |
| 240 | struct drm_fb_helper *helper = priv->fbdev; |
| 241 | struct msm_fbdev *fbdev; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 242 | |
| 243 | DBG(); |
| 244 | |
Archit Taneja | 778014f | 2015-07-22 14:58:08 +0530 | [diff] [blame] | 245 | drm_fb_helper_unregister_fbi(helper); |
| 246 | drm_fb_helper_release_fbi(helper); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 247 | |
| 248 | drm_fb_helper_fini(helper); |
| 249 | |
| 250 | fbdev = to_msm_fbdev(priv->fbdev); |
| 251 | |
| 252 | /* this will free the backing object */ |
| 253 | if (fbdev->fb) { |
| 254 | drm_framebuffer_unregister_private(fbdev->fb); |
| 255 | drm_framebuffer_remove(fbdev->fb); |
| 256 | } |
| 257 | |
| 258 | kfree(fbdev); |
| 259 | |
| 260 | priv->fbdev = NULL; |
| 261 | } |