blob: 114ba4073524b4356abefb626fd91dd29a1ccd42 [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>
18#include <drm/drm_crtc_helper.h>
Vikas Sajjana1bfacf2013-08-06 17:22:04 +053019#include <drm/exynos_drm.h>
Inki Dae1c248b72011-10-04 19:19:01 +090020
21#include "exynos_drm_drv.h"
22#include "exynos_drm_fb.h"
Mark Browne30655d2013-08-13 00:46:40 +010023#include "exynos_drm_fbdev.h"
Inki Daec704f1b2012-12-21 17:59:20 +090024#include "exynos_drm_iommu.h"
Inki Dae1c248b72011-10-04 19:19:01 +090025
26#define MAX_CONNECTOR 4
27#define PREFERRED_BPP 32
28
29#define to_exynos_fbdev(x) container_of(x, struct exynos_drm_fbdev,\
30 drm_fb_helper)
31
32struct exynos_drm_fbdev {
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090033 struct drm_fb_helper drm_fb_helper;
34 struct exynos_drm_gem *exynos_gem;
Inki Dae1c248b72011-10-04 19:19:01 +090035};
36
Prathyush Kdd265852012-11-19 13:55:28 +053037static int exynos_drm_fb_mmap(struct fb_info *info,
38 struct vm_area_struct *vma)
39{
40 struct drm_fb_helper *helper = info->par;
41 struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper);
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090042 struct exynos_drm_gem *exynos_gem = exynos_fbd->exynos_gem;
Prathyush Kdd265852012-11-19 13:55:28 +053043 unsigned long vm_size;
44 int ret;
45
Prathyush Kdd265852012-11-19 13:55:28 +053046 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
47
48 vm_size = vma->vm_end - vma->vm_start;
49
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090050 if (vm_size > exynos_gem->size)
Prathyush Kdd265852012-11-19 13:55:28 +053051 return -EINVAL;
52
Marek Szyprowskif43c3592016-02-29 17:50:53 +090053 ret = dma_mmap_attrs(to_dma_dev(helper->dev), vma, exynos_gem->cookie,
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090054 exynos_gem->dma_addr, exynos_gem->size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070055 exynos_gem->dma_attrs);
Prathyush Kdd265852012-11-19 13:55:28 +053056 if (ret < 0) {
57 DRM_ERROR("failed to mmap.\n");
58 return ret;
59 }
60
61 return 0;
62}
63
Inki Dae1c248b72011-10-04 19:19:01 +090064static struct fb_ops exynos_drm_fb_ops = {
65 .owner = THIS_MODULE,
Stefan Christ2eec8382016-11-14 00:03:17 +010066 DRM_FB_HELPER_DEFAULT_OPS,
Prathyush Kdd265852012-11-19 13:55:28 +053067 .fb_mmap = exynos_drm_fb_mmap,
Archit Taneja7c7d4502015-07-22 14:58:09 +053068 .fb_fillrect = drm_fb_helper_cfb_fillrect,
69 .fb_copyarea = drm_fb_helper_cfb_copyarea,
70 .fb_imageblit = drm_fb_helper_cfb_imageblit,
Inki Dae1c248b72011-10-04 19:19:01 +090071};
72
Inki Dae19c8b832011-10-14 13:29:46 +090073static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
Joonyoung Shimd7619962015-09-01 16:22:49 +090074 struct drm_fb_helper_surface_size *sizes,
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090075 struct exynos_drm_gem *exynos_gem)
Inki Dae1c248b72011-10-04 19:19:01 +090076{
Joonyoung Shimee885ca2015-09-01 16:22:50 +090077 struct fb_info *fbi;
Joonyoung Shimd7619962015-09-01 16:22:49 +090078 struct drm_framebuffer *fb = helper->fb;
Ville Syrjälä272725c2016-12-14 23:32:20 +020079 unsigned int size = fb->width * fb->height * fb->format->cpp[0];
Carlo Caionea5d7ac302015-02-04 10:23:19 +010080 unsigned int nr_pages;
Inki Dae19c8b832011-10-14 13:29:46 +090081 unsigned long offset;
Inki Dae1c248b72011-10-04 19:19:01 +090082
Joonyoung Shimee885ca2015-09-01 16:22:50 +090083 fbi = drm_fb_helper_alloc_fbi(helper);
84 if (IS_ERR(fbi)) {
85 DRM_ERROR("failed to allocate fb info.\n");
86 return PTR_ERR(fbi);
87 }
88
89 fbi->par = helper;
90 fbi->flags = FBINFO_FLAG_DEFAULT;
91 fbi->fbops = &exynos_drm_fb_ops;
92
Ville Syrjäläb00c6002016-12-14 23:31:35 +020093 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
Rob Clarkecbf1d52015-03-11 10:23:11 -040094 drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
Inki Dae1c248b72011-10-04 19:19:01 +090095
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090096 nr_pages = exynos_gem->size >> PAGE_SHIFT;
Inki Daec704f1b2012-12-21 17:59:20 +090097
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090098 exynos_gem->kvaddr = (void __iomem *) vmap(exynos_gem->pages, nr_pages,
99 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
100 if (!exynos_gem->kvaddr) {
Carlo Caionea5d7ac302015-02-04 10:23:19 +0100101 DRM_ERROR("failed to map pages to kernel space.\n");
Joonyoung Shimee885ca2015-09-01 16:22:50 +0900102 drm_fb_helper_release_fbi(helper);
Carlo Caionea5d7ac302015-02-04 10:23:19 +0100103 return -EIO;
Inki Dae4744ad22012-12-07 17:51:27 +0900104 }
105
Ville Syrjälä272725c2016-12-14 23:32:20 +0200106 offset = fbi->var.xoffset * fb->format->cpp[0];
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200107 offset += fbi->var.yoffset * fb->pitches[0];
Inki Dae1c248b72011-10-04 19:19:01 +0900108
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900109 fbi->screen_base = exynos_gem->kvaddr + offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900110 fbi->screen_size = size;
Daniel Kurtz71b1f192014-09-01 21:28:00 +0900111 fbi->fix.smem_len = size;
Inki Dae19c8b832011-10-14 13:29:46 +0900112
113 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900114}
115
116static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
117 struct drm_fb_helper_surface_size *sizes)
118{
119 struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900120 struct exynos_drm_gem *exynos_gem;
Inki Dae1c248b72011-10-04 19:19:01 +0900121 struct drm_device *dev = helper->dev;
Joonyoung Shima794d572011-12-08 15:05:19 +0900122 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Joonyoung Shime1533c02011-12-13 14:46:57 +0900123 unsigned long size;
Inki Dae1c248b72011-10-04 19:19:01 +0900124 int ret;
125
Inki Dae1c248b72011-10-04 19:19:01 +0900126 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
127 sizes->surface_width, sizes->surface_height,
128 sizes->surface_bpp);
129
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)) {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900158 DRM_ERROR("failed to create drm framebuffer.\n");
159 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
Jingoo Han9230ffc2014-04-17 19:07:22 +0900187static bool exynos_drm_fbdev_is_anything_connected(struct drm_device *dev)
Andrzej Hajda3eb578e2014-03-28 12:52:38 +0100188{
189 struct drm_connector *connector;
190 bool ret = false;
191
192 mutex_lock(&dev->mode_config.mutex);
193 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
194 if (connector->status != connector_status_connected)
195 continue;
196
197 ret = true;
198 break;
199 }
200 mutex_unlock(&dev->mode_config.mutex);
201
202 return ret;
203}
204
Inki Dae1c248b72011-10-04 19:19:01 +0900205int exynos_drm_fbdev_init(struct drm_device *dev)
206{
207 struct exynos_drm_fbdev *fbdev;
208 struct exynos_drm_private *private = dev->dev_private;
209 struct drm_fb_helper *helper;
Inki Dae1c248b72011-10-04 19:19:01 +0900210 int ret;
211
Inki Dae1c248b72011-10-04 19:19:01 +0900212 if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
213 return 0;
214
Andrzej Hajda3eb578e2014-03-28 12:52:38 +0100215 if (!exynos_drm_fbdev_is_anything_connected(dev))
216 return 0;
217
Inki Dae1c248b72011-10-04 19:19:01 +0900218 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900219 if (!fbdev)
Inki Dae1c248b72011-10-04 19:19:01 +0900220 return -ENOMEM;
Inki Dae1c248b72011-10-04 19:19:01 +0900221
222 private->fb_helper = helper = &fbdev->drm_fb_helper;
Thierry Reding10a23102014-06-27 17:19:24 +0200223
224 drm_fb_helper_prepare(dev, helper, &exynos_drm_fb_helper_funcs);
Inki Dae1c248b72011-10-04 19:19:01 +0900225
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200226 ret = drm_fb_helper_init(dev, helper, MAX_CONNECTOR);
Inki Dae1c248b72011-10-04 19:19:01 +0900227 if (ret < 0) {
228 DRM_ERROR("failed to initialize drm fb helper.\n");
229 goto err_init;
230 }
231
232 ret = drm_fb_helper_single_add_all_connectors(helper);
233 if (ret < 0) {
234 DRM_ERROR("failed to register drm_fb_helper_connector.\n");
235 goto err_setup;
236
237 }
238
239 ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
240 if (ret < 0) {
241 DRM_ERROR("failed to set up hw configuration.\n");
242 goto err_setup;
243 }
244
245 return 0;
246
247err_setup:
248 drm_fb_helper_fini(helper);
249
250err_init:
251 private->fb_helper = NULL;
252 kfree(fbdev);
253
254 return ret;
255}
256
257static void exynos_drm_fbdev_destroy(struct drm_device *dev,
258 struct drm_fb_helper *fb_helper)
259{
Inki Dae4744ad22012-12-07 17:51:27 +0900260 struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900261 struct exynos_drm_gem *exynos_gem = exynos_fbd->exynos_gem;
Inki Dae1c248b72011-10-04 19:19:01 +0900262 struct drm_framebuffer *fb;
263
Markus Elfringe8d3ef02016-07-21 19:23:25 +0200264 vunmap(exynos_gem->kvaddr);
Inki Dae4744ad22012-12-07 17:51:27 +0900265
Inki Dae1c248b72011-10-04 19:19:01 +0900266 /* release drm framebuffer and real buffer */
267 if (fb_helper->fb && fb_helper->fb->funcs) {
268 fb = fb_helper->fb;
Daniel Vetter328c0572016-12-27 11:49:23 +0100269 if (fb)
Rob Clarkf7eff602012-09-05 21:48:38 +0000270 drm_framebuffer_remove(fb);
Inki Dae1c248b72011-10-04 19:19:01 +0900271 }
272
Archit Taneja7c7d4502015-07-22 14:58:09 +0530273 drm_fb_helper_unregister_fbi(fb_helper);
274 drm_fb_helper_release_fbi(fb_helper);
Inki Dae1c248b72011-10-04 19:19:01 +0900275
276 drm_fb_helper_fini(fb_helper);
277}
278
279void exynos_drm_fbdev_fini(struct drm_device *dev)
280{
281 struct exynos_drm_private *private = dev->dev_private;
282 struct exynos_drm_fbdev *fbdev;
283
284 if (!private || !private->fb_helper)
285 return;
286
287 fbdev = to_exynos_fbdev(private->fb_helper);
288
289 exynos_drm_fbdev_destroy(dev, private->fb_helper);
290 kfree(fbdev);
291 private->fb_helper = NULL;
292}
293
294void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
295{
296 struct exynos_drm_private *private = dev->dev_private;
297
298 if (!private || !private->fb_helper)
299 return;
300
Rob Clark5ea1f752014-05-30 12:29:48 -0400301 drm_fb_helper_restore_fbdev_mode_unlocked(private->fb_helper);
Inki Dae1c248b72011-10-04 19:19:01 +0900302}
Andrzej Hajda25c6a852016-03-15 12:43:21 +0100303
304void exynos_drm_output_poll_changed(struct drm_device *dev)
305{
306 struct exynos_drm_private *private = dev->dev_private;
307 struct drm_fb_helper *fb_helper = private->fb_helper;
308
309 if (fb_helper)
310 drm_fb_helper_hotplug_event(fb_helper);
311 else
312 exynos_drm_fbdev_init(dev);
313}