xref: /openbmc/linux/drivers/gpu/drm/msm/msm_fbdev.c (revision 13a9d0be)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6 
7 #include <drm/drm_aperture.h>
8 #include <drm/drm_crtc.h>
9 #include <drm/drm_fb_helper.h>
10 #include <drm/drm_fourcc.h>
11 #include <drm/drm_framebuffer.h>
12 #include <drm/drm_prime.h>
13 
14 #include "msm_drv.h"
15 #include "msm_gem.h"
16 #include "msm_kms.h"
17 
18 static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma);
19 
20 /*
21  * fbdev funcs, to implement legacy fbdev interface on top of drm driver
22  */
23 
24 #define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base)
25 
26 struct msm_fbdev {
27 	struct drm_fb_helper base;
28 	struct drm_framebuffer *fb;
29 };
30 
31 static const struct fb_ops msm_fb_ops = {
32 	.owner = THIS_MODULE,
33 	DRM_FB_HELPER_DEFAULT_OPS,
34 
35 	/* Note: to properly handle manual update displays, we wrap the
36 	 * basic fbdev ops which write to the framebuffer
37 	 */
38 	.fb_read = drm_fb_helper_sys_read,
39 	.fb_write = drm_fb_helper_sys_write,
40 	.fb_fillrect = drm_fb_helper_sys_fillrect,
41 	.fb_copyarea = drm_fb_helper_sys_copyarea,
42 	.fb_imageblit = drm_fb_helper_sys_imageblit,
43 	.fb_mmap = msm_fbdev_mmap,
44 };
45 
46 static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
47 {
48 	struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par;
49 	struct msm_fbdev *fbdev = to_msm_fbdev(helper);
50 	struct drm_gem_object *bo = msm_framebuffer_bo(fbdev->fb, 0);
51 
52 	return drm_gem_prime_mmap(bo, vma);
53 }
54 
55 static int msm_fbdev_create(struct drm_fb_helper *helper,
56 		struct drm_fb_helper_surface_size *sizes)
57 {
58 	struct msm_fbdev *fbdev = to_msm_fbdev(helper);
59 	struct drm_device *dev = helper->dev;
60 	struct msm_drm_private *priv = dev->dev_private;
61 	struct drm_framebuffer *fb = NULL;
62 	struct drm_gem_object *bo;
63 	struct fb_info *fbi = NULL;
64 	uint64_t paddr;
65 	uint32_t format;
66 	int ret, pitch;
67 
68 	format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
69 
70 	DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
71 			sizes->surface_height, sizes->surface_bpp,
72 			sizes->fb_width, sizes->fb_height);
73 
74 	pitch = align_pitch(sizes->surface_width, sizes->surface_bpp);
75 	fb = msm_alloc_stolen_fb(dev, sizes->surface_width,
76 			sizes->surface_height, pitch, format);
77 
78 	if (IS_ERR(fb)) {
79 		DRM_DEV_ERROR(dev->dev, "failed to allocate fb\n");
80 		return PTR_ERR(fb);
81 	}
82 
83 	bo = msm_framebuffer_bo(fb, 0);
84 
85 	/*
86 	 * NOTE: if we can be guaranteed to be able to map buffer
87 	 * in panic (ie. lock-safe, etc) we could avoid pinning the
88 	 * buffer now:
89 	 */
90 	ret = msm_gem_get_and_pin_iova(bo, priv->kms->aspace, &paddr);
91 	if (ret) {
92 		DRM_DEV_ERROR(dev->dev, "failed to get buffer obj iova: %d\n", ret);
93 		goto fail;
94 	}
95 
96 	fbi = drm_fb_helper_alloc_info(helper);
97 	if (IS_ERR(fbi)) {
98 		DRM_DEV_ERROR(dev->dev, "failed to allocate fb info\n");
99 		ret = PTR_ERR(fbi);
100 		goto fail;
101 	}
102 
103 	DBG("fbi=%p, dev=%p", fbi, dev);
104 
105 	fbdev->fb = fb;
106 	helper->fb = fb;
107 
108 	fbi->fbops = &msm_fb_ops;
109 
110 	drm_fb_helper_fill_info(fbi, helper, sizes);
111 
112 	fbi->screen_base = msm_gem_get_vaddr(bo);
113 	if (IS_ERR(fbi->screen_base)) {
114 		ret = PTR_ERR(fbi->screen_base);
115 		goto fail;
116 	}
117 	fbi->screen_size = bo->size;
118 	fbi->fix.smem_start = paddr;
119 	fbi->fix.smem_len = bo->size;
120 
121 	DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
122 	DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
123 
124 	return 0;
125 
126 fail:
127 	drm_framebuffer_remove(fb);
128 	return ret;
129 }
130 
131 static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
132 	.fb_probe = msm_fbdev_create,
133 };
134 
135 /* initialize fbdev helper */
136 struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
137 {
138 	struct msm_drm_private *priv = dev->dev_private;
139 	struct msm_fbdev *fbdev;
140 	struct drm_fb_helper *helper;
141 	int ret;
142 
143 	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
144 	if (!fbdev)
145 		return NULL;
146 
147 	helper = &fbdev->base;
148 
149 	drm_fb_helper_prepare(dev, helper, 32, &msm_fb_helper_funcs);
150 
151 	ret = drm_fb_helper_init(dev, helper);
152 	if (ret) {
153 		DRM_DEV_ERROR(dev->dev, "could not init fbdev: ret=%d\n", ret);
154 		goto fail;
155 	}
156 
157 	/* the fw fb could be anywhere in memory */
158 	ret = drm_aperture_remove_framebuffers(false, dev->driver);
159 	if (ret)
160 		goto fini;
161 
162 	ret = drm_fb_helper_initial_config(helper);
163 	if (ret)
164 		goto fini;
165 
166 	priv->fbdev = helper;
167 
168 	return helper;
169 
170 fini:
171 	drm_fb_helper_fini(helper);
172 fail:
173 	drm_fb_helper_unprepare(helper);
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;
183 
184 	DBG();
185 
186 	drm_fb_helper_unregister_info(helper);
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) {
194 		struct drm_gem_object *bo =
195 			msm_framebuffer_bo(fbdev->fb, 0);
196 		msm_gem_put_vaddr(bo);
197 		drm_framebuffer_remove(fbdev->fb);
198 	}
199 
200 	drm_fb_helper_unprepare(helper);
201 	kfree(fbdev);
202 
203 	priv->fbdev = NULL;
204 }
205