blob: 724cb52a374aa101be9c0c0316db6c176ecfde13 [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* 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 *
Inki Daed81aecb2012-12-18 02:30:17 +09009 * 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.
Inki Dae1c248b72011-10-04 19:19:01 +090013 */
14
David Howells760285e2012-10-02 18:01:07 +010015#include <drm/drmP.h>
16#include <drm/drm_crtc.h>
17#include <drm/drm_fb_helper.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010018#include <drm/drm_probe_helper.h>
Vikas Sajjana1bfacf2013-08-06 17:22:04 +053019#include <drm/exynos_drm.h>
Inki Dae1c248b72011-10-04 19:19:01 +090020
Marek Szyprowski6e8edf82017-09-14 14:01:01 +020021#include <linux/console.h>
22
Inki Dae1c248b72011-10-04 19:19:01 +090023#include "exynos_drm_drv.h"
24#include "exynos_drm_fb.h"
Mark Browne30655d2013-08-13 00:46:40 +010025#include "exynos_drm_fbdev.h"
Inki Dae1c248b72011-10-04 19:19:01 +090026
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
33struct exynos_drm_fbdev {
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090034 struct drm_fb_helper drm_fb_helper;
35 struct exynos_drm_gem *exynos_gem;
Inki Dae1c248b72011-10-04 19:19:01 +090036};
37
Prathyush Kdd265852012-11-19 13:55:28 +053038static 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);
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090043 struct exynos_drm_gem *exynos_gem = exynos_fbd->exynos_gem;
Prathyush Kdd265852012-11-19 13:55:28 +053044 unsigned long vm_size;
45 int ret;
46
Prathyush Kdd265852012-11-19 13:55:28 +053047 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
48
49 vm_size = vma->vm_end - vma->vm_start;
50
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090051 if (vm_size > exynos_gem->size)
Prathyush Kdd265852012-11-19 13:55:28 +053052 return -EINVAL;
53
Marek Szyprowskif43c3592016-02-29 17:50:53 +090054 ret = dma_mmap_attrs(to_dma_dev(helper->dev), vma, exynos_gem->cookie,
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090055 exynos_gem->dma_addr, exynos_gem->size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070056 exynos_gem->dma_attrs);
Prathyush Kdd265852012-11-19 13:55:28 +053057 if (ret < 0) {
Inki Dae6f83d202019-04-15 14:24:36 +090058 DRM_DEV_ERROR(to_dma_dev(helper->dev), "failed to mmap.\n");
Prathyush Kdd265852012-11-19 13:55:28 +053059 return ret;
60 }
61
62 return 0;
63}
64
Inki Dae1c248b72011-10-04 19:19:01 +090065static struct fb_ops exynos_drm_fb_ops = {
66 .owner = THIS_MODULE,
Stefan Christ2eec8382016-11-14 00:03:17 +010067 DRM_FB_HELPER_DEFAULT_OPS,
Prathyush Kdd265852012-11-19 13:55:28 +053068 .fb_mmap = exynos_drm_fb_mmap,
Archit Taneja7c7d4502015-07-22 14:58:09 +053069 .fb_fillrect = drm_fb_helper_cfb_fillrect,
70 .fb_copyarea = drm_fb_helper_cfb_copyarea,
71 .fb_imageblit = drm_fb_helper_cfb_imageblit,
Inki Dae1c248b72011-10-04 19:19:01 +090072};
73
Inki Dae19c8b832011-10-14 13:29:46 +090074static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
Joonyoung Shimd7619962015-09-01 16:22:49 +090075 struct drm_fb_helper_surface_size *sizes,
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090076 struct exynos_drm_gem *exynos_gem)
Inki Dae1c248b72011-10-04 19:19:01 +090077{
Joonyoung Shimee885ca2015-09-01 16:22:50 +090078 struct fb_info *fbi;
Joonyoung Shimd7619962015-09-01 16:22:49 +090079 struct drm_framebuffer *fb = helper->fb;
Ville Syrjälä272725c2016-12-14 23:32:20 +020080 unsigned int size = fb->width * fb->height * fb->format->cpp[0];
Carlo Caionea5d7ac302015-02-04 10:23:19 +010081 unsigned int nr_pages;
Inki Dae19c8b832011-10-14 13:29:46 +090082 unsigned long offset;
Inki Dae1c248b72011-10-04 19:19:01 +090083
Joonyoung Shimee885ca2015-09-01 16:22:50 +090084 fbi = drm_fb_helper_alloc_fbi(helper);
85 if (IS_ERR(fbi)) {
Inki Dae6f83d202019-04-15 14:24:36 +090086 DRM_DEV_ERROR(to_dma_dev(helper->dev),
87 "failed to allocate fb info.\n");
Joonyoung Shimee885ca2015-09-01 16:22:50 +090088 return PTR_ERR(fbi);
89 }
90
Joonyoung Shimee885ca2015-09-01 16:22:50 +090091 fbi->fbops = &exynos_drm_fb_ops;
92
Daniel Vetterfb68e592019-03-26 14:19:55 +010093 drm_fb_helper_fill_info(fbi, helper, sizes);
Inki Dae1c248b72011-10-04 19:19:01 +090094
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090095 nr_pages = exynos_gem->size >> PAGE_SHIFT;
Inki Daec704f1b2012-12-21 17:59:20 +090096
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090097 exynos_gem->kvaddr = (void __iomem *) vmap(exynos_gem->pages, nr_pages,
98 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
99 if (!exynos_gem->kvaddr) {
Inki Dae6f83d202019-04-15 14:24:36 +0900100 DRM_DEV_ERROR(to_dma_dev(helper->dev),
101 "failed to map pages to kernel space.\n");
Carlo Caionea5d7ac302015-02-04 10:23:19 +0100102 return -EIO;
Inki Dae4744ad242012-12-07 17:51:27 +0900103 }
104
Ville Syrjälä272725c2016-12-14 23:32:20 +0200105 offset = fbi->var.xoffset * fb->format->cpp[0];
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200106 offset += fbi->var.yoffset * fb->pitches[0];
Inki Dae1c248b72011-10-04 19:19:01 +0900107
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900108 fbi->screen_base = exynos_gem->kvaddr + offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900109 fbi->screen_size = size;
Daniel Kurtz71b1f192014-09-01 21:28:00 +0900110 fbi->fix.smem_len = size;
Inki Dae19c8b832011-10-14 13:29:46 +0900111
112 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900113}
114
115static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
116 struct drm_fb_helper_surface_size *sizes)
117{
118 struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900119 struct exynos_drm_gem *exynos_gem;
Inki Dae1c248b72011-10-04 19:19:01 +0900120 struct drm_device *dev = helper->dev;
Joonyoung Shima794d572011-12-08 15:05:19 +0900121 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Joonyoung Shime1533c02011-12-13 14:46:57 +0900122 unsigned long size;
Inki Dae1c248b72011-10-04 19:19:01 +0900123 int ret;
124
Inki Dae6be90052019-04-15 16:25:12 +0900125 DRM_DEV_DEBUG_KMS(dev->dev,
126 "surface width(%d), height(%d) and bpp(%d\n",
127 sizes->surface_width, sizes->surface_height,
128 sizes->surface_bpp);
Inki Dae1c248b72011-10-04 19:19:01 +0900129
130 mode_cmd.width = sizes->surface_width;
131 mode_cmd.height = sizes->surface_height;
Joonyoung Shima794d572011-12-08 15:05:19 +0900132 mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
133 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
134 sizes->surface_depth);
Inki Dae1c248b72011-10-04 19:19:01 +0900135
Joonyoung Shime1533c02011-12-13 14:46:57 +0900136 size = mode_cmd.pitches[0] * mode_cmd.height;
Inki Dae2b358922012-03-16 18:47:05 +0900137
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900138 exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG, size);
Vikas Sajjana1bfacf2013-08-06 17:22:04 +0530139 /*
140 * If physically contiguous memory allocation fails and if IOMMU is
141 * supported then try to get buffer from non physically contiguous
142 * memory area.
143 */
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900144 if (IS_ERR(exynos_gem) && is_drm_iommu_supported(dev)) {
Laurent Pinchart896bbc32016-12-12 11:28:47 +0200145 dev_warn(dev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900146 exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_NONCONTIG,
147 size);
Vikas Sajjana1bfacf2013-08-06 17:22:04 +0530148 }
149
Daniel Vetter2f424202016-03-30 11:40:48 +0200150 if (IS_ERR(exynos_gem))
151 return PTR_ERR(exynos_gem);
Inki Dae1c248b72011-10-04 19:19:01 +0900152
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900153 exynos_fbdev->exynos_gem = exynos_gem;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900154
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900155 helper->fb =
156 exynos_drm_framebuffer_init(dev, &mode_cmd, &exynos_gem, 1);
Sachin Kamat41eab402013-04-29 12:27:05 +0530157 if (IS_ERR(helper->fb)) {
Inki Dae6f83d202019-04-15 14:24:36 +0900158 DRM_DEV_ERROR(dev->dev, "failed to create drm framebuffer.\n");
Joonyoung Shime1533c02011-12-13 14:46:57 +0900159 ret = PTR_ERR(helper->fb);
Inki Dae662aa6d2012-12-07 18:06:43 +0900160 goto err_destroy_gem;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900161 }
162
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900163 ret = exynos_drm_fbdev_update(helper, sizes, exynos_gem);
Inki Dae662aa6d2012-12-07 18:06:43 +0900164 if (ret < 0)
Archit Taneja7c7d4502015-07-22 14:58:09 +0530165 goto err_destroy_framebuffer;
Inki Dae662aa6d2012-12-07 18:06:43 +0900166
Inki Dae662aa6d2012-12-07 18:06:43 +0900167 return ret;
168
Inki Dae662aa6d2012-12-07 18:06:43 +0900169err_destroy_framebuffer:
170 drm_framebuffer_cleanup(helper->fb);
171err_destroy_gem:
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900172 exynos_drm_gem_destroy(exynos_gem);
Inki Dae1c248b72011-10-04 19:19:01 +0900173
Daniel Vetter2f424202016-03-30 11:40:48 +0200174 /*
175 * if failed, all resources allocated above would be released by
176 * drm_mode_config_cleanup() when drm_load() had been called prior
177 * to any specific driver such as fimd or hdmi driver.
178 */
179
Inki Dae1c248b72011-10-04 19:19:01 +0900180 return ret;
181}
182
Thierry Reding3a493872014-06-27 17:19:23 +0200183static const struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
Daniel Vettercd5428a2013-01-21 23:42:49 +0100184 .fb_probe = exynos_drm_fbdev_create,
Inki Dae1c248b72011-10-04 19:19:01 +0900185};
186
187int exynos_drm_fbdev_init(struct drm_device *dev)
188{
189 struct exynos_drm_fbdev *fbdev;
190 struct exynos_drm_private *private = dev->dev_private;
191 struct drm_fb_helper *helper;
Inki Dae1c248b72011-10-04 19:19:01 +0900192 int ret;
193
Andrzej Hajda989534c2018-10-26 12:13:28 +0200194 if (!dev->mode_config.num_crtc)
Inki Dae1c248b72011-10-04 19:19:01 +0900195 return 0;
196
197 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900198 if (!fbdev)
Inki Dae1c248b72011-10-04 19:19:01 +0900199 return -ENOMEM;
Inki Dae1c248b72011-10-04 19:19:01 +0900200
201 private->fb_helper = helper = &fbdev->drm_fb_helper;
Thierry Reding10a23102014-06-27 17:19:24 +0200202
203 drm_fb_helper_prepare(dev, helper, &exynos_drm_fb_helper_funcs);
Inki Dae1c248b72011-10-04 19:19:01 +0900204
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200205 ret = drm_fb_helper_init(dev, helper, MAX_CONNECTOR);
Inki Dae1c248b72011-10-04 19:19:01 +0900206 if (ret < 0) {
Inki Dae6f83d202019-04-15 14:24:36 +0900207 DRM_DEV_ERROR(dev->dev,
208 "failed to initialize drm fb helper.\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900209 goto err_init;
210 }
211
212 ret = drm_fb_helper_single_add_all_connectors(helper);
213 if (ret < 0) {
Inki Dae6f83d202019-04-15 14:24:36 +0900214 DRM_DEV_ERROR(dev->dev,
215 "failed to register drm_fb_helper_connector.\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900216 goto err_setup;
217
218 }
219
220 ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
221 if (ret < 0) {
Inki Dae6f83d202019-04-15 14:24:36 +0900222 DRM_DEV_ERROR(dev->dev,
223 "failed to set up hw configuration.\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900224 goto err_setup;
225 }
226
227 return 0;
228
229err_setup:
230 drm_fb_helper_fini(helper);
231
232err_init:
233 private->fb_helper = NULL;
234 kfree(fbdev);
235
236 return ret;
237}
238
239static void exynos_drm_fbdev_destroy(struct drm_device *dev,
240 struct drm_fb_helper *fb_helper)
241{
Inki Dae4744ad242012-12-07 17:51:27 +0900242 struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900243 struct exynos_drm_gem *exynos_gem = exynos_fbd->exynos_gem;
Inki Dae1c248b72011-10-04 19:19:01 +0900244 struct drm_framebuffer *fb;
245
Markus Elfringe8d3ef02016-07-21 19:23:25 +0200246 vunmap(exynos_gem->kvaddr);
Inki Dae4744ad242012-12-07 17:51:27 +0900247
Inki Dae1c248b72011-10-04 19:19:01 +0900248 /* release drm framebuffer and real buffer */
249 if (fb_helper->fb && fb_helper->fb->funcs) {
250 fb = fb_helper->fb;
Daniel Vetter328c0572016-12-27 11:49:23 +0100251 if (fb)
Rob Clarkf7eff602012-09-05 21:48:38 +0000252 drm_framebuffer_remove(fb);
Inki Dae1c248b72011-10-04 19:19:01 +0900253 }
254
Archit Taneja7c7d4502015-07-22 14:58:09 +0530255 drm_fb_helper_unregister_fbi(fb_helper);
Inki Dae1c248b72011-10-04 19:19:01 +0900256
257 drm_fb_helper_fini(fb_helper);
258}
259
260void exynos_drm_fbdev_fini(struct drm_device *dev)
261{
262 struct exynos_drm_private *private = dev->dev_private;
263 struct exynos_drm_fbdev *fbdev;
264
265 if (!private || !private->fb_helper)
266 return;
267
268 fbdev = to_exynos_fbdev(private->fb_helper);
269
270 exynos_drm_fbdev_destroy(dev, private->fb_helper);
271 kfree(fbdev);
272 private->fb_helper = NULL;
273}
274