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