xref: /linux/drivers/gpu/drm/exynos/exynos_drm_fbdev.c (revision b9bb45aeaf028450c753782280922d31120b342a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* exynos_drm_fbdev.c
3  *
4  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5  * Authors:
6  *	Inki Dae <inki.dae@samsung.com>
7  *	Joonyoung Shim <jy0922.shim@samsung.com>
8  *	Seung-Woo Kim <sw0312.kim@samsung.com>
9  */
10 
11 #include <linux/fb.h>
12 
13 #include <drm/drm_crtc_helper.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_fb_helper.h>
16 #include <drm/drm_framebuffer.h>
17 #include <drm/drm_gem_framebuffer_helper.h>
18 #include <drm/drm_prime.h>
19 #include <drm/drm_print.h>
20 #include <drm/exynos_drm.h>
21 
22 #include "exynos_drm_drv.h"
23 #include "exynos_drm_fb.h"
24 #include "exynos_drm_fbdev.h"
25 #include "exynos_drm_gem.h"
26 
27 #define MAX_CONNECTOR		4
28 
29 static int exynos_drm_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
30 {
31 	struct drm_fb_helper *helper = info->par;
32 	struct drm_gem_object *obj = drm_gem_fb_get_obj(helper->fb, 0);
33 
34 	return drm_gem_prime_mmap(obj, vma);
35 }
36 
37 static void exynos_drm_fb_destroy(struct fb_info *info)
38 {
39 	struct drm_fb_helper *fb_helper = info->par;
40 
41 	drm_fb_helper_fini(fb_helper);
42 
43 	drm_client_buffer_delete(fb_helper->buffer);
44 	drm_client_release(&fb_helper->client);
45 }
46 
47 static const struct fb_ops exynos_drm_fb_ops = {
48 	.owner		= THIS_MODULE,
49 	__FB_DEFAULT_DMAMEM_OPS_RDWR,
50 	DRM_FB_HELPER_DEFAULT_OPS,
51 	__FB_DEFAULT_DMAMEM_OPS_DRAW,
52 	.fb_mmap        = exynos_drm_fb_mmap,
53 	.fb_destroy	= exynos_drm_fb_destroy,
54 };
55 
56 static const struct drm_fb_helper_funcs exynos_drm_fbdev_helper_funcs = {
57 };
58 
59 int exynos_drm_fbdev_driver_fbdev_probe(struct drm_fb_helper *helper,
60 					struct drm_fb_helper_surface_size *sizes)
61 {
62 	struct drm_client_dev *client = &helper->client;
63 	struct drm_device *dev = client->dev;
64 	struct drm_file *file = client->file;
65 	struct fb_info *info = helper->info;
66 	u32 fourcc, pitch;
67 	u64 size;
68 	const struct drm_format_info *format;
69 	struct exynos_drm_gem *exynos_gem;
70 	struct drm_gem_object *obj;
71 	struct drm_client_buffer *buffer;
72 	u32 handle;
73 	int ret;
74 
75 	DRM_DEV_DEBUG_KMS(dev->dev,
76 			  "surface width(%d), height(%d) and bpp(%d\n",
77 			  sizes->surface_width, sizes->surface_height,
78 			  sizes->surface_bpp);
79 
80 	fourcc = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
81 	if (fourcc == DRM_FORMAT_INVALID)
82 		return -EINVAL;
83 	format = drm_get_format_info(dev, fourcc, DRM_FORMAT_MOD_LINEAR);
84 	if (!format)
85 		return -EINVAL;
86 	pitch = drm_format_info_min_pitch(format, 0, sizes->surface_width);
87 	if (!pitch)
88 		return -EINVAL;
89 	if (check_mul_overflow(pitch, sizes->surface_height, &size))
90 		return -EINVAL;
91 	size = ALIGN(size, PAGE_SIZE);
92 	if (size < PAGE_SIZE)
93 		return -EINVAL;
94 
95 	exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_WC, size, true);
96 	if (IS_ERR(exynos_gem))
97 		return PTR_ERR(exynos_gem);
98 	obj = &exynos_gem->base;
99 
100 	ret = drm_gem_handle_create(file, obj, &handle);
101 	if (ret)
102 		goto err_drm_gem_object_put;
103 
104 	buffer = drm_client_buffer_create(client, sizes->surface_width, sizes->surface_height,
105 					  fourcc, handle, pitch);
106 	if (IS_ERR(buffer)) {
107 		ret = PTR_ERR(buffer);
108 		goto err_drm_gem_handle_delete;
109 	}
110 
111 	helper->funcs = &exynos_drm_fbdev_helper_funcs;
112 	helper->buffer = buffer;
113 	helper->fb = buffer->fb;
114 
115 	info->fbops = &exynos_drm_fb_ops;
116 
117 	drm_fb_helper_fill_info(info, helper, sizes);
118 
119 	info->flags |= FBINFO_VIRTFB;
120 	info->screen_buffer = exynos_gem->kvaddr;
121 	info->screen_size = obj->size;
122 	info->fix.smem_len = obj->size;
123 
124 	/* The handle is only needed for creating the framebuffer. */
125 	drm_gem_handle_delete(file, handle);
126 
127 	/* The framebuffer still holds a reference on the GEM object. */
128 	drm_gem_object_put(obj);
129 
130 	return 0;
131 
132 err_drm_gem_handle_delete:
133 	drm_gem_handle_delete(file, handle);
134 err_drm_gem_object_put:
135 	drm_gem_object_put(obj);
136 	return ret;
137 }
138