xref: /linux/drivers/gpu/drm/exynos/exynos_drm_fbdev.c (revision b6ff753a0ca0d209a71a6aa53ac38c53c540f2ee)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21c248b7dSInki Dae /* exynos_drm_fbdev.c
31c248b7dSInki Dae  *
41c248b7dSInki Dae  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
51c248b7dSInki Dae  * Authors:
61c248b7dSInki Dae  *	Inki Dae <inki.dae@samsung.com>
71c248b7dSInki Dae  *	Joonyoung Shim <jy0922.shim@samsung.com>
81c248b7dSInki Dae  *	Seung-Woo Kim <sw0312.kim@samsung.com>
91c248b7dSInki Dae  */
101c248b7dSInki Dae 
112bda34d7SSam Ravnborg #include <linux/console.h>
122bda34d7SSam Ravnborg #include <linux/dma-mapping.h>
13156bdac9SSam Ravnborg #include <linux/vmalloc.h>
142bda34d7SSam Ravnborg 
15760285e7SDavid Howells #include <drm/drm_crtc.h>
16760285e7SDavid Howells #include <drm/drm_fb_helper.h>
172bda34d7SSam Ravnborg #include <drm/drm_fourcc.h>
18fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
19a1bfacf4SVikas Sajjan #include <drm/exynos_drm.h>
201c248b7dSInki Dae 
211c248b7dSInki Dae #include "exynos_drm_drv.h"
221c248b7dSInki Dae #include "exynos_drm_fb.h"
23e30655d0SMark Brown #include "exynos_drm_fbdev.h"
241c248b7dSInki Dae 
251c248b7dSInki Dae #define MAX_CONNECTOR		4
261c248b7dSInki Dae #define PREFERRED_BPP		32
271c248b7dSInki Dae 
281c248b7dSInki Dae #define to_exynos_fbdev(x)	container_of(x, struct exynos_drm_fbdev,\
291c248b7dSInki Dae 				drm_fb_helper)
301c248b7dSInki Dae 
311c248b7dSInki Dae struct exynos_drm_fbdev {
321c248b7dSInki Dae 	struct drm_fb_helper	drm_fb_helper;
33813fd67bSJoonyoung Shim 	struct exynos_drm_gem	*exynos_gem;
341c248b7dSInki Dae };
351c248b7dSInki Dae 
36dd265850SPrathyush K static int exynos_drm_fb_mmap(struct fb_info *info,
37dd265850SPrathyush K 			struct vm_area_struct *vma)
38dd265850SPrathyush K {
39dd265850SPrathyush K 	struct drm_fb_helper *helper = info->par;
40dd265850SPrathyush K 	struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper);
41813fd67bSJoonyoung Shim 	struct exynos_drm_gem *exynos_gem = exynos_fbd->exynos_gem;
42dd265850SPrathyush K 	unsigned long vm_size;
43dd265850SPrathyush K 	int ret;
44dd265850SPrathyush K 
45dd265850SPrathyush K 	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
46dd265850SPrathyush K 
47dd265850SPrathyush K 	vm_size = vma->vm_end - vma->vm_start;
48dd265850SPrathyush K 
49813fd67bSJoonyoung Shim 	if (vm_size > exynos_gem->size)
50dd265850SPrathyush K 		return -EINVAL;
51dd265850SPrathyush K 
52f43c3596SMarek Szyprowski 	ret = dma_mmap_attrs(to_dma_dev(helper->dev), vma, exynos_gem->cookie,
53813fd67bSJoonyoung Shim 			     exynos_gem->dma_addr, exynos_gem->size,
5400085f1eSKrzysztof Kozlowski 			     exynos_gem->dma_attrs);
55dd265850SPrathyush K 	if (ret < 0) {
566f83d208SInki Dae 		DRM_DEV_ERROR(to_dma_dev(helper->dev), "failed to mmap.\n");
57dd265850SPrathyush K 		return ret;
58dd265850SPrathyush K 	}
59dd265850SPrathyush K 
60dd265850SPrathyush K 	return 0;
61dd265850SPrathyush K }
62dd265850SPrathyush K 
63*b6ff753aSJani Nikula static const struct fb_ops exynos_drm_fb_ops = {
641c248b7dSInki Dae 	.owner		= THIS_MODULE,
652eec838cSStefan Christ 	DRM_FB_HELPER_DEFAULT_OPS,
66dd265850SPrathyush K 	.fb_mmap        = exynos_drm_fb_mmap,
677c7d4507SArchit Taneja 	.fb_fillrect	= drm_fb_helper_cfb_fillrect,
687c7d4507SArchit Taneja 	.fb_copyarea	= drm_fb_helper_cfb_copyarea,
697c7d4507SArchit Taneja 	.fb_imageblit	= drm_fb_helper_cfb_imageblit,
701c248b7dSInki Dae };
711c248b7dSInki Dae 
7219c8b834SInki Dae static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
73ecbf1d5aSRob Clark 				   struct drm_fb_helper_surface_size *sizes,
74813fd67bSJoonyoung Shim 				   struct exynos_drm_gem *exynos_gem)
751c248b7dSInki Dae {
76ee885ca5SJoonyoung Shim 	struct fb_info *fbi;
77d7619960SJoonyoung Shim 	struct drm_framebuffer *fb = helper->fb;
78272725c7SVille Syrjälä 	unsigned int size = fb->width * fb->height * fb->format->cpp[0];
79a5d7ac30SCarlo Caione 	unsigned int nr_pages;
8019c8b834SInki Dae 	unsigned long offset;
811c248b7dSInki Dae 
82ee885ca5SJoonyoung Shim 	fbi = drm_fb_helper_alloc_fbi(helper);
83ee885ca5SJoonyoung Shim 	if (IS_ERR(fbi)) {
846f83d208SInki Dae 		DRM_DEV_ERROR(to_dma_dev(helper->dev),
856f83d208SInki Dae 			      "failed to allocate fb info.\n");
86ee885ca5SJoonyoung Shim 		return PTR_ERR(fbi);
87ee885ca5SJoonyoung Shim 	}
88ee885ca5SJoonyoung Shim 
89ee885ca5SJoonyoung Shim 	fbi->fbops = &exynos_drm_fb_ops;
90ee885ca5SJoonyoung Shim 
91fb68e596SDaniel Vetter 	drm_fb_helper_fill_info(fbi, helper, sizes);
921c248b7dSInki Dae 
93813fd67bSJoonyoung Shim 	nr_pages = exynos_gem->size >> PAGE_SHIFT;
94c704f1b4SInki Dae 
95813fd67bSJoonyoung Shim 	exynos_gem->kvaddr = (void __iomem *) vmap(exynos_gem->pages, nr_pages,
96813fd67bSJoonyoung Shim 				VM_MAP, pgprot_writecombine(PAGE_KERNEL));
97813fd67bSJoonyoung Shim 	if (!exynos_gem->kvaddr) {
986f83d208SInki Dae 		DRM_DEV_ERROR(to_dma_dev(helper->dev),
996f83d208SInki Dae 			      "failed to map pages to kernel space.\n");
1004744ad24SInki Dae 		return -EIO;
1014744ad24SInki Dae 	}
1024744ad24SInki Dae 
103272725c7SVille Syrjälä 	offset = fbi->var.xoffset * fb->format->cpp[0];
10401f2c773SVille Syrjälä 	offset += fbi->var.yoffset * fb->pitches[0];
1051c248b7dSInki Dae 
106813fd67bSJoonyoung Shim 	fbi->screen_base = exynos_gem->kvaddr + offset;
1071c248b7dSInki Dae 	fbi->screen_size = size;
10871b1f195SDaniel Kurtz 	fbi->fix.smem_len = size;
10919c8b834SInki Dae 
11019c8b834SInki Dae 	return 0;
1111c248b7dSInki Dae }
1121c248b7dSInki Dae 
1131c248b7dSInki Dae static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
1141c248b7dSInki Dae 				    struct drm_fb_helper_surface_size *sizes)
1151c248b7dSInki Dae {
1161c248b7dSInki Dae 	struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
117813fd67bSJoonyoung Shim 	struct exynos_drm_gem *exynos_gem;
1181c248b7dSInki Dae 	struct drm_device *dev = helper->dev;
119a794d57dSJoonyoung Shim 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
120e1533c08SJoonyoung Shim 	unsigned long size;
1211c248b7dSInki Dae 	int ret;
1221c248b7dSInki Dae 
1236be90056SInki Dae 	DRM_DEV_DEBUG_KMS(dev->dev,
1246be90056SInki Dae 			  "surface width(%d), height(%d) and bpp(%d\n",
1251c248b7dSInki Dae 			  sizes->surface_width, sizes->surface_height,
1261c248b7dSInki Dae 			  sizes->surface_bpp);
1271c248b7dSInki Dae 
1281c248b7dSInki Dae 	mode_cmd.width = sizes->surface_width;
1291c248b7dSInki Dae 	mode_cmd.height = sizes->surface_height;
130a794d57dSJoonyoung Shim 	mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
131a794d57dSJoonyoung Shim 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
132a794d57dSJoonyoung Shim 							  sizes->surface_depth);
1331c248b7dSInki Dae 
134e1533c08SJoonyoung Shim 	size = mode_cmd.pitches[0] * mode_cmd.height;
1352b35892eSInki Dae 
136813fd67bSJoonyoung Shim 	exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG, size);
137a1bfacf4SVikas Sajjan 	/*
138a1bfacf4SVikas Sajjan 	 * If physically contiguous memory allocation fails and if IOMMU is
139a1bfacf4SVikas Sajjan 	 * supported then try to get buffer from non physically contiguous
140a1bfacf4SVikas Sajjan 	 * memory area.
141a1bfacf4SVikas Sajjan 	 */
142813fd67bSJoonyoung Shim 	if (IS_ERR(exynos_gem) && is_drm_iommu_supported(dev)) {
143896bbc3eSLaurent Pinchart 		dev_warn(dev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
144813fd67bSJoonyoung Shim 		exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_NONCONTIG,
145813fd67bSJoonyoung Shim 						   size);
146a1bfacf4SVikas Sajjan 	}
147a1bfacf4SVikas Sajjan 
1482f424200SDaniel Vetter 	if (IS_ERR(exynos_gem))
1492f424200SDaniel Vetter 		return PTR_ERR(exynos_gem);
1501c248b7dSInki Dae 
151813fd67bSJoonyoung Shim 	exynos_fbdev->exynos_gem = exynos_gem;
152e1533c08SJoonyoung Shim 
153813fd67bSJoonyoung Shim 	helper->fb =
154813fd67bSJoonyoung Shim 		exynos_drm_framebuffer_init(dev, &mode_cmd, &exynos_gem, 1);
15541eab402SSachin Kamat 	if (IS_ERR(helper->fb)) {
1566f83d208SInki Dae 		DRM_DEV_ERROR(dev->dev, "failed to create drm framebuffer.\n");
157e1533c08SJoonyoung Shim 		ret = PTR_ERR(helper->fb);
158662aa6d7SInki Dae 		goto err_destroy_gem;
159e1533c08SJoonyoung Shim 	}
160e1533c08SJoonyoung Shim 
161813fd67bSJoonyoung Shim 	ret = exynos_drm_fbdev_update(helper, sizes, exynos_gem);
162662aa6d7SInki Dae 	if (ret < 0)
1637c7d4507SArchit Taneja 		goto err_destroy_framebuffer;
164662aa6d7SInki Dae 
165662aa6d7SInki Dae 	return ret;
166662aa6d7SInki Dae 
167662aa6d7SInki Dae err_destroy_framebuffer:
168662aa6d7SInki Dae 	drm_framebuffer_cleanup(helper->fb);
169662aa6d7SInki Dae err_destroy_gem:
170813fd67bSJoonyoung Shim 	exynos_drm_gem_destroy(exynos_gem);
1711c248b7dSInki Dae 
1721c248b7dSInki Dae 	/*
1731c248b7dSInki Dae 	 * if failed, all resources allocated above would be released by
1741c248b7dSInki Dae 	 * drm_mode_config_cleanup() when drm_load() had been called prior
1751c248b7dSInki Dae 	 * to any specific driver such as fimd or hdmi driver.
1761c248b7dSInki Dae 	 */
1772f424200SDaniel Vetter 
1781c248b7dSInki Dae 	return ret;
1791c248b7dSInki Dae }
1801c248b7dSInki Dae 
1813a493879SThierry Reding static const struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
182cd5428a5SDaniel Vetter 	.fb_probe =	exynos_drm_fbdev_create,
1831c248b7dSInki Dae };
1841c248b7dSInki Dae 
1851c248b7dSInki Dae int exynos_drm_fbdev_init(struct drm_device *dev)
1861c248b7dSInki Dae {
1871c248b7dSInki Dae 	struct exynos_drm_fbdev *fbdev;
1881c248b7dSInki Dae 	struct exynos_drm_private *private = dev->dev_private;
1891c248b7dSInki Dae 	struct drm_fb_helper *helper;
1901c248b7dSInki Dae 	int ret;
1911c248b7dSInki Dae 
192989534cfSAndrzej Hajda 	if (!dev->mode_config.num_crtc)
1931c248b7dSInki Dae 		return 0;
1941c248b7dSInki Dae 
1951c248b7dSInki Dae 	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
19638bb5253SSachin Kamat 	if (!fbdev)
1971c248b7dSInki Dae 		return -ENOMEM;
1981c248b7dSInki Dae 
1991c248b7dSInki Dae 	private->fb_helper = helper = &fbdev->drm_fb_helper;
20010a23102SThierry Reding 
20110a23102SThierry Reding 	drm_fb_helper_prepare(dev, helper, &exynos_drm_fb_helper_funcs);
2021c248b7dSInki Dae 
203e4563f6bSGabriel Krisman Bertazi 	ret = drm_fb_helper_init(dev, helper, MAX_CONNECTOR);
2041c248b7dSInki Dae 	if (ret < 0) {
2056f83d208SInki Dae 		DRM_DEV_ERROR(dev->dev,
2066f83d208SInki Dae 			      "failed to initialize drm fb helper.\n");
2071c248b7dSInki Dae 		goto err_init;
2081c248b7dSInki Dae 	}
2091c248b7dSInki Dae 
2101c248b7dSInki Dae 	ret = drm_fb_helper_single_add_all_connectors(helper);
2111c248b7dSInki Dae 	if (ret < 0) {
2126f83d208SInki Dae 		DRM_DEV_ERROR(dev->dev,
2136f83d208SInki Dae 			      "failed to register drm_fb_helper_connector.\n");
2141c248b7dSInki Dae 		goto err_setup;
2151c248b7dSInki Dae 
2161c248b7dSInki Dae 	}
2171c248b7dSInki Dae 
2181c248b7dSInki Dae 	ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
2191c248b7dSInki Dae 	if (ret < 0) {
2206f83d208SInki Dae 		DRM_DEV_ERROR(dev->dev,
2216f83d208SInki Dae 			      "failed to set up hw configuration.\n");
2221c248b7dSInki Dae 		goto err_setup;
2231c248b7dSInki Dae 	}
2241c248b7dSInki Dae 
2251c248b7dSInki Dae 	return 0;
2261c248b7dSInki Dae 
2271c248b7dSInki Dae err_setup:
2281c248b7dSInki Dae 	drm_fb_helper_fini(helper);
2291c248b7dSInki Dae 
2301c248b7dSInki Dae err_init:
2311c248b7dSInki Dae 	private->fb_helper = NULL;
2321c248b7dSInki Dae 	kfree(fbdev);
2331c248b7dSInki Dae 
2341c248b7dSInki Dae 	return ret;
2351c248b7dSInki Dae }
2361c248b7dSInki Dae 
2371c248b7dSInki Dae static void exynos_drm_fbdev_destroy(struct drm_device *dev,
2381c248b7dSInki Dae 				      struct drm_fb_helper *fb_helper)
2391c248b7dSInki Dae {
2404744ad24SInki Dae 	struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
241813fd67bSJoonyoung Shim 	struct exynos_drm_gem *exynos_gem = exynos_fbd->exynos_gem;
2421c248b7dSInki Dae 	struct drm_framebuffer *fb;
2431c248b7dSInki Dae 
244813fd67bSJoonyoung Shim 	vunmap(exynos_gem->kvaddr);
2454744ad24SInki Dae 
2461c248b7dSInki Dae 	/* release drm framebuffer and real buffer */
2471c248b7dSInki Dae 	if (fb_helper->fb && fb_helper->fb->funcs) {
2481c248b7dSInki Dae 		fb = fb_helper->fb;
249328c057cSDaniel Vetter 		if (fb)
250f7eff60eSRob Clark 			drm_framebuffer_remove(fb);
2511c248b7dSInki Dae 	}
2521c248b7dSInki Dae 
2537c7d4507SArchit Taneja 	drm_fb_helper_unregister_fbi(fb_helper);
2541c248b7dSInki Dae 
2551c248b7dSInki Dae 	drm_fb_helper_fini(fb_helper);
2561c248b7dSInki Dae }
2571c248b7dSInki Dae 
2581c248b7dSInki Dae void exynos_drm_fbdev_fini(struct drm_device *dev)
2591c248b7dSInki Dae {
2601c248b7dSInki Dae 	struct exynos_drm_private *private = dev->dev_private;
2611c248b7dSInki Dae 	struct exynos_drm_fbdev *fbdev;
2621c248b7dSInki Dae 
2631c248b7dSInki Dae 	if (!private || !private->fb_helper)
2641c248b7dSInki Dae 		return;
2651c248b7dSInki Dae 
2661c248b7dSInki Dae 	fbdev = to_exynos_fbdev(private->fb_helper);
2671c248b7dSInki Dae 
2681c248b7dSInki Dae 	exynos_drm_fbdev_destroy(dev, private->fb_helper);
2691c248b7dSInki Dae 	kfree(fbdev);
2701c248b7dSInki Dae 	private->fb_helper = NULL;
2711c248b7dSInki Dae }
2721c248b7dSInki Dae 
273