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 <drm/drmP.h>
12 #include <drm/drm_crtc.h>
13 #include <drm/drm_fb_helper.h>
14 #include <drm/drm_probe_helper.h>
15 #include <drm/exynos_drm.h>
16 
17 #include <linux/console.h>
18 
19 #include "exynos_drm_drv.h"
20 #include "exynos_drm_fb.h"
21 #include "exynos_drm_fbdev.h"
22 
23 #define MAX_CONNECTOR		4
24 #define PREFERRED_BPP		32
25 
26 #define to_exynos_fbdev(x)	container_of(x, struct exynos_drm_fbdev,\
27 				drm_fb_helper)
28 
29 struct exynos_drm_fbdev {
30 	struct drm_fb_helper	drm_fb_helper;
31 	struct exynos_drm_gem	*exynos_gem;
32 };
33 
34 static int exynos_drm_fb_mmap(struct fb_info *info,
35 			struct vm_area_struct *vma)
36 {
37 	struct drm_fb_helper *helper = info->par;
38 	struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper);
39 	struct exynos_drm_gem *exynos_gem = exynos_fbd->exynos_gem;
40 	unsigned long vm_size;
41 	int ret;
42 
43 	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
44 
45 	vm_size = vma->vm_end - vma->vm_start;
46 
47 	if (vm_size > exynos_gem->size)
48 		return -EINVAL;
49 
50 	ret = dma_mmap_attrs(to_dma_dev(helper->dev), vma, exynos_gem->cookie,
51 			     exynos_gem->dma_addr, exynos_gem->size,
52 			     exynos_gem->dma_attrs);
53 	if (ret < 0) {
54 		DRM_DEV_ERROR(to_dma_dev(helper->dev), "failed to mmap.\n");
55 		return ret;
56 	}
57 
58 	return 0;
59 }
60 
61 static struct fb_ops exynos_drm_fb_ops = {
62 	.owner		= THIS_MODULE,
63 	DRM_FB_HELPER_DEFAULT_OPS,
64 	.fb_mmap        = exynos_drm_fb_mmap,
65 	.fb_fillrect	= drm_fb_helper_cfb_fillrect,
66 	.fb_copyarea	= drm_fb_helper_cfb_copyarea,
67 	.fb_imageblit	= drm_fb_helper_cfb_imageblit,
68 };
69 
70 static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
71 				   struct drm_fb_helper_surface_size *sizes,
72 				   struct exynos_drm_gem *exynos_gem)
73 {
74 	struct fb_info *fbi;
75 	struct drm_framebuffer *fb = helper->fb;
76 	unsigned int size = fb->width * fb->height * fb->format->cpp[0];
77 	unsigned int nr_pages;
78 	unsigned long offset;
79 
80 	fbi = drm_fb_helper_alloc_fbi(helper);
81 	if (IS_ERR(fbi)) {
82 		DRM_DEV_ERROR(to_dma_dev(helper->dev),
83 			      "failed to allocate fb info.\n");
84 		return PTR_ERR(fbi);
85 	}
86 
87 	fbi->fbops = &exynos_drm_fb_ops;
88 
89 	drm_fb_helper_fill_info(fbi, helper, sizes);
90 
91 	nr_pages = exynos_gem->size >> PAGE_SHIFT;
92 
93 	exynos_gem->kvaddr = (void __iomem *) vmap(exynos_gem->pages, nr_pages,
94 				VM_MAP, pgprot_writecombine(PAGE_KERNEL));
95 	if (!exynos_gem->kvaddr) {
96 		DRM_DEV_ERROR(to_dma_dev(helper->dev),
97 			      "failed to map pages to kernel space.\n");
98 		return -EIO;
99 	}
100 
101 	offset = fbi->var.xoffset * fb->format->cpp[0];
102 	offset += fbi->var.yoffset * fb->pitches[0];
103 
104 	fbi->screen_base = exynos_gem->kvaddr + offset;
105 	fbi->screen_size = size;
106 	fbi->fix.smem_len = size;
107 
108 	return 0;
109 }
110 
111 static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
112 				    struct drm_fb_helper_surface_size *sizes)
113 {
114 	struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
115 	struct exynos_drm_gem *exynos_gem;
116 	struct drm_device *dev = helper->dev;
117 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
118 	unsigned long size;
119 	int ret;
120 
121 	DRM_DEV_DEBUG_KMS(dev->dev,
122 			  "surface width(%d), height(%d) and bpp(%d\n",
123 			  sizes->surface_width, sizes->surface_height,
124 			  sizes->surface_bpp);
125 
126 	mode_cmd.width = sizes->surface_width;
127 	mode_cmd.height = sizes->surface_height;
128 	mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
129 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
130 							  sizes->surface_depth);
131 
132 	size = mode_cmd.pitches[0] * mode_cmd.height;
133 
134 	exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG, size);
135 	/*
136 	 * If physically contiguous memory allocation fails and if IOMMU is
137 	 * supported then try to get buffer from non physically contiguous
138 	 * memory area.
139 	 */
140 	if (IS_ERR(exynos_gem) && is_drm_iommu_supported(dev)) {
141 		dev_warn(dev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
142 		exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_NONCONTIG,
143 						   size);
144 	}
145 
146 	if (IS_ERR(exynos_gem))
147 		return PTR_ERR(exynos_gem);
148 
149 	exynos_fbdev->exynos_gem = exynos_gem;
150 
151 	helper->fb =
152 		exynos_drm_framebuffer_init(dev, &mode_cmd, &exynos_gem, 1);
153 	if (IS_ERR(helper->fb)) {
154 		DRM_DEV_ERROR(dev->dev, "failed to create drm framebuffer.\n");
155 		ret = PTR_ERR(helper->fb);
156 		goto err_destroy_gem;
157 	}
158 
159 	ret = exynos_drm_fbdev_update(helper, sizes, exynos_gem);
160 	if (ret < 0)
161 		goto err_destroy_framebuffer;
162 
163 	return ret;
164 
165 err_destroy_framebuffer:
166 	drm_framebuffer_cleanup(helper->fb);
167 err_destroy_gem:
168 	exynos_drm_gem_destroy(exynos_gem);
169 
170 	/*
171 	 * if failed, all resources allocated above would be released by
172 	 * drm_mode_config_cleanup() when drm_load() had been called prior
173 	 * to any specific driver such as fimd or hdmi driver.
174 	 */
175 
176 	return ret;
177 }
178 
179 static const struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
180 	.fb_probe =	exynos_drm_fbdev_create,
181 };
182 
183 int exynos_drm_fbdev_init(struct drm_device *dev)
184 {
185 	struct exynos_drm_fbdev *fbdev;
186 	struct exynos_drm_private *private = dev->dev_private;
187 	struct drm_fb_helper *helper;
188 	int ret;
189 
190 	if (!dev->mode_config.num_crtc)
191 		return 0;
192 
193 	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
194 	if (!fbdev)
195 		return -ENOMEM;
196 
197 	private->fb_helper = helper = &fbdev->drm_fb_helper;
198 
199 	drm_fb_helper_prepare(dev, helper, &exynos_drm_fb_helper_funcs);
200 
201 	ret = drm_fb_helper_init(dev, helper, MAX_CONNECTOR);
202 	if (ret < 0) {
203 		DRM_DEV_ERROR(dev->dev,
204 			      "failed to initialize drm fb helper.\n");
205 		goto err_init;
206 	}
207 
208 	ret = drm_fb_helper_single_add_all_connectors(helper);
209 	if (ret < 0) {
210 		DRM_DEV_ERROR(dev->dev,
211 			      "failed to register drm_fb_helper_connector.\n");
212 		goto err_setup;
213 
214 	}
215 
216 	ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
217 	if (ret < 0) {
218 		DRM_DEV_ERROR(dev->dev,
219 			      "failed to set up hw configuration.\n");
220 		goto err_setup;
221 	}
222 
223 	return 0;
224 
225 err_setup:
226 	drm_fb_helper_fini(helper);
227 
228 err_init:
229 	private->fb_helper = NULL;
230 	kfree(fbdev);
231 
232 	return ret;
233 }
234 
235 static void exynos_drm_fbdev_destroy(struct drm_device *dev,
236 				      struct drm_fb_helper *fb_helper)
237 {
238 	struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
239 	struct exynos_drm_gem *exynos_gem = exynos_fbd->exynos_gem;
240 	struct drm_framebuffer *fb;
241 
242 	vunmap(exynos_gem->kvaddr);
243 
244 	/* release drm framebuffer and real buffer */
245 	if (fb_helper->fb && fb_helper->fb->funcs) {
246 		fb = fb_helper->fb;
247 		if (fb)
248 			drm_framebuffer_remove(fb);
249 	}
250 
251 	drm_fb_helper_unregister_fbi(fb_helper);
252 
253 	drm_fb_helper_fini(fb_helper);
254 }
255 
256 void exynos_drm_fbdev_fini(struct drm_device *dev)
257 {
258 	struct exynos_drm_private *private = dev->dev_private;
259 	struct exynos_drm_fbdev *fbdev;
260 
261 	if (!private || !private->fb_helper)
262 		return;
263 
264 	fbdev = to_exynos_fbdev(private->fb_helper);
265 
266 	exynos_drm_fbdev_destroy(dev, private->fb_helper);
267 	kfree(fbdev);
268 	private->fb_helper = NULL;
269 }
270 
271