blob: 59ebbe5472907fad2b1395eab2b72c464b37e483 [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>
Gustavo Padovand6562a22015-06-01 12:04:52 -030019#include <drm/drm_atomic.h>
Gustavo Padovan910874a2015-06-01 12:04:46 -030020#include <drm/drm_atomic_helper.h>
Inki Dae0519f9a2012-10-20 07:53:42 -070021#include <uapi/drm/exynos_drm.h>
Inki Dae1c248b72011-10-04 19:19:01 +090022
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +090023#include "exynos_drm_drv.h"
Inki Dae1c248b72011-10-04 19:19:01 +090024#include "exynos_drm_fb.h"
Andrzej Hajda25928a32014-03-17 11:27:17 +010025#include "exynos_drm_fbdev.h"
Inki Dae1c248b72011-10-04 19:19:01 +090026#include "exynos_drm_gem.h"
Inki Dae0519f9a2012-10-20 07:53:42 -070027#include "exynos_drm_iommu.h"
Sean Paul080be03d2014-02-19 21:02:55 +090028#include "exynos_drm_crtc.h"
Inki Dae1c248b72011-10-04 19:19:01 +090029
30#define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb)
31
32/*
33 * exynos specific framebuffer structure.
34 *
35 * @fb: drm framebuffer obejct.
Inki Dae01ed8122012-08-20 20:05:56 +090036 * @buf_cnt: a buffer count to drm framebuffer.
Seung-Woo Kim229d3532011-12-15 14:36:22 +090037 * @exynos_gem_obj: array of exynos specific gem object containing a gem object.
Inki Dae1c248b72011-10-04 19:19:01 +090038 */
39struct exynos_drm_fb {
40 struct drm_framebuffer fb;
Inki Dae01ed8122012-08-20 20:05:56 +090041 unsigned int buf_cnt;
Seung-Woo Kim229d3532011-12-15 14:36:22 +090042 struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER];
Inki Dae1c248b72011-10-04 19:19:01 +090043};
44
Inki Dae0519f9a2012-10-20 07:53:42 -070045static int check_fb_gem_memory_type(struct drm_device *drm_dev,
46 struct exynos_drm_gem_obj *exynos_gem_obj)
47{
48 unsigned int flags;
49
50 /*
51 * if exynos drm driver supports iommu then framebuffer can use
52 * all the buffer types.
53 */
54 if (is_drm_iommu_supported(drm_dev))
55 return 0;
56
57 flags = exynos_gem_obj->flags;
58
59 /*
60 * without iommu support, not support physically non-continuous memory
61 * for framebuffer.
62 */
63 if (IS_NONCONTIG_BUFFER(flags)) {
64 DRM_ERROR("cannot use this gem memory type for fb.\n");
65 return -EINVAL;
66 }
67
68 return 0;
69}
70
Inki Dae1c248b72011-10-04 19:19:01 +090071static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
72{
73 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
Laurent Pinchart07b68352012-05-16 17:08:56 +020074 unsigned int i;
Inki Dae1c248b72011-10-04 19:19:01 +090075
Inki Dae1daa8922012-11-22 17:41:23 +090076 /* make sure that overlay data are updated before relesing fb. */
Sean Paul080be03d2014-02-19 21:02:55 +090077 exynos_drm_crtc_complete_scanout(fb);
Inki Dae1daa8922012-11-22 17:41:23 +090078
Inki Dae1c248b72011-10-04 19:19:01 +090079 drm_framebuffer_cleanup(fb);
80
Laurent Pinchart07b68352012-05-16 17:08:56 +020081 for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem_obj); i++) {
82 struct drm_gem_object *obj;
83
84 if (exynos_fb->exynos_gem_obj[i] == NULL)
85 continue;
86
87 obj = &exynos_fb->exynos_gem_obj[i]->base;
88 drm_gem_object_unreference_unlocked(obj);
89 }
90
Inki Dae1c248b72011-10-04 19:19:01 +090091 kfree(exynos_fb);
92 exynos_fb = NULL;
93}
94
95static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
96 struct drm_file *file_priv,
97 unsigned int *handle)
98{
99 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
100
Inki Daeb9ede272013-01-29 17:51:09 +0900101 /* This fb should have only one gem object. */
102 if (WARN_ON(exynos_fb->buf_cnt != 1))
103 return -EINVAL;
104
Inki Dae1c248b72011-10-04 19:19:01 +0900105 return drm_gem_handle_create(file_priv,
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900106 &exynos_fb->exynos_gem_obj[0]->base, handle);
Inki Dae1c248b72011-10-04 19:19:01 +0900107}
108
109static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
110 struct drm_file *file_priv, unsigned flags,
111 unsigned color, struct drm_clip_rect *clips,
112 unsigned num_clips)
113{
Inki Dae1c248b72011-10-04 19:19:01 +0900114 /* TODO */
115
116 return 0;
117}
118
119static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
120 .destroy = exynos_drm_fb_destroy,
121 .create_handle = exynos_drm_fb_create_handle,
122 .dirty = exynos_drm_fb_dirty,
123};
124
Inki Dae01ed8122012-08-20 20:05:56 +0900125void exynos_drm_fb_set_buf_cnt(struct drm_framebuffer *fb,
126 unsigned int cnt)
127{
128 struct exynos_drm_fb *exynos_fb;
129
130 exynos_fb = to_exynos_fb(fb);
131
132 exynos_fb->buf_cnt = cnt;
133}
134
135unsigned int exynos_drm_fb_get_buf_cnt(struct drm_framebuffer *fb)
136{
137 struct exynos_drm_fb *exynos_fb;
138
139 exynos_fb = to_exynos_fb(fb);
140
141 return exynos_fb->buf_cnt;
142}
143
Joonyoung Shime1533c02011-12-13 14:46:57 +0900144struct drm_framebuffer *
145exynos_drm_framebuffer_init(struct drm_device *dev,
146 struct drm_mode_fb_cmd2 *mode_cmd,
147 struct drm_gem_object *obj)
Inki Dae1c248b72011-10-04 19:19:01 +0900148{
149 struct exynos_drm_fb *exynos_fb;
Inki Dae0519f9a2012-10-20 07:53:42 -0700150 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900151 int ret;
152
Inki Dae0519f9a2012-10-20 07:53:42 -0700153 exynos_gem_obj = to_exynos_gem_obj(obj);
154
155 ret = check_fb_gem_memory_type(dev, exynos_gem_obj);
Tobias Jakobi7ded8582015-04-07 01:14:51 +0200156 if (ret < 0)
157 return ERR_PTR(ret);
Inki Dae0519f9a2012-10-20 07:53:42 -0700158
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) {
Andrzej Hajda849b4312014-09-09 15:16:08 +0200168 kfree(exynos_fb);
Joonyoung Shime1533c02011-12-13 14:46:57 +0900169 DRM_ERROR("failed to initialize framebuffer\n");
170 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900171 }
172
Joonyoung Shime1533c02011-12-13 14:46:57 +0900173 return &exynos_fb->fb;
Inki Dae1c248b72011-10-04 19:19:01 +0900174}
175
Joonyoung Shime1533c02011-12-13 14:46:57 +0900176static struct drm_framebuffer *
177exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
178 struct drm_mode_fb_cmd2 *mode_cmd)
Inki Dae1c248b72011-10-04 19:19:01 +0900179{
Joonyoung Shime1533c02011-12-13 14:46:57 +0900180 struct drm_gem_object *obj;
YoungJun Cho979c0c72013-02-12 21:23:54 +0900181 struct exynos_drm_gem_obj *exynos_gem_obj;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900182 struct exynos_drm_fb *exynos_fb;
Daniel Vetterf2c00952012-12-14 13:39:03 +0900183 int i, ret;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900184
Daniel Vetterf2c00952012-12-14 13:39:03 +0900185 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900186 if (!exynos_fb)
Daniel Vetterf2c00952012-12-14 13:39:03 +0900187 return ERR_PTR(-ENOMEM);
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900188
YoungJun Cho979c0c72013-02-12 21:23:54 +0900189 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
190 if (!obj) {
191 DRM_ERROR("failed to lookup gem object\n");
192 ret = -ENOENT;
193 goto err_free;
194 }
195
Daniel Vetterf2c00952012-12-14 13:39:03 +0900196 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
197 exynos_fb->exynos_gem_obj[0] = to_exynos_gem_obj(obj);
Tobias Jakobid10ebb92015-04-27 23:10:13 +0200198 exynos_fb->buf_cnt = drm_format_num_planes(mode_cmd->pixel_format);
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900199
Inki Dae01ed8122012-08-20 20:05:56 +0900200 DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt);
201
202 for (i = 1; i < exynos_fb->buf_cnt; i++) {
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900203 obj = drm_gem_object_lookup(dev, file_priv,
204 mode_cmd->handles[i]);
205 if (!obj) {
206 DRM_ERROR("failed to lookup gem object\n");
YoungJun Cho979c0c72013-02-12 21:23:54 +0900207 ret = -ENOENT;
208 exynos_fb->buf_cnt = i;
209 goto err_unreference;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900210 }
211
Inki Dae0519f9a2012-10-20 07:53:42 -0700212 exynos_gem_obj = to_exynos_gem_obj(obj);
YoungJun Cho979c0c72013-02-12 21:23:54 +0900213 exynos_fb->exynos_gem_obj[i] = exynos_gem_obj;
Inki Dae0519f9a2012-10-20 07:53:42 -0700214
215 ret = check_fb_gem_memory_type(dev, exynos_gem_obj);
Tobias Jakobi7ded8582015-04-07 01:14:51 +0200216 if (ret < 0)
YoungJun Cho979c0c72013-02-12 21:23:54 +0900217 goto err_unreference;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900218 }
219
Daniel Vetterf2c00952012-12-14 13:39:03 +0900220 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
221 if (ret) {
YoungJun Cho979c0c72013-02-12 21:23:54 +0900222 DRM_ERROR("failed to init framebuffer.\n");
223 goto err_unreference;
Daniel Vetterf2c00952012-12-14 13:39:03 +0900224 }
225
226 return &exynos_fb->fb;
YoungJun Cho979c0c72013-02-12 21:23:54 +0900227
228err_unreference:
229 for (i = 0; i < exynos_fb->buf_cnt; i++) {
230 struct drm_gem_object *obj;
231
232 obj = &exynos_fb->exynos_gem_obj[i]->base;
233 if (obj)
234 drm_gem_object_unreference_unlocked(obj);
235 }
236err_free:
237 kfree(exynos_fb);
238 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900239}
240
Joonyoung Shim2a8cb482015-08-16 14:38:49 +0900241struct exynos_drm_gem_obj *exynos_drm_fb_gem_obj(struct drm_framebuffer *fb,
242 int index)
Inki Dae1c248b72011-10-04 19:19:01 +0900243{
244 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
Joonyoung Shim2a8cb482015-08-16 14:38:49 +0900245 struct exynos_drm_gem_obj *obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900246
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900247 if (index >= MAX_FB_BUFFER)
248 return NULL;
249
Joonyoung Shim2a8cb482015-08-16 14:38:49 +0900250 obj = exynos_fb->exynos_gem_obj[index];
251 if (!obj)
Inki Dae19c8b832011-10-14 13:29:46 +0900252 return NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900253
Joonyoung Shim2a8cb482015-08-16 14:38:49 +0900254 DRM_DEBUG_KMS("dma_addr = 0x%lx\n", (unsigned long)obj->dma_addr);
Inki Dae1c248b72011-10-04 19:19:01 +0900255
Joonyoung Shim2a8cb482015-08-16 14:38:49 +0900256 return obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900257}
258
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900259static void exynos_drm_output_poll_changed(struct drm_device *dev)
260{
261 struct exynos_drm_private *private = dev->dev_private;
262 struct drm_fb_helper *fb_helper = private->fb_helper;
263
264 if (fb_helper)
265 drm_fb_helper_hotplug_event(fb_helper);
Andrzej Hajda25928a32014-03-17 11:27:17 +0100266 else
267 exynos_drm_fbdev_init(dev);
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900268}
269
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200270static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900271 .fb_create = exynos_user_fb_create,
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900272 .output_poll_changed = exynos_drm_output_poll_changed,
Gustavo Padovan910874a2015-06-01 12:04:46 -0300273 .atomic_check = drm_atomic_helper_check,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300274 .atomic_commit = exynos_atomic_commit,
Inki Dae1c248b72011-10-04 19:19:01 +0900275};
276
277void exynos_drm_mode_config_init(struct drm_device *dev)
278{
279 dev->mode_config.min_width = 0;
280 dev->mode_config.min_height = 0;
281
282 /*
283 * set max width and height as default value(4096x4096).
284 * this value would be used to check framebuffer size limitation
285 * at drm_mode_addfb().
286 */
287 dev->mode_config.max_width = 4096;
288 dev->mode_config.max_height = 4096;
289
290 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
291}