xref: /linux/drivers/gpu/drm/exynos/exynos_drm_fbdev.c (revision c0e297dc61f8d4453e07afbea1fa8d0e67cd4a34)
1 /* exynos_drm_fbdev.c
2  *
3  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4  * Authors:
5  *	Inki Dae <inki.dae@samsung.com>
6  *	Joonyoung Shim <jy0922.shim@samsung.com>
7  *	Seung-Woo Kim <sw0312.kim@samsung.com>
8  *
9  * This program is free software; you can redistribute  it and/or modify it
10  * under  the terms of  the GNU General  Public License as published by the
11  * Free Software Foundation;  either version 2 of the  License, or (at your
12  * option) any later version.
13  */
14 
15 #include <drm/drmP.h>
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_fb_helper.h>
18 #include <drm/drm_crtc_helper.h>
19 #include <drm/exynos_drm.h>
20 
21 #include "exynos_drm_drv.h"
22 #include "exynos_drm_fb.h"
23 #include "exynos_drm_fbdev.h"
24 #include "exynos_drm_gem.h"
25 #include "exynos_drm_iommu.h"
26 
27 #define MAX_CONNECTOR		4
28 #define PREFERRED_BPP		32
29 
30 #define to_exynos_fbdev(x)	container_of(x, struct exynos_drm_fbdev,\
31 				drm_fb_helper)
32 
33 struct exynos_drm_fbdev {
34 	struct drm_fb_helper		drm_fb_helper;
35 	struct exynos_drm_gem_obj	*exynos_gem_obj;
36 };
37 
38 static int exynos_drm_fb_mmap(struct fb_info *info,
39 			struct vm_area_struct *vma)
40 {
41 	struct drm_fb_helper *helper = info->par;
42 	struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper);
43 	struct exynos_drm_gem_obj *obj = exynos_fbd->exynos_gem_obj;
44 	unsigned long vm_size;
45 	int ret;
46 
47 	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
48 
49 	vm_size = vma->vm_end - vma->vm_start;
50 
51 	if (vm_size > obj->size)
52 		return -EINVAL;
53 
54 	ret = dma_mmap_attrs(helper->dev->dev, vma, obj->pages, obj->dma_addr,
55 			     obj->size, &obj->dma_attrs);
56 	if (ret < 0) {
57 		DRM_ERROR("failed to mmap.\n");
58 		return ret;
59 	}
60 
61 	return 0;
62 }
63 
64 static struct fb_ops exynos_drm_fb_ops = {
65 	.owner		= THIS_MODULE,
66 	.fb_mmap        = exynos_drm_fb_mmap,
67 	.fb_fillrect	= drm_fb_helper_cfb_fillrect,
68 	.fb_copyarea	= drm_fb_helper_cfb_copyarea,
69 	.fb_imageblit	= drm_fb_helper_cfb_imageblit,
70 	.fb_check_var	= drm_fb_helper_check_var,
71 	.fb_set_par	= drm_fb_helper_set_par,
72 	.fb_blank	= drm_fb_helper_blank,
73 	.fb_pan_display	= drm_fb_helper_pan_display,
74 	.fb_setcmap	= drm_fb_helper_setcmap,
75 };
76 
77 static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
78 				     struct drm_fb_helper_surface_size *sizes,
79 				     struct drm_framebuffer *fb)
80 {
81 	struct fb_info *fbi = helper->fbdev;
82 	struct exynos_drm_gem_obj *obj;
83 	unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
84 	unsigned int nr_pages;
85 	unsigned long offset;
86 
87 	drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
88 	drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
89 
90 	/* RGB formats use only one buffer */
91 	obj = exynos_drm_fb_gem_obj(fb, 0);
92 	if (!obj) {
93 		DRM_DEBUG_KMS("gem object is null.\n");
94 		return -EFAULT;
95 	}
96 
97 	nr_pages = obj->size >> PAGE_SHIFT;
98 
99 	obj->kvaddr = (void __iomem *) vmap(obj->pages, nr_pages, VM_MAP,
100 			pgprot_writecombine(PAGE_KERNEL));
101 	if (!obj->kvaddr) {
102 		DRM_ERROR("failed to map pages to kernel space.\n");
103 		return -EIO;
104 	}
105 
106 	/* buffer count to framebuffer always is 1 at booting time. */
107 	exynos_drm_fb_set_buf_cnt(fb, 1);
108 
109 	offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
110 	offset += fbi->var.yoffset * fb->pitches[0];
111 
112 	fbi->screen_base = obj->kvaddr + offset;
113 	fbi->screen_size = size;
114 	fbi->fix.smem_len = size;
115 
116 	return 0;
117 }
118 
119 static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
120 				    struct drm_fb_helper_surface_size *sizes)
121 {
122 	struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
123 	struct exynos_drm_gem_obj *exynos_gem_obj;
124 	struct drm_device *dev = helper->dev;
125 	struct fb_info *fbi;
126 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
127 	struct platform_device *pdev = dev->platformdev;
128 	unsigned long size;
129 	int ret;
130 
131 	DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
132 			sizes->surface_width, sizes->surface_height,
133 			sizes->surface_bpp);
134 
135 	mode_cmd.width = sizes->surface_width;
136 	mode_cmd.height = sizes->surface_height;
137 	mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
138 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
139 							  sizes->surface_depth);
140 
141 	mutex_lock(&dev->struct_mutex);
142 
143 	fbi = drm_fb_helper_alloc_fbi(helper);
144 	if (IS_ERR(fbi)) {
145 		DRM_ERROR("failed to allocate fb info.\n");
146 		ret = PTR_ERR(fbi);
147 		goto out;
148 	}
149 
150 	size = mode_cmd.pitches[0] * mode_cmd.height;
151 
152 	exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG, size);
153 	/*
154 	 * If physically contiguous memory allocation fails and if IOMMU is
155 	 * supported then try to get buffer from non physically contiguous
156 	 * memory area.
157 	 */
158 	if (IS_ERR(exynos_gem_obj) && is_drm_iommu_supported(dev)) {
159 		dev_warn(&pdev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
160 		exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_NONCONTIG,
161 							size);
162 	}
163 
164 	if (IS_ERR(exynos_gem_obj)) {
165 		ret = PTR_ERR(exynos_gem_obj);
166 		goto err_release_fbi;
167 	}
168 
169 	exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
170 
171 	helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
172 			&exynos_gem_obj->base);
173 	if (IS_ERR(helper->fb)) {
174 		DRM_ERROR("failed to create drm framebuffer.\n");
175 		ret = PTR_ERR(helper->fb);
176 		goto err_destroy_gem;
177 	}
178 
179 	fbi->par = helper;
180 	fbi->flags = FBINFO_FLAG_DEFAULT;
181 	fbi->fbops = &exynos_drm_fb_ops;
182 
183 	ret = exynos_drm_fbdev_update(helper, sizes, helper->fb);
184 	if (ret < 0)
185 		goto err_destroy_framebuffer;
186 
187 	mutex_unlock(&dev->struct_mutex);
188 	return ret;
189 
190 err_destroy_framebuffer:
191 	drm_framebuffer_cleanup(helper->fb);
192 err_destroy_gem:
193 	exynos_drm_gem_destroy(exynos_gem_obj);
194 err_release_fbi:
195 	drm_fb_helper_release_fbi(helper);
196 
197 /*
198  * if failed, all resources allocated above would be released by
199  * drm_mode_config_cleanup() when drm_load() had been called prior
200  * to any specific driver such as fimd or hdmi driver.
201  */
202 out:
203 	mutex_unlock(&dev->struct_mutex);
204 	return ret;
205 }
206 
207 static const struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
208 	.fb_probe =	exynos_drm_fbdev_create,
209 };
210 
211 static bool exynos_drm_fbdev_is_anything_connected(struct drm_device *dev)
212 {
213 	struct drm_connector *connector;
214 	bool ret = false;
215 
216 	mutex_lock(&dev->mode_config.mutex);
217 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
218 		if (connector->status != connector_status_connected)
219 			continue;
220 
221 		ret = true;
222 		break;
223 	}
224 	mutex_unlock(&dev->mode_config.mutex);
225 
226 	return ret;
227 }
228 
229 int exynos_drm_fbdev_init(struct drm_device *dev)
230 {
231 	struct exynos_drm_fbdev *fbdev;
232 	struct exynos_drm_private *private = dev->dev_private;
233 	struct drm_fb_helper *helper;
234 	unsigned int num_crtc;
235 	int ret;
236 
237 	if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
238 		return 0;
239 
240 	if (!exynos_drm_fbdev_is_anything_connected(dev))
241 		return 0;
242 
243 	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
244 	if (!fbdev)
245 		return -ENOMEM;
246 
247 	private->fb_helper = helper = &fbdev->drm_fb_helper;
248 
249 	drm_fb_helper_prepare(dev, helper, &exynos_drm_fb_helper_funcs);
250 
251 	num_crtc = dev->mode_config.num_crtc;
252 
253 	ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
254 	if (ret < 0) {
255 		DRM_ERROR("failed to initialize drm fb helper.\n");
256 		goto err_init;
257 	}
258 
259 	ret = drm_fb_helper_single_add_all_connectors(helper);
260 	if (ret < 0) {
261 		DRM_ERROR("failed to register drm_fb_helper_connector.\n");
262 		goto err_setup;
263 
264 	}
265 
266 	ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
267 	if (ret < 0) {
268 		DRM_ERROR("failed to set up hw configuration.\n");
269 		goto err_setup;
270 	}
271 
272 	return 0;
273 
274 err_setup:
275 	drm_fb_helper_fini(helper);
276 
277 err_init:
278 	private->fb_helper = NULL;
279 	kfree(fbdev);
280 
281 	return ret;
282 }
283 
284 static void exynos_drm_fbdev_destroy(struct drm_device *dev,
285 				      struct drm_fb_helper *fb_helper)
286 {
287 	struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
288 	struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
289 	struct drm_framebuffer *fb;
290 
291 	if (exynos_gem_obj->kvaddr)
292 		vunmap(exynos_gem_obj->kvaddr);
293 
294 	/* release drm framebuffer and real buffer */
295 	if (fb_helper->fb && fb_helper->fb->funcs) {
296 		fb = fb_helper->fb;
297 		if (fb) {
298 			drm_framebuffer_unregister_private(fb);
299 			drm_framebuffer_remove(fb);
300 		}
301 	}
302 
303 	drm_fb_helper_unregister_fbi(fb_helper);
304 	drm_fb_helper_release_fbi(fb_helper);
305 
306 	drm_fb_helper_fini(fb_helper);
307 }
308 
309 void exynos_drm_fbdev_fini(struct drm_device *dev)
310 {
311 	struct exynos_drm_private *private = dev->dev_private;
312 	struct exynos_drm_fbdev *fbdev;
313 
314 	if (!private || !private->fb_helper)
315 		return;
316 
317 	fbdev = to_exynos_fbdev(private->fb_helper);
318 
319 	exynos_drm_fbdev_destroy(dev, private->fb_helper);
320 	kfree(fbdev);
321 	private->fb_helper = NULL;
322 }
323 
324 void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
325 {
326 	struct exynos_drm_private *private = dev->dev_private;
327 
328 	if (!private || !private->fb_helper)
329 		return;
330 
331 	drm_fb_helper_restore_fbdev_mode_unlocked(private->fb_helper);
332 }
333