1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* exynos_drm_fb.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>
9 */
10
11 #include <drm/drm_atomic.h>
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_crtc.h>
14 #include <drm/drm_framebuffer.h>
15 #include <drm/drm_fourcc.h>
16 #include <drm/drm_gem_framebuffer_helper.h>
17 #include <drm/drm_print.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/exynos_drm.h>
20
21 #include "exynos_drm_crtc.h"
22 #include "exynos_drm_drv.h"
23 #include "exynos_drm_fb.h"
24 #include "exynos_drm_fbdev.h"
25
check_fb_gem_memory_type(struct drm_device * drm_dev,struct exynos_drm_gem * exynos_gem)26 static int check_fb_gem_memory_type(struct drm_device *drm_dev,
27 struct exynos_drm_gem *exynos_gem)
28 {
29 unsigned int flags;
30
31 /*
32 * if exynos drm driver supports iommu then framebuffer can use
33 * all the buffer types.
34 */
35 if (is_drm_iommu_supported(drm_dev))
36 return 0;
37
38 flags = exynos_gem->flags;
39
40 /*
41 * Physically non-contiguous memory type for framebuffer is not
42 * supported without IOMMU.
43 */
44 if (IS_NONCONTIG_BUFFER(flags)) {
45 DRM_DEV_ERROR(drm_dev->dev,
46 "Non-contiguous GEM memory is not supported.\n");
47 return -EINVAL;
48 }
49
50 return 0;
51 }
52
53 static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
54 .destroy = drm_gem_fb_destroy,
55 .create_handle = drm_gem_fb_create_handle,
56 };
57
58 struct drm_framebuffer *
exynos_drm_framebuffer_init(struct drm_device * dev,const struct drm_format_info * info,const struct drm_mode_fb_cmd2 * mode_cmd,struct exynos_drm_gem ** exynos_gem,int count)59 exynos_drm_framebuffer_init(struct drm_device *dev,
60 const struct drm_format_info *info,
61 const struct drm_mode_fb_cmd2 *mode_cmd,
62 struct exynos_drm_gem **exynos_gem,
63 int count)
64 {
65 struct drm_framebuffer *fb;
66 int i;
67 int ret;
68
69 fb = kzalloc_obj(*fb);
70 if (!fb)
71 return ERR_PTR(-ENOMEM);
72
73 for (i = 0; i < count; i++) {
74 ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
75 if (ret < 0)
76 goto err;
77
78 fb->obj[i] = &exynos_gem[i]->base;
79 }
80
81 drm_helper_mode_fill_fb_struct(dev, fb, info, mode_cmd);
82
83 ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs);
84 if (ret < 0) {
85 DRM_DEV_ERROR(dev->dev,
86 "failed to initialize framebuffer\n");
87 goto err;
88 }
89
90 return fb;
91
92 err:
93 kfree(fb);
94 return ERR_PTR(ret);
95 }
96
97 static struct drm_framebuffer *
exynos_user_fb_create(struct drm_device * dev,struct drm_file * file_priv,const struct drm_format_info * info,const struct drm_mode_fb_cmd2 * mode_cmd)98 exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
99 const struct drm_format_info *info,
100 const struct drm_mode_fb_cmd2 *mode_cmd)
101 {
102 struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
103 struct drm_framebuffer *fb;
104 int i;
105 int ret;
106
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
113 exynos_gem[i] = exynos_drm_gem_get(file_priv,
114 mode_cmd->handles[i]);
115 if (!exynos_gem[i]) {
116 DRM_DEV_ERROR(dev->dev,
117 "failed to lookup gem object\n");
118 ret = -ENOENT;
119 goto err;
120 }
121
122 if (size > exynos_gem[i]->size) {
123 i++;
124 ret = -EINVAL;
125 goto err;
126 }
127 }
128
129 fb = exynos_drm_framebuffer_init(dev, info, mode_cmd, exynos_gem, i);
130 if (IS_ERR(fb)) {
131 ret = PTR_ERR(fb);
132 goto err;
133 }
134
135 return fb;
136
137 err:
138 while (i--)
139 exynos_drm_gem_put(exynos_gem[i]);
140
141 return ERR_PTR(ret);
142 }
143
exynos_drm_fb_dma_addr(struct drm_framebuffer * fb,int index)144 dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
145 {
146 struct exynos_drm_gem *exynos_gem;
147
148 if (WARN_ON_ONCE(index >= MAX_FB_BUFFER))
149 return 0;
150
151 exynos_gem = to_exynos_gem(fb->obj[index]);
152 return exynos_gem->dma_addr + fb->offsets[index];
153 }
154
155 static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers = {
156 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
157 };
158
159 static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
160 .fb_create = exynos_user_fb_create,
161 .atomic_check = drm_atomic_helper_check,
162 .atomic_commit = drm_atomic_helper_commit,
163 };
164
exynos_drm_mode_config_init(struct drm_device * dev)165 void 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;
179 dev->mode_config.helper_private = &exynos_drm_mode_config_helpers;
180
181 dev->mode_config.normalize_zpos = true;
182 }
183