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 
20 #include "exynos_drm_drv.h"
21 #include "exynos_drm_fb.h"
22 #include "exynos_drm_gem.h"
23 #include "exynos_drm_iommu.h"
24 
25 #define MAX_CONNECTOR		4
26 #define PREFERRED_BPP		32
27 
28 #define to_exynos_fbdev(x)	container_of(x, struct exynos_drm_fbdev,\
29 				drm_fb_helper)
30 
31 struct exynos_drm_fbdev {
32 	struct drm_fb_helper		drm_fb_helper;
33 	struct exynos_drm_gem_obj	*exynos_gem_obj;
34 };
35 
36 static int exynos_drm_fb_mmap(struct fb_info *info,
37 			struct vm_area_struct *vma)
38 {
39 	struct drm_fb_helper *helper = info->par;
40 	struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper);
41 	struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
42 	struct exynos_drm_gem_buf *buffer = exynos_gem_obj->buffer;
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 > buffer->size)
51 		return -EINVAL;
52 
53 	ret = dma_mmap_attrs(helper->dev->dev, vma, buffer->pages,
54 		buffer->dma_addr, buffer->size, &buffer->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	= cfb_fillrect,
67 	.fb_copyarea	= cfb_copyarea,
68 	.fb_imageblit	= 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_framebuffer *fb)
78 {
79 	struct fb_info *fbi = helper->fbdev;
80 	struct drm_device *dev = helper->dev;
81 	struct exynos_drm_gem_buf *buffer;
82 	unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
83 	unsigned long offset;
84 
85 	drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
86 	drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height);
87 
88 	/* RGB formats use only one buffer */
89 	buffer = exynos_drm_fb_buffer(fb, 0);
90 	if (!buffer) {
91 		DRM_LOG_KMS("buffer is null.\n");
92 		return -EFAULT;
93 	}
94 
95 	/* map pages with kernel virtual space. */
96 	if (!buffer->kvaddr) {
97 		if (is_drm_iommu_supported(dev)) {
98 			unsigned int nr_pages = buffer->size >> PAGE_SHIFT;
99 
100 			buffer->kvaddr = vmap(buffer->pages, nr_pages, VM_MAP,
101 					pgprot_writecombine(PAGE_KERNEL));
102 		} else {
103 			phys_addr_t dma_addr = buffer->dma_addr;
104 			if (dma_addr)
105 				buffer->kvaddr = phys_to_virt(dma_addr);
106 			else
107 				buffer->kvaddr = (void __iomem *)NULL;
108 		}
109 		if (!buffer->kvaddr) {
110 			DRM_ERROR("failed to map pages to kernel space.\n");
111 			return -EIO;
112 		}
113 	}
114 
115 	/* buffer count to framebuffer always is 1 at booting time. */
116 	exynos_drm_fb_set_buf_cnt(fb, 1);
117 
118 	offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
119 	offset += fbi->var.yoffset * fb->pitches[0];
120 
121 	dev->mode_config.fb_base = (resource_size_t)buffer->dma_addr;
122 	fbi->screen_base = buffer->kvaddr + offset;
123 	if (is_drm_iommu_supported(dev))
124 		fbi->fix.smem_start = (unsigned long)
125 			(page_to_phys(sg_page(buffer->sgt->sgl)) + offset);
126 	else
127 		fbi->fix.smem_start = (unsigned long)buffer->dma_addr;
128 
129 	fbi->screen_size = size;
130 	fbi->fix.smem_len = size;
131 
132 	return 0;
133 }
134 
135 static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
136 				    struct drm_fb_helper_surface_size *sizes)
137 {
138 	struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
139 	struct exynos_drm_gem_obj *exynos_gem_obj;
140 	struct drm_device *dev = helper->dev;
141 	struct fb_info *fbi;
142 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
143 	struct platform_device *pdev = dev->platformdev;
144 	unsigned long size;
145 	int ret;
146 
147 	DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
148 			sizes->surface_width, sizes->surface_height,
149 			sizes->surface_bpp);
150 
151 	mode_cmd.width = sizes->surface_width;
152 	mode_cmd.height = sizes->surface_height;
153 	mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
154 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
155 							  sizes->surface_depth);
156 
157 	mutex_lock(&dev->struct_mutex);
158 
159 	fbi = framebuffer_alloc(0, &pdev->dev);
160 	if (!fbi) {
161 		DRM_ERROR("failed to allocate fb info.\n");
162 		ret = -ENOMEM;
163 		goto out;
164 	}
165 
166 	size = mode_cmd.pitches[0] * mode_cmd.height;
167 
168 	/* 0 means to allocate physically continuous memory */
169 	exynos_gem_obj = exynos_drm_gem_create(dev, 0, size);
170 	if (IS_ERR(exynos_gem_obj)) {
171 		ret = PTR_ERR(exynos_gem_obj);
172 		goto err_release_framebuffer;
173 	}
174 
175 	exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
176 
177 	helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
178 			&exynos_gem_obj->base);
179 	if (IS_ERR(helper->fb)) {
180 		DRM_ERROR("failed to create drm framebuffer.\n");
181 		ret = PTR_ERR(helper->fb);
182 		goto err_destroy_gem;
183 	}
184 
185 	helper->fbdev = fbi;
186 
187 	fbi->par = helper;
188 	fbi->flags = FBINFO_FLAG_DEFAULT;
189 	fbi->fbops = &exynos_drm_fb_ops;
190 
191 	ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
192 	if (ret) {
193 		DRM_ERROR("failed to allocate cmap.\n");
194 		goto err_destroy_framebuffer;
195 	}
196 
197 	ret = exynos_drm_fbdev_update(helper, helper->fb);
198 	if (ret < 0)
199 		goto err_dealloc_cmap;
200 
201 	mutex_unlock(&dev->struct_mutex);
202 	return ret;
203 
204 err_dealloc_cmap:
205 	fb_dealloc_cmap(&fbi->cmap);
206 err_destroy_framebuffer:
207 	drm_framebuffer_cleanup(helper->fb);
208 err_destroy_gem:
209 	exynos_drm_gem_destroy(exynos_gem_obj);
210 err_release_framebuffer:
211 	framebuffer_release(fbi);
212 
213 /*
214  * if failed, all resources allocated above would be released by
215  * drm_mode_config_cleanup() when drm_load() had been called prior
216  * to any specific driver such as fimd or hdmi driver.
217  */
218 out:
219 	mutex_unlock(&dev->struct_mutex);
220 	return ret;
221 }
222 
223 static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
224 	.fb_probe =	exynos_drm_fbdev_create,
225 };
226 
227 int exynos_drm_fbdev_init(struct drm_device *dev)
228 {
229 	struct exynos_drm_fbdev *fbdev;
230 	struct exynos_drm_private *private = dev->dev_private;
231 	struct drm_fb_helper *helper;
232 	unsigned int num_crtc;
233 	int ret;
234 
235 	if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
236 		return 0;
237 
238 	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
239 	if (!fbdev) {
240 		DRM_ERROR("failed to allocate drm fbdev.\n");
241 		return -ENOMEM;
242 	}
243 
244 	private->fb_helper = helper = &fbdev->drm_fb_helper;
245 	helper->funcs = &exynos_drm_fb_helper_funcs;
246 
247 	num_crtc = dev->mode_config.num_crtc;
248 
249 	ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
250 	if (ret < 0) {
251 		DRM_ERROR("failed to initialize drm fb helper.\n");
252 		goto err_init;
253 	}
254 
255 	ret = drm_fb_helper_single_add_all_connectors(helper);
256 	if (ret < 0) {
257 		DRM_ERROR("failed to register drm_fb_helper_connector.\n");
258 		goto err_setup;
259 
260 	}
261 
262 	/* disable all the possible outputs/crtcs before entering KMS mode */
263 	drm_helper_disable_unused_functions(dev);
264 
265 	ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
266 	if (ret < 0) {
267 		DRM_ERROR("failed to set up hw configuration.\n");
268 		goto err_setup;
269 	}
270 
271 	return 0;
272 
273 err_setup:
274 	drm_fb_helper_fini(helper);
275 
276 err_init:
277 	private->fb_helper = NULL;
278 	kfree(fbdev);
279 
280 	return ret;
281 }
282 
283 static void exynos_drm_fbdev_destroy(struct drm_device *dev,
284 				      struct drm_fb_helper *fb_helper)
285 {
286 	struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
287 	struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
288 	struct drm_framebuffer *fb;
289 
290 	if (is_drm_iommu_supported(dev) && exynos_gem_obj->buffer->kvaddr)
291 		vunmap(exynos_gem_obj->buffer->kvaddr);
292 
293 	/* release drm framebuffer and real buffer */
294 	if (fb_helper->fb && fb_helper->fb->funcs) {
295 		fb = fb_helper->fb;
296 		if (fb) {
297 			drm_framebuffer_unregister_private(fb);
298 			drm_framebuffer_remove(fb);
299 		}
300 	}
301 
302 	/* release linux framebuffer */
303 	if (fb_helper->fbdev) {
304 		struct fb_info *info;
305 		int ret;
306 
307 		info = fb_helper->fbdev;
308 		ret = unregister_framebuffer(info);
309 		if (ret < 0)
310 			DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
311 
312 		if (info->cmap.len)
313 			fb_dealloc_cmap(&info->cmap);
314 
315 		framebuffer_release(info);
316 	}
317 
318 	drm_fb_helper_fini(fb_helper);
319 }
320 
321 void exynos_drm_fbdev_fini(struct drm_device *dev)
322 {
323 	struct exynos_drm_private *private = dev->dev_private;
324 	struct exynos_drm_fbdev *fbdev;
325 
326 	if (!private || !private->fb_helper)
327 		return;
328 
329 	fbdev = to_exynos_fbdev(private->fb_helper);
330 
331 	if (fbdev->exynos_gem_obj)
332 		exynos_drm_gem_destroy(fbdev->exynos_gem_obj);
333 
334 	exynos_drm_fbdev_destroy(dev, private->fb_helper);
335 	kfree(fbdev);
336 	private->fb_helper = NULL;
337 }
338 
339 void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
340 {
341 	struct exynos_drm_private *private = dev->dev_private;
342 
343 	if (!private || !private->fb_helper)
344 		return;
345 
346 	drm_modeset_lock_all(dev);
347 	drm_fb_helper_restore_fbdev_mode(private->fb_helper);
348 	drm_modeset_unlock_all(dev);
349 }
350