blob: 4ccfe4328fab130ab1e7c1269ff793ac8a9ee004 [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 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 */
28
29#include "drmP.h"
30#include "drm_crtc.h"
31#include "drm_crtc_helper.h"
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +090032#include "drm_fb_helper.h"
Inki Dae1c248b72011-10-04 19:19:01 +090033
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +090034#include "exynos_drm_drv.h"
Inki Dae1c248b72011-10-04 19:19:01 +090035#include "exynos_drm_fb.h"
Inki Dae1c248b72011-10-04 19:19:01 +090036#include "exynos_drm_gem.h"
37
38#define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb)
39
40/*
41 * exynos specific framebuffer structure.
42 *
43 * @fb: drm framebuffer obejct.
Seung-Woo Kim229d3532011-12-15 14:36:22 +090044 * @exynos_gem_obj: array of exynos specific gem object containing a gem object.
Inki Dae1c248b72011-10-04 19:19:01 +090045 */
46struct exynos_drm_fb {
47 struct drm_framebuffer fb;
Seung-Woo Kim229d3532011-12-15 14:36:22 +090048 struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER];
Inki Dae1c248b72011-10-04 19:19:01 +090049};
50
51static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
52{
53 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
Laurent Pinchart07b68352012-05-16 17:08:56 +020054 unsigned int i;
Inki Dae1c248b72011-10-04 19:19:01 +090055
56 DRM_DEBUG_KMS("%s\n", __FILE__);
57
58 drm_framebuffer_cleanup(fb);
59
Laurent Pinchart07b68352012-05-16 17:08:56 +020060 for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem_obj); i++) {
61 struct drm_gem_object *obj;
62
63 if (exynos_fb->exynos_gem_obj[i] == NULL)
64 continue;
65
66 obj = &exynos_fb->exynos_gem_obj[i]->base;
67 drm_gem_object_unreference_unlocked(obj);
68 }
69
Inki Dae1c248b72011-10-04 19:19:01 +090070 kfree(exynos_fb);
71 exynos_fb = NULL;
72}
73
74static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
75 struct drm_file *file_priv,
76 unsigned int *handle)
77{
78 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
79
80 DRM_DEBUG_KMS("%s\n", __FILE__);
81
82 return drm_gem_handle_create(file_priv,
Seung-Woo Kim229d3532011-12-15 14:36:22 +090083 &exynos_fb->exynos_gem_obj[0]->base, handle);
Inki Dae1c248b72011-10-04 19:19:01 +090084}
85
86static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
87 struct drm_file *file_priv, unsigned flags,
88 unsigned color, struct drm_clip_rect *clips,
89 unsigned num_clips)
90{
91 DRM_DEBUG_KMS("%s\n", __FILE__);
92
93 /* TODO */
94
95 return 0;
96}
97
98static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
99 .destroy = exynos_drm_fb_destroy,
100 .create_handle = exynos_drm_fb_create_handle,
101 .dirty = exynos_drm_fb_dirty,
102};
103
Joonyoung Shime1533c02011-12-13 14:46:57 +0900104struct drm_framebuffer *
105exynos_drm_framebuffer_init(struct drm_device *dev,
106 struct drm_mode_fb_cmd2 *mode_cmd,
107 struct drm_gem_object *obj)
Inki Dae1c248b72011-10-04 19:19:01 +0900108{
109 struct exynos_drm_fb *exynos_fb;
Inki Dae1c248b72011-10-04 19:19:01 +0900110 int ret;
111
Inki Dae1c248b72011-10-04 19:19:01 +0900112 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
113 if (!exynos_fb) {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900114 DRM_ERROR("failed to allocate exynos drm framebuffer\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900115 return ERR_PTR(-ENOMEM);
116 }
117
Joonyoung Shime1533c02011-12-13 14:46:57 +0900118 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
Inki Dae1c248b72011-10-04 19:19:01 +0900119 if (ret) {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900120 DRM_ERROR("failed to initialize framebuffer\n");
121 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900122 }
123
Joonyoung Shime1533c02011-12-13 14:46:57 +0900124 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900125 exynos_fb->exynos_gem_obj[0] = to_exynos_gem_obj(obj);
Inki Dae1c248b72011-10-04 19:19:01 +0900126
Joonyoung Shime1533c02011-12-13 14:46:57 +0900127 return &exynos_fb->fb;
Inki Dae1c248b72011-10-04 19:19:01 +0900128}
129
Joonyoung Shime1533c02011-12-13 14:46:57 +0900130static struct drm_framebuffer *
131exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
132 struct drm_mode_fb_cmd2 *mode_cmd)
Inki Dae1c248b72011-10-04 19:19:01 +0900133{
Joonyoung Shime1533c02011-12-13 14:46:57 +0900134 struct drm_gem_object *obj;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900135 struct drm_framebuffer *fb;
136 struct exynos_drm_fb *exynos_fb;
137 int nr;
138 int i;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900139
Inki Dae1c248b72011-10-04 19:19:01 +0900140 DRM_DEBUG_KMS("%s\n", __FILE__);
141
Joonyoung Shime1533c02011-12-13 14:46:57 +0900142 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
143 if (!obj) {
144 DRM_ERROR("failed to lookup gem object\n");
145 return ERR_PTR(-ENOENT);
146 }
147
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900148 fb = exynos_drm_framebuffer_init(dev, mode_cmd, obj);
Laurent Pinchart07b68352012-05-16 17:08:56 +0200149 if (IS_ERR(fb)) {
150 drm_gem_object_unreference_unlocked(obj);
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900151 return fb;
Laurent Pinchart07b68352012-05-16 17:08:56 +0200152 }
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900153
154 exynos_fb = to_exynos_fb(fb);
155 nr = exynos_drm_format_num_buffers(fb->pixel_format);
156
157 for (i = 1; i < nr; i++) {
158 obj = drm_gem_object_lookup(dev, file_priv,
159 mode_cmd->handles[i]);
160 if (!obj) {
161 DRM_ERROR("failed to lookup gem object\n");
162 exynos_drm_fb_destroy(fb);
163 return ERR_PTR(-ENOENT);
164 }
165
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900166 exynos_fb->exynos_gem_obj[i] = to_exynos_gem_obj(obj);
167 }
168
169 return fb;
Inki Dae1c248b72011-10-04 19:19:01 +0900170}
171
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900172struct exynos_drm_gem_buf *exynos_drm_fb_buffer(struct drm_framebuffer *fb,
173 int index)
Inki Dae1c248b72011-10-04 19:19:01 +0900174{
175 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
Inki Dae2c871122011-11-12 15:23:32 +0900176 struct exynos_drm_gem_buf *buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900177
178 DRM_DEBUG_KMS("%s\n", __FILE__);
179
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900180 if (index >= MAX_FB_BUFFER)
181 return NULL;
182
183 buffer = exynos_fb->exynos_gem_obj[index]->buffer;
Inki Dae2c871122011-11-12 15:23:32 +0900184 if (!buffer)
Inki Dae19c8b832011-10-14 13:29:46 +0900185 return NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900186
Inki Dae2c871122011-11-12 15:23:32 +0900187 DRM_DEBUG_KMS("vaddr = 0x%lx, dma_addr = 0x%lx\n",
188 (unsigned long)buffer->kvaddr,
189 (unsigned long)buffer->dma_addr);
Inki Dae1c248b72011-10-04 19:19:01 +0900190
Inki Dae2c871122011-11-12 15:23:32 +0900191 return buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900192}
193
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900194static void exynos_drm_output_poll_changed(struct drm_device *dev)
195{
196 struct exynos_drm_private *private = dev->dev_private;
197 struct drm_fb_helper *fb_helper = private->fb_helper;
198
199 if (fb_helper)
200 drm_fb_helper_hotplug_event(fb_helper);
201}
202
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200203static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900204 .fb_create = exynos_user_fb_create,
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900205 .output_poll_changed = exynos_drm_output_poll_changed,
Inki Dae1c248b72011-10-04 19:19:01 +0900206};
207
208void exynos_drm_mode_config_init(struct drm_device *dev)
209{
210 dev->mode_config.min_width = 0;
211 dev->mode_config.min_height = 0;
212
213 /*
214 * set max width and height as default value(4096x4096).
215 * this value would be used to check framebuffer size limitation
216 * at drm_mode_addfb().
217 */
218 dev->mode_config.max_width = 4096;
219 dev->mode_config.max_height = 4096;
220
221 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
222}