Loading...
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 * without iommu support, not support physically non-continuous memory
59 * for framebuffer.
60 */
61 if (IS_NONCONTIG_BUFFER(flags)) {
62 DRM_ERROR("cannot use this gem memory type for fb.\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 int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
101 struct drm_file *file_priv, unsigned flags,
102 unsigned color, struct drm_clip_rect *clips,
103 unsigned num_clips)
104{
105 /* TODO */
106
107 return 0;
108}
109
110static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
111 .destroy = exynos_drm_fb_destroy,
112 .create_handle = exynos_drm_fb_create_handle,
113 .dirty = exynos_drm_fb_dirty,
114};
115
116struct drm_framebuffer *
117exynos_drm_framebuffer_init(struct drm_device *dev,
118 const struct drm_mode_fb_cmd2 *mode_cmd,
119 struct exynos_drm_gem **exynos_gem,
120 int count)
121{
122 struct exynos_drm_fb *exynos_fb;
123 int i;
124 int ret;
125
126 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
127 if (!exynos_fb)
128 return ERR_PTR(-ENOMEM);
129
130 for (i = 0; i < count; i++) {
131 ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
132 if (ret < 0)
133 goto err;
134
135 exynos_fb->exynos_gem[i] = exynos_gem[i];
136 exynos_fb->dma_addr[i] = exynos_gem[i]->dma_addr
137 + mode_cmd->offsets[i];
138 }
139
140 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
141
142 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
143 if (ret < 0) {
144 DRM_ERROR("failed to initialize framebuffer\n");
145 goto err;
146 }
147
148 return &exynos_fb->fb;
149
150err:
151 kfree(exynos_fb);
152 return ERR_PTR(ret);
153}
154
155static struct drm_framebuffer *
156exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
157 const struct drm_mode_fb_cmd2 *mode_cmd)
158{
159 struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
160 struct drm_gem_object *obj;
161 struct drm_framebuffer *fb;
162 int i;
163 int ret;
164
165 for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) {
166 obj = drm_gem_object_lookup(dev, file_priv,
167 mode_cmd->handles[i]);
168 if (!obj) {
169 DRM_ERROR("failed to lookup gem object\n");
170 ret = -ENOENT;
171 goto err;
172 }
173
174 exynos_gem[i] = to_exynos_gem(obj);
175 }
176
177 fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
178 if (IS_ERR(fb)) {
179 ret = PTR_ERR(fb);
180 goto err;
181 }
182
183 return fb;
184
185err:
186 while (i--)
187 drm_gem_object_unreference_unlocked(&exynos_gem[i]->base);
188
189 return ERR_PTR(ret);
190}
191
192dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
193{
194 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
195
196 if (index >= MAX_FB_BUFFER)
197 return DMA_ERROR_CODE;
198
199 return exynos_fb->dma_addr[index];
200}
201
202static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
203 .fb_create = exynos_user_fb_create,
204 .output_poll_changed = exynos_drm_output_poll_changed,
205 .atomic_check = drm_atomic_helper_check,
206 .atomic_commit = exynos_atomic_commit,
207};
208
209void exynos_drm_mode_config_init(struct drm_device *dev)
210{
211 dev->mode_config.min_width = 0;
212 dev->mode_config.min_height = 0;
213
214 /*
215 * set max width and height as default value(4096x4096).
216 * this value would be used to check framebuffer size limitation
217 * at drm_mode_addfb().
218 */
219 dev->mode_config.max_width = 4096;
220 dev->mode_config.max_height = 4096;
221
222 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
223}
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_framebuffer.h>
16#include <drm/drm_fourcc.h>
17#include <drm/drm_gem_framebuffer_helper.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
26static 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
53static 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
58struct drm_framebuffer *
59exynos_drm_framebuffer_init(struct drm_device *dev,
60 const struct drm_mode_fb_cmd2 *mode_cmd,
61 struct exynos_drm_gem **exynos_gem,
62 int count)
63{
64 struct drm_framebuffer *fb;
65 int i;
66 int ret;
67
68 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
69 if (!fb)
70 return ERR_PTR(-ENOMEM);
71
72 for (i = 0; i < count; i++) {
73 ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
74 if (ret < 0)
75 goto err;
76
77 fb->obj[i] = &exynos_gem[i]->base;
78 }
79
80 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
81
82 ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs);
83 if (ret < 0) {
84 DRM_DEV_ERROR(dev->dev,
85 "failed to initialize framebuffer\n");
86 goto err;
87 }
88
89 return fb;
90
91err:
92 kfree(fb);
93 return ERR_PTR(ret);
94}
95
96static struct drm_framebuffer *
97exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
98 const struct drm_mode_fb_cmd2 *mode_cmd)
99{
100 const struct drm_format_info *info = drm_get_format_info(dev, mode_cmd);
101 struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
102 struct drm_framebuffer *fb;
103 int i;
104 int ret;
105
106 for (i = 0; i < info->num_planes; i++) {
107 unsigned int height = (i == 0) ? mode_cmd->height :
108 DIV_ROUND_UP(mode_cmd->height, info->vsub);
109 unsigned long size = height * mode_cmd->pitches[i] +
110 mode_cmd->offsets[i];
111
112 exynos_gem[i] = exynos_drm_gem_get(file_priv,
113 mode_cmd->handles[i]);
114 if (!exynos_gem[i]) {
115 DRM_DEV_ERROR(dev->dev,
116 "failed to lookup gem object\n");
117 ret = -ENOENT;
118 goto err;
119 }
120
121 if (size > exynos_gem[i]->size) {
122 i++;
123 ret = -EINVAL;
124 goto err;
125 }
126 }
127
128 fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
129 if (IS_ERR(fb)) {
130 ret = PTR_ERR(fb);
131 goto err;
132 }
133
134 return fb;
135
136err:
137 while (i--)
138 exynos_drm_gem_put(exynos_gem[i]);
139
140 return ERR_PTR(ret);
141}
142
143dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
144{
145 struct exynos_drm_gem *exynos_gem;
146
147 if (WARN_ON_ONCE(index >= MAX_FB_BUFFER))
148 return 0;
149
150 exynos_gem = to_exynos_gem(fb->obj[index]);
151 return exynos_gem->dma_addr + fb->offsets[index];
152}
153
154static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers = {
155 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
156};
157
158static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
159 .fb_create = exynos_user_fb_create,
160 .output_poll_changed = drm_fb_helper_output_poll_changed,
161 .atomic_check = drm_atomic_helper_check,
162 .atomic_commit = drm_atomic_helper_commit,
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;
179 dev->mode_config.helper_private = &exynos_drm_mode_config_helpers;
180
181 dev->mode_config.normalize_zpos = true;
182}