blob: df5eec6c1aba785911c6d8c0110d77f8a82ee7e3 [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"
36#include "exynos_drm_buf.h"
37#include "exynos_drm_gem.h"
38
39#define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb)
40
41/*
42 * exynos specific framebuffer structure.
43 *
44 * @fb: drm framebuffer obejct.
45 * @exynos_gem_obj: exynos specific gem object containing a gem object.
Inki Dae2c871122011-11-12 15:23:32 +090046 * @buffer: pointer to exynos_drm_gem_buffer object.
47 * - contain the memory information to memory region allocated
48 * at default framebuffer creation.
Inki Dae1c248b72011-10-04 19:19:01 +090049 */
50struct exynos_drm_fb {
51 struct drm_framebuffer fb;
52 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae2c871122011-11-12 15:23:32 +090053 struct exynos_drm_gem_buf *buffer;
Inki Dae1c248b72011-10-04 19:19:01 +090054};
55
56static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
57{
58 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
59
60 DRM_DEBUG_KMS("%s\n", __FILE__);
61
62 drm_framebuffer_cleanup(fb);
63
64 /*
65 * default framebuffer has no gem object so
66 * a buffer of the default framebuffer should be released at here.
67 */
Inki Dae2c871122011-11-12 15:23:32 +090068 if (!exynos_fb->exynos_gem_obj && exynos_fb->buffer)
69 exynos_drm_buf_destroy(fb->dev, exynos_fb->buffer);
Inki Dae1c248b72011-10-04 19:19:01 +090070
71 kfree(exynos_fb);
72 exynos_fb = NULL;
73}
74
75static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
76 struct drm_file *file_priv,
77 unsigned int *handle)
78{
79 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
80
81 DRM_DEBUG_KMS("%s\n", __FILE__);
82
83 return drm_gem_handle_create(file_priv,
84 &exynos_fb->exynos_gem_obj->base, handle);
85}
86
87static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
88 struct drm_file *file_priv, unsigned flags,
89 unsigned color, struct drm_clip_rect *clips,
90 unsigned num_clips)
91{
92 DRM_DEBUG_KMS("%s\n", __FILE__);
93
94 /* TODO */
95
96 return 0;
97}
98
99static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
100 .destroy = exynos_drm_fb_destroy,
101 .create_handle = exynos_drm_fb_create_handle,
102 .dirty = exynos_drm_fb_dirty,
103};
104
105static struct drm_framebuffer *
106exynos_drm_fb_init(struct drm_file *file_priv, struct drm_device *dev,
Joonyoung Shima794d572011-12-08 15:05:19 +0900107 struct drm_mode_fb_cmd2 *mode_cmd)
Inki Dae1c248b72011-10-04 19:19:01 +0900108{
109 struct exynos_drm_fb *exynos_fb;
110 struct drm_framebuffer *fb;
111 struct exynos_drm_gem_obj *exynos_gem_obj = NULL;
112 struct drm_gem_object *obj;
113 unsigned int size;
114 int ret;
115
116 DRM_DEBUG_KMS("%s\n", __FILE__);
117
Inki Dae1c248b72011-10-04 19:19:01 +0900118 DRM_LOG_KMS("drm fb create(%dx%d)\n",
119 mode_cmd->width, mode_cmd->height);
120
121 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
122 if (!exynos_fb) {
123 DRM_ERROR("failed to allocate exynos drm framebuffer.\n");
124 return ERR_PTR(-ENOMEM);
125 }
126
127 fb = &exynos_fb->fb;
128 ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs);
129 if (ret) {
130 DRM_ERROR("failed to initialize framebuffer.\n");
131 goto err_init;
132 }
133
134 DRM_LOG_KMS("create: fb id: %d\n", fb->base.id);
135
Joonyoung Shima794d572011-12-08 15:05:19 +0900136 size = mode_cmd->pitches[0] * mode_cmd->height;
Inki Dae1c248b72011-10-04 19:19:01 +0900137
138 /*
Joonyoung Shima794d572011-12-08 15:05:19 +0900139 * mode_cmd->handles[0] could be NULL at booting time or
Inki Dae1c248b72011-10-04 19:19:01 +0900140 * with user request. if NULL, a new buffer or a gem object
141 * would be allocated.
142 */
Joonyoung Shima794d572011-12-08 15:05:19 +0900143 if (!mode_cmd->handles[0]) {
Inki Dae1c248b72011-10-04 19:19:01 +0900144 if (!file_priv) {
Inki Dae2c871122011-11-12 15:23:32 +0900145 struct exynos_drm_gem_buf *buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900146
147 /*
148 * in case that file_priv is NULL, it allocates
149 * only buffer and this buffer would be used
150 * for default framebuffer.
151 */
Inki Dae2c871122011-11-12 15:23:32 +0900152 buffer = exynos_drm_buf_create(dev, size);
153 if (IS_ERR(buffer)) {
154 ret = PTR_ERR(buffer);
Inki Dae1c248b72011-10-04 19:19:01 +0900155 goto err_buffer;
156 }
157
Inki Dae2c871122011-11-12 15:23:32 +0900158 exynos_fb->buffer = buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900159
Inki Dae2c871122011-11-12 15:23:32 +0900160 DRM_LOG_KMS("default: dma_addr = 0x%lx, size = 0x%x\n",
161 (unsigned long)buffer->dma_addr, size);
Inki Dae1c248b72011-10-04 19:19:01 +0900162
163 goto out;
164 } else {
Inki Daef088d5a2011-11-12 14:51:23 +0900165 exynos_gem_obj = exynos_drm_gem_create(dev, file_priv,
Joonyoung Shima794d572011-12-08 15:05:19 +0900166 &mode_cmd->handles[0],
Inki Daef088d5a2011-11-12 14:51:23 +0900167 size);
Inki Dae1c248b72011-10-04 19:19:01 +0900168 if (IS_ERR(exynos_gem_obj)) {
169 ret = PTR_ERR(exynos_gem_obj);
170 goto err_buffer;
171 }
172 }
173 } else {
Joonyoung Shima794d572011-12-08 15:05:19 +0900174 obj = drm_gem_object_lookup(dev, file_priv,
175 mode_cmd->handles[0]);
Inki Dae1c248b72011-10-04 19:19:01 +0900176 if (!obj) {
177 DRM_ERROR("failed to lookup gem object.\n");
178 goto err_buffer;
179 }
180
181 exynos_gem_obj = to_exynos_gem_obj(obj);
182
183 drm_gem_object_unreference_unlocked(obj);
184 }
185
186 /*
187 * if got a exynos_gem_obj from either a handle or
188 * a new creation then exynos_fb->exynos_gem_obj is NULL
189 * so that default framebuffer has no its own gem object,
190 * only its own buffer object.
191 */
Inki Dae2c871122011-11-12 15:23:32 +0900192 exynos_fb->buffer = exynos_gem_obj->buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900193
Inki Dae2c871122011-11-12 15:23:32 +0900194 DRM_LOG_KMS("dma_addr = 0x%lx, size = 0x%x, gem object = 0x%x\n",
195 (unsigned long)exynos_fb->buffer->dma_addr, size,
Inki Dae1c248b72011-10-04 19:19:01 +0900196 (unsigned int)&exynos_gem_obj->base);
197
198out:
199 exynos_fb->exynos_gem_obj = exynos_gem_obj;
200
201 drm_helper_mode_fill_fb_struct(fb, mode_cmd);
202
203 return fb;
204
205err_buffer:
206 drm_framebuffer_cleanup(fb);
207
208err_init:
209 kfree(exynos_fb);
210
211 return ERR_PTR(ret);
212}
213
214struct drm_framebuffer *exynos_drm_fb_create(struct drm_device *dev,
Joonyoung Shima794d572011-12-08 15:05:19 +0900215 struct drm_file *file_priv,
216 struct drm_mode_fb_cmd2 *mode_cmd)
Inki Dae1c248b72011-10-04 19:19:01 +0900217{
218 DRM_DEBUG_KMS("%s\n", __FILE__);
219
220 return exynos_drm_fb_init(file_priv, dev, mode_cmd);
221}
222
Inki Dae2c871122011-11-12 15:23:32 +0900223struct exynos_drm_gem_buf *exynos_drm_fb_get_buf(struct drm_framebuffer *fb)
Inki Dae1c248b72011-10-04 19:19:01 +0900224{
225 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
Inki Dae2c871122011-11-12 15:23:32 +0900226 struct exynos_drm_gem_buf *buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900227
228 DRM_DEBUG_KMS("%s\n", __FILE__);
229
Inki Dae2c871122011-11-12 15:23:32 +0900230 buffer = exynos_fb->buffer;
231 if (!buffer)
Inki Dae19c8b832011-10-14 13:29:46 +0900232 return NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900233
Inki Dae2c871122011-11-12 15:23:32 +0900234 DRM_DEBUG_KMS("vaddr = 0x%lx, dma_addr = 0x%lx\n",
235 (unsigned long)buffer->kvaddr,
236 (unsigned long)buffer->dma_addr);
Inki Dae1c248b72011-10-04 19:19:01 +0900237
Inki Dae2c871122011-11-12 15:23:32 +0900238 return buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900239}
240
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900241static void exynos_drm_output_poll_changed(struct drm_device *dev)
242{
243 struct exynos_drm_private *private = dev->dev_private;
244 struct drm_fb_helper *fb_helper = private->fb_helper;
245
246 if (fb_helper)
247 drm_fb_helper_hotplug_event(fb_helper);
248}
249
Inki Dae1c248b72011-10-04 19:19:01 +0900250static struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
251 .fb_create = exynos_drm_fb_create,
Seung-Woo Kim7db3eba2011-10-18 16:58:05 +0900252 .output_poll_changed = exynos_drm_output_poll_changed,
Inki Dae1c248b72011-10-04 19:19:01 +0900253};
254
255void exynos_drm_mode_config_init(struct drm_device *dev)
256{
257 dev->mode_config.min_width = 0;
258 dev->mode_config.min_height = 0;
259
260 /*
261 * set max width and height as default value(4096x4096).
262 * this value would be used to check framebuffer size limitation
263 * at drm_mode_addfb().
264 */
265 dev->mode_config.max_width = 4096;
266 dev->mode_config.max_height = 4096;
267
268 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
269}
270
271MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
272MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
273MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
274MODULE_DESCRIPTION("Samsung SoC DRM FB Driver");
275MODULE_LICENSE("GPL");