blob: 65a22cad7b36300811abf39b3a0d13020a479cc0 [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* 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 *
Inki Daed81aecb2012-12-18 02:30:17 +09009 * 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.
Inki Dae1c248b72011-10-04 19:19:01 +090013 */
14
David Howells760285e2012-10-02 18:01:07 +010015#include <drm/drmP.h>
16#include <drm/drm_crtc.h>
17#include <drm/drm_crtc_helper.h>
18#include <drm/drm_fb_helper.h>
Inki Dae0519f9a2012-10-20 07:53:42 -070019#include <uapi/drm/exynos_drm.h>
Inki Dae1c248b72011-10-04 19:19:01 +090020
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +090021#include "exynos_drm_drv.h"
Inki Dae1c248b72011-10-04 19:19:01 +090022#include "exynos_drm_fb.h"
Andrzej Hajda25928a32014-03-17 11:27:17 +010023#include "exynos_drm_fbdev.h"
Inki Dae1c248b72011-10-04 19:19:01 +090024#include "exynos_drm_gem.h"
Inki Dae0519f9a2012-10-20 07:53:42 -070025#include "exynos_drm_iommu.h"
Sean Paul080be03d2014-02-19 21:02:55 +090026#include "exynos_drm_crtc.h"
Inki Dae1c248b72011-10-04 19:19:01 +090027
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.
Inki Dae01ed8122012-08-20 20:05:56 +090034 * @buf_cnt: a buffer count to drm framebuffer.
Seung-Woo Kim229d3532011-12-15 14:36:22 +090035 * @exynos_gem_obj: array of exynos specific gem object containing a gem object.
Inki Dae1c248b72011-10-04 19:19:01 +090036 */
37struct exynos_drm_fb {
38 struct drm_framebuffer fb;
Inki Dae01ed8122012-08-20 20:05:56 +090039 unsigned int buf_cnt;
Seung-Woo Kim229d3532011-12-15 14:36:22 +090040 struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER];
Inki Dae1c248b72011-10-04 19:19:01 +090041};
42
Inki Dae0519f9a2012-10-20 07:53:42 -070043static 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
Inki Dae1c248b72011-10-04 19:19:01 +090069static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
70{
71 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
Laurent Pinchart07b68352012-05-16 17:08:56 +020072 unsigned int i;
Inki Dae1c248b72011-10-04 19:19:01 +090073
Inki Dae1daa8922012-11-22 17:41:23 +090074 /* make sure that overlay data are updated before relesing fb. */
Sean Paul080be03d2014-02-19 21:02:55 +090075 exynos_drm_crtc_complete_scanout(fb);
Inki Dae1daa8922012-11-22 17:41:23 +090076
Inki Dae1c248b72011-10-04 19:19:01 +090077 drm_framebuffer_cleanup(fb);
78
Laurent Pinchart07b68352012-05-16 17:08:56 +020079 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
Inki Dae1c248b72011-10-04 19:19:01 +090089 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
Inki Daeb9ede272013-01-29 17:51:09 +090099 /* This fb should have only one gem object. */
100 if (WARN_ON(exynos_fb->buf_cnt != 1))
101 return -EINVAL;
102
Inki Dae1c248b72011-10-04 19:19:01 +0900103 return drm_gem_handle_create(file_priv,
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900104 &exynos_fb->exynos_gem_obj[0]->base, handle);
Inki Dae1c248b72011-10-04 19:19:01 +0900105}
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{
Inki Dae1c248b72011-10-04 19:19:01 +0900112 /* 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
Inki Dae01ed8122012-08-20 20:05:56 +0900123void 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
Joonyoung Shime1533c02011-12-13 14:46:57 +0900142struct drm_framebuffer *
143exynos_drm_framebuffer_init(struct drm_device *dev,
144 struct drm_mode_fb_cmd2 *mode_cmd,
145 struct drm_gem_object *obj)
Inki Dae1c248b72011-10-04 19:19:01 +0900146{
147 struct exynos_drm_fb *exynos_fb;
Inki Dae0519f9a2012-10-20 07:53:42 -0700148 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900149 int ret;
150
Inki Dae0519f9a2012-10-20 07:53:42 -0700151 exynos_gem_obj = to_exynos_gem_obj(obj);
152
153 ret = check_fb_gem_memory_type(dev, exynos_gem_obj);
154 if (ret < 0) {
155 DRM_ERROR("cannot use this gem memory type for fb.\n");
156 return ERR_PTR(-EINVAL);
157 }
158
Inki Dae1c248b72011-10-04 19:19:01 +0900159 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900160 if (!exynos_fb)
Inki Dae1c248b72011-10-04 19:19:01 +0900161 return ERR_PTR(-ENOMEM);
Inki Dae1c248b72011-10-04 19:19:01 +0900162
Daniel Vetterf2c00952012-12-14 13:39:03 +0900163 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
Inki Dae0519f9a2012-10-20 07:53:42 -0700164 exynos_fb->exynos_gem_obj[0] = exynos_gem_obj;
165
Joonyoung Shime1533c02011-12-13 14:46:57 +0900166 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
Inki Dae1c248b72011-10-04 19:19:01 +0900167 if (ret) {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900168 DRM_ERROR("failed to initialize framebuffer\n");
169 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900170 }
171
Joonyoung Shime1533c02011-12-13 14:46:57 +0900172 return &exynos_fb->fb;
Inki Dae1c248b72011-10-04 19:19:01 +0900173}
174
Inki Dae01ed8122012-08-20 20:05:56 +0900175static u32 exynos_drm_format_num_buffers(struct drm_mode_fb_cmd2 *mode_cmd)
176{
177 unsigned int cnt = 0;
178
179 if (mode_cmd->pixel_format != DRM_FORMAT_NV12)
180 return drm_format_num_planes(mode_cmd->pixel_format);
181
182 while (cnt != MAX_FB_BUFFER) {
183 if (!mode_cmd->handles[cnt])
184 break;
185 cnt++;
186 }
187
188 /*
189 * check if NV12 or NV12M.
190 *
191 * NV12
192 * handles[0] = base1, offsets[0] = 0
193 * handles[1] = base1, offsets[1] = Y_size
194 *
195 * NV12M
196 * handles[0] = base1, offsets[0] = 0
197 * handles[1] = base2, offsets[1] = 0
198 */
199 if (cnt == 2) {
200 /*
201 * in case of NV12 format, offsets[1] is not 0 and
202 * handles[0] is same as handles[1].
203 */
204 if (mode_cmd->offsets[1] &&
205 mode_cmd->handles[0] == mode_cmd->handles[1])
206 cnt = 1;
207 }
208
209 return cnt;
210}
211
Joonyoung Shime1533c02011-12-13 14:46:57 +0900212static struct drm_framebuffer *
213exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
214 struct drm_mode_fb_cmd2 *mode_cmd)
Inki Dae1c248b72011-10-04 19:19:01 +0900215{
Joonyoung Shime1533c02011-12-13 14:46:57 +0900216 struct drm_gem_object *obj;
YoungJun Cho979c0c72013-02-12 21:23:54 +0900217 struct exynos_drm_gem_obj *exynos_gem_obj;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900218 struct exynos_drm_fb *exynos_fb;
Daniel Vetterf2c00952012-12-14 13:39:03 +0900219 int i, ret;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900220
Daniel Vetterf2c00952012-12-14 13:39:03 +0900221 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900222 if (!exynos_fb)
Daniel Vetterf2c00952012-12-14 13:39:03 +0900223 return ERR_PTR(-ENOMEM);
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900224
YoungJun Cho979c0c72013-02-12 21:23:54 +0900225 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
226 if (!obj) {
227 DRM_ERROR("failed to lookup gem object\n");
228 ret = -ENOENT;
229 goto err_free;
230 }
231
Daniel Vetterf2c00952012-12-14 13:39:03 +0900232 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
233 exynos_fb->exynos_gem_obj[0] = to_exynos_gem_obj(obj);
Inki Dae01ed8122012-08-20 20:05:56 +0900234 exynos_fb->buf_cnt = exynos_drm_format_num_buffers(mode_cmd);
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900235
Inki Dae01ed8122012-08-20 20:05:56 +0900236 DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt);
237
238 for (i = 1; i < exynos_fb->buf_cnt; i++) {
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900239 obj = drm_gem_object_lookup(dev, file_priv,
240 mode_cmd->handles[i]);
241 if (!obj) {
242 DRM_ERROR("failed to lookup gem object\n");
YoungJun Cho979c0c72013-02-12 21:23:54 +0900243 ret = -ENOENT;
244 exynos_fb->buf_cnt = i;
245 goto err_unreference;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900246 }
247
Inki Dae0519f9a2012-10-20 07:53:42 -0700248 exynos_gem_obj = to_exynos_gem_obj(obj);
YoungJun Cho979c0c72013-02-12 21:23:54 +0900249 exynos_fb->exynos_gem_obj[i] = exynos_gem_obj;
Inki Dae0519f9a2012-10-20 07:53:42 -0700250
251 ret = check_fb_gem_memory_type(dev, exynos_gem_obj);
252 if (ret < 0) {
253 DRM_ERROR("cannot use this gem memory type for fb.\n");
YoungJun Cho979c0c72013-02-12 21:23:54 +0900254 goto err_unreference;
Inki Dae0519f9a2012-10-20 07:53:42 -0700255 }
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900256 }
257
Daniel Vetterf2c00952012-12-14 13:39:03 +0900258 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
259 if (ret) {
YoungJun Cho979c0c72013-02-12 21:23:54 +0900260 DRM_ERROR("failed to init framebuffer.\n");
261 goto err_unreference;
Daniel Vetterf2c00952012-12-14 13:39:03 +0900262 }
263
264 return &exynos_fb->fb;
YoungJun Cho979c0c72013-02-12 21:23:54 +0900265
266err_unreference:
267 for (i = 0; i < exynos_fb->buf_cnt; i++) {
268 struct drm_gem_object *obj;
269
270 obj = &exynos_fb->exynos_gem_obj[i]->base;
271 if (obj)
272 drm_gem_object_unreference_unlocked(obj);
273 }
274err_free:
275 kfree(exynos_fb);
276 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900277}
278
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900279struct exynos_drm_gem_buf *exynos_drm_fb_buffer(struct drm_framebuffer *fb,
280 int index)
Inki Dae1c248b72011-10-04 19:19:01 +0900281{
282 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
Inki Dae2c871122011-11-12 15:23:32 +0900283 struct exynos_drm_gem_buf *buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900284
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900285 if (index >= MAX_FB_BUFFER)
286 return NULL;
287
288 buffer = exynos_fb->exynos_gem_obj[index]->buffer;
Inki Dae2c871122011-11-12 15:23:32 +0900289 if (!buffer)
Inki Dae19c8b832011-10-14 13:29:46 +0900290 return NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900291
Inki Dae4744ad22012-12-07 17:51:27 +0900292 DRM_DEBUG_KMS("dma_addr = 0x%lx\n", (unsigned long)buffer->dma_addr);
Inki Dae1c248b72011-10-04 19:19:01 +0900293
Inki Dae2c871122011-11-12 15:23:32 +0900294 return buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900295}
296
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900297static void exynos_drm_output_poll_changed(struct drm_device *dev)
298{
299 struct exynos_drm_private *private = dev->dev_private;
300 struct drm_fb_helper *fb_helper = private->fb_helper;
301
302 if (fb_helper)
303 drm_fb_helper_hotplug_event(fb_helper);
Andrzej Hajda25928a32014-03-17 11:27:17 +0100304 else
305 exynos_drm_fbdev_init(dev);
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900306}
307
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200308static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900309 .fb_create = exynos_user_fb_create,
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900310 .output_poll_changed = exynos_drm_output_poll_changed,
Inki Dae1c248b72011-10-04 19:19:01 +0900311};
312
313void exynos_drm_mode_config_init(struct drm_device *dev)
314{
315 dev->mode_config.min_width = 0;
316 dev->mode_config.min_height = 0;
317
318 /*
319 * set max width and height as default value(4096x4096).
320 * this value would be used to check framebuffer size limitation
321 * at drm_mode_addfb().
322 */
323 dev->mode_config.max_width = 4096;
324 dev->mode_config.max_height = 4096;
325
326 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
327}