Linux Audio

Check our new training course

Loading...
v4.6
 
  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"
 22#include "msm_gem.h"
 
 23
 24extern int msm_gem_mmap_obj(struct drm_gem_object *obj,
 25					struct vm_area_struct *vma);
 26static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma);
 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
 34struct msm_fbdev {
 35	struct drm_fb_helper base;
 36	struct drm_framebuffer *fb;
 37	struct drm_gem_object *bo;
 38};
 39
 40static 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	 */
 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,
 51	.fb_mmap = msm_fbdev_mmap,
 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
 60static 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;
 65	int ret = 0;
 66
 67	ret = drm_gem_mmap_obj(drm_obj, drm_obj->size, vma);
 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
 
 
 
 
 
 
 
 
 
 76static 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};
 84	uint32_t paddr;
 85	int ret, size;
 
 
 86
 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);
104	fbdev->bo = msm_gem_new(dev, size, MSM_BO_SCANOUT |
105			MSM_BO_WC | MSM_BO_STOLEN);
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		 */
120		drm_gem_object_unreference_unlocked(fbdev->bo);
121		ret = PTR_ERR(fb);
122		goto fail;
123	}
124
125	mutex_lock(&dev->struct_mutex);
126
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);
135		goto fail_unlock;
136	}
137
138	fbi = drm_fb_helper_alloc_fbi(helper);
139	if (IS_ERR(fbi)) {
140		dev_err(dev->dev, "failed to allocate fb info\n");
141		ret = PTR_ERR(fbi);
142		goto fail_unlock;
143	}
144
145	DBG("fbi=%p, dev=%p", fbi, dev);
146
147	fbdev->fb = fb;
148	helper->fb = fb;
149
150	fbi->par = helper;
151	fbi->flags = FBINFO_DEFAULT;
152	fbi->fbops = &msm_fb_ops;
153
154	strcpy(fbi->fix.id, "msm");
155
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);
162	fbi->screen_size = fbdev->bo->size;
 
 
163	fbi->fix.smem_start = paddr;
164	fbi->fix.smem_len = fbdev->bo->size;
165
166	DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
167	DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
168
169	mutex_unlock(&dev->struct_mutex);
170
171	return 0;
172
173fail_unlock:
174	mutex_unlock(&dev->struct_mutex);
175fail:
176
177	if (ret) {
178		if (fb) {
179			drm_framebuffer_unregister_private(fb);
180			drm_framebuffer_remove(fb);
181		}
182	}
183
184	return ret;
185}
186
187static void msm_crtc_fb_gamma_set(struct drm_crtc *crtc,
188		u16 red, u16 green, u16 blue, int regno)
189{
190	DBG("fbdev: set gamma");
191}
192
193static void msm_crtc_fb_gamma_get(struct drm_crtc *crtc,
194		u16 *red, u16 *green, u16 *blue, int regno)
195{
196	DBG("fbdev: get gamma");
 
 
 
 
 
 
 
197}
198
199static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
200	.gamma_set = msm_crtc_fb_gamma_set,
201	.gamma_get = msm_crtc_fb_gamma_get,
202	.fb_probe = msm_fbdev_create,
 
203};
204
205/* initialize fbdev helper */
206struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
 
 
 
207{
208	struct msm_drm_private *priv = dev->dev_private;
209	struct msm_fbdev *fbdev = NULL;
210	struct drm_fb_helper *helper;
211	int ret;
212
213	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
214	if (!fbdev)
215		goto fail;
 
 
 
 
 
216
217	helper = &fbdev->base;
 
 
218
219	drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
 
220
221	ret = drm_fb_helper_init(dev, helper,
222			priv->num_crtcs, priv->num_connectors);
223	if (ret) {
224		dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
225		goto fail;
226	}
227
228	ret = drm_fb_helper_single_add_all_connectors(helper);
229	if (ret)
230		goto fini;
231
232	ret = drm_fb_helper_initial_config(helper, 32);
233	if (ret)
234		goto fini;
235
236	priv->fbdev = helper;
 
237
238	return helper;
 
 
239
240fini:
241	drm_fb_helper_fini(helper);
242fail:
243	kfree(fbdev);
244	return NULL;
 
 
245}
246
247void msm_fbdev_free(struct drm_device *dev)
248{
249	struct msm_drm_private *priv = dev->dev_private;
250	struct drm_fb_helper *helper = priv->fbdev;
251	struct msm_fbdev *fbdev;
 
252
253	DBG();
 
 
 
 
254
255	drm_fb_helper_unregister_fbi(helper);
256	drm_fb_helper_release_fbi(helper);
257
258	drm_fb_helper_fini(helper);
 
259
260	fbdev = to_msm_fbdev(priv->fbdev);
 
 
 
261
262	/* this will free the backing object */
263	if (fbdev->fb) {
264		drm_framebuffer_unregister_private(fbdev->fb);
265		drm_framebuffer_remove(fbdev->fb);
266	}
267
268	kfree(fbdev);
 
 
269
270	priv->fbdev = NULL;
 
 
271}
v6.8
  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
 20static bool fbdev = true;
 21MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
 22module_param(fbdev, bool, 0600);
 23
 24/*
 25 * fbdev funcs, to implement legacy fbdev interface on top of drm driver
 26 */
 27
 28FB_GEN_DEFAULT_DEFERRED_SYSMEM_OPS(msm_fbdev,
 29				   drm_fb_helper_damage_range,
 30				   drm_fb_helper_damage_area)
 
 
 
 
 
 
 
 31
 32static 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
 40static 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
 59static 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
 68static 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
 
 
