xref: /openbmc/linux/drivers/gpu/drm/exynos/exynos_drm_fbdev.c (revision df3305156f989339529b3d6744b898d498fb1f7b)
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 *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
44 	struct exynos_drm_gem_buf *buffer = exynos_gem_obj->buffer;
45 	unsigned long vm_size;
46 	int ret;
47 
48 	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
49 
50 	vm_size = vma->vm_end - vma->vm_start;
51 
52 	if (vm_size > buffer->size)
53 		return -EINVAL;
54 
55 	ret = dma_mmap_attrs(helper->dev->dev, vma, buffer->pages,
56 		buffer->dma_addr, buffer->size, &buffer->dma_attrs);
57 	if (ret < 0) {
58 		DRM_ERROR("failed to mmap.\n");
59 		return ret;
60 	}
61 
62 	return 0;
63 }
64 
65 static struct fb_ops exynos_drm_fb_ops = {
66 	.owner		= THIS_MODULE,
67 	.fb_mmap        = exynos_drm_fb_mmap,
68 	.fb_fillrect	= cfb_fillrect,
69 	.fb_copyarea	= cfb_copyarea,
70 	.fb_imageblit	= cfb_imageblit,
71 	.fb_check_var	= drm_fb_helper_check_var,
72 	.fb_set_par	= drm_fb_helper_set_par,
73 	.fb_blank	= drm_fb_helper_blank,
74 	.fb_pan_display	= drm_fb_helper_pan_display,
75 	.fb_setcmap	= drm_fb_helper_setcmap,
76 };
77 
78 static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
79 				     struct drm_framebuffer *fb)
80 {
81 	struct fb_info *fbi = helper->fbdev;
82 	struct exynos_drm_gem_buf *buffer;
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, fb->width, fb->height);
89 
90 	/* RGB formats use only one buffer */
91 	buffer = exynos_drm_fb_buffer(fb, 0);
92 	if (!buffer) {
93 		DRM_DEBUG_KMS("buffer is null.\n");
94 		return -EFAULT;
95 	}
96 
97 	nr_pages = buffer->size >> PAGE_SHIFT;
98 
99 	buffer->kvaddr = (void __iomem *) vmap(buffer->pages,
100 			nr_pages, VM_MAP,
101 			pgprot_writecombine(PAGE_KERNEL));
102 	if (!buffer->kvaddr) {
103 		DRM_ERROR("failed to map pages to kernel space.\n");
104 		return -EIO;
105 	}
106 
107 	/* buffer count to framebuffer always is 1 at booting time. */
108 	exynos_drm_fb_set_buf_cnt(fb, 1);
109 
110 	offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
111 	offset += fbi->var.yoffset * fb->pitches[0];
112 
113 	fbi->screen_base = buffer->kvaddr + offset;
114 	fbi->screen_size = size;
115 	fbi->fix.smem_len = size;
116 
117 	return 0;
118 }
119 
120 static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
121 				    struct drm_fb_helper_surface_size *sizes)
122 {
123 	struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
124 	struct exynos_drm_gem_obj *exynos_gem_obj;
125 	struct drm_device *dev = helper->dev;
126 	struct fb_info *fbi;
127 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
128 	struct platform_device *pdev = dev->platformdev;
129 	unsigned long size;
130 	int ret;
131 
132 	DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
133 			sizes->surface_width, sizes->surface_height,
134 			sizes->surface_bpp);
135 
136 	mode_cmd.width = sizes->surface_width;
137 	mode_cmd.height = sizes->surface_height;
138 	mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
139 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
140 							  sizes->surface_depth);
141 
142 	mutex_lock(&dev->struct_mutex);
143 
144 	fbi = framebuffer_alloc(0, &pdev->dev);
145 	if (!fbi) {
146 		DRM_ERROR("failed to allocate fb info.\n");
147 		ret = -ENOMEM;
148 		goto out;
149 	}
150 
151 	size = mode_cmd.pitches[0] * mode_cmd.height;
152 
153 	exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG, size);
154 	/*
155 	 * If physically contiguous memory allocation fails and if IOMMU is
156 	 * supported then try to get buffer from non physically contiguous
157 	 * memory area.
158 	 */
159 	if (IS_ERR(exynos_gem_obj) && is_drm_iommu_supported(dev)) {
160 		dev_warn(&pdev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
161 		exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_NONCONTIG,
162 							size);
163 	}
164 
165 	if (IS_ERR(exynos_gem_obj)) {
166 		ret = PTR_ERR(exynos_gem_obj);
167 		goto err_release_framebuffer;
168 	}
169 
170 	exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
171 
172 	helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
173 			&exynos_gem_obj->base);
174 	if (IS_ERR(helper->fb)) {
175 		DRM_ERROR("failed to create drm framebuffer.\n");
176 		ret = PTR_ERR(helper->fb);
177 		goto err_destroy_gem;
178 	}
179 
180 	helper->fbdev = fbi;
181 
182 	fbi->par = helper;
183 	fbi->flags = FBINFO_FLAG_DEFAULT;
184 	fbi->fbops = &exynos_drm_fb_ops;
185 
186 	ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
187 	if (ret) {
188 		DRM_ERROR("failed to allocate cmap.\n");
189 		goto err_destroy_framebuffer;
190 	}
191 
192 	ret = exynos_drm_fbdev_update(helper, helper->fb);
193 	if (ret < 0)
194 		goto err_dealloc_cmap;
195 
196 	mutex_unlock(&dev->struct_mutex);
197 	return ret;
198 
199 err_dealloc_cmap:
200 	fb_dealloc_cmap(&fbi->cmap);
201 err_destroy_framebuffer:
202 	drm_framebuffer_cleanup(helper->fb);
203 err_destroy_gem:
204 	exynos_drm_gem_destroy(exynos_gem_obj);
205 err_release_framebuffer:
206 	framebuffer_release(fbi);
207 
208 /*
209  * if failed, all resources allocated above would be released by
210  * drm_mode_config_cleanup() when drm_load() had been called prior
211  * to any specific driver such as fimd or hdmi driver.
212  */
213 out:
214 	mutex_unlock(&dev->struct_mutex);
215 	return ret;
216 }
217 
218 static const struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
219 	.fb_probe =	exynos_drm_fbdev_create,
220 };
221 
222 static bool exynos_drm_fbdev_is_anything_connected(struct drm_device *dev)
223 {
224 	struct drm_connector *connector;
225 	bool ret = false;
226 
227 	mutex_lock(&dev->mode_config.mutex);
228 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
229 		if (connector->status != connector_status_connected)
230 			continue;
231 
232 		ret = true;
233 		break;
234 	}
235 	mutex_unlock(&dev->mode_config.mutex);
236 
237 	return ret;
238 }
239 
240 int exynos_drm_fbdev_init(struct drm_device *dev)
241 {
242 	struct exynos_drm_fbdev *fbdev;
243 	struct exynos_drm_private *private = dev->dev_private;
244 	struct drm_fb_helper *helper;
245 	unsigned int num_crtc;
246 	int ret;
247 
248 	if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
249 		return 0;
250 
251 	if (!exynos_drm_fbdev_is_anything_connected(dev))
252 		return 0;
253 
254 	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
255 	if (!fbdev)
256 		return -ENOMEM;
257 
258 	private->fb_helper = helper = &fbdev->drm_fb_helper;
259 
260 	drm_fb_helper_prepare(dev, helper, &exynos_drm_fb_helper_funcs);
261 
262 	num_crtc = dev->mode_config.num_crtc;
263 
264 	ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
265 	if (ret < 0) {
266 		DRM_ERROR("failed to initialize drm fb helper.\n");
267 		goto err_init;
268 	}
269 
270 	ret = drm_fb_helper_single_add_all_connectors(helper);
271 	if (ret < 0) {
272 		DRM_ERROR("failed to register drm_fb_helper_connector.\n");
273 		goto err_setup;
274 
275 	}
276 
277 	/* disable all the possible outputs/crtcs before entering KMS mode */
278 	drm_helper_disable_unused_functions(dev);
279 
280 	ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
281 	if (ret < 0) {
282 		DRM_ERROR("failed to set up hw configuration.\n");
283 		goto err_setup;
284 	}
285 
286 	return 0;
287 
288 err_setup:
289 	drm_fb_helper_fini(helper);
290 
291 err_init:
292 	private->fb_helper = NULL;
293 	kfree(fbdev);
294 
295 	return ret;
296 }
297 
298 static void exynos_drm_fbdev_destroy(struct drm_device *dev,
299 				      struct drm_fb_helper *fb_helper)
300 {
301 	struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
302 	struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
303 	struct drm_framebuffer *fb;
304 
305 	if (exynos_gem_obj->buffer->kvaddr)
306 		vunmap(exynos_gem_obj->buffer->kvaddr);
307 
308 	/* release drm framebuffer and real buffer */
309 	if (fb_helper->fb && fb_helper->fb->funcs) {
310 		fb = fb_helper->fb;
311 		if (fb) {
312 			drm_framebuffer_unregister_private(fb);
313 			drm_framebuffer_remove(fb);
314 		}
315 	}
316 
317 	/* release linux framebuffer */
318 	if (fb_helper->fbdev) {
319 		struct fb_info *info;
320 		int ret;
321 
322 		info = fb_helper->fbdev;
323 		ret = unregister_framebuffer(info);
324 		if (ret < 0)
325 			DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
326 
327 		if (info->cmap.len)
328 			fb_dealloc_cmap(&info->cmap);
329 
330 		framebuffer_release(info);
331 	}
332 
333 	drm_fb_helper_fini(fb_helper);
334 }
335 
336 void exynos_drm_fbdev_fini(struct drm_device *dev)
337 {
338 	struct exynos_drm_private *private = dev->dev_private;
339 	struct exynos_drm_fbdev *fbdev;
340 
341 	if (!private || !private->fb_helper)
342 		return;
343 
344 	fbdev = to_exynos_fbdev(private->fb_helper);
345 
346 	exynos_drm_fbdev_destroy(dev, private->fb_helper);
347 	kfree(fbdev);
348 	private->fb_helper = NULL;
349 }
350 
351 void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
352 {
353 	struct exynos_drm_private *private = dev->dev_private;
354 
355 	if (!private || !private->fb_helper)
356 		return;
357 
358 	drm_fb_helper_restore_fbdev_mode_unlocked(private->fb_helper);
359 }
360