blob: 9dc33c6b6687489bef192bf144678a9586a611cc [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Inki Dae1c248b72011-10-04 19:19:01 +09002/* exynos_drm_fbdev.c
3 *
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Authors:
6 * Inki Dae <inki.dae@samsung.com>
7 * Joonyoung Shim <jy0922.shim@samsung.com>
8 * Seung-Woo Kim <sw0312.kim@samsung.com>
Inki Dae1c248b72011-10-04 19:19:01 +09009 */
10
David Howells760285e2012-10-02 18:01:07 +010011#include <drm/drmP.h>
12#include <drm/drm_crtc.h>
13#include <drm/drm_fb_helper.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010014#include <drm/drm_probe_helper.h>
Vikas Sajjana1bfacf2013-08-06 17:22:04 +053015#include <drm/exynos_drm.h>
Inki Dae1c248b72011-10-04 19:19:01 +090016
Marek Szyprowski6e8edf82017-09-14 14:01:01 +020017#include <linux/console.h>
18
Inki Dae1c248b72011-10-04 19:19:01 +090019#include "exynos_drm_drv.h"
20#include "exynos_drm_fb.h"
Mark Browne30655d2013-08-13 00:46:40 +010021#include "exynos_drm_fbdev.h"
Inki Dae1c248b72011-10-04 19:19:01 +090022
23#define MAX_CONNECTOR 4
24#define PREFERRED_BPP 32
25
26#define to_exynos_fbdev(x) container_of(x, struct exynos_drm_fbdev,\
27 drm_fb_helper)
28
29struct exynos_drm_fbdev {
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090030 struct drm_fb_helper drm_fb_helper;
31 struct exynos_drm_gem *exynos_gem;
Inki Dae1c248b72011-10-04 19:19:01 +090032};
33
Prathyush Kdd265852012-11-19 13:55:28 +053034static int exynos_drm_fb_mmap(struct fb_info *info,
35 struct vm_area_struct *vma)
36{
37 struct drm_fb_helper *helper = info->par;
38 struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper);
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090039 struct exynos_drm_gem *exynos_gem = exynos_fbd->exynos_gem;
Prathyush Kdd265852012-11-19 13:55:28 +053040 unsigned long vm_size;
41 int ret;
42
Prathyush Kdd265852012-11-19 13:55:28 +053043 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
44
45 vm_size = vma->vm_end - vma->vm_start;
46
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090047 if (vm_size > exynos_gem->size)
Prathyush Kdd265852012-11-19 13:55:28 +053048 return -EINVAL;
49
Marek Szyprowskif43c3592016-02-29 17:50:53 +090050 ret = dma_mmap_attrs(to_dma_dev(helper->dev), vma, exynos_gem->cookie,
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090051 exynos_gem->dma_addr, exynos_gem->size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070052 exynos_gem->dma_attrs);
Prathyush Kdd265852012-11-19 13:55:28 +053053 if (ret < 0) {
Inki Dae6f83d202019-04-15 14:24:36 +090054 DRM_DEV_ERROR(to_dma_dev(helper->dev), "failed to mmap.\n");
Prathyush Kdd265852012-11-19 13:55:28 +053055 return ret;
56 }
57
58 return 0;
59}
60
Inki Dae1c248b72011-10-04 19:19:01 +090061static struct fb_ops exynos_drm_fb_ops = {
62 .owner = THIS_MODULE,
Stefan Christ2eec8382016-11-14 00:03:17 +010063 DRM_FB_HELPER_DEFAULT_OPS,
Prathyush Kdd265852012-11-19 13:55:28 +053064 .fb_mmap = exynos_drm_fb_mmap,
Archit Taneja7c7d4502015-07-22 14:58:09 +053065 .fb_fillrect = drm_fb_helper_cfb_fillrect,
66 .fb_copyarea = drm_fb_helper_cfb_copyarea,
67 .fb_imageblit = drm_fb_helper_cfb_imageblit,
Inki Dae1c248b72011-10-04 19:19:01 +090068};
69
Inki Dae19c8b832011-10-14 13:29:46 +090070static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
Joonyoung Shimd7619962015-09-01 16:22:49 +090071 struct drm_fb_helper_surface_size *sizes,
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090072 struct exynos_drm_gem *exynos_gem)
Inki Dae1c248b72011-10-04 19:19:01 +090073{
Joonyoung Shimee885ca2015-09-01 16:22:50 +090074 struct fb_info *fbi;
Joonyoung Shimd7619962015-09-01 16:22:49 +090075 struct drm_framebuffer *fb = helper->fb;
Ville Syrjälä272725c2016-12-14 23:32:20 +020076 unsigned int size = fb->width * fb->height * fb->format->cpp[0];
Carlo Caionea5d7ac302015-02-04 10:23:19 +010077 unsigned int nr_pages;
Inki Dae19c8b832011-10-14 13:29:46 +090078 unsigned long offset;
Inki Dae1c248b72011-10-04 19:19:01 +090079
Joonyoung Shimee885ca2015-09-01 16:22:50 +090080 fbi = drm_fb_helper_alloc_fbi(helper);
81 if (IS_ERR(fbi)) {
Inki Dae6f83d202019-04-15 14:24:36 +090082 DRM_DEV_ERROR(to_dma_dev(helper->dev),
83 "failed to allocate fb info.\n");
Joonyoung Shimee885ca2015-09-01 16:22:50 +090084 return PTR_ERR(fbi);
85 }
86
Joonyoung Shimee885ca2015-09-01 16:22:50 +090087 fbi->fbops = &exynos_drm_fb_ops;
88
Daniel Vetterfb68e592019-03-26 14:19:55 +010089 drm_fb_helper_fill_info(fbi, helper, sizes);
Inki Dae1c248b72011-10-04 19:19:01 +090090
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090091 nr_pages = exynos_gem->size >> PAGE_SHIFT;
Inki Daec704f1b2012-12-21 17:59:20 +090092
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090093 exynos_gem->kvaddr = (void __iomem *) vmap(exynos_gem->pages, nr_pages,
94 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
95 if (!exynos_gem->kvaddr) {
Inki Dae6f83d202019-04-15 14:24:36 +090096 DRM_DEV_ERROR(to_dma_dev(helper->dev),
97 "failed to map pages to kernel space.\n");
Carlo Caionea5d7ac302015-02-04 10:23:19 +010098 return -EIO;
Inki Dae4744ad242012-12-07 17:51:27 +090099 }
100
Ville Syrjälä272725c2016-12-14 23:32:20 +0200101 offset = fbi->var.xoffset * fb->format->cpp[0];
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200102 offset += fbi->var.yoffset * fb->pitches[0];
Inki Dae1c248b72011-10-04 19:19:01 +0900103
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900104 fbi->screen_base = exynos_gem->kvaddr + offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900105 fbi->screen_size = size;
Daniel Kurtz71b1f192014-09-01 21:28:00 +0900106 fbi->fix.smem_len = size;
Inki Dae19c8b832011-10-14 13:29:46 +0900107
108 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900109}
110
111static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
112 struct drm_fb_helper_surface_size *sizes)
113{
114 struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900115 struct exynos_drm_gem *exynos_gem;
Inki Dae1c248b72011-10-04 19:19:01 +0900116 struct drm_device *dev = helper->dev;
Joonyoung Shima794d572011-12-08 15:05:19 +0900117 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Joonyoung Shime1533c02011-12-13 14:46:57 +0900118 unsigned long size;
Inki Dae1c248b72011-10-04 19:19:01 +0900119 int ret;
120
Inki Dae6be90052019-04-15 16:25:12 +0900121 DRM_DEV_DEBUG_KMS(dev->dev,
122 "surface width(%d), height(%d) and bpp(%d\n",
123 sizes->surface_width, sizes->surface_height,
124 sizes->surface_bpp);
Inki Dae1c248b72011-10-04 19:19:01 +0900125
126 mode_cmd.width = sizes->surface_width;
127 mode_cmd.height = sizes->surface_height;
Joonyoung Shima794d572011-12-08 15:05:19 +0900128 mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
129 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
130 sizes->surface_depth);
Inki Dae1c248b72011-10-04 19:19:01 +0900131
Joonyoung Shime1533c02011-12-13 14:46:57 +0900132 size = mode_cmd.pitches[0] * mode_cmd.height;
Inki Dae2b358922012-03-16 18:47:05 +0900133
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900134 exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG, size);
Vikas Sajjana1bfacf2013-08-06 17:22:04 +0530135 /*
136 * If physically contiguous memory allocation fails and if IOMMU is
137 * supported then try to get buffer from non physically contiguous
138 * memory area.
139 */
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900140 if (IS_ERR(exynos_gem) && is_drm_iommu_supported(dev)) {
Laurent Pinchart896bbc32016-12-12 11:28:47 +0200141 dev_warn(dev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900142 exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_NONCONTIG,
143 size);
Vikas Sajjana1bfacf2013-08-06 17:22:04 +0530144 }
145
Daniel Vetter2f424202016-03-30 11:40:48 +0200146 if (IS_ERR(exynos_gem))
147 return PTR_ERR(exynos_gem);
Inki Dae1c248b72011-10-04 19:19:01 +0900148
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900149 exynos_fbdev->exynos_gem = exynos_gem;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900150
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900151 helper->fb =
152 exynos_drm_framebuffer_init(dev, &mode_cmd, &exynos_gem, 1);
Sachin Kamat41eab402013-04-29 12:27:05 +0530153 if (IS_ERR(helper->fb)) {
Inki Dae6f83d202019-04-15 14:24:36 +0900154 DRM_DEV_ERROR(dev->dev, "failed to create drm framebuffer.\n");
Joonyoung Shime1533c02011-12-13 14:46:57 +0900155 ret = PTR_ERR(helper->fb);
Inki Dae662aa6d2012-12-07 18:06:43 +0900156 goto err_destroy_gem;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900157 }
158
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900159 ret = exynos_drm_fbdev_update(helper, sizes, exynos_gem);
Inki Dae662aa6d2012-12-07 18:06:43 +0900160 if (ret < 0)
Archit Taneja7c7d4502015-07-22 14:58:09 +0530161 goto err_destroy_framebuffer;
Inki Dae662aa6d2012-12-07 18:06:43 +0900162
Inki Dae662aa6d2012-12-07 18:06:43 +0900163 return ret;
164
Inki Dae662aa6d2012-12-07 18:06:43 +0900165err_destroy_framebuffer:
166 drm_framebuffer_cleanup(helper->fb);
167err_destroy_gem:
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900168 exynos_drm_gem_destroy(exynos_gem);
Inki Dae1c248b72011-10-04 19:19:01 +0900169
Daniel Vetter2f424202016-03-30 11:40:48 +0200170 /*
171 * if failed, all resources allocated above would be released by
172 * drm_mode_config_cleanup() when drm_load() had been called prior
173 * to any specific driver such as fimd or hdmi driver.
174 */
175
Inki Dae1c248b72011-10-04 19:19:01 +0900176 return ret;
177}
178
Thierry Reding3a493872014-06-27 17:19:23 +0200179static const struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
Daniel Vettercd5428a2013-01-21 23:42:49 +0100180 .fb_probe = exynos_drm_fbdev_create,
Inki Dae1c248b72011-10-04 19:19:01 +0900181};
182
183int exynos_drm_fbdev_init(struct drm_device *dev)
184{
185 struct exynos_drm_fbdev *fbdev;
186 struct exynos_drm_private *private = dev->dev_private;
187 struct drm_fb_helper *helper;
Inki Dae1c248b72011-10-04 19:19:01 +0900188 int ret;
189
Andrzej Hajda989534c2018-10-26 12:13:28 +0200190 if (!dev->mode_config.num_crtc)
Inki Dae1c248b72011-10-04 19:19:01 +0900191 return 0;
192
193 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900194 if (!fbdev)
Inki Dae1c248b72011-10-04 19:19:01 +0900195 return -ENOMEM;
Inki Dae1c248b72011-10-04 19:19:01 +0900196
197 private->fb_helper = helper = &fbdev->drm_fb_helper;
Thierry Reding10a23102014-06-27 17:19:24 +0200198
199 drm_fb_helper_prepare(dev, helper, &exynos_drm_fb_helper_funcs);
Inki Dae1c248b72011-10-04 19:19:01 +0900200
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200201 ret = drm_fb_helper_init(dev, helper, MAX_CONNECTOR);
Inki Dae1c248b72011-10-04 19:19:01 +0900202 if (ret < 0) {
Inki Dae6f83d202019-04-15 14:24:36 +0900203 DRM_DEV_ERROR(dev->dev,
204 "failed to initialize drm fb helper.\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900205 goto err_init;
206 }
207
208 ret = drm_fb_helper_single_add_all_connectors(helper);
209 if (ret < 0) {
Inki Dae6f83d202019-04-15 14:24:36 +0900210 DRM_DEV_ERROR(dev->dev,
211 "failed to register drm_fb_helper_connector.\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900212 goto err_setup;
213
214 }
215
216 ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
217 if (ret < 0) {
Inki Dae6f83d202019-04-15 14:24:36 +0900218 DRM_DEV_ERROR(dev->dev,
219 "failed to set up hw configuration.\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900220 goto err_setup;
221 }
222
223 return 0;
224
225err_setup:
226 drm_fb_helper_fini(helper);
227
228err_init:
229 private->fb_helper = NULL;
230 kfree(fbdev);
231
232 return ret;
233}
234
235static void exynos_drm_fbdev_destroy(struct drm_device *dev,
236 struct drm_fb_helper *fb_helper)
237{
Inki Dae4744ad242012-12-07 17:51:27 +0900238 struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900239 struct exynos_drm_gem *exynos_gem = exynos_fbd->exynos_gem;
Inki Dae1c248b72011-10-04 19:19:01 +0900240 struct drm_framebuffer *fb;
241
Markus Elfringe8d3ef02016-07-21 19:23:25 +0200242 vunmap(exynos_gem->kvaddr);
Inki Dae4744ad242012-12-07 17:51:27 +0900243
Inki Dae1c248b72011-10-04 19:19:01 +0900244 /* release drm framebuffer and real buffer */
245 if (fb_helper->fb && fb_helper->fb->funcs) {
246 fb = fb_helper->fb;
Daniel Vetter328c0572016-12-27 11:49:23 +0100247 if (fb)
Rob Clarkf7eff602012-09-05 21:48:38 +0000248 drm_framebuffer_remove(fb);
Inki Dae1c248b72011-10-04 19:19:01 +0900249 }
250
Archit Taneja7c7d4502015-07-22 14:58:09 +0530251 drm_fb_helper_unregister_fbi(fb_helper);
Inki Dae1c248b72011-10-04 19:19:01 +0900252
253 drm_fb_helper_fini(fb_helper);
254}
255
256void exynos_drm_fbdev_fini(struct drm_device *dev)
257{
258 struct exynos_drm_private *private = dev->dev_private;
259 struct exynos_drm_fbdev *fbdev;
260
261 if (!private || !private->fb_helper)
262 return;
263
264 fbdev = to_exynos_fbdev(private->fb_helper);
265
266 exynos_drm_fbdev_destroy(dev, private->fb_helper);
267 kfree(fbdev);
268 private->fb_helper = NULL;
269}
270