137fail:
138	drm_framebuffer_remove(fb);
 
 
 
 
 
 
 
139	return ret;
140}
141
142static int msm_fbdev_fb_dirty(struct drm_fb_helper *helper,
143			      struct drm_clip_rect *clip)
144{
145	struct drm_device *dev = helper->dev;
146	int ret;
147
148	/* Call damage handlers only if necessary */
149	if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
150		return 0;
151
152	if (helper->fb->funcs->dirty) {
153		ret = helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
154		if (drm_WARN_ONCE(dev, ret, "Dirty helper failed: ret=%d\n", ret))
155			return ret;
156	}
157
158	return 0;
159}
160
161static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
 
 
162	.fb_probe = msm_fbdev_create,
163	.fb_dirty = msm_fbdev_fb_dirty,
164};
165
166/*
167 * struct drm_client
168 */
169
170static void msm_fbdev_client_unregister(struct drm_client_dev *client)
171{
172	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
 
 
 
173
174	if (fb_helper->info) {
175		drm_fb_helper_unregister_info(fb_helper);
176	} else {
177		drm_client_release(&fb_helper->client);
178		drm_fb_helper_unprepare(fb_helper);
179		kfree(fb_helper);
180	}
181}
182
183static int msm_fbdev_client_restore(struct drm_client_dev *client)
184{
185	drm_fb_helper_lastclose(client->dev);
186
187	return 0;
188}
189
190static int msm_fbdev_client_hotplug(struct drm_client_dev *client)
191{
192	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
193	struct drm_device *dev = client->dev;
194	int ret;
 
195
196	if (dev->fb_helper)
197		return drm_fb_helper_hotplug_event(dev->fb_helper);
 
198
199	ret = drm_fb_helper_init(dev, fb_helper);
200	if (ret)
201		goto err_drm_err;
202
203	if (!drm_drv_uses_atomic_modeset(dev))
204		drm_helper_disable_unused_functions(dev);
205
206	ret = drm_fb_helper_initial_config(fb_helper);
207	if (ret)
208		goto err_drm_fb_helper_fini;
209
210	return 0;
211
212err_drm_fb_helper_fini:
213	drm_fb_helper_fini(fb_helper);
214err_drm_err:
215	drm_err(dev, "Failed to setup fbdev emulation (ret=%d)\n", ret);
216	return ret;
217}
218
219static const struct drm_client_funcs msm_fbdev_client_funcs = {
220	.owner		= THIS_MODULE,
221	.unregister	= msm_fbdev_client_unregister,
222	.restore	= msm_fbdev_client_restore,
223	.hotplug	= msm_fbdev_client_hotplug,
224};
225
226/* initialize fbdev helper */
227void msm_fbdev_setup(struct drm_device *dev)
228{
229	struct drm_fb_helper *helper;
230	int ret;
231
232	if (!fbdev)
233		return;
234
235	drm_WARN(dev, !dev->registered, "Device has not been registered.\n");
236	drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n");
237
238	helper = kzalloc(sizeof(*helper), GFP_KERNEL);
239	if (!helper)
240		return;
241	drm_fb_helper_prepare(dev, helper, 32, &msm_fb_helper_funcs);
242
243	ret = drm_client_init(dev, &helper->client, "fbdev", &msm_fbdev_client_funcs);
244	if (ret) {
245		drm_err(dev, "Failed to register client: %d\n", ret);
246		goto err_drm_fb_helper_unprepare;
247	}
248
249	drm_client_register(&helper->client);
250
251	return;
252
253err_drm_fb_helper_unprepare:
254	drm_fb_helper_unprepare(helper);
255	kfree(helper);
256}