blob: ea39e0ef2ae4c61d530bd2f7b729a26a6ad1e752 [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"
Inki Dae1c248b72011-10-04 19:19:01 +090023#include "exynos_drm_gem.h"
Inki Dae0519f9a2012-10-20 07:53:42 -070024#include "exynos_drm_iommu.h"
Inki Dae1daa8922012-11-22 17:41:23 +090025#include "exynos_drm_encoder.h"
Inki Dae1c248b72011-10-04 19:19:01 +090026
27#define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb)
28
29/*
30 * exynos specific framebuffer structure.
31 *
32 * @fb: drm framebuffer obejct.
Inki Dae01ed8122012-08-20 20:05:56 +090033 * @buf_cnt: a buffer count to drm framebuffer.
Seung-Woo Kim229d3532011-12-15 14:36:22 +090034 * @exynos_gem_obj: array of exynos specific gem object containing a gem object.
Inki Dae1c248b72011-10-04 19:19:01 +090035 */
36struct exynos_drm_fb {
37 struct drm_framebuffer fb;
Inki Dae01ed8122012-08-20 20:05:56 +090038 unsigned int buf_cnt;
Seung-Woo Kim229d3532011-12-15 14:36:22 +090039 struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER];
Inki Dae1c248b72011-10-04 19:19:01 +090040};
41
Inki Dae0519f9a2012-10-20 07:53:42 -070042static int check_fb_gem_memory_type(struct drm_device *drm_dev,
43 struct exynos_drm_gem_obj *exynos_gem_obj)
44{
45 unsigned int flags;
46
47 /*
48 * if exynos drm driver supports iommu then framebuffer can use
49 * all the buffer types.
50 */
51 if (is_drm_iommu_supported(drm_dev))
52 return 0;
53
54 flags = exynos_gem_obj->flags;
55
56 /*
57 * without iommu support, not support physically non-continuous memory
58 * for framebuffer.
59 */
60 if (IS_NONCONTIG_BUFFER(flags)) {
61 DRM_ERROR("cannot use this gem memory type for fb.\n");
62 return -EINVAL;
63 }
64
65 return 0;
66}
67
Inki Dae1c248b72011-10-04 19:19:01 +090068static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
69{
70 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
Laurent Pinchart07b68352012-05-16 17:08:56 +020071 unsigned int i;
Inki Dae1c248b72011-10-04 19:19:01 +090072
Inki Dae1daa8922012-11-22 17:41:23 +090073 /* make sure that overlay data are updated before relesing fb. */
74 exynos_drm_encoder_complete_scanout(fb);
75
Inki Dae1c248b72011-10-04 19:19:01 +090076 drm_framebuffer_cleanup(fb);
77
Laurent Pinchart07b68352012-05-16 17:08:56 +020078 for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem_obj); i++) {
79 struct drm_gem_object *obj;
80
81 if (exynos_fb->exynos_gem_obj[i] == NULL)
82 continue;
83
84 obj = &exynos_fb->exynos_gem_obj[i]->base;
85 drm_gem_object_unreference_unlocked(obj);
86 }
87
Inki Dae1c248b72011-10-04 19:19:01 +090088 kfree(exynos_fb);
89 exynos_fb = NULL;
90}
91
92static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
93 struct drm_file *file_priv,
94 unsigned int *handle)
95{
96 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
97
Inki Daeb9ede272013-01-29 17:51:09 +090098 /* This fb should have only one gem object. */
99 if (WARN_ON(exynos_fb->buf_cnt != 1))
100 return -EINVAL;
101
Inki Dae1c248b72011-10-04 19:19:01 +0900102 return drm_gem_handle_create(file_priv,
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900103 &exynos_fb->exynos_gem_obj[0]->base, handle);
Inki Dae1c248b72011-10-04 19:19:01 +0900104}
105
106static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
107 struct drm_file *file_priv, unsigned flags,
108 unsigned color, struct drm_clip_rect *clips,
109 unsigned num_clips)
110{
Inki Dae1c248b72011-10-04 19:19:01 +0900111 /* TODO */
112
113 return 0;
114}
115
116static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
117 .destroy = exynos_drm_fb_destroy,
118 .create_handle = exynos_drm_fb_create_handle,
119 .dirty = exynos_drm_fb_dirty,
120};
121
Inki Dae01ed8122012-08-20 20:05:56 +0900122void exynos_drm_fb_set_buf_cnt(struct drm_framebuffer *fb,
123 unsigned int cnt)
124{
125 struct exynos_drm_fb *exynos_fb;
126
127 exynos_fb = to_exynos_fb(fb);
128
129 exynos_fb->buf_cnt = cnt;
130}
131
132unsigned int exynos_drm_fb_get_buf_cnt(struct drm_framebuffer *fb)
133{
134 struct exynos_drm_fb *exynos_fb;
135
136 exynos_fb = to_exynos_fb(fb);
137
138 return exynos_fb->buf_cnt;
139}
140
Joonyoung Shime1533c02011-12-13 14:46:57 +0900141struct drm_framebuffer *
142exynos_drm_framebuffer_init(struct drm_device *dev,
143 struct drm_mode_fb_cmd2 *mode_cmd,
144 struct drm_gem_object *obj)
Inki Dae1c248b72011-10-04 19:19:01 +0900145{
146 struct exynos_drm_fb *exynos_fb;
Inki Dae0519f9a2012-10-20 07:53:42 -0700147 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900148 int ret;
149
Inki Dae0519f9a2012-10-20 07:53:42 -0700150 exynos_gem_obj = to_exynos_gem_obj(obj);
151
152 ret = check_fb_gem_memory_type(dev, exynos_gem_obj);
153 if (ret < 0) {
154 DRM_ERROR("cannot use this gem memory type for fb.\n");
155 return ERR_PTR(-EINVAL);
156 }
157
Inki Dae1c248b72011-10-04 19:19:01 +0900158 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900159 if (!exynos_fb)
Inki Dae1c248b72011-10-04 19:19:01 +0900160 return ERR_PTR(-ENOMEM);
Inki Dae1c248b72011-10-04 19:19:01 +0900161
Daniel Vetterf2c00952012-12-14 13:39:03 +0900162 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
Inki Dae0519f9a2012-10-20 07:53:42 -0700163 exynos_fb->exynos_gem_obj[0] = exynos_gem_obj;
164
Joonyoung Shime1533c02011-12-13 14:46:57 +0900165 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
Inki Dae1c248b72011-10-04 19:19:01 +0900166 if (ret) {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900167 DRM_ERROR("failed to initialize framebuffer\n");
168 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900169 }
170
Joonyoung Shime1533c02011-12-13 14:46:57 +0900171 return &exynos_fb->fb;
Inki Dae1c248b72011-10-04 19:19:01 +0900172}
173
Inki Dae01ed8122012-08-20 20:05:56 +0900174static u32 exynos_drm_format_num_buffers(struct drm_mode_fb_cmd2 *mode_cmd)
175{
176 unsigned int cnt = 0;
177
178 if (mode_cmd->pixel_format != DRM_FORMAT_NV12)
179 return drm_format_num_planes(mode_cmd->pixel_format);
180
181 while (cnt != MAX_FB_BUFFER) {
182 if (!mode_cmd->handles[cnt])
183 break;
184 cnt++;
185 }
186
187 /*
188 * check if NV12 or NV12M.
189 *
190 * NV12
191 * handles[0] = base1, offsets[0] = 0
192 * handles[1] = base1, offsets[1] = Y_size
193 *
194 * NV12M
195 * handles[0] = base1, offsets[0] = 0
196 * handles[1] = base2, offsets[1] = 0
197 */
198 if (cnt == 2) {
199 /*
200 * in case of NV12 format, offsets[1] is not 0 and
201 * handles[0] is same as handles[1].
202 */
203 if (mode_cmd->offsets[1] &&
204 mode_cmd->handles[0] == mode_cmd->handles[1])
205 cnt = 1;
206 }
207
208 return cnt;
209}
210
Joonyoung Shime1533c02011-12-13 14:46:57 +0900211static struct drm_framebuffer *
212exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
213 struct drm_mode_fb_cmd2 *mode_cmd)
Inki Dae1c248b72011-10-04 19:19:01 +0900214{
Joonyoung Shime1533c02011-12-13 14:46:57 +0900215 struct drm_gem_object *obj;
YoungJun Cho979c0c72013-02-12 21:23:54 +0900216 struct exynos_drm_gem_obj *exynos_gem_obj;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900217 struct exynos_drm_fb *exynos_fb;
Daniel Vetterf2c00952012-12-14 13:39:03 +0900218 int i, ret;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900219
Daniel Vetterf2c00952012-12-14 13:39:03 +0900220 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900221 if (!exynos_fb)
Daniel Vetterf2c00952012-12-14 13:39:03 +0900222 return ERR_PTR(-ENOMEM);
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900223
YoungJun Cho979c0c72013-02-12 21:23:54 +0900224 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
225 if (!obj) {
226 DRM_ERROR("failed to lookup gem object\n");
227 ret = -ENOENT;
228 goto err_free;
229 }
230
Daniel Vetterf2c00952012-12-14 13:39:03 +0900231 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
232 exynos_fb->exynos_gem_obj[0] = to_exynos_gem_obj(obj);
Inki Dae01ed8122012-08-20 20:05:56 +0900233 exynos_fb->buf_cnt = exynos_drm_format_num_buffers(mode_cmd);
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900234
Inki Dae01ed8122012-08-20 20:05:56 +0900235 DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt);
236
237 for (i = 1; i < exynos_fb->buf_cnt; i++) {
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900238 obj = drm_gem_object_lookup(dev, file_priv,
239 mode_cmd->handles[i]);
240 if (!obj) {
241 DRM_ERROR("failed to lookup gem object\n");
YoungJun Cho979c0c72013-02-12 21:23:54 +0900242 ret = -ENOENT;
243 exynos_fb->buf_cnt = i;
244 goto err_unreference;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900245 }
246
Inki Dae0519f9a2012-10-20 07:53:42 -0700247 exynos_gem_obj = to_exynos_gem_obj(obj);
YoungJun Cho979c0c72013-02-12 21:23:54 +0900248 exynos_fb->exynos_gem_obj[i] = exynos_gem_obj;
Inki Dae0519f9a2012-10-20 07:53:42 -0700249
250 ret = check_fb_gem_memory_type(dev, exynos_gem_obj);
251 if (ret < 0) {
252 DRM_ERROR("cannot use this gem memory type for fb.\n");
YoungJun Cho979c0c72013-02-12 21:23:54 +0900253 goto err_unreference;
Inki Dae0519f9a2012-10-20 07:53:42 -0700254 }
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900255 }
256
Daniel Vetterf2c00952012-12-14 13:39:03 +0900257 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
258 if (ret) {
YoungJun Cho979c0c72013-02-12 21:23:54 +0900259 DRM_ERROR("failed to init framebuffer.\n");
260 goto err_unreference;
Daniel Vetterf2c00952012-12-14 13:39:03 +0900261 }
262
263 return &exynos_fb->fb;
YoungJun Cho979c0c72013-02-12 21:23:54 +0900264
265err_unreference:
266 for (i = 0; i < exynos_fb->buf_cnt; i++) {
267 struct drm_gem_object *obj;
268
269 obj = &exynos_fb->exynos_gem_obj[i]->base;
270 if (obj)
271 drm_gem_object_unreference_unlocked(obj);
272 }
273err_free:
274 kfree(exynos_fb);
275 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900276}
277
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900278struct exynos_drm_gem_buf *exynos_drm_fb_buffer(struct drm_framebuffer *fb,
279 int index)
Inki Dae1c248b72011-10-04 19:19:01 +0900280{
281 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
Inki Dae2c871122011-11-12 15:23:32 +0900282 struct exynos_drm_gem_buf *buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900283
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900284 if (index >= MAX_FB_BUFFER)
285 return NULL;
286
287 buffer = exynos_fb->exynos_gem_obj[index]->buffer;
Inki Dae2c871122011-11-12 15:23:32 +0900288 if (!buffer)
Inki Dae19c8b832011-10-14 13:29:46 +0900289 return NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900290
Inki Dae4744ad22012-12-07 17:51:27 +0900291 DRM_DEBUG_KMS("dma_addr = 0x%lx\n", (unsigned long)buffer->dma_addr);
Inki Dae1c248b72011-10-04 19:19:01 +0900292
Inki Dae2c871122011-11-12 15:23:32 +0900293 return buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900294}
295
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900296static void exynos_drm_output_poll_changed(struct drm_device *dev)
297{
298 struct exynos_drm_private *private = dev->dev_private;
299 struct drm_fb_helper *fb_helper = private->fb_helper;
300
301 if (fb_helper)
302 drm_fb_helper_hotplug_event(fb_helper);
303}
304
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200305static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900306 .fb_create = exynos_user_fb_create,
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900307 .output_poll_changed = exynos_drm_output_poll_changed,
Inki Dae1c248b72011-10-04 19:19:01 +0900308};
309
310void exynos_drm_mode_config_init(struct drm_device *dev)
311{
312 dev->mode_config.min_width = 0;
313 dev->mode_config.min_height = 0;
314
315 /*
316 * set max width and height as default value(4096x4096).
317 * this value would be used to check framebuffer size limitation
318 * at drm_mode_addfb().
319 */
320 dev->mode_config.max_width = 4096;
321 dev->mode_config.max_height = 4096;
322
323 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
324}