blob: 849925ca0cdd6471efc7b3dac003936d5609d289 [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 Dae0519f9a2012-10-20 07:53:42 -070026#include "exynos_drm_iommu.h"
Sean Paul080be03d2014-02-19 21:02:55 +090027#include "exynos_drm_crtc.h"
Inki Dae1c248b72011-10-04 19:19:01 +090028
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.
Inki Dae01ed8122012-08-20 20:05:56 +090035 * @buf_cnt: a buffer count to drm framebuffer.
Seung-Woo Kim229d3532011-12-15 14:36:22 +090036 * @exynos_gem_obj: array of exynos specific gem object containing a gem object.
Inki Dae1c248b72011-10-04 19:19:01 +090037 */
38struct exynos_drm_fb {
39 struct drm_framebuffer fb;
Inki Dae01ed8122012-08-20 20:05:56 +090040 unsigned int buf_cnt;
Seung-Woo Kim229d3532011-12-15 14:36:22 +090041 struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER];
Inki Dae1c248b72011-10-04 19:19:01 +090042};
43
Inki Dae0519f9a2012-10-20 07:53:42 -070044static 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
Inki Dae1c248b72011-10-04 19:19:01 +090070static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
71{
72 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
Laurent Pinchart07b68352012-05-16 17:08:56 +020073 unsigned int i;
Inki Dae1c248b72011-10-04 19:19:01 +090074
Inki Dae1daa8922012-11-22 17:41:23 +090075 /* make sure that overlay data are updated before relesing fb. */
Sean Paul080be03d2014-02-19 21:02:55 +090076 exynos_drm_crtc_complete_scanout(fb);
Inki Dae1daa8922012-11-22 17:41:23 +090077
Inki Dae1c248b72011-10-04 19:19:01 +090078 drm_framebuffer_cleanup(fb);
79
Laurent Pinchart07b68352012-05-16 17:08:56 +020080 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
Inki Dae1c248b72011-10-04 19:19:01 +090090 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
Inki Daeb9ede272013-01-29 17:51:09 +0900100 /* This fb should have only one gem object. */
101 if (WARN_ON(exynos_fb->buf_cnt != 1))
102 return -EINVAL;
103
Inki Dae1c248b72011-10-04 19:19:01 +0900104 return drm_gem_handle_create(file_priv,
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900105 &exynos_fb->exynos_gem_obj[0]->base, handle);
Inki Dae1c248b72011-10-04 19:19:01 +0900106}
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{
Inki Dae1c248b72011-10-04 19:19:01 +0900113 /* 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
Inki Dae01ed8122012-08-20 20:05:56 +0900124unsigned 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
Joonyoung Shime1533c02011-12-13 14:46:57 +0900133struct drm_framebuffer *
134exynos_drm_framebuffer_init(struct drm_device *dev,
135 struct drm_mode_fb_cmd2 *mode_cmd,
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900136 struct exynos_drm_gem_obj **gem_obj,
137 int count)
Inki Dae1c248b72011-10-04 19:19:01 +0900138{
139 struct exynos_drm_fb *exynos_fb;
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900140 int i;
Inki Dae1c248b72011-10-04 19:19:01 +0900141 int ret;
142
Inki Dae1c248b72011-10-04 19:19:01 +0900143 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900144 if (!exynos_fb)
Inki Dae1c248b72011-10-04 19:19:01 +0900145 return ERR_PTR(-ENOMEM);
Inki Dae1c248b72011-10-04 19:19:01 +0900146
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900147 exynos_fb->buf_cnt = count;
148 DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt);
Inki Dae0519f9a2012-10-20 07:53:42 -0700149
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900150 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);
Joonyoung Shim94e30d92015-09-01 16:22:47 +0900159
Joonyoung Shime1533c02011-12-13 14:46:57 +0900160 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900161 if (ret < 0) {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900162 DRM_ERROR("failed to initialize framebuffer\n");
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900163 goto err;
Inki Dae1c248b72011-10-04 19:19:01 +0900164 }
165
Joonyoung Shime1533c02011-12-13 14:46:57 +0900166 return &exynos_fb->fb;
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900167
168err:
169 kfree(exynos_fb);
170 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900171}
172
Joonyoung Shime1533c02011-12-13 14:46:57 +0900173static struct drm_framebuffer *
174exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
175 struct drm_mode_fb_cmd2 *mode_cmd)
Inki Dae1c248b72011-10-04 19:19:01 +0900176{
Joonyoung Shim8d317582015-09-01 16:22:53 +0900177 struct exynos_drm_gem_obj *gem_objs[MAX_FB_BUFFER];
Joonyoung Shime1533c02011-12-13 14:46:57 +0900178 struct drm_gem_object *obj;
Joonyoung Shim8d317582015-09-01 16:22:53 +0900179 struct drm_framebuffer *fb;
180 int i;
181 int ret;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900182
Joonyoung Shim8d317582015-09-01 16:22:53 +0900183 for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) {
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900184 obj = drm_gem_object_lookup(dev, file_priv,
Joonyoung Shim8d317582015-09-01 16:22:53 +0900185 mode_cmd->handles[i]);
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900186 if (!obj) {
187 DRM_ERROR("failed to lookup gem object\n");
YoungJun Cho979c0c72013-02-12 21:23:54 +0900188 ret = -ENOENT;
Joonyoung Shimdcbb85a2015-09-01 16:22:51 +0900189 goto err;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900190 }
191
Joonyoung Shim8d317582015-09-01 16:22:53 +0900192 gem_objs[i] = to_exynos_gem_obj(obj);
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900193 }
194
Joonyoung Shim8d317582015-09-01 16:22:53 +0900195 fb = exynos_drm_framebuffer_init(dev, mode_cmd, gem_objs, i);
196 if (IS_ERR(fb)) {
197 ret = PTR_ERR(fb);
Joonyoung Shimdcbb85a2015-09-01 16:22:51 +0900198 goto err;
Daniel Vetterf2c00952012-12-14 13:39:03 +0900199 }
200
Joonyoung Shim8d317582015-09-01 16:22:53 +0900201 return fb;
YoungJun Cho979c0c72013-02-12 21:23:54 +0900202
Joonyoung Shimdcbb85a2015-09-01 16:22:51 +0900203err:
Joonyoung Shim8d317582015-09-01 16:22:53 +0900204 while (i--)
205 drm_gem_object_unreference_unlocked(&gem_objs[i]->base);
YoungJun Cho979c0c72013-02-12 21:23:54 +0900206
YoungJun Cho979c0c72013-02-12 21:23:54 +0900207 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900208}
209
Joonyoung Shim2a8cb482015-08-16 14:38:49 +0900210struct exynos_drm_gem_obj *exynos_drm_fb_gem_obj(struct drm_framebuffer *fb,
211 int index)
Inki Dae1c248b72011-10-04 19:19:01 +0900212{
213 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
Joonyoung Shim2a8cb482015-08-16 14:38:49 +0900214 struct exynos_drm_gem_obj *obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900215
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900216 if (index >= MAX_FB_BUFFER)
217 return NULL;
218
Joonyoung Shim2a8cb482015-08-16 14:38:49 +0900219 obj = exynos_fb->exynos_gem_obj[index];
220 if (!obj)
Inki Dae19c8b832011-10-14 13:29:46 +0900221 return NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900222
Joonyoung Shim2a8cb482015-08-16 14:38:49 +0900223 DRM_DEBUG_KMS("dma_addr = 0x%lx\n", (unsigned long)obj->dma_addr);
Inki Dae1c248b72011-10-04 19:19:01 +0900224
Joonyoung Shim2a8cb482015-08-16 14:38:49 +0900225 return obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900226}
227
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900228static void exynos_drm_output_poll_changed(struct drm_device *dev)
229{
230 struct exynos_drm_private *private = dev->dev_private;
231 struct drm_fb_helper *fb_helper = private->fb_helper;
232
233 if (fb_helper)
234 drm_fb_helper_hotplug_event(fb_helper);
Andrzej Hajda25928a32014-03-17 11:27:17 +0100235 else
236 exynos_drm_fbdev_init(dev);
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900237}
238
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200239static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900240 .fb_create = exynos_user_fb_create,
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900241 .output_poll_changed = exynos_drm_output_poll_changed,
Gustavo Padovan910874a2015-06-01 12:04:46 -0300242 .atomic_check = drm_atomic_helper_check,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300243 .atomic_commit = exynos_atomic_commit,
Inki Dae1c248b72011-10-04 19:19:01 +0900244};
245
246void exynos_drm_mode_config_init(struct drm_device *dev)
247{
248 dev->mode_config.min_width = 0;
249 dev->mode_config.min_height = 0;
250
251 /*
252 * set max width and height as default value(4096x4096).
253 * this value would be used to check framebuffer size limitation
254 * at drm_mode_addfb().
255 */
256 dev->mode_config.max_width = 4096;
257 dev->mode_config.max_height = 4096;
258
259 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
260}