xref: /linux/drivers/gpu/drm/exynos/exynos_drm_fbdev.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
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_iommu.h"
25 
26 #define MAX_CONNECTOR		4
27 #define PREFERRED_BPP		32
28 
29 #define to_exynos_fbdev(x)	container_of(x, struct exynos_drm_fbdev,\
30 				drm_fb_helper)
31 
32 struct exynos_drm_fbdev {
33 	struct drm_fb_helper		drm_fb_helper;
34 	struct exynos_drm_gem_obj	*obj;
35 };
36 
37 static int exynos_drm_fb_mmap(struct fb_info *info,
38 			struct vm_area_struct *vma)
39 {
40 	struct drm_fb_helper *helper = info->par;
41 	struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper);
42 	struct exynos_drm_gem_obj *obj = exynos_fbd->obj;
43 	unsigned long vm_size;
44 	int ret;
45 
46 	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
47 
48 	vm_size = vma->vm_end - vma->vm_start;
49 
50 	if (vm_size > obj->size)
51 		return -EINVAL;
52 
53 	ret = dma_mmap_attrs(helper->dev->dev, vma, obj->pages, obj->dma_addr,
54 			     obj->size, &obj->dma_attrs);
55 	if (ret < 0) {
56 		DRM_ERROR("failed to mmap.\n");
57 		return ret;
58 	}
59 
60 	return 0;
61 }
62 
63 static struct fb_ops exynos_drm_fb_ops = {
64 	.owner		= THIS_MODULE,
65 	.fb_mmap        = exynos_drm_fb_mmap,
66 	.fb_fillrect	= drm_fb_helper_cfb_fillrect,
67 	.fb_copyarea	= drm_fb_helper_cfb_copyarea,
68 	.fb_imageblit	= drm_fb_helper_cfb_imageblit,
69 	.fb_check_var	= drm_fb_helper_check_var,
70 	.fb_set_par	= drm_fb_helper_set_par,
71 	.fb_blank	= drm_fb_helper_blank,
72 	.fb_pan_display	= drm_fb_helper_pan_display,
73 	.fb_setcmap	= drm_fb_helper_setcmap,
74 };
75 
76 static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
77 				   struct drm_fb_helper_surface_size *sizes,
78 				   struct exynos_drm_gem_obj *obj)
79 {
80 	struct fb_info *fbi;
81 	struct drm_framebuffer *fb = helper->fb;
82 	unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
83 	unsigned int nr_pages;
84 	unsigned long offset;
85 
86 	fbi = drm_fb_helper_alloc_fbi(helper);
87 	if (IS_ERR(fbi)) {
88 		DRM_ERROR("failed to allocate fb info.\n");
89 		return PTR_ERR(fbi);
90 	}
91 
92 	fbi->par = helper;
93 	fbi->flags = FBINFO_FLAG_DEFAULT;
94 	fbi->fbops = &exynos_drm_fb_ops;
95 
96 	drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
97 	drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
98 
99 	nr_pages = obj->size >> PAGE_SHIFT;
100 
101 	obj->kvaddr = (void __iomem *) vmap(obj->pages, nr_pages, VM_MAP,
102 			pgprot_writecombine(PAGE_KERNEL));
103 	if (!obj->kvaddr) {
104 		DRM_ERROR("failed to map pages to kernel space.\n");
105 		drm_fb_helper_release_fbi(helper);
106 		return -EIO;
107 	}
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 *obj;
124 	struct drm_device *dev = helper->dev;
125 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
126 	struct platform_device *pdev = dev->platformdev;
127 	unsigned long size;
128 	int ret;
129 
130 	DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
131 			sizes->surface_width, sizes->surface_height,
132 			sizes->surface_bpp);
133 
134 	mode_cmd.width = sizes->surface_width;
135 	mode_cmd.height = sizes->surface_height;
136 	mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
137 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
138 							  sizes->surface_depth);
139 
140 	mutex_lock(&dev->struct_mutex);
141 
142 	size = mode_cmd.pitches[0] * mode_cmd.height;
143 
144 	obj = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG, size);
145 	/*
146 	 * If physically contiguous memory allocation fails and if IOMMU is
147 	 * supported then try to get buffer from non physically contiguous
148 	 * memory area.
149 	 */
150 	if (IS_ERR(obj) && is_drm_iommu_supported(dev)) {
151 		dev_warn(&pdev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
152 		obj = exynos_drm_gem_create(dev, EXYNOS_BO_NONCONTIG, size);
153 	}
154 
155 	if (IS_ERR(obj)) {
156 		ret = PTR_ERR(obj);
157 		goto out;
158 	}
159 
160 	exynos_fbdev->obj = obj;
161 
162 	helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd, &obj, 1);
163 	if (IS_ERR(helper->fb)) {
164 		DRM_ERROR("failed to create drm framebuffer.\n");
165 		ret = PTR_ERR(helper->fb);
166 		goto err_destroy_gem;
167 	}
168 
169 	ret = exynos_drm_fbdev_update(helper, sizes, obj);
170 	if (ret < 0)
171 		goto err_destroy_framebuffer;
172 
173 	mutex_unlock(&dev->struct_mutex);
174 	return ret;
175 
176 err_destroy_framebuffer:
177 	drm_framebuffer_cleanup(helper->fb);
178 err_destroy_gem:
179 	exynos_drm_gem_destroy(obj);
180 
181 /*
182  * if failed, all resources allocated above would be released by
183  * drm_mode_config_cleanup() when drm_load() had been called prior
184  * to any specific driver such as fimd or hdmi driver.
185  */
186 out:
187 	mutex_unlock(&dev->struct_mutex);
188 	return ret;
189 }
190 
191 static const struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
192 	.fb_probe =	exynos_drm_fbdev_create,
193 };
194 
195 static bool exynos_drm_fbdev_is_anything_connected(struct drm_device *dev)
196 {
197 	struct drm_connector *connector;
198 	bool ret = false;
199 
200 	mutex_lock(&dev->mode_config.mutex);
201 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
202 		if (connector->status != connector_status_connected)
203 			continue;
204 
205 		ret = true;
206 		break;
207 	}
208 	mutex_unlock(&dev->mode_config.mutex);
209 
210 	return ret;
211 }
212 
213 int exynos_drm_fbdev_init(struct drm_device *dev)
214 {
215 	struct exynos_drm_fbdev *fbdev;
216 	struct exynos_drm_private *private = dev->dev_private;
217 	struct drm_fb_helper *helper;
218 	unsigned int num_crtc;
219 	int ret;
220 
221 	if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
222 		return 0;
223 
224 	if (!exynos_drm_fbdev_is_anything_connected(dev))
225 		return 0;
226 
227 	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
228 	if (!fbdev)
229 		return -ENOMEM;
230 
231 	private->fb_helper = helper = &fbdev->drm_fb_helper;
232 
233 	drm_fb_helper_prepare(dev, helper, &exynos_drm_fb_helper_funcs);
234 
235 	num_crtc = dev->mode_config.num_crtc;
236 
237 	ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
238 	if (ret < 0) {
239 		DRM_ERROR("failed to initialize drm fb helper.\n");
240 		goto err_init;
241 	}
242 
243 	ret = drm_fb_helper_single_add_all_connectors(helper);
244 	if (ret < 0) {
245 		DRM_ERROR("failed to register drm_fb_helper_connector.\n");
246 		goto err_setup;
247 
248 	}
249 
250 	ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
251 	if (ret < 0) {
252 		DRM_ERROR("failed to set up hw configuration.\n");
253 		goto err_setup;
254 	}
255 
256 	return 0;
257 
258 err_setup:
259 	drm_fb_helper_fini(helper);
260 
261 err_init:
262 	private->fb_helper = NULL;
263 	kfree(fbdev);
264 
265 	return ret;
266 }
267 
268 static void exynos_drm_fbdev_destroy(struct drm_device *dev,
269 				      struct drm_fb_helper *fb_helper)
270 {
271 	struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
272 	struct exynos_drm_gem_obj *obj = exynos_fbd->obj;
273 	struct drm_framebuffer *fb;
274 
275 	if (obj->kvaddr)
276 		vunmap(obj->kvaddr);
277 
278 	/* release drm framebuffer and real buffer */
279 	if (fb_helper->fb && fb_helper->fb->funcs) {
280 		fb = fb_helper->fb;
281 		if (fb) {
282 			drm_framebuffer_unregister_private(fb);
283 			drm_framebuffer_remove(fb);
284 		}
285 	}
286 
287 	drm_fb_helper_unregister_fbi(fb_helper);
288 	drm_fb_helper_release_fbi(fb_helper);
289 
290 	drm_fb_helper_fini(fb_helper);
291 }
292 
293 void exynos_drm_fbdev_fini(struct drm_device *dev)
294 {
295 	struct exynos_drm_private *private = dev->dev_private;
296 	struct exynos_drm_fbdev *fbdev;
297 
298 	if (!private || !private->fb_helper)
299 		return;
300 
301 	fbdev = to_exynos_fbdev(private->fb_helper);
302 
303 	exynos_drm_fbdev_destroy(dev, private->fb_helper);
304 	kfree(fbdev);
305 	private->fb_helper = NULL;
306 }
307 
308 void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
309 {
310 	struct exynos_drm_private *private = dev->dev_private;
311 
312 	if (!private || !private->fb_helper)
313 		return;
314 
315 	drm_fb_helper_restore_fbdev_mode_unlocked(private->fb_helper);
316 }
317