blob: 9f52382e19ee338e18dbe8212b0d1a69c604d3e9 [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* exynos_drm_fb.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_crtc_helper.h>
18#include <drm/drm_fb_helper.h>
Gustavo Padovand6562a22015-06-01 12:04:52 -030019#include <drm/drm_atomic.h>
Gustavo Padovan910874a2015-06-01 12:04:46 -030020#include <drm/drm_atomic_helper.h>
Daniel Stoneb11954a2018-03-30 15:11:30 +010021#include <drm/drm_gem_framebuffer_helper.h>
Inki Dae0519f9a2012-10-20 07:53:42 -070022#include <uapi/drm/exynos_drm.h>
Inki Dae1c248b72011-10-04 19:19:01 +090023
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +090024#include "exynos_drm_drv.h"
Inki Dae1c248b72011-10-04 19:19:01 +090025#include "exynos_drm_fb.h"
Andrzej Hajda25928a32014-03-17 11:27:17 +010026#include "exynos_drm_fbdev.h"
Inki Dae0519f9a2012-10-20 07:53:42 -070027#include "exynos_drm_iommu.h"
Sean Paul080be03d2014-02-19 21:02:55 +090028#include "exynos_drm_crtc.h"
Inki Dae1c248b72011-10-04 19:19:01 +090029
Inki Dae0519f9a2012-10-20 07:53:42 -070030static int check_fb_gem_memory_type(struct drm_device *drm_dev,
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090031 struct exynos_drm_gem *exynos_gem)
Inki Dae0519f9a2012-10-20 07:53:42 -070032{
33 unsigned int flags;
34
35 /*
36 * if exynos drm driver supports iommu then framebuffer can use
37 * all the buffer types.
38 */
39 if (is_drm_iommu_supported(drm_dev))
40 return 0;
41
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090042 flags = exynos_gem->flags;
Inki Dae0519f9a2012-10-20 07:53:42 -070043
44 /*
Shuah Khan6244bd62016-08-08 17:48:20 -060045 * Physically non-contiguous memory type for framebuffer is not
46 * supported without IOMMU.
Inki Dae0519f9a2012-10-20 07:53:42 -070047 */
48 if (IS_NONCONTIG_BUFFER(flags)) {
Shuah Khan6244bd62016-08-08 17:48:20 -060049 DRM_ERROR("Non-contiguous GEM memory is not supported.\n");
Inki Dae0519f9a2012-10-20 07:53:42 -070050 return -EINVAL;
51 }
52
53 return 0;
54}
55
Ville Syrjälä800ba2b2015-12-15 12:21:06 +010056static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
Daniel Stoneb11954a2018-03-30 15:11:30 +010057 .destroy = drm_gem_fb_destroy,
58 .create_handle = drm_gem_fb_create_handle,
Inki Dae1c248b72011-10-04 19:19:01 +090059};
60
Joonyoung Shime1533c02011-12-13 14:46:57 +090061struct drm_framebuffer *
62exynos_drm_framebuffer_init(struct drm_device *dev,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020063 const struct drm_mode_fb_cmd2 *mode_cmd,
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090064 struct exynos_drm_gem **exynos_gem,
Joonyoung Shimd56125a2015-09-01 16:22:52 +090065 int count)
Inki Dae1c248b72011-10-04 19:19:01 +090066{
Daniel Stoneff059fc2018-03-30 15:11:32 +010067 struct drm_framebuffer *fb;
Joonyoung Shimd56125a2015-09-01 16:22:52 +090068 int i;
Inki Dae1c248b72011-10-04 19:19:01 +090069 int ret;
70
Daniel Stoneff059fc2018-03-30 15:11:32 +010071 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
72 if (!fb)
Inki Dae1c248b72011-10-04 19:19:01 +090073 return ERR_PTR(-ENOMEM);
Inki Dae1c248b72011-10-04 19:19:01 +090074
Joonyoung Shimd56125a2015-09-01 16:22:52 +090075 for (i = 0; i < count; i++) {
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090076 ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
Joonyoung Shimd56125a2015-09-01 16:22:52 +090077 if (ret < 0)
78 goto err;
79
Daniel Stoneff059fc2018-03-30 15:11:32 +010080 fb->obj[i] = &exynos_gem[i]->base;
Joonyoung Shimd56125a2015-09-01 16:22:52 +090081 }
82
Daniel Stoneff059fc2018-03-30 15:11:32 +010083 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
Joonyoung Shim94e30d92015-09-01 16:22:47 +090084
Daniel Stoneff059fc2018-03-30 15:11:32 +010085 ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs);
Joonyoung Shimd56125a2015-09-01 16:22:52 +090086 if (ret < 0) {
Joonyoung Shime1533c02011-12-13 14:46:57 +090087 DRM_ERROR("failed to initialize framebuffer\n");
Joonyoung Shimd56125a2015-09-01 16:22:52 +090088 goto err;
Inki Dae1c248b72011-10-04 19:19:01 +090089 }
90
Daniel Stoneff059fc2018-03-30 15:11:32 +010091 return fb;
Joonyoung Shimd56125a2015-09-01 16:22:52 +090092
93err:
Daniel Stoneff059fc2018-03-30 15:11:32 +010094 kfree(fb);
Joonyoung Shimd56125a2015-09-01 16:22:52 +090095 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +090096}
97
Joonyoung Shime1533c02011-12-13 14:46:57 +090098static struct drm_framebuffer *
99exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
Ville Syrjälä1eb834512015-11-11 19:11:29 +0200100 const struct drm_mode_fb_cmd2 *mode_cmd)
Inki Dae1c248b72011-10-04 19:19:01 +0900101{
Marek Szyprowski1899bd52017-07-12 12:09:22 +0200102 const struct drm_format_info *info = drm_get_format_info(dev, mode_cmd);
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900103 struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
Joonyoung Shim8d317582015-09-01 16:22:53 +0900104 struct drm_framebuffer *fb;
105 int i;
106 int ret;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900107
Marek Szyprowski1899bd52017-07-12 12:09:22 +0200108 for (i = 0; i < info->num_planes; i++) {
109 unsigned int height = (i == 0) ? mode_cmd->height :
110 DIV_ROUND_UP(mode_cmd->height, info->vsub);
111 unsigned long size = height * mode_cmd->pitches[i] +
112 mode_cmd->offsets[i];
113
Marek Szyprowskie978de52018-07-09 15:44:32 +0200114 exynos_gem[i] = exynos_drm_gem_get(file_priv,
115 mode_cmd->handles[i]);
116 if (!exynos_gem[i]) {
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900117 DRM_ERROR("failed to lookup gem object\n");
YoungJun Cho979c0c72013-02-12 21:23:54 +0900118 ret = -ENOENT;
Joonyoung Shimdcbb85a2015-09-01 16:22:51 +0900119 goto err;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900120 }
121
Marek Szyprowski1899bd52017-07-12 12:09:22 +0200122 if (size > exynos_gem[i]->size) {
123 i++;
124 ret = -EINVAL;
125 goto err;
126 }
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900127 }
128
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900129 fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
Joonyoung Shim8d317582015-09-01 16:22:53 +0900130 if (IS_ERR(fb)) {
131 ret = PTR_ERR(fb);
Joonyoung Shimdcbb85a2015-09-01 16:22:51 +0900132 goto err;
Daniel Vetterf2c00952012-12-14 13:39:03 +0900133 }
134
Joonyoung Shim8d317582015-09-01 16:22:53 +0900135 return fb;
YoungJun Cho979c0c72013-02-12 21:23:54 +0900136
Joonyoung Shimdcbb85a2015-09-01 16:22:51 +0900137err:
Joonyoung Shim8d317582015-09-01 16:22:53 +0900138 while (i--)
Marek Szyprowskie978de52018-07-09 15:44:32 +0200139 exynos_drm_gem_put(exynos_gem[i]);
YoungJun Cho979c0c72013-02-12 21:23:54 +0900140
YoungJun Cho979c0c72013-02-12 21:23:54 +0900141 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900142}
143
Marek Szyprowski0488f502015-11-30 14:53:21 +0100144dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
Inki Dae1c248b72011-10-04 19:19:01 +0900145{
Daniel Stone7b305082018-03-30 15:11:31 +0100146 struct exynos_drm_gem *exynos_gem;
Inki Dae1c248b72011-10-04 19:19:01 +0900147
Christoph Hellwige0c7a512017-05-22 10:40:52 +0200148 if (WARN_ON_ONCE(index >= MAX_FB_BUFFER))
149 return 0;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900150
Daniel Stone7b305082018-03-30 15:11:31 +0100151 exynos_gem = to_exynos_gem(fb->obj[index]);
152 return exynos_gem->dma_addr + fb->offsets[index];
Inki Dae1c248b72011-10-04 19:19:01 +0900153}
154
Inki Dae41cbf0f2017-01-20 12:51:41 +0900155static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers = {
Maxime Ripard81a099a2017-07-20 15:01:16 +0200156 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
Inki Dae41cbf0f2017-01-20 12:51:41 +0900157};
158
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200159static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900160 .fb_create = exynos_user_fb_create,
Noralf Trønnesd29361532017-12-05 19:24:57 +0100161 .output_poll_changed = drm_fb_helper_output_poll_changed,
Peter Ujfalusia7da5cf2018-03-21 12:20:25 +0200162 .atomic_check = drm_atomic_helper_check,
Inki Dae41cbf0f2017-01-20 12:51:41 +0900163 .atomic_commit = drm_atomic_helper_commit,
Inki Dae1c248b72011-10-04 19:19:01 +0900164};
165
166void exynos_drm_mode_config_init(struct drm_device *dev)
167{
168 dev->mode_config.min_width = 0;
169 dev->mode_config.min_height = 0;
170
171 /*
172 * set max width and height as default value(4096x4096).
173 * this value would be used to check framebuffer size limitation
174 * at drm_mode_addfb().
175 */
176 dev->mode_config.max_width = 4096;
177 dev->mode_config.max_height = 4096;
178
179 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
Inki Dae41cbf0f2017-01-20 12:51:41 +0900180 dev->mode_config.helper_private = &exynos_drm_mode_config_helpers;
Tobias Jakobif40031c2017-08-22 16:19:37 +0200181
182 dev->mode_config.allow_fb_modifiers = true;
Peter Ujfalusia7da5cf2018-03-21 12:20:25 +0200183
184 dev->mode_config.normalize_zpos = true;
Inki Dae1c248b72011-10-04 19:19:01 +0900185}