Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Red Hat |
| 4 | * Author: Rob Clark <robdclark@gmail.com> |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
Thomas Zimmermann | 6848c29 | 2021-04-12 15:10:42 +0200 | [diff] [blame] | 7 | #include <drm/drm_aperture.h> |
Masahiro Yamada | 78f27b1 | 2017-04-24 13:50:28 +0900 | [diff] [blame] | 8 | #include <drm/drm_crtc.h> |
| 9 | #include <drm/drm_fb_helper.h> |
Sam Ravnborg | feea39a | 2019-08-04 08:55:51 +0200 | [diff] [blame] | 10 | #include <drm/drm_fourcc.h> |
Thomas Zimmermann | 510410b | 2021-07-06 10:47:53 +0200 | [diff] [blame] | 11 | #include <drm/drm_prime.h> |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 12 | |
Masahiro Yamada | 78f27b1 | 2017-04-24 13:50:28 +0900 | [diff] [blame] | 13 | #include "msm_drv.h" |
Rob Clark | 8f64237 | 2020-10-23 09:51:05 -0700 | [diff] [blame] | 14 | #include "msm_gem.h" |
Rob Clark | f59f62d | 2017-06-13 10:22:37 -0400 | [diff] [blame] | 15 | #include "msm_kms.h" |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 16 | |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 17 | 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] | 18 | |
| 19 | /* |
| 20 | * fbdev funcs, to implement legacy fbdev interface on top of drm driver |
| 21 | */ |
| 22 | |
| 23 | #define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base) |
| 24 | |
| 25 | struct msm_fbdev { |
| 26 | struct drm_fb_helper base; |
| 27 | struct drm_framebuffer *fb; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 28 | }; |
| 29 | |
Jani Nikula | b6ff753 | 2019-12-03 18:38:48 +0200 | [diff] [blame] | 30 | static const struct fb_ops msm_fb_ops = { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 31 | .owner = THIS_MODULE, |
Stefan Christ | 00d4406 | 2016-11-14 00:03:24 +0100 | [diff] [blame] | 32 | DRM_FB_HELPER_DEFAULT_OPS, |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 33 | |
| 34 | /* Note: to properly handle manual update displays, we wrap the |
| 35 | * basic fbdev ops which write to the framebuffer |
| 36 | */ |
Archit Taneja | 778014f | 2015-07-22 14:58:08 +0530 | [diff] [blame] | 37 | .fb_read = drm_fb_helper_sys_read, |
| 38 | .fb_write = drm_fb_helper_sys_write, |
| 39 | .fb_fillrect = drm_fb_helper_sys_fillrect, |
| 40 | .fb_copyarea = drm_fb_helper_sys_copyarea, |
| 41 | .fb_imageblit = drm_fb_helper_sys_imageblit, |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 42 | .fb_mmap = msm_fbdev_mmap, |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 43 | }; |
| 44 | |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 45 | static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma) |
| 46 | { |
| 47 | struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par; |
| 48 | struct msm_fbdev *fbdev = to_msm_fbdev(helper); |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 49 | struct drm_gem_object *bo = msm_framebuffer_bo(fbdev->fb, 0); |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 50 | |
Thomas Zimmermann | 510410b | 2021-07-06 10:47:53 +0200 | [diff] [blame] | 51 | return drm_gem_prime_mmap(bo, vma); |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 52 | } |
| 53 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 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; |
Rob Clark | f59f62d | 2017-06-13 10:22:37 -0400 | [diff] [blame] | 59 | struct msm_drm_private *priv = dev->dev_private; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 60 | struct drm_framebuffer *fb = NULL; |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 61 | struct drm_gem_object *bo; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 62 | struct fb_info *fbi = NULL; |
Rob Clark | 78babc1 | 2016-11-11 12:06:46 -0500 | [diff] [blame] | 63 | uint64_t paddr; |
Rob Clark | 466e560 | 2017-07-11 10:40:13 -0400 | [diff] [blame] | 64 | uint32_t format; |
| 65 | int ret, pitch; |
| 66 | |
| 67 | format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 68 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 69 | DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width, |
| 70 | sizes->surface_height, sizes->surface_bpp, |
| 71 | sizes->fb_width, sizes->fb_height); |
| 72 | |
Rob Clark | 466e560 | 2017-07-11 10:40:13 -0400 | [diff] [blame] | 73 | pitch = align_pitch(sizes->surface_width, sizes->surface_bpp); |
| 74 | fb = msm_alloc_stolen_fb(dev, sizes->surface_width, |
| 75 | sizes->surface_height, pitch, format); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 76 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 77 | if (IS_ERR(fb)) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 78 | DRM_DEV_ERROR(dev->dev, "failed to allocate fb\n"); |
Emil Velikov | 789d4c3 | 2018-03-28 17:22:16 +0100 | [diff] [blame] | 79 | return PTR_ERR(fb); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 82 | bo = msm_framebuffer_bo(fb, 0); |
| 83 | |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 84 | /* |
| 85 | * NOTE: if we can be guaranteed to be able to map buffer |
| 86 | * in panic (ie. lock-safe, etc) we could avoid pinning the |
| 87 | * buffer now: |
| 88 | */ |
Jordan Crouse | 9fe041f | 2018-11-07 15:35:50 -0700 | [diff] [blame] | 89 | ret = msm_gem_get_and_pin_iova(bo, priv->kms->aspace, &paddr); |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 90 | if (ret) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 91 | DRM_DEV_ERROR(dev->dev, "failed to get buffer obj iova: %d\n", ret); |
Rob Clark | d8c00a8 | 2021-11-09 10:11:01 -0800 | [diff] [blame] | 92 | goto fail; |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 93 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 94 | |
Archit Taneja | 778014f | 2015-07-22 14:58:08 +0530 | [diff] [blame] | 95 | fbi = drm_fb_helper_alloc_fbi(helper); |
| 96 | if (IS_ERR(fbi)) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 97 | DRM_DEV_ERROR(dev->dev, "failed to allocate fb info\n"); |
Archit Taneja | 778014f | 2015-07-22 14:58:08 +0530 | [diff] [blame] | 98 | ret = PTR_ERR(fbi); |
Rob Clark | d8c00a8 | 2021-11-09 10:11:01 -0800 | [diff] [blame] | 99 | goto fail; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | DBG("fbi=%p, dev=%p", fbi, dev); |
| 103 | |
| 104 | fbdev->fb = fb; |
| 105 | helper->fb = fb; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 106 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 107 | fbi->fbops = &msm_fb_ops; |
| 108 | |
Daniel Vetter | d4eae71 | 2019-03-26 14:20:00 +0100 | [diff] [blame] | 109 | drm_fb_helper_fill_info(fbi, helper, sizes); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 110 | |
| 111 | dev->mode_config.fb_base = paddr; |
| 112 | |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 113 | fbi->screen_base = msm_gem_get_vaddr(bo); |
Rob Clark | 69a834c | 2016-05-24 18:29:38 -0400 | [diff] [blame] | 114 | if (IS_ERR(fbi->screen_base)) { |
| 115 | ret = PTR_ERR(fbi->screen_base); |
Rob Clark | d8c00a8 | 2021-11-09 10:11:01 -0800 | [diff] [blame] | 116 | goto fail; |
Rob Clark | 69a834c | 2016-05-24 18:29:38 -0400 | [diff] [blame] | 117 | } |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 118 | fbi->screen_size = bo->size; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 119 | fbi->fix.smem_start = paddr; |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 120 | fbi->fix.smem_len = bo->size; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 121 | |
| 122 | DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres); |
| 123 | DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height); |
| 124 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 125 | return 0; |
| 126 | |
Rob Clark | d8c00a8 | 2021-11-09 10:11:01 -0800 | [diff] [blame] | 127 | fail: |
Emil Velikov | 789d4c3 | 2018-03-28 17:22:16 +0100 | [diff] [blame] | 128 | drm_framebuffer_remove(fb); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 129 | return ret; |
| 130 | } |
| 131 | |
Thierry Reding | 3a49387 | 2014-06-27 17:19:23 +0200 | [diff] [blame] | 132 | static const struct drm_fb_helper_funcs msm_fb_helper_funcs = { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 133 | .fb_probe = msm_fbdev_create, |
| 134 | }; |
| 135 | |
| 136 | /* initialize fbdev helper */ |
| 137 | struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev) |
| 138 | { |
| 139 | struct msm_drm_private *priv = dev->dev_private; |
| 140 | struct msm_fbdev *fbdev = NULL; |
| 141 | struct drm_fb_helper *helper; |
Hai Li | 8f67da3 | 2014-06-18 16:55:27 -0400 | [diff] [blame] | 142 | int ret; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 143 | |
| 144 | fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL); |
| 145 | if (!fbdev) |
| 146 | goto fail; |
| 147 | |
| 148 | helper = &fbdev->base; |
| 149 | |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 150 | drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 151 | |
Pankaj Bharadiya | 2dea2d1 | 2020-03-05 17:34:28 +0530 | [diff] [blame] | 152 | ret = drm_fb_helper_init(dev, helper); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 153 | if (ret) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 154 | DRM_DEV_ERROR(dev->dev, "could not init fbdev: ret=%d\n", ret); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 155 | goto fail; |
| 156 | } |
| 157 | |
Jeffrey Hugo | 23c2597 | 2019-06-28 09:28:31 -0700 | [diff] [blame] | 158 | /* the fw fb could be anywhere in memory */ |
Thomas Zimmermann | 97c9bfe | 2021-06-29 15:58:33 +0200 | [diff] [blame] | 159 | ret = drm_aperture_remove_framebuffers(false, dev->driver); |
Thomas Zimmermann | 6848c29 | 2021-04-12 15:10:42 +0200 | [diff] [blame] | 160 | if (ret) |
| 161 | goto fini; |
Jeffrey Hugo | 23c2597 | 2019-06-28 09:28:31 -0700 | [diff] [blame] | 162 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 163 | ret = drm_fb_helper_initial_config(helper, 32); |
| 164 | if (ret) |
| 165 | goto fini; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 166 | |
| 167 | priv->fbdev = helper; |
| 168 | |
| 169 | return helper; |
| 170 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 171 | fini: |
| 172 | drm_fb_helper_fini(helper); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 173 | fail: |
| 174 | kfree(fbdev); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | void msm_fbdev_free(struct drm_device *dev) |
| 179 | { |
| 180 | struct msm_drm_private *priv = dev->dev_private; |
| 181 | struct drm_fb_helper *helper = priv->fbdev; |
| 182 | struct msm_fbdev *fbdev; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 183 | |
| 184 | DBG(); |
| 185 | |
Archit Taneja | 778014f | 2015-07-22 14:58:08 +0530 | [diff] [blame] | 186 | drm_fb_helper_unregister_fbi(helper); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 187 | |
| 188 | drm_fb_helper_fini(helper); |
| 189 | |
| 190 | fbdev = to_msm_fbdev(priv->fbdev); |
| 191 | |
| 192 | /* this will free the backing object */ |
| 193 | if (fbdev->fb) { |
Rob Clark | 38958ba | 2017-07-11 10:13:29 -0400 | [diff] [blame] | 194 | struct drm_gem_object *bo = |
| 195 | msm_framebuffer_bo(fbdev->fb, 0); |
| 196 | msm_gem_put_vaddr(bo); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 197 | drm_framebuffer_remove(fbdev->fb); |
| 198 | } |
| 199 | |
| 200 | kfree(fbdev); |
| 201 | |
| 202 | priv->fbdev = NULL; |
| 203 | } |