blob: fcea28bdbc42095244f2f08a5bafbb82ef43eeda [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.
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090035 * @exynos_gem: array of exynos specific gem object containing a gem object.
Inki Dae1c248b72011-10-04 19:19:01 +090036 */
37struct exynos_drm_fb {
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090038 struct drm_framebuffer fb;
39 struct exynos_drm_gem *exynos_gem[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,
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090043 struct exynos_drm_gem *exynos_gem)
Inki Dae0519f9a2012-10-20 07:53:42 -070044{
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
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090054 flags = exynos_gem->flags;
Inki Dae0519f9a2012-10-20 07:53:42 -070055
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. */
Sean Paul080be03d2014-02-19 21:02:55 +090074 exynos_drm_crtc_complete_scanout(fb);
Inki Dae1daa8922012-11-22 17:41:23 +090075
Inki Dae1c248b72011-10-04 19:19:01 +090076 drm_framebuffer_cleanup(fb);
77
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090078 for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem); i++) {
Laurent Pinchart07b68352012-05-16 17:08:56 +020079 struct drm_gem_object *obj;
80
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090081 if (exynos_fb->exynos_gem[i] == NULL)
Laurent Pinchart07b68352012-05-16 17:08:56 +020082 continue;
83
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090084 obj = &exynos_fb->exynos_gem[i]->base;
Laurent Pinchart07b68352012-05-16 17:08:56 +020085 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 Dae1c248b72011-10-04 19:19:01 +090098 return drm_gem_handle_create(file_priv,
Joonyoung Shim813fd67b2015-10-02 09:33:47 +090099 &exynos_fb->exynos_gem[0]->base, handle);
Inki Dae1c248b72011-10-04 19:19:01 +0900100}
101
102static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
103 struct drm_file *file_priv, unsigned flags,
104 unsigned color, struct drm_clip_rect *clips,
105 unsigned num_clips)
106{
Inki Dae1c248b72011-10-04 19:19:01 +0900107 /* TODO */
108
109 return 0;
110}
111
112static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
113 .destroy = exynos_drm_fb_destroy,
114 .create_handle = exynos_drm_fb_create_handle,
115 .dirty = exynos_drm_fb_dirty,
116};
117
Joonyoung Shime1533c02011-12-13 14:46:57 +0900118struct drm_framebuffer *
119exynos_drm_framebuffer_init(struct drm_device *dev,
120 struct drm_mode_fb_cmd2 *mode_cmd,
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900121 struct exynos_drm_gem **exynos_gem,
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900122 int count)
Inki Dae1c248b72011-10-04 19:19:01 +0900123{
124 struct exynos_drm_fb *exynos_fb;
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900125 int i;
Inki Dae1c248b72011-10-04 19:19:01 +0900126 int ret;
127
Inki Dae1c248b72011-10-04 19:19:01 +0900128 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900129 if (!exynos_fb)
Inki Dae1c248b72011-10-04 19:19:01 +0900130 return ERR_PTR(-ENOMEM);
Inki Dae1c248b72011-10-04 19:19:01 +0900131
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900132 for (i = 0; i < count; i++) {
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900133 ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900134 if (ret < 0)
135 goto err;
136
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900137 exynos_fb->exynos_gem[i] = exynos_gem[i];
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900138 }
139
140 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
Joonyoung Shim94e30d92015-09-01 16:22:47 +0900141
Joonyoung Shime1533c02011-12-13 14:46:57 +0900142 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900143 if (ret < 0) {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900144 DRM_ERROR("failed to initialize framebuffer\n");
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900145 goto err;
Inki Dae1c248b72011-10-04 19:19:01 +0900146 }
147
Joonyoung Shime1533c02011-12-13 14:46:57 +0900148 return &exynos_fb->fb;
Joonyoung Shimd56125a2015-09-01 16:22:52 +0900149
150err:
151 kfree(exynos_fb);
152 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900153}
154
Joonyoung Shime1533c02011-12-13 14:46:57 +0900155static struct drm_framebuffer *
156exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
157 struct drm_mode_fb_cmd2 *mode_cmd)
Inki Dae1c248b72011-10-04 19:19:01 +0900158{
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900159 struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
Joonyoung Shime1533c02011-12-13 14:46:57 +0900160 struct drm_gem_object *obj;
Joonyoung Shim8d317582015-09-01 16:22:53 +0900161 struct drm_framebuffer *fb;
162 int i;
163 int ret;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900164
Joonyoung Shim8d317582015-09-01 16:22:53 +0900165 for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) {
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900166 obj = drm_gem_object_lookup(dev, file_priv,
Joonyoung Shim8d317582015-09-01 16:22:53 +0900167 mode_cmd->handles[i]);
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900168 if (!obj) {
169 DRM_ERROR("failed to lookup gem object\n");
YoungJun Cho979c0c72013-02-12 21:23:54 +0900170 ret = -ENOENT;
Joonyoung Shimdcbb85a2015-09-01 16:22:51 +0900171 goto err;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900172 }
173
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900174 exynos_gem[i] = to_exynos_gem(obj);
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900175 }
176
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900177 fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
Joonyoung Shim8d317582015-09-01 16:22:53 +0900178 if (IS_ERR(fb)) {
179 ret = PTR_ERR(fb);
Joonyoung Shimdcbb85a2015-09-01 16:22:51 +0900180 goto err;
Daniel Vetterf2c00952012-12-14 13:39:03 +0900181 }
182
Joonyoung Shim8d317582015-09-01 16:22:53 +0900183 return fb;
YoungJun Cho979c0c72013-02-12 21:23:54 +0900184
Joonyoung Shimdcbb85a2015-09-01 16:22:51 +0900185err:
Joonyoung Shim8d317582015-09-01 16:22:53 +0900186 while (i--)
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900187 drm_gem_object_unreference_unlocked(&exynos_gem[i]->base);
YoungJun Cho979c0c72013-02-12 21:23:54 +0900188
YoungJun Cho979c0c72013-02-12 21:23:54 +0900189 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900190}
191
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900192struct exynos_drm_gem *exynos_drm_fb_gem(struct drm_framebuffer *fb, int index)
Inki Dae1c248b72011-10-04 19:19:01 +0900193{
194 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900195 struct exynos_drm_gem *exynos_gem;
Inki Dae1c248b72011-10-04 19:19:01 +0900196
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900197 if (index >= MAX_FB_BUFFER)
198 return NULL;
199
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900200 exynos_gem = exynos_fb->exynos_gem[index];
201 if (!exynos_gem)
Inki Dae19c8b832011-10-14 13:29:46 +0900202 return NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900203
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900204 DRM_DEBUG_KMS("dma_addr: 0x%lx\n", (unsigned long)exynos_gem->dma_addr);
Inki Dae1c248b72011-10-04 19:19:01 +0900205
Joonyoung Shim813fd67b2015-10-02 09:33:47 +0900206 return exynos_gem;
Inki Dae1c248b72011-10-04 19:19:01 +0900207}
208
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900209static void exynos_drm_output_poll_changed(struct drm_device *dev)
210{
211 struct exynos_drm_private *private = dev->dev_private;
212 struct drm_fb_helper *fb_helper = private->fb_helper;
213
214 if (fb_helper)
215 drm_fb_helper_hotplug_event(fb_helper);
Andrzej Hajda25928a32014-03-17 11:27:17 +0100216 else
217 exynos_drm_fbdev_init(dev);
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900218}
219
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200220static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900221 .fb_create = exynos_user_fb_create,
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900222 .output_poll_changed = exynos_drm_output_poll_changed,
Gustavo Padovan910874a2015-06-01 12:04:46 -0300223 .atomic_check = drm_atomic_helper_check,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300224 .atomic_commit = exynos_atomic_commit,
Inki Dae1c248b72011-10-04 19:19:01 +0900225};
226
227void exynos_drm_mode_config_init(struct drm_device *dev)
228{
229 dev->mode_config.min_width = 0;
230 dev->mode_config.min_height = 0;
231
232 /*
233 * set max width and height as default value(4096x4096).
234 * this value would be used to check framebuffer size limitation
235 * at drm_mode_addfb().
236 */
237 dev->mode_config.max_width = 4096;
238 dev->mode_config.max_height = 4096;
239
240 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
241}