drm/exynos: update exynos_drm_framebuffer_init() for multiple buffers
[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>
0519f9a1 21#include <uapi/drm/exynos_drm.h>
1c248b7d 22
7db3eba6 23#include "exynos_drm_drv.h"
1c248b7d 24#include "exynos_drm_fb.h"
25928a39 25#include "exynos_drm_fbdev.h"
0519f9a1 26#include "exynos_drm_iommu.h"
080be03d 27#include "exynos_drm_crtc.h"
1c248b7d
ID
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.
01ed8126 35 * @buf_cnt: a buffer count to drm framebuffer.
229d3534 36 * @exynos_gem_obj: array of exynos specific gem object containing a gem object.
1c248b7d
ID
37 */
38struct exynos_drm_fb {
39 struct drm_framebuffer fb;
01ed8126 40 unsigned int buf_cnt;
229d3534 41 struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER];
1c248b7d
ID
42};
43
0519f9a1
ID
44static int check_fb_gem_memory_type(struct drm_device *drm_dev,
45 struct exynos_drm_gem_obj *exynos_gem_obj)
46{
47 unsigned int flags;
48
49 /*
50 * if exynos drm driver supports iommu then framebuffer can use
51 * all the buffer types.
52 */
53 if (is_drm_iommu_supported(drm_dev))
54 return 0;
55
56 flags = exynos_gem_obj->flags;
57
58 /*
59 * without iommu support, not support physically non-continuous memory
60 * for framebuffer.
61 */
62 if (IS_NONCONTIG_BUFFER(flags)) {
63 DRM_ERROR("cannot use this gem memory type for fb.\n");
64 return -EINVAL;
65 }
66
67 return 0;
68}
69
1c248b7d
ID
70static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
71{
72 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
07b6835f 73 unsigned int i;
1c248b7d 74
1daa892c 75 /* make sure that overlay data are updated before relesing fb. */
080be03d 76 exynos_drm_crtc_complete_scanout(fb);
1daa892c 77
1c248b7d
ID
78 drm_framebuffer_cleanup(fb);
79
07b6835f
LP
80 for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem_obj); i++) {
81 struct drm_gem_object *obj;
82
83 if (exynos_fb->exynos_gem_obj[i] == NULL)
84 continue;
85
86 obj = &exynos_fb->exynos_gem_obj[i]->base;
87 drm_gem_object_unreference_unlocked(obj);
88 }
89
1c248b7d
ID
90 kfree(exynos_fb);
91 exynos_fb = NULL;
92}
93
94static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
95 struct drm_file *file_priv,
96 unsigned int *handle)
97{
98 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
99
b9ede277
ID
100 /* This fb should have only one gem object. */
101 if (WARN_ON(exynos_fb->buf_cnt != 1))
102 return -EINVAL;
103
1c248b7d 104 return drm_gem_handle_create(file_priv,
229d3534 105 &exynos_fb->exynos_gem_obj[0]->base, handle);
1c248b7d
ID
106}
107
108static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
109 struct drm_file *file_priv, unsigned flags,
110 unsigned color, struct drm_clip_rect *clips,
111 unsigned num_clips)
112{
1c248b7d
ID
113 /* TODO */
114
115 return 0;
116}
117
118static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
119 .destroy = exynos_drm_fb_destroy,
120 .create_handle = exynos_drm_fb_create_handle,
121 .dirty = exynos_drm_fb_dirty,
122};
123
01ed8126
ID
124unsigned int exynos_drm_fb_get_buf_cnt(struct drm_framebuffer *fb)
125{
126 struct exynos_drm_fb *exynos_fb;
127
128 exynos_fb = to_exynos_fb(fb);
129
130 return exynos_fb->buf_cnt;
131}
132
e1533c08
JS
133struct drm_framebuffer *
134exynos_drm_framebuffer_init(struct drm_device *dev,
135 struct drm_mode_fb_cmd2 *mode_cmd,
d56125af
JS
136 struct exynos_drm_gem_obj **gem_obj,
137 int count)
1c248b7d
ID
138{
139 struct exynos_drm_fb *exynos_fb;
d56125af 140 int i;
1c248b7d
ID
141 int ret;
142
1c248b7d 143 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
38bb5253 144 if (!exynos_fb)
1c248b7d 145 return ERR_PTR(-ENOMEM);
1c248b7d 146
d56125af
JS
147 exynos_fb->buf_cnt = count;
148 DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt);
0519f9a1 149
d56125af
JS
150 for (i = 0; i < count; i++) {
151 ret = check_fb_gem_memory_type(dev, gem_obj[i]);
152 if (ret < 0)
153 goto err;
154
155 exynos_fb->exynos_gem_obj[i] = gem_obj[i];
156 }
157
158 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
94e30d93 159
e1533c08 160 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
d56125af 161 if (ret < 0) {
e1533c08 162 DRM_ERROR("failed to initialize framebuffer\n");
d56125af 163 goto err;
1c248b7d
ID
164 }
165
e1533c08 166 return &exynos_fb->fb;
d56125af
JS
167
168err:
169 kfree(exynos_fb);
170 return ERR_PTR(ret);
1c248b7d
ID
171}
172
e1533c08
JS
173static struct drm_framebuffer *
174exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
175 struct drm_mode_fb_cmd2 *mode_cmd)
1c248b7d 176{
e1533c08 177 struct drm_gem_object *obj;
979c0c7e 178 struct exynos_drm_gem_obj *exynos_gem_obj;
229d3534 179 struct exynos_drm_fb *exynos_fb;
f2c0095a 180 int i, ret;
e1533c08 181
f2c0095a 182 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
38bb5253 183 if (!exynos_fb)
f2c0095a 184 return ERR_PTR(-ENOMEM);
229d3534 185
d10ebb9f 186 exynos_fb->buf_cnt = drm_format_num_planes(mode_cmd->pixel_format);
01ed8126
ID
187
188 DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt);
229d3534 189
dcbb85a1 190 for (i = 0; i < exynos_fb->buf_cnt; i++) {
229d3534
SWK
191 obj = drm_gem_object_lookup(dev, file_priv,
192 mode_cmd->handles[i]);
193 if (!obj) {
194 DRM_ERROR("failed to lookup gem object\n");
979c0c7e
YC
195 ret = -ENOENT;
196 exynos_fb->buf_cnt = i;
dcbb85a1 197 goto err;
229d3534
SWK
198 }
199
0519f9a1 200 exynos_gem_obj = to_exynos_gem_obj(obj);
979c0c7e 201 exynos_fb->exynos_gem_obj[i] = exynos_gem_obj;
0519f9a1
ID
202
203 ret = check_fb_gem_memory_type(dev, exynos_gem_obj);
7ded8588 204 if (ret < 0)
dcbb85a1 205 goto err;
229d3534
SWK
206 }
207
dcbb85a1
JS
208 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
209
f2c0095a
DV
210 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
211 if (ret) {
979c0c7e 212 DRM_ERROR("failed to init framebuffer.\n");
dcbb85a1 213 goto err;
f2c0095a
DV
214 }
215
216 return &exynos_fb->fb;
979c0c7e 217
dcbb85a1 218err:
979c0c7e
YC
219 for (i = 0; i < exynos_fb->buf_cnt; i++) {
220 struct drm_gem_object *obj;
221
222 obj = &exynos_fb->exynos_gem_obj[i]->base;
223 if (obj)
224 drm_gem_object_unreference_unlocked(obj);
225 }
dcbb85a1 226
979c0c7e
YC
227 kfree(exynos_fb);
228 return ERR_PTR(ret);
1c248b7d
ID
229}
230
2a8cb489
JS
231struct exynos_drm_gem_obj *exynos_drm_fb_gem_obj(struct drm_framebuffer *fb,
232 int index)
1c248b7d
ID
233{
234 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
2a8cb489 235 struct exynos_drm_gem_obj *obj;
1c248b7d 236
229d3534
SWK
237 if (index >= MAX_FB_BUFFER)
238 return NULL;
239
2a8cb489
JS
240 obj = exynos_fb->exynos_gem_obj[index];
241 if (!obj)
19c8b834 242 return NULL;
1c248b7d 243
2a8cb489 244 DRM_DEBUG_KMS("dma_addr = 0x%lx\n", (unsigned long)obj->dma_addr);
1c248b7d 245
2a8cb489 246 return obj;
1c248b7d
ID
247}
248
7db3eba6
SWK
249static void exynos_drm_output_poll_changed(struct drm_device *dev)
250{
251 struct exynos_drm_private *private = dev->dev_private;
252 struct drm_fb_helper *fb_helper = private->fb_helper;
253
254 if (fb_helper)
255 drm_fb_helper_hotplug_event(fb_helper);
25928a39
AH
256 else
257 exynos_drm_fbdev_init(dev);
7db3eba6
SWK
258}
259
e6ecefaa 260static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
e1533c08 261 .fb_create = exynos_user_fb_create,
7db3eba6 262 .output_poll_changed = exynos_drm_output_poll_changed,
910874a8 263 .atomic_check = drm_atomic_helper_check,
9d5ab6a0 264 .atomic_commit = exynos_atomic_commit,
1c248b7d
ID
265};
266
267void exynos_drm_mode_config_init(struct drm_device *dev)
268{
269 dev->mode_config.min_width = 0;
270 dev->mode_config.min_height = 0;
271
272 /*
273 * set max width and height as default value(4096x4096).
274 * this value would be used to check framebuffer size limitation
275 * at drm_mode_addfb().
276 */
277 dev->mode_config.max_width = 4096;
278 dev->mode_config.max_height = 4096;
279
280 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
281}