Loading...
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_fb_helper.h>
15#include <drm/drm_fourcc.h>
16#include <drm/drm_gem_framebuffer_helper.h>
17#include <drm/drm_probe_helper.h>
18#include <drm/exynos_drm.h>
19
20#include "exynos_drm_crtc.h"
21#include "exynos_drm_drv.h"
22#include "exynos_drm_fb.h"
23#include "exynos_drm_fbdev.h"
24
25static int check_fb_gem_memory_type(struct drm_device *drm_dev,
26 struct exynos_drm_gem *exynos_gem)
27{
28 unsigned int flags;
29
30 /*
31 * if exynos drm driver supports iommu then framebuffer can use
32 * all the buffer types.
33 */
34 if (is_drm_iommu_supported(drm_dev))
35 return 0;
36
37 flags = exynos_gem->flags;
38
39 /*
40 * Physically non-contiguous memory type for framebuffer is not
41 * supported without IOMMU.
42 */
43 if (IS_NONCONTIG_BUFFER(flags)) {
44 DRM_DEV_ERROR(drm_dev->dev,
45 "Non-contiguous GEM memory is not supported.\n");
46 return -EINVAL;
47 }
48
49 return 0;
50}
51
52static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
53 .destroy = drm_gem_fb_destroy,
54 .create_handle = drm_gem_fb_create_handle,
55};
56
57struct drm_framebuffer *
58exynos_drm_framebuffer_init(struct drm_device *dev,
59 const struct drm_mode_fb_cmd2 *mode_cmd,
60 struct exynos_drm_gem **exynos_gem,
61 int count)
62{
63 struct drm_framebuffer *fb;
64 int i;
65 int ret;
66
67 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
68 if (!fb)
69 return ERR_PTR(-ENOMEM);
70
71 for (i = 0; i < count; i++) {
72 ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
73 if (ret < 0)
74 goto err;
75
76 fb->obj[i] = &exynos_gem[i]->base;
77 }
78
79 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
80
81 ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs);
82 if (ret < 0) {
83 DRM_DEV_ERROR(dev->dev,
84 "failed to initialize framebuffer\n");
85 goto err;
86 }
87
88 return fb;
89
90err:
91 kfree(fb);
92 return ERR_PTR(ret);
93}
94
95static struct drm_framebuffer *
96exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
97 const struct drm_mode_fb_cmd2 *mode_cmd)
98{
99 const struct drm_format_info *info = drm_get_format_info(dev, mode_cmd);
100 struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
101 struct drm_framebuffer *fb;
102 int i;
103 int ret;
104
105 for (i = 0; i < info->num_planes; i++) {
106 unsigned int height = (i == 0) ? mode_cmd->height :
107 DIV_ROUND_UP(mode_cmd->height, info->vsub);
108 unsigned long size = height * mode_cmd->pitches[i] +
109 mode_cmd->offsets[i];
110
111 exynos_gem[i] = exynos_drm_gem_get(file_priv,
112 mode_cmd->handles[i]);
113 if (!exynos_gem[i]) {
114 DRM_DEV_ERROR(dev->dev,
115 "failed to lookup gem object\n");
116 ret = -ENOENT;
117 goto err;
118 }
119
120 if (size > exynos_gem[i]->size) {
121 i++;
122 ret = -EINVAL;
123 goto err;
124 }
125 }
126
127 fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
128 if (IS_ERR(fb)) {
129 ret = PTR_ERR(fb);
130 goto err;
131 }
132
133 return fb;
134
135err:
136 while (i--)
137 exynos_drm_gem_put(exynos_gem[i]);
138
139 return ERR_PTR(ret);
140}
141
142dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
143{
144 struct exynos_drm_gem *exynos_gem;
145
146 if (WARN_ON_ONCE(index >= MAX_FB_BUFFER))
147 return 0;
148
149 exynos_gem = to_exynos_gem(fb->obj[index]);
150 return exynos_gem->dma_addr + fb->offsets[index];
151}
152
153static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers = {
154 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
155};
156
157static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
158 .fb_create = exynos_user_fb_create,
159 .output_poll_changed = drm_fb_helper_output_poll_changed,
160 .atomic_check = drm_atomic_helper_check,
161 .atomic_commit = drm_atomic_helper_commit,
162};
163
164void exynos_drm_mode_config_init(struct drm_device *dev)
165{
166 dev->mode_config.min_width = 0;
167 dev->mode_config.min_height = 0;
168
169 /*
170 * set max width and height as default value(4096x4096).
171 * this value would be used to check framebuffer size limitation
172 * at drm_mode_addfb().
173 */
174 dev->mode_config.max_width = 4096;
175 dev->mode_config.max_height = 4096;
176
177 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
178 dev->mode_config.helper_private = &exynos_drm_mode_config_helpers;
179
180 dev->mode_config.allow_fb_modifiers = true;
181
182 dev->mode_config.normalize_zpos = true;
183}
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 *
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.
13 */
14
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>
19#include <drm/drm_atomic.h>
20#include <drm/drm_atomic_helper.h>
21#include <uapi/drm/exynos_drm.h>
22
23#include "exynos_drm_drv.h"
24#include "exynos_drm_fb.h"
25#include "exynos_drm_fbdev.h"
26#include "exynos_drm_iommu.h"
27#include "exynos_drm_crtc.h"
28
29#define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb)
30
31/*
32 * exynos specific framebuffer structure.
33 *
34 * @fb: drm framebuffer obejct.
35 * @exynos_gem: array of exynos specific gem object containing a gem object.
36 */
37struct exynos_drm_fb {
38 struct drm_framebuffer fb;
39 struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
40 dma_addr_t dma_addr[MAX_FB_BUFFER];
41};
42
43static int check_fb_gem_memory_type(struct drm_device *drm_dev,
44 struct exynos_drm_gem *exynos_gem)
45{
46 unsigned int flags;
47
48 /*
49 * if exynos drm driver supports iommu then framebuffer can use
50 * all the buffer types.
51 */
52 if (is_drm_iommu_supported(drm_dev))
53 return 0;
54
55 flags = exynos_gem->flags;
56
57 /*
58 * Physically non-contiguous memory type for framebuffer is not
59 * supported without IOMMU.
60 */
61 if (IS_NONCONTIG_BUFFER(flags)) {
62 DRM_ERROR("Non-contiguous GEM memory is not supported.\n");
63 return -EINVAL;
64 }
65
66 return 0;
67}
68
69static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
70{
71 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
72 unsigned int i;
73
74 drm_framebuffer_cleanup(fb);
75
76 for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem); i++) {
77 struct drm_gem_object *obj;
78
79 if (exynos_fb->exynos_gem[i] == NULL)
80 continue;
81
82 obj = &exynos_fb->exynos_gem[i]->base;
83 drm_gem_object_unreference_unlocked(obj);
84 }
85
86 kfree(exynos_fb);
87 exynos_fb = NULL;
88}
89
90static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
91 struct drm_file *file_priv,
92 unsigned int *handle)
93{
94 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
95
96 return drm_gem_handle_create(file_priv,
97 &exynos_fb->exynos_gem[0]->base, handle);
98}
99
100static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
101 .destroy = exynos_drm_fb_destroy,
102 .create_handle = exynos_drm_fb_create_handle,
103};
104
105struct drm_framebuffer *
106exynos_drm_framebuffer_init(struct drm_device *dev,
107 const struct drm_mode_fb_cmd2 *mode_cmd,
108 struct exynos_drm_gem **exynos_gem,
109 int count)
110{
111 struct exynos_drm_fb *exynos_fb;
112 int i;
113 int ret;
114
115 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
116 if (!exynos_fb)
117 return ERR_PTR(-ENOMEM);
118
119 for (i = 0; i < count; i++) {
120 ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
121 if (ret < 0)
122 goto err;
123
124 exynos_fb->exynos_gem[i] = exynos_gem[i];
125 exynos_fb->dma_addr[i] = exynos_gem[i]->dma_addr
126 + mode_cmd->offsets[i];
127 }
128
129 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
130
131 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
132 if (ret < 0) {
133 DRM_ERROR("failed to initialize framebuffer\n");
134 goto err;
135 }
136
137 return &exynos_fb->fb;
138
139err:
140 kfree(exynos_fb);
141 return ERR_PTR(ret);
142}
143
144static struct drm_framebuffer *
145exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
146 const struct drm_mode_fb_cmd2 *mode_cmd)
147{
148 struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
149 struct drm_gem_object *obj;
150 struct drm_framebuffer *fb;
151 int i;
152 int ret;
153
154 for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) {
155 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]);
156 if (!obj) {
157 DRM_ERROR("failed to lookup gem object\n");
158 ret = -ENOENT;
159 goto err;
160 }
161
162 exynos_gem[i] = to_exynos_gem(obj);
163 }
164
165 fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
166 if (IS_ERR(fb)) {
167 ret = PTR_ERR(fb);
168 goto err;
169 }
170
171 return fb;
172
173err:
174 while (i--)
175 drm_gem_object_unreference_unlocked(&exynos_gem[i]->base);
176
177 return ERR_PTR(ret);
178}
179
180dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
181{
182 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
183
184 if (index >= MAX_FB_BUFFER)
185 return DMA_ERROR_CODE;
186
187 return exynos_fb->dma_addr[index];
188}
189
190static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
191 .fb_create = exynos_user_fb_create,
192 .output_poll_changed = exynos_drm_output_poll_changed,
193 .atomic_check = exynos_atomic_check,
194 .atomic_commit = exynos_atomic_commit,
195};
196
197void exynos_drm_mode_config_init(struct drm_device *dev)
198{
199 dev->mode_config.min_width = 0;
200 dev->mode_config.min_height = 0;
201
202 /*
203 * set max width and height as default value(4096x4096).
204 * this value would be used to check framebuffer size limitation
205 * at drm_mode_addfb().
206 */
207 dev->mode_config.max_width = 4096;
208 dev->mode_config.max_height = 4096;
209
210 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
211}