xref: /openbmc/linux/drivers/gpu/drm/msm/msm_fbdev.c (revision e8001973)
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 <linux/fb.h>
8 
9 #include <drm/drm_drv.h>
10 #include <drm/drm_crtc_helper.h>
11 #include <drm/drm_fb_helper.h>
12 #include <drm/drm_fourcc.h>
13 #include <drm/drm_framebuffer.h>
14 #include <drm/drm_prime.h>
15 
16 #include "msm_drv.h"
17 #include "msm_gem.h"
18 #include "msm_kms.h"
19 
20 static bool fbdev = true;
21 MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
22 module_param(fbdev, bool, 0600);
23 
24 /*
25  * fbdev funcs, to implement legacy fbdev interface on top of drm driver
26  */
27 
28 FB_GEN_DEFAULT_DEFERRED_SYS_OPS(msm_fbdev,
29 				drm_fb_helper_damage_range,
30 				drm_fb_helper_damage_area)
31 
32 static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
33 {
34 	struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par;
35 	struct drm_gem_object *bo = msm_framebuffer_bo(helper->fb, 0);
36 
37 	return drm_gem_prime_mmap(bo, vma);
38 }
39 
40 static void msm_fbdev_fb_destroy(struct fb_info *info)
41 {
42 	struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par;
43 	struct drm_framebuffer *fb = helper->fb;
44 	struct drm_gem_object *bo = msm_framebuffer_bo(fb, 0);
45 
46 	DBG();
47 
48 	drm_fb_helper_fini(helper);
49 
50 	/* this will free the backing object */
51 	msm_gem_put_vaddr(bo);
52 	drm_framebuffer_remove(fb);
53 
54 	drm_client_release(&helper->client);
55 	drm_fb_helper_unprepare(helper);
56 	kfree(helper);
57 }
58 
59 static const struct fb_ops msm_fb_ops = {
60 	.owner = THIS_MODULE,
61 	__FB_DEFAULT_DEFERRED_OPS_RDWR(msm_fbdev),
62 	DRM_FB_HELPER_DEFAULT_OPS,
63 	__FB_DEFAULT_DEFERRED_OPS_DRAW(msm_fbdev),
64 	.fb_mmap = msm_fbdev_mmap,
65 	.fb_destroy = msm_fbdev_fb_destroy,
66 };
67 
68 static int msm_fbdev_create(struct drm_fb_helper *helper,
69 		struct drm_fb_helper_surface_size *sizes)
70 {
71 	struct drm_device *dev = helper->dev;
72 	struct msm_drm_private *priv = dev->dev_private;
73 	struct drm_framebuffer *fb = NULL;
74 	struct drm_gem_object *bo;
75 	struct fb_info *fbi = NULL;
76 	uint64_t paddr;
77 	uint32_t format;
78 	int ret, pitch;
79 
80 	format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
81 
82 	DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
83 			sizes->surface_height, sizes->surface_bpp,
84 			sizes->fb_width, sizes->fb_height);
85 
86 	pitch = align_pitch(sizes->surface_width, sizes->surface_bpp);
87 	fb = msm_alloc_stolen_fb(dev, sizes->surface_width,
88 			sizes->surface_height, pitch, format);
89 
90 	if (IS_ERR(fb)) {
91 		DRM_DEV_ERROR(dev->dev, "failed to allocate fb\n");
92 		return PTR_ERR(fb);
93 	}
94 
95 	bo = msm_framebuffer_bo(fb, 0);
96 
97 	/*
98 	 * NOTE: if we can be guaranteed to be able to map buffer
99 	 * in panic (ie. lock-safe, etc) we could avoid pinning the
100 	 * buffer now:
101 	 */
102 	ret = msm_gem_get_and_pin_iova(bo, priv->kms->aspace, &paddr);
103 	if (ret) {
104 		DRM_DEV_ERROR(dev->dev, "failed to get buffer obj iova: %d\n", ret);
105 		goto fail;
106 	}
107 
108 	fbi = drm_fb_helper_alloc_info(helper);
109 	if (IS_ERR(fbi)) {
110 		DRM_DEV_ERROR(dev->dev, "failed to allocate fb info\n");
111 		ret = PTR_ERR(fbi);
112 		goto fail;
113 	}
114 
115 	DBG("fbi=%p, dev=%p", fbi, dev);
116 
117 	helper->fb = fb;
118 
119 	fbi->fbops = &msm_fb_ops;
120 
121 	drm_fb_helper_fill_info(fbi, helper, sizes);
122 
123 	fbi->screen_buffer = msm_gem_get_vaddr(bo);
124 	if (IS_ERR(fbi->screen_buffer)) {
125 		ret = PTR_ERR(fbi->screen_buffer);
126 		goto fail;
127 	}
128 	fbi->screen_size = bo->size;
129 	fbi->fix.smem_start = paddr;
130 	fbi->fix.smem_len = bo->size;
131 
132 	DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
133 	DBG("allocated %dx%d fb", fb->width, fb->height);
134 
135 	return 0;
136 
137 fail:
138 	drm_framebuffer_remove(fb);
139 	return ret;
140 }
141 
142 static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
143 	.fb_probe = msm_fbdev_create,
144 };
145 
146 /*
147  * struct drm_client
148  */
149 
150 static void msm_fbdev_client_unregister(struct drm_client_dev *client)
151 {
152 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
153 
154 	if (fb_helper->info) {
155 		drm_fb_helper_unregister_info(fb_helper);
156 	} else {
157 		drm_client_release(&fb_helper->client);
158 		drm_fb_helper_unprepare(fb_helper);
159 		kfree(fb_helper);
160 	}
161 }
162 
163 static int msm_fbdev_client_restore(struct drm_client_dev *client)
164 {
165 	drm_fb_helper_lastclose(client->dev);
166 
167 	return 0;
168 }
169 
170 static int msm_fbdev_client_hotplug(struct drm_client_dev *client)
171 {
172 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
173 	struct drm_device *dev = client->dev;
174 	int ret;
175 
176 	if (dev->fb_helper)
177 		return drm_fb_helper_hotplug_event(dev->fb_helper);
178 
179 	ret = drm_fb_helper_init(dev, fb_helper);
180 	if (ret)
181 		goto err_drm_err;
182 
183 	if (!drm_drv_uses_atomic_modeset(dev))
184 		drm_helper_disable_unused_functions(dev);
185 
186 	ret = drm_fb_helper_initial_config(fb_helper);
187 	if (ret)
188 		goto err_drm_fb_helper_fini;
189 
190 	return 0;
191 
192 err_drm_fb_helper_fini:
193 	drm_fb_helper_fini(fb_helper);
194 err_drm_err:
195 	drm_err(dev, "Failed to setup fbdev emulation (ret=%d)\n", ret);
196 	return ret;
197 }
198 
199 static const struct drm_client_funcs msm_fbdev_client_funcs = {
200 	.owner		= THIS_MODULE,
201 	.unregister	= msm_fbdev_client_unregister,
202 	.restore	= msm_fbdev_client_restore,
203 	.hotplug	= msm_fbdev_client_hotplug,
204 };
205 
206 /* initialize fbdev helper */
207 void msm_fbdev_setup(struct drm_device *dev)
208 {
209 	struct drm_fb_helper *helper;
210 	int ret;
211 
212 	if (!fbdev)
213 		return;
214 
215 	drm_WARN(dev, !dev->registered, "Device has not been registered.\n");
216 	drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n");
217 
218 	helper = kzalloc(sizeof(*helper), GFP_KERNEL);
219 	if (!helper)
220 		return;
221 	drm_fb_helper_prepare(dev, helper, 32, &msm_fb_helper_funcs);
222 
223 	ret = drm_client_init(dev, &helper->client, "fbdev", &msm_fbdev_client_funcs);
224 	if (ret) {
225 		drm_err(dev, "Failed to register client: %d\n", ret);
226 		goto err_drm_fb_helper_unprepare;
227 	}
228 
229 	ret = msm_fbdev_client_hotplug(&helper->client);
230 	if (ret)
231 		drm_dbg_kms(dev, "client hotplug ret=%d\n", ret);
232 
233 	drm_client_register(&helper->client);
234 
235 	return;
236 
237 err_drm_fb_helper_unprepare:
238 	drm_fb_helper_unprepare(helper);
239 	kfree(helper);
240 }
241