Merge tag 'for-linus-5.0-rc6-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / gpu / drm / exynos / exynos_drm_fb.c
CommitLineData
1c248b7d
ID
1/* 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 *
d81aecb5
ID
9 * 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.
1c248b7d
ID
13 */
14
760285e7
DH
15#include <drm/drmP.h>
16#include <drm/drm_crtc.h>
17#include <drm/drm_crtc_helper.h>
18#include <drm/drm_fb_helper.h>
d6562a29 19#include <drm/drm_atomic.h>
910874a8 20#include <drm/drm_atomic_helper.h>
b11954a6 21#include <drm/drm_gem_framebuffer_helper.h>
0519f9a1 22#include <uapi/drm/exynos_drm.h>
1c248b7d 23
7db3eba6 24#include "exynos_drm_drv.h"
1c248b7d 25#include "exynos_drm_fb.h"
25928a39 26#include "exynos_drm_fbdev.h"
080be03d 27#include "exynos_drm_crtc.h"
1c248b7d 28
0519f9a1 29static int check_fb_gem_memory_type(struct drm_device *drm_dev,
813fd67b 30 struct exynos_drm_gem *exynos_gem)
0519f9a1
ID
31{
32 unsigned int flags;
33
34 /*
35 * if exynos drm driver supports iommu then framebuffer can use
36 * all the buffer types.
37 */
38 if (is_drm_iommu_supported(drm_dev))
39 return 0;
40
813fd67b 41 flags = exynos_gem->flags;
0519f9a1
ID
42
43 /*
6244bd65
SK
44 * Physically non-contiguous memory type for framebuffer is not
45 * supported without IOMMU.
0519f9a1
ID
46 */
47 if (IS_NONCONTIG_BUFFER(flags)) {
6244bd65 48 DRM_ERROR("Non-contiguous GEM memory is not supported.\n");
0519f9a1
ID
49 return -EINVAL;
50 }
51
52 return 0;
53}
54
800ba2b5 55static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
b11954a6
DS
56 .destroy = drm_gem_fb_destroy,
57 .create_handle = drm_gem_fb_create_handle,
1c248b7d
ID
58};
59
e1533c08
JS
60struct drm_framebuffer *
61exynos_drm_framebuffer_init(struct drm_device *dev,
1eb83451 62 const struct drm_mode_fb_cmd2 *mode_cmd,
813fd67b 63 struct exynos_drm_gem **exynos_gem,
d56125af 64 int count)
1c248b7d 65{
ff059fcb 66 struct drm_framebuffer *fb;
d56125af 67 int i;
1c248b7d
ID
68 int ret;
69
ff059fcb
DS
70 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
71 if (!fb)
1c248b7d 72 return ERR_PTR(-ENOMEM);
1c248b7d 73
d56125af 74 for (i = 0; i < count; i++) {
813fd67b 75 ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
d56125af
JS
76 if (ret < 0)
77 goto err;
78
ff059fcb 79 fb->obj[i] = &exynos_gem[i]->base;
d56125af
JS
80 }
81
ff059fcb 82 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
94e30d93 83
ff059fcb 84 ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs);
d56125af 85 if (ret < 0) {
e1533c08 86 DRM_ERROR("failed to initialize framebuffer\n");
d56125af 87 goto err;
1c248b7d
ID
88 }
89
ff059fcb 90 return fb;
d56125af
JS
91
92err:
ff059fcb 93 kfree(fb);
d56125af 94 return ERR_PTR(ret);
1c248b7d
ID
95}
96
e1533c08
JS
97static struct drm_framebuffer *
98exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
1eb83451 99 const struct drm_mode_fb_cmd2 *mode_cmd)
1c248b7d 100{
1899bd57 101 const struct drm_format_info *info = drm_get_format_info(dev, mode_cmd);
813fd67b 102 struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
8d31758e
JS
103 struct drm_framebuffer *fb;
104 int i;
105 int ret;
229d3534 106
1899bd57
MS
107 for (i = 0; i < info->num_planes; i++) {
108 unsigned int height = (i == 0) ? mode_cmd->height :
109 DIV_ROUND_UP(mode_cmd->height, info->vsub);
110 unsigned long size = height * mode_cmd->pitches[i] +
111 mode_cmd->offsets[i];
112
e978de54
MS
113 exynos_gem[i] = exynos_drm_gem_get(file_priv,
114 mode_cmd->handles[i]);
115 if (!exynos_gem[i]) {
229d3534 116 DRM_ERROR("failed to lookup gem object\n");
979c0c7e 117 ret = -ENOENT;
dcbb85a1 118 goto err;
229d3534
SWK
119 }
120
1899bd57
MS
121 if (size > exynos_gem[i]->size) {
122 i++;
123 ret = -EINVAL;
124 goto err;
125 }
229d3534
SWK
126 }
127
813fd67b 128 fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
8d31758e
JS
129 if (IS_ERR(fb)) {
130 ret = PTR_ERR(fb);
dcbb85a1 131 goto err;
f2c0095a
DV
132 }
133
8d31758e 134 return fb;
979c0c7e 135
dcbb85a1 136err:
8d31758e 137 while (i--)
e978de54 138 exynos_drm_gem_put(exynos_gem[i]);
979c0c7e 139
979c0c7e 140 return ERR_PTR(ret);
1c248b7d
ID
141}
142
0488f50e 143dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
1c248b7d 144{
7b30508f 145 struct exynos_drm_gem *exynos_gem;
1c248b7d 146
e0c7a510
CH
147 if (WARN_ON_ONCE(index >= MAX_FB_BUFFER))
148 return 0;
229d3534 149
7b30508f
DS
150 exynos_gem = to_exynos_gem(fb->obj[index]);
151 return exynos_gem->dma_addr + fb->offsets[index];
1c248b7d
ID
152}
153
41cbf0fd 154static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers = {
81a099ac 155 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
41cbf0fd
ID
156};
157
e6ecefaa 158static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
e1533c08 159 .fb_create = exynos_user_fb_create,
d2936153 160 .output_poll_changed = drm_fb_helper_output_poll_changed,
a7da5cfe 161 .atomic_check = drm_atomic_helper_check,
41cbf0fd 162 .atomic_commit = drm_atomic_helper_commit,
1c248b7d
ID
163};
164
165void exynos_drm_mode_config_init(struct drm_device *dev)
166{
167 dev->mode_config.min_width = 0;
168 dev->mode_config.min_height = 0;
169
170 /*
171 * set max width and height as default value(4096x4096).
172 * this value would be used to check framebuffer size limitation
173 * at drm_mode_addfb().
174 */
175 dev->mode_config.max_width = 4096;
176 dev->mode_config.max_height = 4096;
177
178 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
41cbf0fd 179 dev->mode_config.helper_private = &exynos_drm_mode_config_helpers;
f40031c2
TJ
180
181 dev->mode_config.allow_fb_modifiers = true;
a7da5cfe
PU
182
183 dev->mode_config.normalize_zpos = true;
1c248b7d 184}