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 | |
Masahiro Yamada | 78f27b1 | 2017-04-24 13:50:28 +0900 | [diff] [blame] | 18 | #include <drm/drm_crtc.h> |
| 19 | #include <drm/drm_fb_helper.h> |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 20 | |
Masahiro Yamada | 78f27b1 | 2017-04-24 13:50:28 +0900 | [diff] [blame] | 21 | #include "msm_drv.h" |
Rob Clark | f59f62d | 2017-06-13 10:22:37 -0400 | [diff] [blame] | 22 | #include "msm_kms.h" |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 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; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static struct fb_ops msm_fb_ops = { |
| 40 | .owner = THIS_MODULE, |
Stefan Christ | 00d4406 | 2016-11-14 00:03:24 +0100 | [diff] [blame] | 41 | DRM_FB_HELPER_DEFAULT_OPS, |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 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 | |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 54 | static 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); |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 58 | struct drm_gem_object *bo = msm_framebuffer_bo(fbdev->fb, 0); |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 59 | int ret = 0; |
| 60 | |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 61 | ret = drm_gem_mmap_obj(bo, bo->size, vma); |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 62 | if (ret) { |
| 63 | pr_err("%s:drm_gem_mmap_obj fail\n", __func__); |
| 64 | return ret; |
| 65 | } |
| 66 | |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 67 | return msm_gem_mmap_obj(bo, vma); |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 70 | static 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; |
Rob Clark | f59f62d | 2017-06-13 10:22:37 -0400 | [diff] [blame] | 75 | struct msm_drm_private *priv = dev->dev_private; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 76 | struct drm_framebuffer *fb = NULL; |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 77 | struct drm_gem_object *bo; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 78 | struct fb_info *fbi = NULL; |
Rob Clark | 78babc1 | 2016-11-11 12:06:46 -0500 | [diff] [blame] | 79 | uint64_t paddr; |
Rob Clark | 466e560 | 2017-07-11 10:40:13 -0400 | [diff] [blame] | 80 | uint32_t format; |
| 81 | int ret, pitch; |
| 82 | |
| 83 | format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 84 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 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 | |
Rob Clark | 466e560 | 2017-07-11 10:40:13 -0400 | [diff] [blame] | 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); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 92 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 93 | if (IS_ERR(fb)) { |
| 94 | dev_err(dev->dev, "failed to allocate fb\n"); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 95 | ret = PTR_ERR(fb); |
| 96 | goto fail; |
| 97 | } |
| 98 | |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 99 | bo = msm_framebuffer_bo(fb, 0); |
| 100 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 101 | mutex_lock(&dev->struct_mutex); |
| 102 | |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 103 | /* |
| 104 | * NOTE: if we can be guaranteed to be able to map buffer |
| 105 | * in panic (ie. lock-safe, etc) we could avoid pinning the |
| 106 | * buffer now: |
| 107 | */ |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 108 | ret = msm_gem_get_iova(bo, priv->kms->aspace, &paddr); |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 109 | if (ret) { |
| 110 | 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] | 111 | goto fail_unlock; |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 112 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 113 | |
Archit Taneja | 778014f | 2015-07-22 14:58:08 +0530 | [diff] [blame] | 114 | fbi = drm_fb_helper_alloc_fbi(helper); |
| 115 | if (IS_ERR(fbi)) { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 116 | dev_err(dev->dev, "failed to allocate fb info\n"); |
Archit Taneja | 778014f | 2015-07-22 14:58:08 +0530 | [diff] [blame] | 117 | ret = PTR_ERR(fbi); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 118 | goto fail_unlock; |
| 119 | } |
| 120 | |
| 121 | DBG("fbi=%p, dev=%p", fbi, dev); |
| 122 | |
| 123 | fbdev->fb = fb; |
| 124 | helper->fb = fb; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 125 | |
| 126 | fbi->par = helper; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 127 | fbi->fbops = &msm_fb_ops; |
| 128 | |
| 129 | strcpy(fbi->fix.id, "msm"); |
| 130 | |
Ville Syrjälä | b00c600 | 2016-12-14 23:31:35 +0200 | [diff] [blame] | 131 | drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 132 | drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height); |
| 133 | |
| 134 | dev->mode_config.fb_base = paddr; |
| 135 | |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 136 | fbi->screen_base = msm_gem_get_vaddr(bo); |
Rob Clark | 69a834c | 2016-05-24 18:29:38 -0400 | [diff] [blame] | 137 | if (IS_ERR(fbi->screen_base)) { |
| 138 | ret = PTR_ERR(fbi->screen_base); |
| 139 | goto fail_unlock; |
| 140 | } |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 141 | fbi->screen_size = bo->size; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 142 | fbi->fix.smem_start = paddr; |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 143 | fbi->fix.smem_len = bo->size; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 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 | |
| 152 | fail_unlock: |
| 153 | mutex_unlock(&dev->struct_mutex); |
| 154 | fail: |
| 155 | |
| 156 | if (ret) { |
Daniel Vetter | f5c5d57 | 2016-12-27 11:49:21 +0100 | [diff] [blame] | 157 | if (fb) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 158 | drm_framebuffer_remove(fb); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | return ret; |
| 162 | } |
| 163 | |
Thierry Reding | 3a49387 | 2014-06-27 17:19:23 +0200 | [diff] [blame] | 164 | static const struct drm_fb_helper_funcs msm_fb_helper_funcs = { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 165 | .fb_probe = msm_fbdev_create, |
| 166 | }; |
| 167 | |
| 168 | /* initialize fbdev helper */ |
| 169 | struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev) |
| 170 | { |
| 171 | struct msm_drm_private *priv = dev->dev_private; |
| 172 | struct msm_fbdev *fbdev = NULL; |
| 173 | struct drm_fb_helper *helper; |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 174 | int ret; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 175 | |
| 176 | fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL); |
| 177 | if (!fbdev) |
| 178 | goto fail; |
| 179 | |
| 180 | helper = &fbdev->base; |
| 181 | |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 182 | drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 183 | |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 184 | ret = drm_fb_helper_init(dev, helper, priv->num_connectors); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 185 | if (ret) { |
| 186 | dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret); |
| 187 | goto fail; |
| 188 | } |
| 189 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 190 | ret = drm_fb_helper_single_add_all_connectors(helper); |
| 191 | if (ret) |
| 192 | goto fini; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 193 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 194 | ret = drm_fb_helper_initial_config(helper, 32); |
| 195 | if (ret) |
| 196 | goto fini; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 197 | |
| 198 | priv->fbdev = helper; |
| 199 | |
| 200 | return helper; |
| 201 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 202 | fini: |
| 203 | drm_fb_helper_fini(helper); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 204 | fail: |
| 205 | kfree(fbdev); |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | void msm_fbdev_free(struct drm_device *dev) |
| 210 | { |
| 211 | struct msm_drm_private *priv = dev->dev_private; |
| 212 | struct drm_fb_helper *helper = priv->fbdev; |
| 213 | struct msm_fbdev *fbdev; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 214 | |
| 215 | DBG(); |
| 216 | |
Archit Taneja | 778014f | 2015-07-22 14:58:08 +0530 | [diff] [blame] | 217 | drm_fb_helper_unregister_fbi(helper); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 218 | |
| 219 | drm_fb_helper_fini(helper); |
| 220 | |
| 221 | fbdev = to_msm_fbdev(priv->fbdev); |
| 222 | |
| 223 | /* this will free the backing object */ |
| 224 | if (fbdev->fb) { |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 225 | struct drm_gem_object *bo = |
| 226 | msm_framebuffer_bo(fbdev->fb, 0); |
| 227 | msm_gem_put_vaddr(bo); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 228 | drm_framebuffer_remove(fbdev->fb); |
| 229 | } |
| 230 | |
| 231 | kfree(fbdev); |
| 232 | |
| 233 | priv->fbdev = NULL; |
| 234 | } |